fix(canvas-webview): 网页压住地址栏(原生/ DOM 坐标原点差)、内容跟随画布缩放

用户反馈:地址栏和前进/后退/刷新按钮被网页盖住,只剩左上角一小块;
缩小画布后网页内容不跟着缩,反而按原宽度重排。

根因一:坐标原点差
  add_child / set_bounds 的 LogicalPosition 以**窗口边框**左上角为原点,
  而 getBoundingClientRect() 以**内容视图**左上角为原点 —— macOS 标准标题栏下差 32pt。
  于是每个画布网页整体上浮 32px,正好压住 34px 高的工具栏。
  最小 Tauri 工程实测:窗口 frame=1000x760、DOM innerHeight=728;子视图 y 传 200
  实际落在 168;把 y 改成 232 后红框精确落在内容原点+(300,200)。
  修法:新增 canvas_webview_metrics 回报窗口边框尺寸,渲染层用它减 window.innerHeight
  得到补偿值,推 rect 时加上(全屏时标题栏消失,差值自动回 0)。

根因二:页面 zoom 恒为 1
  rect 跟着画布缩,但页面缩放没变 ⇒ 视口变窄后网页重排,看起来「内容没缩小」。
  现在 zoom = 画布缩放 × 节点缩放(新命令参数 + Webview::set_zoom),
  网页像图片一样跟着画布一起缩放。

其它:
- 引擎把原生视图裁到「节点 ∩ 画布」,且上边界不越过工具栏底 —— 布局再变也压不住按钮
- 取消「选中时内缩 12px」:缩放手柄在节点外侧,本来就不用让位;点选不再让网页重排
- 尺寸推送从「只等静止」改为「静止优先 + 连续变化时每 120ms 跟一次」,缩放过程不脱节
- 暂停语义:插件写 data-iwb-cwv-paused,宿主关掉视图并保持关闭(✕ 才真的能关)
- 诊断:ensure/bounds 日志带上 off/zoom;page:started/finished 回传加载态与真实地址

版本 3.25.29
This commit is contained in:
qiqi committed 2026-11-19 07:13:51 +08:00
1 parent dc7c1cc65a
commit 596d5fdf82
7 files changed
+314 -71

No files matched your search

+66 -10
View File
@@ -508,14 +508,40 @@ fn cwv_parse(url: &str) -> Result<url::Url, String> {
}
// 位置 + 尺寸打包成一次 set_bounds 的入参。
fn cwv_rect(x: f64, y: f64, w: f64, h: f64) -> tauri::Rect {
//
// ox/oy 是「原生层坐标原点到 DOM 坐标原点」的差值(见 canvas_webview_metrics 的注释)。
// 渲染层量到的是内容视图坐标,而 add_child/set_bounds 用的是**窗口边框**坐标,
// 两者相差一个标题栏高度 —— 不补这一下,网页会整体上浮盖住地址栏。
fn cwv_rect(x: f64, y: f64, w: f64, h: f64, ox: f64, oy: f64) -> tauri::Rect {
tauri::Rect {
position: tauri::Position::Logical(tauri::LogicalPosition::new(x, y)),
position: tauri::Position::Logical(tauri::LogicalPosition::new(x + ox, y + oy)),
// 下限 2px:wry/WebKit 对 0 尺寸视图的处理不稳定,1px 也没有任何显示价值
size: tauri::Size::Logical(tauri::LogicalSize::new(w.max(2.0), h.max(2.0))),
}
}
// 窗口边框尺寸 vs 内容视图尺寸。
//
// **为什么需要它(v3.25.29 实测)**:`add_child` / `set_bounds` 的 LogicalPosition
// 以**窗口边框左上角**为原点,而 `getBoundingClientRect()` 以**内容视图左上角**为原点。
// macOS 标准标题栏下两者差 32pt,于是每个画布网页都整体上移 32px —— 正好把地址栏、
// 前进/后退/刷新按钮全部盖住(用户看到的就是「按钮被遮住了」)。
// 最小工程实测:窗口 frame=1000x760,DOM innerHeight=728,子视图 y 传 200 实际落在 168。
//
// 这里只回报「窗口边框」尺寸,差值由渲染层用 window.innerHeight 算 —— 全屏时标题栏消失,
// 差值自然回到 0,不需要额外分支。
#[tauri::command]
fn canvas_webview_metrics(app: AppHandle) -> Result<serde_json::Value, String> {
let win = app.get_window("main").ok_or_else(|| "主窗口不存在".to_string())?;
let size = win.inner_size().map_err(|e| e.to_string())?;
let scale = win.scale_factor().unwrap_or(1.0);
Ok(serde_json::json!({
"frameW": size.width as f64 / scale,
"frameH": size.height as f64 / scale,
"scale": scale,
}))
}
// 确保某节点对应的子 webview 存在。返回 true = 这次真的新建了。
#[tauri::command]
fn canvas_webview_ensure(
@@ -526,14 +552,20 @@ fn canvas_webview_ensure(
y: f64,
w: f64,
h: f64,
ox: f64,
oy: f64,
zoom: Option<f64>,
) -> Result<bool, String> {
let parsed = cwv_parse(&url)?;
let label = cwv_label(&id);
if let Some(wv) = app.get_webview(&label) {
// 已存在:只校正 bounds(不 show/hide —— 见文件头约束 1)
if let Err(e) = wv.set_bounds(cwv_rect(x, y, w, h)) {
if let Err(e) = wv.set_bounds(cwv_rect(x, y, w, h, ox, oy)) {
cwv_log(&app, &format!("ensure:bounds-err label={} err={}", label, e));
}
if let Some(z) = zoom {
let _ = wv.set_zoom(z);
}
return Ok(false);
}
// 必须用 get_window 而不是 get_webview_window:后者要求 `is_webview_window()`,
@@ -560,18 +592,28 @@ fn canvas_webview_ensure(
serde_json::json!({ "id": id, "kind": kind, "url": payload.url().to_string() }),
);
}),
tauri::LogicalPosition::new(x, y),
tauri::LogicalPosition::new(x + ox, y + oy),
tauri::LogicalSize::new(w.max(2.0), h.max(2.0)),
);
match built {
Ok(wv) => {
// add_child 传入的 bounds 在部分 macOS 版本上不立刻生效,用 set_bounds 再摆一次
if let Err(e) = wv.set_bounds(cwv_rect(x, y, w, h)) {
if let Err(e) = wv.set_bounds(cwv_rect(x, y, w, h, ox, oy)) {
cwv_log(&app, &format!("ensure:post-bounds-err label={} err={}", label, e));
}
// 缩放:页面 CSS 像素按 zoom 放大/缩小,配合「矩形随画布缩放」就能让网页
// 像图片一样跟着画布一起缩放,而不是一直按原宽度重排。
if let Some(z) = zoom {
if let Err(e) = wv.set_zoom(z) {
cwv_log(&app, &format!("ensure:zoom-err label={} zoom={} err={}", label, z, e));
}
}
cwv_log(
&app,
&format!("ensure:ok label={} url={} rect=({:.0},{:.0} {:.0}x{:.0})", label, parsed, x, y, w, h),
&format!(
"ensure:ok label={} url={} rect=({:.0},{:.0} {:.0}x{:.0}) off=({:.0},{:.0}) zoom={:?}",
label, parsed, x, y, w, h, ox, oy, zoom
),
);
Ok(true)
}
@@ -593,6 +635,9 @@ fn canvas_webview_bounds(
w: f64,
h: f64,
visible: bool,
zoom: Option<f64>,
ox: f64,
oy: f64,
) -> Result<bool, String> {
let label = cwv_label(&id);
let Some(wv) = app.get_webview(&label) else {
@@ -600,19 +645,28 @@ fn canvas_webview_bounds(
};
// 隐藏 = 停到窗口外,尺寸保持不变(别裁到 1px:那会触发 WebKit 重排)
let rect = if visible {
cwv_rect(x, y, w, h)
cwv_rect(x, y, w, h, ox, oy)
} else {
cwv_rect(CWV_PARK, CWV_PARK, w, h)
cwv_rect(CWV_PARK, CWV_PARK, w, h, 0.0, 0.0)
};
if let Err(e) = wv.set_bounds(rect) {
cwv_log(&app, &format!("bounds:err label={} err={}", label, e));
return Err(e.to_string());
}
// 页面缩放:只在渲染层判定「该改了」时才带过来,避免每条消息都重排一次
if let Some(z) = zoom {
if let Err(e) = wv.set_zoom(z) {
cwv_log(&app, &format!("bounds:zoom-err label={} zoom={} err={}", label, z, e));
}
}
let n = CWV_BOUNDS_CALLS.fetch_add(1, Ordering::Relaxed);
if n < 8 || n % 400 == 0 {
cwv_log(
&app,
&format!("bounds label={} n={} rect=({:.0},{:.0} {:.0}x{:.0}) visible={}", label, n, x, y, w, h, visible),
&format!(
"bounds label={} n={} rect=({:.0},{:.0} {:.0}x{:.0}) visible={} zoom={:?} off=({:.0},{:.0})",
label, n, x, y, w, h, visible, zoom, ox, oy
),
);
}
Ok(true)
@@ -641,6 +695,8 @@ fn canvas_webview_action(app: AppHandle, id: String, action: String) -> Result<b
"back" => wv.eval("history.back()").map_err(|e| e.to_string())?,
"forward" => wv.eval("history.forward()").map_err(|e| e.to_string())?,
"reload" => wv.eval("location.reload()").map_err(|e| e.to_string())?,
// 加载中把 ⟳ 变成「停止」:只中断当前请求,不销毁视图(销毁会丢滚动位置)
"stop" => wv.eval("window.stop()").map_err(|e| e.to_string())?,
"close" => {
let _ = wv.close();
}
@@ -708,7 +764,7 @@ pub fn run() {
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_updater::Builder::new().build())
.invoke_handler(tauri::generate_handler![open_app_data_dir, grant_dir, copy_tree, check_update, install_update, clipboard_read_image, clipboard_write_image, clipboard_write_text, clipboard_read_text, open_external_url, open_browser_window, browser_control, browser_state, canvas_webview_ensure, canvas_webview_bounds, canvas_webview_navigate, canvas_webview_action, canvas_webview_state, canvas_webview_close_all, notify_desktop])
.invoke_handler(tauri::generate_handler![open_app_data_dir, grant_dir, copy_tree, check_update, install_update, clipboard_read_image, clipboard_write_image, clipboard_write_text, clipboard_read_text, open_external_url, open_browser_window, browser_control, browser_state, canvas_webview_ensure, canvas_webview_bounds, canvas_webview_navigate, canvas_webview_action, canvas_webview_state, canvas_webview_close_all, canvas_webview_metrics, notify_desktop])
.setup(|app| {
if cfg!(debug_assertions) {
app.handle().plugin(