From 9787e25bc5371dd55689f4840d46ceb9ae7e7b2c Mon Sep 17 00:00:00 2001 From: lgou2w Date: Wed, 17 Jul 2024 19:46:28 +0800 Subject: [PATCH] fix(#40): unable to resize window at high dpi --- Cargo.lock | 1 + src-tauri/Cargo.toml | 6 +++++ src-tauri/src/main.rs | 49 +++++++++++++++++++++++++++++++++++---- src-tauri/tauri.conf.json | 4 +--- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 287ac00f..b7baa3a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1585,6 +1585,7 @@ dependencies = [ "tracing", "tracing-subscriber", "url", + "windows 0.39.0", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 1858c081..3ddd575a 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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"] diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 270399d3..077f2e46 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -20,10 +20,14 @@ 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(()) }) @@ -31,3 +35,40 @@ fn main() { .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(); +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 339d8166..f10f9c92 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -63,13 +63,11 @@ "windows": [ { "fullscreen": false, - "height": 864, "resizable": true, "center": true, "title": "HoYo.Gacha", "width": 1152, - "minHeight": 864, - "minWidth": 1152 + "height": 864 } ] }