问题
我想使用 tauri 时,可以使用类似 electron 的 , 我当初的想法也是,用div占位, 用 webview填充, 和
https://blog.csdn.net/gitblog_00343/article/details/151808322
描述的一样 , 我在页面 onmount 后调用接口, 或者点击界面调用接口, 但是都会出现一个透明框,覆盖整个电脑界面。
创建的windows界面代码:
// 第二个窗口
let _second_window =
WebviewWindowBuilder::new(app, "second", WebviewUrl::App("/#/layout".into()))
.title("副窗口")
.position(0.0, 0.0)
.decorations(false) // 去掉标题栏
.maximized(true) // 最大化
.transparent(true) // 透明
.always_on_top(true)
.skip_taskbar(true)
.build()?;
调用的接口代码:
use tauri::{
AppHandle, Manager, Runtime, WebviewBuilder, WebviewUrl, LogicalPosition, LogicalSize
};
#[tauri::command]
pub fn createwebview<R: Runtime>(app: AppHandle<R>, window_label: &str, url: &str, width: f64, height: f64) -> Result<(), String>{
let parent_window = app.get_window("main").ok_or_else(|| format!("找不到标识为'{}'的窗口", window_label))?;
println!("I was invoked from JavaScript! {} {} {} {} ", window_label, url, width, height); // 这里在控制台显示
// 创建子WebView
let _webview1 = parent_window.add_child(
tauri::webview::WebviewBuilder::new("main3", WebviewUrl::External("https://flowus.cn/".parse().unwrap()))
.auto_resize(),
tauri::LogicalPosition::new(20., 20.),
tauri::LogicalSize::new( 60.0, 60.0),
).map_err(|e| format!("创建WebView失败: {}", e))?;
println!("创建 webview1"); // 这里在控制台不显示, 且没有见上面创建webview失败的报错
Ok(())
}
前端点击事件调用接口:
const test = () => {
console.log('click -- ', windowLabel, elementHeight, elementWidth)
// 调用Rust命令,传递当前窗口label和WebView参数
invoke('createwebview', {
windowLabel: 'second',
url: 'https://flowus.cn/',
width: 400,
height: 300,
x: 50, // 相对于父窗口的X偏移
y: 50 // 相对于父窗口的Y偏移
});
}
点击之后, 出现透明框,用任务管理器关掉之后, exe删不掉显示被占用 。
如果 window窗口 不要最大化,按默认大小, 点击后没有展示 webview