fix(window): 启动时把窗口夹进当前屏幕,修「画面被顶起来」
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m21s

用户两次反馈:底部工具栏跑到屏幕中间、左右栏底部被拉长。之前按「文档滚动」修过(html/body overflow:hidden),
仍复现 —— 说明不是滚动,而是**窗口本身比当前屏幕还高**:
src-tauri 的 restore_geom 会恢复上次窗口尺寸,只校验中心点是否落在某块显示器内,不夹尺寸。
换显示器 / 远程会话分辨率变小后,恢复出超大窗口 ⇒ 底部工具栏与侧栏底部落到屏幕外,看起来就是「被顶起来」。

改动:
- src-tauri/src/lib.rs:新增 clamp_geom(win, min_w, min_h) —— 取窗口中心所在显示器,
  把宽高夹到 屏幕尺寸-40px(不小于窗口下限),并把位置夹回屏幕内;
  restore_geom 末尾调用一次,无历史几何时在 setup 里也调用一次
- src/runtime-boot-load.js:启动时往 _boot.log 记一行 viewport/screen/dpr,
  下次再出现可直接判断是不是窗口比屏幕大
This commit is contained in:
qiqi committed 2026-11-28 00:31:33 +08:00
1 parent ab3833ade7
commit 0b46512667
3 files changed
+51 -1

No files matched your search

+48
View File
@@ -60,6 +60,51 @@ fn save_geom(app: &AppHandle, label: &str) {
}
// 恢复上次窗口大小与位置;中心点不在任何显示器内就只恢复尺寸(换过显示器不至于把窗口丢到屏幕外)
fn clamp_geom(win: &tauri::Window, min_w: u32, min_h: u32) {
// v3.26.109:窗口不能被“恢复”成比当前屏幕还大。
// 换显示器 / 远程会话分辨率变小后,恢复出的超大窗口会让底部工具栏与左右栏底部落到屏幕外,
// 看起来就是“画面被顶起来、工具栏跑到中间”(2026-11-27 用户两次报告)。
let Ok(size) = win.outer_size() else { return };
let monitors = win.available_monitors().unwrap_or_default();
if monitors.is_empty() {
return;
}
let pos = win.outer_position().unwrap_or(PhysicalPosition::new(0, 0));
let cx = pos.x + size.width as i32 / 2;
let cy = pos.y + size.height as i32 / 2;
let mut target = None;
for m in &monitors {
let mp = m.position();
let ms = m.size();
if cx >= mp.x && cx <= mp.x + ms.width as i32 && cy >= mp.y && cy <= mp.y + ms.height as i32 {
target = Some((mp.x, mp.y, ms.width, ms.height));
break;
}
}
let Some((mx, my, mw, mh)) = target.or_else(|| {
monitors.first().map(|m| {
let mp = m.position();
let ms = m.size();
(mp.x, mp.y, ms.width, ms.height)
})
}) else {
return;
};
const MARGIN: u32 = 40;
let max_w = mw.saturating_sub(MARGIN).max(min_w);
let max_h = mh.saturating_sub(MARGIN).max(min_h);
let w = size.width.min(max_w);
let h = size.height.min(max_h);
if w != size.width || h != size.height {
let _ = win.set_size(PhysicalSize::new(w, h));
}
let x = pos.x.max(mx).min(mx + mw as i32 - w as i32);
let y = pos.y.max(my).min(my + mh as i32 - h as i32);
if x != pos.x || y != pos.y {
let _ = win.set_position(PhysicalPosition::new(x, y));
}
}
fn restore_geom(app: &AppHandle, label: &str, min_w: u32, min_h: u32) {
let Some(win) = app.get_window(label) else {
return;
@@ -96,6 +141,7 @@ fn restore_geom(app: &AppHandle, label: &str, min_w: u32, min_h: u32) {
if visible {
let _ = win.set_position(PhysicalPosition::new(g.x, g.y));
}
clamp_geom(&win, min_w, min_h);
}
fn show_main(app: &AppHandle) {
@@ -1547,6 +1593,8 @@ pub fn run() {
restore_file_grants(app.handle());
// 恢复上次窗口大小/位置(主窗口下限与 tauri.conf 的 minWidth/minHeight 对齐)
restore_geom(app.handle(), "main", 1024, 640);
// 没有历史几何时也要夹一次:默认尺寸可能大于当前屏幕(远程/小屏场景)
if let Some(w) = app.handle().get_window("main") { clamp_geom(&w, 1024, 640); }
build_tray(app.handle())?;
// macOS 必须有应用菜单,否则 Cmd+C / Cmd+V / Cmd+X 根本不会进 WebView(Tauri 已知问题)
#[cfg(target_os = "macos")]