feat(canvas-webview): 画布内嵌网页节点(原生子 webview)
- lib.rs:新增 canvas_webview_ensure/bounds/navigate/action/state/close_all, 用 Window::add_child 建 label=cwv-<节点id> 的子 webview(tauri 启用 unstable feature)。 子 webview 不在 capabilities ⇒ 外部页面拿不到宿主 IPC。 - 新增 src/canvas-webview.js:锚点扫描 + 每帧位置同步。自启动、对 app.js 零侵入 (app.js 总行数棘轮只剩 1 行余量)。快速平移缩放拖拽时先隐藏避免拖影; 节点必须完整落在画布内才显示(原生层不受 overflow:hidden 约束,溢出会盖住侧边栏); 选中时内缩 12px 露出缩放手柄;锚点消失 900ms 后才回收(LOD 降级不重载页面)。 - 插件节点可声明初始宽度:nodes.register 的 width 经 addPluginNode 生效。 - browser 插件 1.1.0:新增「网页」节点(地址栏 + 后退/前进/刷新/关闭)。 - 同步模块顺序快照与架构文档计数(89 个模块)。
This commit is contained in:
1 parent
c078626a5c
commit
05dd343155
11 files changed
+598
-17
No files matched your search
+127
-1
@@ -429,6 +429,132 @@ fn browser_state(app: AppHandle) -> Result<serde_json::Value, String> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── 画布内嵌浏览器(主窗口的**子 webview**)────────────────────────────────────
|
||||
// 与上面「独立窗口」是两条完全不同的路:这里用 `Window::add_child` 把 webview 作为
|
||||
// 主窗口的子视图挂在画布节点上,网页真正**长在画布里面**(跟着平移、缩放、拖出视野就藏起来),
|
||||
// 而不是另开一个窗口。
|
||||
//
|
||||
// 为什么不用 iframe:GitHub / 站酷 / Figma / Pinterest 这些站用 X-Frame-Options 或
|
||||
// CSP frame-ancestors 明确拒绝被嵌,iframe 里只会白屏,且主窗口 CSP 也没有 frame-src。
|
||||
// 子 webview 是真实浏览器实例,不受这些响应头约束。
|
||||
//
|
||||
// 代价(已知且不可消除):子 webview 是原生层,永远浮在 HTML 之上 —— 它无法被画布节点
|
||||
// 遮挡,也无法被 CSS 裁剪。所以可见性由渲染层每帧算:节点离开画布可视区、被折叠、
|
||||
// 或画布正在拖拽/缩放时主动 hide,避免它盖住菜单与侧边栏。
|
||||
//
|
||||
// `add_child` 需要 tauri 的 `unstable` feature(见 Cargo.toml)。
|
||||
// 子 webview 不在 capabilities 里 ⇒ 被它加载的外部页面拿不到宿主 IPC。
|
||||
const CWV_PREFIX: &str = "cwv-";
|
||||
|
||||
// label 只允许 [a-zA-Z0-9-_:/]:节点 id 本身是 uid() 生成的安全串,这里仍过一遍白名单兜底。
|
||||
fn cwv_label(id: &str) -> String {
|
||||
let cleaned: String = id
|
||||
.chars()
|
||||
.filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
|
||||
.collect();
|
||||
format!("{}{}", CWV_PREFIX, cleaned)
|
||||
}
|
||||
|
||||
fn cwv_parse(url: &str) -> Result<url::Url, String> {
|
||||
let parsed = url::Url::parse(url).map_err(|_| "网址格式不合法".to_string())?;
|
||||
if parsed.scheme() != "http" && parsed.scheme() != "https" {
|
||||
return Err("只允许 http/https".into());
|
||||
}
|
||||
Ok(parsed)
|
||||
}
|
||||
|
||||
// 确保某节点对应的子 webview 存在。返回 true 表示这次真的新建了。
|
||||
#[tauri::command]
|
||||
fn canvas_webview_ensure(app: AppHandle, id: String, url: String, x: f64, y: f64, w: f64, h: f64) -> Result<bool, String> {
|
||||
let parsed = cwv_parse(&url)?;
|
||||
let label = cwv_label(&id);
|
||||
if app.get_webview(&label).is_some() {
|
||||
return Ok(false);
|
||||
}
|
||||
let main = app.get_webview_window("main").ok_or_else(|| "主窗口不存在".to_string())?;
|
||||
// WebviewWindow → &Webview → Window(Window 才是 add_child 的宿主)
|
||||
let window = AsRef::<tauri::Webview<_>>::as_ref(&main).window();
|
||||
window
|
||||
.add_child(
|
||||
tauri::webview::WebviewBuilder::new(&label, WebviewUrl::External(parsed)),
|
||||
tauri::LogicalPosition::new(x, y),
|
||||
tauri::LogicalSize::new(w.max(1.0), h.max(1.0)),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
// 热路径:渲染层每帧(rect 有变化时)调一次。位置/尺寸/可见性一把推过来。
|
||||
#[tauri::command]
|
||||
fn canvas_webview_bounds(app: AppHandle, id: String, x: f64, y: f64, w: f64, h: f64, visible: bool) -> Result<bool, String> {
|
||||
let Some(wv) = app.get_webview(&cwv_label(&id)) else { return Ok(false) };
|
||||
// 单参数失败不整体报错:节点可能正被拖到画布外,set_size(0) 之类会返回 Err,
|
||||
// 但下一次调用就会恢复正常,没必要把前端的 rAF 打断。
|
||||
let _ = wv.set_position(tauri::LogicalPosition::new(x, y));
|
||||
let _ = wv.set_size(tauri::LogicalSize::new(w.max(1.0), h.max(1.0)));
|
||||
if visible {
|
||||
let _ = wv.show();
|
||||
} else {
|
||||
let _ = wv.hide();
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn canvas_webview_navigate(app: AppHandle, id: String, url: String) -> Result<bool, String> {
|
||||
let parsed = cwv_parse(&url)?;
|
||||
let wv = app.get_webview(&cwv_label(&id)).ok_or_else(|| "该节点还没有浏览器".to_string())?;
|
||||
wv.navigate(parsed).map_err(|e| e.to_string())?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
// back/forward 用固定字符串且不拼接外部输入,不构成注入面。
|
||||
#[tauri::command]
|
||||
fn canvas_webview_action(app: AppHandle, id: String, action: String) -> Result<bool, String> {
|
||||
let wv = app.get_webview(&cwv_label(&id)).ok_or_else(|| "该节点还没有浏览器".to_string())?;
|
||||
match action.as_str() {
|
||||
"back" => wv.eval("history.back()").map_err(|e| e.to_string())?,
|
||||
"forward" => wv.eval("history.forward()").map_err(|e| e.to_string())?,
|
||||
"reload" => wv.reload().map_err(|e| e.to_string())?,
|
||||
// 关掉的是子 webview 本身,节点还在;节点下次可见时 ensure 会重建。
|
||||
"close" => wv.close().map_err(|e| e.to_string())?,
|
||||
_ => return Err("未知操作".into()),
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn canvas_webview_state(app: AppHandle, id: String) -> Result<serde_json::Value, String> {
|
||||
match app.get_webview(&cwv_label(&id)) {
|
||||
Some(wv) => Ok(serde_json::json!({
|
||||
"open": true,
|
||||
"url": wv.url().map(|u| u.to_string()).unwrap_or_default()
|
||||
})),
|
||||
None => Ok(serde_json::json!({ "open": false, "url": "" })),
|
||||
}
|
||||
}
|
||||
|
||||
// 切换工作区 / 清空画布 / 卸载渲染层时统一回收:子 webview 不随 DOM 消失,
|
||||
// 漏掉就会在窗口上留一块「无主网页」,而且它永远盖在最上面。
|
||||
#[tauri::command]
|
||||
fn canvas_webview_close_all(app: AppHandle) -> Result<u32, String> {
|
||||
let labels: Vec<String> = app
|
||||
.webviews()
|
||||
.keys()
|
||||
.filter(|k| k.starts_with(CWV_PREFIX))
|
||||
.cloned()
|
||||
.collect();
|
||||
let mut n = 0u32;
|
||||
for label in labels {
|
||||
if let Some(wv) = app.get_webview(&label) {
|
||||
if wv.close().is_ok() {
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn notify_desktop(app: tauri::AppHandle, title: String, body: String) -> Result<bool, String> {
|
||||
use tauri_plugin_notification::NotificationExt;
|
||||
@@ -454,7 +580,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, 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, notify_desktop])
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
app.handle().plugin(
|
||||
|
||||
Reference in new issue
Block a user