Skip to content

Commit cc29c82

Browse files
fix: add minimum size clamps and restore validation for window sizing (#2392)
- Add MIN_MAIN_WIDTH/HEIGHT and MIN_ONBOARDING_WIDTH/HEIGHT constants - Update window_size_with_ratio to enforce minimum sizes - Add explicit min_inner_size to window builders - Validate restored window size after restore_state to prevent tiny windows This fixes issues where windows could become unusably small in screen mirroring scenarios or when bad sizes were persisted across DPI changes. Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: yujonglee <[email protected]>
1 parent bfd6dfc commit cc29c82

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

plugins/windows/src/ext.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,26 @@ impl AppWindow {
9191
let window = self.build_window(app)?;
9292

9393
if matches!(self, Self::Main) {
94+
use tauri::LogicalSize;
9495
use tauri_plugin_window_state::{StateFlags, WindowExt};
96+
9597
let _ = window.restore_state(StateFlags::SIZE);
98+
99+
if let Ok(size) = window.inner_size() {
100+
let scale = window.scale_factor().unwrap_or(1.0);
101+
let logical_width = size.width as f64 / scale;
102+
let logical_height = size.height as f64 / scale;
103+
104+
const MIN_WIDTH: f64 = 620.0;
105+
const MIN_HEIGHT: f64 = 500.0;
106+
107+
if logical_width < MIN_WIDTH || logical_height < MIN_HEIGHT {
108+
let _ = window.set_size(LogicalSize::new(
109+
logical_width.max(MIN_WIDTH),
110+
logical_height.max(MIN_HEIGHT),
111+
));
112+
}
113+
}
96114
}
97115

98116
window.show()?;

plugins/windows/src/window/v1.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,21 @@ impl AppWindow {
9191

9292
const MAX_MAIN_WIDTH: f64 = 1600.0;
9393
const MAX_MAIN_HEIGHT: f64 = 1000.0;
94+
const MIN_MAIN_WIDTH: f64 = 620.0;
95+
const MIN_MAIN_HEIGHT: f64 = 500.0;
96+
9497
const MAX_ONBOARDING_WIDTH: f64 = 900.0;
9598
const MAX_ONBOARDING_HEIGHT: f64 = 700.0;
99+
const MIN_ONBOARDING_WIDTH: f64 = 400.0;
100+
const MIN_ONBOARDING_HEIGHT: f64 = 600.0;
96101

97102
fn window_size_with_ratio(
98103
monitor_width: f64,
99104
monitor_height: f64,
100105
aspect_ratio: f64,
101106
scale: f64,
107+
min_width: f64,
108+
min_height: f64,
102109
max_width: f64,
103110
max_height: f64,
104111
) -> (f64, f64) {
@@ -112,14 +119,16 @@ fn window_size_with_ratio(
112119
(height * aspect_ratio, height)
113120
};
114121

115-
if width > max_width || height > max_height {
122+
let (width, height) = if width > max_width || height > max_height {
116123
let scale_w = max_width / width;
117124
let scale_h = max_height / height;
118125
let scale = scale_w.min(scale_h);
119126
(width * scale, height * scale)
120127
} else {
121128
(width, height)
122-
}
129+
};
130+
131+
(width.max(min_width), height.max(min_height))
123132
}
124133

125134
impl WindowImpl for AppWindow {
@@ -157,13 +166,16 @@ impl WindowImpl for AppWindow {
157166
monitor_height,
158167
2.0 / 3.0,
159168
0.7,
169+
MIN_ONBOARDING_WIDTH,
170+
MIN_ONBOARDING_HEIGHT,
160171
MAX_ONBOARDING_WIDTH,
161172
MAX_ONBOARDING_HEIGHT,
162173
);
163174

164175
self.window_builder(app, "/app/onboarding")
165176
.resizable(false)
166177
.inner_size(width, height)
178+
.min_inner_size(MIN_ONBOARDING_WIDTH, MIN_ONBOARDING_HEIGHT)
167179
.prevent_overflow_with_margin(margin)
168180
.center()
169181
.build()?
@@ -174,23 +186,17 @@ impl WindowImpl for AppWindow {
174186
monitor_height,
175187
4.0 / 3.0,
176188
0.8,
189+
MIN_MAIN_WIDTH,
190+
MIN_MAIN_HEIGHT,
177191
MAX_MAIN_WIDTH,
178192
MAX_MAIN_HEIGHT,
179193
);
180-
let (min_w, min_h) = window_size_with_ratio(
181-
monitor_width,
182-
monitor_height,
183-
4.0 / 3.0,
184-
0.4,
185-
MAX_MAIN_WIDTH * 0.5,
186-
MAX_MAIN_HEIGHT * 0.5,
187-
);
188194

189195
self.window_builder(app, "/app/main")
190196
.maximizable(true)
191197
.minimizable(true)
192198
.inner_size(width, height)
193-
.min_inner_size(min_w, min_h)
199+
.min_inner_size(MIN_MAIN_WIDTH, MIN_MAIN_HEIGHT)
194200
.prevent_overflow_with_margin(margin)
195201
.center()
196202
.build()?

0 commit comments

Comments
 (0)