Skip to content

Commit c6f8225

Browse files
author
Carl
committed
Keep restored desktop window headers on a connected monitor
Signed-off-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz>
1 parent 82e83e8 commit c6f8225

3 files changed

Lines changed: 179 additions & 6 deletions

File tree

‎docs/shell-design.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ directory (`.window-state.json`); missing or unreadable state falls back to the
118118
configured 1200×800 window. Visibility and decorations are not restored, so a
119119
macOS window hidden by Close reopens visibly after Quit/relaunch and platform
120120
chrome stays configuration-owned. Close/reopen without quitting on macOS still
121-
uses the existing window. When a saved position no longer intersects an attached
122-
monitor, the plugin leaves placement to the OS. Linux window placement remains
123-
subject to the window manager/compositor (notably Wayland).
121+
uses the existing window. Before restoring maximized/fullscreen mode, Buzz checks
122+
that the entire 48px header fits within a connected monitor's work area. An
123+
unreachable header falls back to the top of the primary work area (or the first
124+
available monitor), shrinking an oversized frame to fit. This covers partial
125+
overlap after removing a monitor, not just fully off-screen windows. Linux
126+
placement remains subject to the window manager/compositor (notably Wayland).
127+
The plugin stores physical pixels, so changing display scaling can change the
128+
window's apparent size. On macOS, quitting in fullscreen can preserve the
129+
fullscreen-sized frame instead of the earlier normal size; leaving fullscreen
130+
after relaunch may therefore produce a screen-sized window.
124131

125132
The top-right group contains enabled plugin launchers, a page finder, and the local
126133
avatar. Search, sidebar and history controls use unfilled ghost icon buttons with

‎src-tauri/src/lib.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ pub fn run() {
513513
.plugin(tauri_plugin_dialog::init())
514514
.plugin(tauri_plugin_opener::init())
515515
.setup(|app| {
516+
if let Some(window) = app.get_window("main") {
517+
if let Err(error) = window_state::restore(&window) {
518+
eprintln!("Could not restore Buzz window: {error}");
519+
}
520+
}
516521
#[cfg(target_os = "macos")]
517522
notifications::macos::init();
518523
deep_links::setup(app.handle());

‎src-tauri/src/window_state.rs‎

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use tauri_plugin_window_state::{Builder, StateFlags};
1+
use tauri::{PhysicalPosition, PhysicalRect, PhysicalSize, Runtime, Window};
2+
use tauri_plugin_window_state::{Builder, StateFlags, WindowExt};
23

34
pub(crate) fn builder() -> Builder {
45
Builder::default()
@@ -11,6 +12,84 @@ pub(crate) fn builder() -> Builder {
1112
| StateFlags::FULLSCREEN,
1213
)
1314
.with_filter(|label| label == "main")
15+
// Restore geometry and validate reachability before entering a saved mode.
16+
.skip_initial_state("main")
17+
}
18+
19+
// Called from app setup, after the configured main window has been created.
20+
pub(crate) fn restore<R: Runtime>(window: &Window<R>) -> tauri::Result<()> {
21+
window.restore_state(StateFlags::SIZE | StateFlags::POSITION)?;
22+
let monitors = window.available_monitors()?;
23+
let areas: Vec<_> = monitors
24+
.iter()
25+
.map(|monitor| *monitor.work_area())
26+
.collect();
27+
let fallback = window
28+
.primary_monitor()?
29+
.map(|monitor| *monitor.work_area())
30+
.or_else(|| areas.first().copied());
31+
if let Some(fallback) = fallback {
32+
let frame = PhysicalRect {
33+
position: window.outer_position()?,
34+
size: window.outer_size()?,
35+
};
36+
// AppShell owns a 48 logical-pixel header on all desktop platforms.
37+
let header_height = (48.0 * window.scale_factor()?).ceil() as u32;
38+
if let Some(target) = reachable_frame(frame, header_height, &areas, fallback) {
39+
if target.size != frame.size {
40+
let inner = window.inner_size()?;
41+
window.set_size(PhysicalSize::new(
42+
target
43+
.size
44+
.width
45+
.saturating_sub(frame.size.width.saturating_sub(inner.width)),
46+
target
47+
.size
48+
.height
49+
.saturating_sub(frame.size.height.saturating_sub(inner.height)),
50+
))?;
51+
}
52+
window.set_position(target.position)?;
53+
}
54+
}
55+
window.restore_state(StateFlags::MAXIMIZED | StateFlags::FULLSCREEN)
56+
}
57+
58+
fn reachable_frame(
59+
frame: PhysicalRect<i32, u32>,
60+
header_height: u32,
61+
areas: &[PhysicalRect<i32, u32>],
62+
fallback: PhysicalRect<i32, u32>,
63+
) -> Option<PhysicalRect<i32, u32>> {
64+
let x = i64::from(frame.position.x);
65+
let y = i64::from(frame.position.y);
66+
let reachable = areas.iter().any(|area| {
67+
x >= i64::from(area.position.x)
68+
&& y >= i64::from(area.position.y)
69+
&& x + i64::from(frame.size.width)
70+
<= i64::from(area.position.x) + i64::from(area.size.width)
71+
&& y + i64::from(header_height)
72+
<= i64::from(area.position.y) + i64::from(area.size.height)
73+
});
74+
if reachable {
75+
return None;
76+
}
77+
// Unlike any-corner intersection, this keeps the drag strip and both sets
78+
// of platform controls on-screen, including after removing an upper monitor.
79+
let size = PhysicalSize::new(
80+
frame.size.width.min(fallback.size.width),
81+
frame.size.height.min(fallback.size.height),
82+
);
83+
Some(PhysicalRect {
84+
position: PhysicalPosition::new(
85+
fallback
86+
.position
87+
.x
88+
.saturating_add(((fallback.size.width - size.width) / 2) as i32),
89+
fallback.position.y,
90+
),
91+
size,
92+
})
1493
}
1594

1695
#[cfg(test)]
@@ -23,6 +102,83 @@ mod tests {
23102
};
24103
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
25104

105+
fn rect(x: i32, y: i32, width: u32, height: u32) -> tauri::PhysicalRect<i32, u32> {
106+
tauri::PhysicalRect {
107+
position: tauri::PhysicalPosition::new(x, y),
108+
size: tauri::PhysicalSize::new(width, height),
109+
}
110+
}
111+
112+
fn assert_frame(
113+
actual: Option<tauri::PhysicalRect<i32, u32>>,
114+
expected: Option<tauri::PhysicalRect<i32, u32>>,
115+
) {
116+
assert_eq!(
117+
actual.map(|r| (r.position, r.size)),
118+
expected.map(|r| (r.position, r.size))
119+
);
120+
}
121+
122+
#[test]
123+
fn partial_overlap_after_removing_upper_monitor_recovers_entire_header() {
124+
let laptop = rect(0, 25, 1440, 875);
125+
let saved = rect(100, -450, 960, 640);
126+
assert_frame(
127+
super::reachable_frame(saved, 48, &[laptop], laptop),
128+
Some(rect(240, 25, 960, 640)),
129+
);
130+
let upper = rect(0, -900, 1440, 900);
131+
assert_frame(
132+
super::reachable_frame(saved, 48, &[laptop, upper], laptop),
133+
None,
134+
);
135+
}
136+
137+
#[test]
138+
fn reachable_geometry_is_unchanged_including_negative_coordinates() {
139+
let primary = rect(0, 25, 1440, 875);
140+
let left = rect(-1920, 0, 1920, 1080);
141+
for saved in [rect(100, 120, 960, 640), rect(-1800, 80, 960, 640)] {
142+
assert_frame(
143+
super::reachable_frame(saved, 48, &[primary, left], primary),
144+
None,
145+
);
146+
}
147+
}
148+
149+
#[test]
150+
fn side_overlap_and_menu_bar_occlusion_recover_controls_not_just_a_corner() {
151+
let area = rect(0, 25, 1440, 875);
152+
for saved in [
153+
rect(-600, 100, 960, 640),
154+
rect(1300, 100, 960, 640),
155+
rect(100, 0, 960, 640),
156+
rect(100, 860, 960, 640),
157+
] {
158+
assert_frame(
159+
super::reachable_frame(saved, 48, &[area], area),
160+
Some(rect(240, 25, 960, 640)),
161+
);
162+
}
163+
}
164+
165+
#[test]
166+
fn oversized_window_fits_remaining_work_area_and_scaled_header_is_checked() {
167+
let area = rect(0, 50, 1440, 850);
168+
assert_frame(
169+
super::reachable_frame(rect(0, 50, 2800, 1800), 96, &[area], area),
170+
Some(area),
171+
);
172+
assert_frame(
173+
super::reachable_frame(rect(100, 820, 960, 640), 48, &[area], area),
174+
None,
175+
);
176+
assert_frame(
177+
super::reachable_frame(rect(100, 820, 960, 640), 96, &[area], area),
178+
Some(rect(240, 50, 960, 640)),
179+
);
180+
}
181+
26182
fn fixture() -> (tempfile::TempDir, tauri::App<MockRuntime>) {
27183
let dir = tempfile::tempdir().unwrap();
28184
let mut context = mock_context(noop_assets());
@@ -41,7 +197,10 @@ mod tests {
41197
plugin.initialize(app.handle(), Value::Null).unwrap();
42198
for label in ["main", "other"] {
43199
let window = tauri::WindowBuilder::new(&app, label).build().unwrap();
44-
plugin.window_created(window);
200+
plugin.window_created(window.clone());
201+
if label == "main" {
202+
super::restore(&window).unwrap();
203+
}
45204
}
46205
plugin.on_event(app.handle(), &tauri::RunEvent::Exit);
47206
let state: Value = serde_json::from_slice(
@@ -68,6 +227,7 @@ mod tests {
68227
.build()
69228
.unwrap();
70229
plugin.window_created(view.as_ref().window().clone());
230+
super::restore(&view.as_ref().window()).unwrap();
71231
app.handle().save_window_state(StateFlags::empty()).unwrap();
72232
let loaded: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
73233
assert_eq!(loaded, saved);
@@ -99,7 +259,8 @@ mod tests {
99259
let mut plugin = super::builder().build();
100260
plugin.initialize(app.handle(), Value::Null).unwrap();
101261
let window = tauri::WindowBuilder::new(&app, "main").build().unwrap();
102-
plugin.window_created(window);
262+
plugin.window_created(window.clone());
263+
super::restore(&window).unwrap();
103264
plugin.on_event(app.handle(), &tauri::RunEvent::Exit);
104265
let state: Value = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();
105266
assert!(state.get("main").is_some());

0 commit comments

Comments
 (0)