From 8114bcb34adadeb47a9767b7adf69edb8b0fb536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orvar=20Segerstr=C3=B6m?= Date: Mon, 13 Nov 2023 09:47:54 +0100 Subject: [PATCH] Switch commands from PhyicalSize to LogicalSize LogicalSize is what we want, to make the app work properly on retina displays --- app/tauri/src/commands.rs | 90 +++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/app/tauri/src/commands.rs b/app/tauri/src/commands.rs index 4344448e..cf043da8 100644 --- a/app/tauri/src/commands.rs +++ b/app/tauri/src/commands.rs @@ -3,7 +3,7 @@ use std::sync::Mutex; // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command -use tauri::{Builder, PhysicalSize, Runtime, Wry}; +use tauri::{Builder, LogicalSize, Runtime, Wry}; #[non_exhaustive] struct WindowSize; @@ -12,13 +12,18 @@ use crate::system_tray; use crate::updater; impl WindowSize { - // Not sure why, if its due to UI scaling or what though these values seem to size smaller than the same values on electron - pub const MIN: PhysicalSize = PhysicalSize { width : 420, height: 602}; - pub const COMPACT: PhysicalSize = PhysicalSize { width : 420, height: 80}; + pub const MIN: LogicalSize = LogicalSize { + width: 420, + height: 602, + }; + pub const COMPACT: LogicalSize = LogicalSize { + width: 420, + height: 80, + }; } -static ORIGINAL_SIZE: Mutex> = Mutex::new(WindowSize::MIN); +static ORIGINAL_SIZE: Mutex> = Mutex::new(WindowSize::MIN); static HAS_DECORATIONS: Mutex = Mutex::new(true); static IS_COMPACT: Mutex = Mutex::new(false); @@ -55,8 +60,15 @@ fn set_always_on_top(always_on_top: bool, window: tauri::Window) } #[tauri::command] -fn set_fullscreen_break(should_fullscreen: bool, always_on_top: bool, window: tauri::Window) { - println!("set_fullscreen_break! {} {}", should_fullscreen, always_on_top); +fn set_fullscreen_break( + should_fullscreen: bool, + always_on_top: bool, + window: tauri::Window, +) { + println!( + "set_fullscreen_break! {} {}", + should_fullscreen, always_on_top + ); try_set_always_on_top(always_on_top, &window); if should_fullscreen { try_set_native_titlebar(true, &window); @@ -84,14 +96,15 @@ fn try_set_always_on_top(always_on_top: bool, window: &tauri::Window } } -fn try_set_min_size(size: Option>, window: &tauri::Window) { - match window.set_min_size(size) {//Some(size)) { +fn try_set_min_size(size: Option>, window: &tauri::Window) { + match window.set_min_size(size) { + //Some(size)) { Ok(_) => (), Err(e) => println!("There was a problem setting min size! {:?}", e), } } -fn try_set_size(size: PhysicalSize, window: &tauri::Window) { +fn try_set_size(size: LogicalSize, window: &tauri::Window) { match window.set_size(size) { Ok(_) => (), Err(e) => println!("There was a problem setting the window size! {:?}", e), @@ -103,27 +116,33 @@ fn try_set_resizeable(resizeable: bool, window: &tauri::Window) { println!("Window resizeable {:?}", resizeable); match window.set_resizable(resizeable) { Ok(_) => (), - Err(e) => println!("There was a problem making the window resizeable {:?}", e) + Err(e) => println!("There was a problem making the window resizeable {:?}", e), } } // Atm resizeable seems to cause a problem where the size cannot be set smaller than a certain amount // https://github.com/tauri-apps/tao/issues/561, just use resizeable until this is fixed. -fn set_window_fixed_size(size: PhysicalSize, window: &tauri::Window) { +fn set_window_fixed_size(size: LogicalSize, window: &tauri::Window) { let decorations = HAS_DECORATIONS.lock().unwrap(); let new_height = size.height + (if *decorations { 0 } else { 34 }); - let new_size = PhysicalSize { width : size.width, height: new_height}; + let new_size = LogicalSize { + width: size.width, + height: new_height, + }; try_set_size(new_size, window); try_set_resizeable(false, window); } -fn set_window_resizeable(min_size: PhysicalSize, size: PhysicalSize, window: &tauri::Window) { +fn set_window_resizeable( + min_size: LogicalSize, + size: LogicalSize, + window: &tauri::Window, +) { try_set_resizeable(true, window); try_set_size(size, window); try_set_min_size(Some(min_size), window); } - #[tauri::command] fn set_compact_mode(compact_mode: bool, window: tauri::Window) { { @@ -134,11 +153,18 @@ fn set_compact_mode(compact_mode: bool, window: tauri::Window) { if compact_mode { { let mut size = ORIGINAL_SIZE.lock().unwrap(); - *size = window.outer_size().unwrap().clone(); + *size = window + .outer_size() + .unwrap() + .to_logical(window.scale_factor().unwrap()); } set_window_fixed_size(WindowSize::COMPACT, &window); } else { - set_window_resizeable(WindowSize::MIN, ORIGINAL_SIZE.lock().unwrap().clone(), &window); + set_window_resizeable( + WindowSize::MIN, + ORIGINAL_SIZE.lock().unwrap().clone(), + &window, + ); } } @@ -164,28 +190,38 @@ fn set_native_titlebar(use_native_titlebar: bool, window: tauri::Win fn try_set_native_titlebar(use_native_titlebar: bool, window: &tauri::Window) { match window.set_decorations(use_native_titlebar) { Ok(_) => (), - Err(e) => println!("There was a problem setting the window decorations! {:?}", e), + Err(e) => println!( + "There was a problem setting the window decorations! {:?}", + e + ), } println!("set_native_titlebar! {}", use_native_titlebar); } - /** - * We could do this by passing the object into a custom function that adds the commands but I wanted - * to practice more with rust. Plus it makes the setup cleaner. +* We could do this by passing the object into a custom function that adds the commands but I wanted +* to practice more with rust. Plus it makes the setup cleaner. - * Switch to a function that takes and returns tauri::Builder or uses a reference if we need to - * switch it. - */ +* Switch to a function that takes and returns tauri::Builder or uses a reference if we need to +* switch it. +*/ pub trait PomatezCommands { fn register_pomatez_commands(self) -> tauri::Builder; } impl PomatezCommands for Builder { fn register_pomatez_commands(self) -> tauri::Builder { - self.invoke_handler(tauri::generate_handler![set_show, set_always_on_top, - set_fullscreen_break, set_compact_mode, set_ui_theme, set_native_titlebar, - system_tray::tray_icon_update, set_close, set_minimize, updater::check_for_updates, + self.invoke_handler(tauri::generate_handler![ + set_show, + set_always_on_top, + set_fullscreen_break, + set_compact_mode, + set_ui_theme, + set_native_titlebar, + system_tray::tray_icon_update, + set_close, + set_minimize, + updater::check_for_updates, updater::install_update ]) }