Skip to content

Commit

Permalink
fix(#40): unable to resize window at high dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
lgou2w committed Jul 17, 2024
1 parent 817074b commit 9787e25
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = "2.5.2"

[target."cfg(windows)".dependencies.windows]
version = "0.39.0"
features = [
"Win32_UI_WindowsAndMessaging"
]

[features]
default = ["custom-protocol"]
custom-protocol = ["tauri/custom-protocol"]
49 changes: 45 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,55 @@ fn main() {
.plugin(storage::StoragePluginBuilder::new().build())
.plugin(gacha::GachaPluginBuilder::new().build())
.setup(|app| {
let open_devtools = cfg!(debug_assertions) || std::env::var("DEVTOOLS").is_ok();
if open_devtools {
use tauri::Manager;
app.get_window("main").unwrap().open_devtools();
use tauri::Manager;
let main_window = app.get_window("main").unwrap();

#[cfg(windows)]
fixed_hdpi_problem(&main_window);

if cfg!(debug_assertions) || std::env::var("DEVTOOLS").is_ok() {
main_window.open_devtools();
}
Ok(())
})
.invoke_handler(commands::get_handlers())
.run(tauri::generate_context!())
.expect("error while running tauri application")
}

// See: https://github.com/lgou2w/HoYo.Gacha/issues/40
// It's not a perfect fix.
#[cfg(windows)]
fn fixed_hdpi_problem(window: &tauri::Window) {
use windows::Win32::UI::WindowsAndMessaging::GetSystemMetrics;
use windows::Win32::UI::WindowsAndMessaging::{SM_CXSCREEN, SM_CYSCREEN};

let (_screen_width, screen_height) =
unsafe { (GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)) };

// If under 1080P, maximize the window
// and allow resize width and height.
if screen_height < 1080 {
window.maximize().unwrap();
return;
}

let scale_factor = window.scale_factor().unwrap();

const WIDTH: f64 = 1152.;
const HEIGHT: f64 = 864.;

// If the scaling factor is greater than 1.0,
// then adjust the window height.
if scale_factor > 1. {
let new_height = HEIGHT / scale_factor;
window
.set_size(tauri::LogicalSize::new(WIDTH, new_height))
.unwrap();
}

// Only the minimum height adjustment is allowed
window
.set_min_size(Some(tauri::LogicalSize::new(WIDTH, 0.)))
.unwrap();
}
4 changes: 1 addition & 3 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@
"windows": [
{
"fullscreen": false,
"height": 864,
"resizable": true,
"center": true,
"title": "HoYo.Gacha",
"width": 1152,
"minHeight": 864,
"minWidth": 1152
"height": 864
}
]
}
Expand Down

0 comments on commit 9787e25

Please sign in to comment.