首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

  • 24-11-26 09:04
  • 3938
  • 6792
juejin.cn

官方文档:Configuration | Tauri Apps

WindowConfig在tauri中是一个结构体,用于创建window窗口的类型规范。如果我们想自己使用WindowBuilder::from_config来创建一个窗口的话,就需要定义一个这样的类型传递进去才可以创建。定义的方式大概有下面四种,亲测都可以实现。

1.让tauri自己来转换

我们只需要在invoke中传递配置的时候,声明传递的参数类型为WindowConfig就可以了:

在传递的时候,直接传递js的对象就好了,使用的时候:

javascript
代码解读
复制代码
let _window = WindowBuilder::from_config(&handle, config) .initialization_script(contents.as_str()) .build() .unwrap();

2.从tauri.config.json里面获取

也可以使用app.config()从配置中拿到配置文件,然后找到config.tauri.windows.get(0)里面的第一个windows配置,再将配置传递进去就可以了:

注意:如果想修改配置,就需要clone一份出来

javascript
代码解读
复制代码
let config = app.config(); let windows_config = config.tauri.windows.get(0).unwrap(); let mut windows_config: WindowConfig = windows_config.clone(); windows_config.label = "submian".to_string(); windows_config.visible = true;

然后再传递到WindowBuilder::from_config里面:

3.手动创建一个

直接在rust文件中手动创建一个也是可以的:

javascript
代码解读
复制代码
let windows_config = WindowConfig { label: "custom_window".into(), url: WindowUrl::App("https://juejin.cn/".into()), // 加载的 URL,可以是本地文件或远程网址 width: 800.0, // 窗口的宽度 height: 600.0, // 窗口的高度 resizable: true, // 是否允许调整窗口大小 fullscreen: false, // 是否全屏 transparent: false, // 是否透明 maximized: false, // 是否最大化 visible: true, // 是否可见 decorations: true, // 是否显示窗口边框 always_on_top: false, // 是否置顶 center: true, // 是否居中 theme: None, // 主题(如果需要可用 Some("light".into()) 或 Some("dark".into())) ..Default::default() // 填充剩余字段为默认值 };

4.从json字符串转换

最后一种方式就是从json字符串转换得来:

javascript
代码解读
复制代码
fn json_to_window_config(json: &str) -> Result<WindowConfig, Error> { serde_json::from_str(json) } // 转换 let json = r#"{"label":"submain","url":"https://juejin.cn/","userAgent":"","fileDropEnabled":true,"center":false,"width":800,"height":600,"minWidth":null,"minHeight":null,"maxWidth":null,"maxHeight":null,"resizable":true,"maximizable":true,"minimizable":true,"closable":true,"title":"","fullscreen":false,"focus":false,"transparent":false,"maximized":false,"visible":true,"decorations":true,"alwaysOnTop":false,"contentProtected":false,"skipTaskbar":false,"titleBarStyle":"Visible","hiddenTitle":false,"acceptFirstMouse":false,"tabbingIdentifier":"","additionalBrowserArgs":""}"#; match json_to_window_config(json) { Ok(config) => { println!("Parsed WindowConfig: {:?}", config); let _main_window = WindowBuilder::from_config( &app_handle, config, // 从配置中读取 ) .build() .unwrap(); } Err(err) => { eprintln!("Failed to parse JSON: {}", err); } }

总结的参考代码:

rust
代码解读
复制代码
// Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use serde_json::Error; use tauri::{utils::config::WindowConfig, Menu, MenuItem, Submenu, WindowBuilder, WindowUrl}; fn json_to_window_config(json: &str) -> Result { serde_json::from_str(json) } fn main() { let edit_menu = Submenu::new( "Edit", Menu::new() .add_native_item(MenuItem::Undo) .add_native_item(MenuItem::Redo) .add_native_item(MenuItem::Copy) .add_native_item(MenuItem::Cut) .add_native_item(MenuItem::Paste) .add_native_item(MenuItem::SelectAll) .add_native_item(MenuItem::CloseWindow) .add_native_item(MenuItem::Quit), ); tauri::Builder::default() .setup(|app| { let app_handle = app.handle(); // 1. 从配置中读取 // let config = app.config(); // let windows_config = config.tauri.windows.get(0).unwrap(); // let mut windows_config: WindowConfig = windows_config.clone(); // windows_config.label = "submian".to_string(); // windows_config.visible = true; // // 2. 直接创建 // let windows_config = WindowConfig { // label: "custom_window".into(), // url: WindowUrl::App("https://juejin.cn/".into()), // 加载的 URL,可以是本地文件或远程网址 // width: 800.0, // 窗口的宽度 // height: 600.0, // 窗口的高度 // resizable: true, // 是否允许调整窗口大小 // fullscreen: false, // 是否全屏 // transparent: false, // 是否透明 // maximized: false, // 是否最大化 // visible: true, // 是否可见 // decorations: true, // 是否显示窗口边框 // always_on_top: false, // 是否置顶 // center: true, // 是否居中 // theme: None, // 主题(如果需要可用 Some("light".into()) 或 Some("dark".into())) // ..Default::default() // 填充剩余字段为默认值 // }; // 3. 从 JSON 字符串创建 let json = r#"{"label":"submain","url":"https://juejin.cn/","userAgent":"","fileDropEnabled":true,"center":false,"width":800,"height":600,"minWidth":null,"minHeight":null,"maxWidth":null,"maxHeight":null,"resizable":true,"maximizable":true,"minimizable":true,"closable":true,"title":"","fullscreen":false,"focus":false,"transparent":false,"maximized":false,"visible":true,"decorations":true,"alwaysOnTop":false,"contentProtected":false,"skipTaskbar":false,"titleBarStyle":"Visible","hiddenTitle":false,"acceptFirstMouse":false,"tabbingIdentifier":"","additionalBrowserArgs":""}"#; match json_to_window_config(json) { Ok(config) => { println!("Parsed WindowConfig: {:?}", config); let _main_window = WindowBuilder::from_config( &app_handle, config, // 从配置中读取 ) .build() .unwrap(); } Err(err) => { eprintln!("Failed to parse JSON: {}", err); } } // 使用 from_config 创建主窗口 // let _main_window = WindowBuilder::from_config( // &app_handle, // windows_config, // 从配置中读取 // ) // .build() // .unwrap(); // let _window = tauri::WindowBuilder::new( // app, // "PakePlus", // tauri::WindowUrl::App("https://www.douyin.com".into()), // ) // .initialization_script(include_str!("./extension/custom.js")) // .title("抖音") // .inner_size(1024.0, 768.0) // .center() // .user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15") // .build()?; Ok(()) }) .menu(Menu::new().add_submenu(edit_menu)) .run(tauri::generate_context!()) .expect("error while running tauri application"); }

注:本文转载自juejin.cn的1024小神的文章"https://juejin.cn/post/7441048126145789964"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

104
前端
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top