Skip to content

Commit 3d27909

Browse files
cauyxyFabianLars
andauthored
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 <fabianlars@fabianlars.de>
1 parent 6c7a4c0 commit 3d27909

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

.changes/tray-position.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"positioner": patch
3+
---
4+
5+
`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.

plugins/positioner/src/ext.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,20 @@ impl<R: Runtime> WindowExt for Window<R> {
107107
},
108108
#[cfg(feature = "system-tray")]
109109
TrayLeft => {
110-
if let Some((tray_x, tray_y)) = tray_position {
111-
PhysicalPosition {
112-
x: tray_x,
113-
y: tray_y - window_size.height,
114-
}
110+
if let (Some((tray_x, tray_y)), Some((_, _tray_height))) =
111+
(tray_position, tray_size)
112+
{
113+
let y = tray_y - window_size.height;
114+
// Choose y value based on the target OS
115+
#[cfg(target_os = "windows")]
116+
let y = if y < 0 { tray_y + _tray_height } else { y };
117+
118+
#[cfg(target_os = "macos")]
119+
let y = if y < 0 { tray_y } else { y };
120+
121+
PhysicalPosition { x: tray_x, y }
115122
} else {
116-
panic!("tray position not set");
123+
panic!("Tray position not set");
117124
}
118125
}
119126
#[cfg(feature = "system-tray")]
@@ -129,11 +136,20 @@ impl<R: Runtime> WindowExt for Window<R> {
129136
}
130137
#[cfg(feature = "system-tray")]
131138
TrayRight => {
132-
if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size)
139+
if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) =
140+
(tray_position, tray_size)
133141
{
142+
let y = tray_y - window_size.height;
143+
// Choose y value based on the target OS
144+
#[cfg(target_os = "windows")]
145+
let y = if y < 0 { tray_y + _tray_height } else { y };
146+
147+
#[cfg(target_os = "macos")]
148+
let y = if y < 0 { tray_y } else { y };
149+
134150
PhysicalPosition {
135151
x: tray_x + tray_width,
136-
y: tray_y - window_size.height,
152+
y,
137153
}
138154
} else {
139155
panic!("Tray position not set");
@@ -153,12 +169,19 @@ impl<R: Runtime> WindowExt for Window<R> {
153169
}
154170
#[cfg(feature = "system-tray")]
155171
TrayCenter => {
156-
if let (Some((tray_x, tray_y)), Some((tray_width, _))) = (tray_position, tray_size)
172+
if let (Some((tray_x, tray_y)), Some((tray_width, _tray_height))) =
173+
(tray_position, tray_size)
157174
{
158-
PhysicalPosition {
159-
x: tray_x + (tray_width / 2) - (window_size.width / 2),
160-
y: tray_y - window_size.height,
161-
}
175+
let x = tray_x + tray_width / 2 - window_size.width / 2;
176+
let y = tray_y - window_size.height;
177+
// Choose y value based on the target OS
178+
#[cfg(target_os = "windows")]
179+
let y = if y < 0 { tray_y + _tray_height } else { y };
180+
181+
#[cfg(target_os = "macos")]
182+
let y = if y < 0 { tray_y } else { y };
183+
184+
PhysicalPosition { x, y }
162185
} else {
163186
panic!("Tray position not set");
164187
}

0 commit comments

Comments
 (0)