From 3d279094d44be78cdc5d1de3938f1414e13db6b0 Mon Sep 17 00:00:00 2001 From: Xinyu Yang Date: Wed, 27 Sep 2023 21:55:44 +0800 Subject: [PATCH] fix(positioner): Prevent tray relative windows from being moved off-screen (#291) * feat: add system tray positioning method * apply patch * remove pos * fix linter * fix cargo fmt * linter * add changefile * Update and rename stronghold-tray-position.md to tray-position.md --------- Co-authored-by: Fabian-Lars --- .changes/tray-position.md | 5 ++++ plugins/positioner/src/ext.rs | 49 +++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 .changes/tray-position.md diff --git a/.changes/tray-position.md b/.changes/tray-position.md new file mode 100644 index 00000000..c1fa71cb --- /dev/null +++ b/.changes/tray-position.md @@ -0,0 +1,5 @@ +--- +"positioner": patch +--- + +`TrayLeft`, `TrayRight` and `TrayCenter` will now position the window according to the tray position relative to the monitor dimensions to prevent windows being displayed partially off-screen. diff --git a/plugins/positioner/src/ext.rs b/plugins/positioner/src/ext.rs index 01d34cf5..2e97c475 100644 --- a/plugins/positioner/src/ext.rs +++ b/plugins/positioner/src/ext.rs @@ -107,13 +107,20 @@ impl WindowExt for Window { }, #[cfg(feature = "system-tray")] TrayLeft => { - if let Some((tray_x, tray_y)) = tray_position { - PhysicalPosition { - x: tray_x, - y: tray_y - window_size.height, - } + if let (Some((tray_x, tray_y)), Some((_, _tray_height))) = + (tray_position, tray_size) + { + let y = tray_y - window_size.height; + // Choose y value based on the target OS + #[cfg(target_os = "windows")] + let y = if y < 0 { tray_y + _tray_height } else { y }; + + #[cfg(target_os = "macos")] + let y = if y < 0 { tray_y } else { y }; + + PhysicalPosition { x: tray_x, y } } else { - panic!("tray position not set"); + panic!("Tray position not set"); } } #[cfg(feature = "system-tray")] @@ -129,11 +136,20 @@ impl WindowExt for Window { } #[cfg(feature = "system-tray")] TrayRight => { - if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) + if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) = + (tray_position, tray_size) { + let y = tray_y - window_size.height; + // Choose y value based on the target OS + #[cfg(target_os = "windows")] + let y = if y < 0 { tray_y + _tray_height } else { y }; + + #[cfg(target_os = "macos")] + let y = if y < 0 { tray_y } else { y }; + PhysicalPosition { x: tray_x + tray_width, - y: tray_y - window_size.height, + y, } } else { panic!("Tray position not set"); @@ -153,12 +169,19 @@ impl WindowExt for Window { } #[cfg(feature = "system-tray")] TrayCenter => { - if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size) + if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) = + (tray_position, tray_size) { - PhysicalPosition { - x: tray_x + (tray_width / 2) - (window_size.width / 2), - y: tray_y - window_size.height, - } + let x = tray_x + tray_width / 2 - window_size.width / 2; + let y = tray_y - window_size.height; + // Choose y value based on the target OS + #[cfg(target_os = "windows")] + let y = if y < 0 { tray_y + _tray_height } else { y }; + + #[cfg(target_os = "macos")] + let y = if y < 0 { tray_y } else { y }; + + PhysicalPosition { x, y } } else { panic!("Tray position not set"); }