From 2d7278a44d00c48f3d3b08a6e220a2cf388acb25 Mon Sep 17 00:00:00 2001 From: Tlntin Date: Tue, 27 Dec 2022 22:15:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86rust=E6=BA=90=E7=A0=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/lib.rs | 113 +++++++++++++++++++++++++++++++++++++++- src-tauri/src/main.rs | 117 +----------------------------------------- 2 files changed, 113 insertions(+), 117 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d552858..ace0516 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2,7 +2,9 @@ use tauri::MenuItem; use tauri::{ - CustomMenuItem, Menu, Submenu, WindowMenuEvent, window::PlatformWebview, Config + CustomMenuItem, Menu, Submenu, WindowMenuEvent, window::PlatformWebview, + Config, SystemTrayMenu, SystemTray, SystemTrayEvent, Manager, + WindowBuilder, WindowUrl, App, Window, }; @@ -173,4 +175,113 @@ pub fn get_pake_config() -> (PakeConfig, Config){ let tauri_config: Config = serde_json::from_str(tauri_config_path) .expect("failed to parse tauri config"); (pake_config, tauri_config) +} + + +pub fn get_system_tray() -> SystemTray { + let hide = CustomMenuItem::new("hide".to_string(), "Hide"); + let show = CustomMenuItem::new("show".to_string(), "Show"); + let quit = CustomMenuItem::new("quit".to_string(), "Quit"); + let about = CustomMenuItem::new("about".to_string(), "About"); + let tray_menu = SystemTrayMenu::new() + .add_item(hide) + .add_item(show) + .add_item(quit) + .add_item(about); + SystemTray::new().with_menu(tray_menu) +} + + +pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) { + if let SystemTrayEvent::MenuItemClick { tray_id: _, id, .. } = event { + match id.as_str() { + "hide" => { + app.get_window("pake").unwrap().hide().unwrap(); + }, + "show" => { + app.get_window("pake").unwrap().show().unwrap(); + }, + "quit" => { + std::process::exit(0); + }, + "about" => { + let _about_window = WindowBuilder::new( + app, + "about", + WindowUrl::App(std::path::PathBuf::from("about_pake.html")) + ) + .resizable(true) + .title("About") + .inner_size(100.0, 100.0) + .build() + .expect("can't open about!") + ; + } + _ => {}, + } + }; +} + + +pub fn get_data_dir(tauri_config: Config) -> std::path::PathBuf { + let package_name = tauri_config.package.product_name.unwrap(); + let home_dir = match home::home_dir() { + Some(path1) => path1, + None => panic!("Error, can't found you home dir!!"), + }; + #[cfg(target_os = "windows")] + let data_dir = home_dir.join("AppData").join("Roaming").join(package_name); + #[cfg(target_os = "linux")] + let data_dir = home_dir.join(".config").join(package_name); + if !data_dir.exists() { + std::fs::create_dir(&data_dir) + .unwrap_or_else(|_| panic!("can't create dir {}", data_dir.display())); + } + data_dir +} + + +pub fn get_window(app: & mut App, config: PakeConfig, data_dir: std::path::PathBuf) -> Window { + let window_config = config.windows.first().unwrap(); + let user_agent = config.user_agent; + let url = match window_config.url_type.as_str() { + "web" => WindowUrl::External(window_config.url.parse().unwrap()), + "local" => WindowUrl::App(std::path::PathBuf::from(&window_config.url)), + _ => panic!("url type only can be web or local"), + }; + #[cfg(target_os = "macos")] + let window = WindowBuilder::new( + app, + "pake", + url + ) + .title("") + .user_agent(user_agent.macos.as_str()) + .resizable(window_config.resizable) + .fullscreen(window_config.fullscreen) + .transparent(window_config.transparent) + .inner_size(window_config.width, window_config.height) + .initialization_script(include_str!("pake.js")); + + #[cfg(any(target_os = "linux", target_os = "windows"))] + let window = { + #[cfg(target_os = "linux")] + let user_agent = user_agent.linux.as_str(); + #[cfg(target_os = "windows")] + let user_agent = user_agent.windows.as_str(); + WindowBuilder::new( + app, + "pake", + url + ) + .title("") + .data_directory(data_dir) + .resizable(window_config.resizable) + .fullscreen(window_config.fullscreen) + .user_agent(user_agent) + .inner_size(window_config.width, window_config.height) + .initialization_script(include_str!("pake.js")) + }; + window.build().unwrap() + } \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1cd23e2..cba98ff 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,123 +3,10 @@ windows_subsystem = "windows" )] -use tauri::{ - CustomMenuItem, WindowBuilder, App, Config, Window, WindowUrl, - SystemTrayMenu, SystemTray, SystemTrayEvent, Manager -}; #[cfg(target_os = "macos")] use app::{get_menu, menu_event_handle}; -use app::{get_pake_config, pake::PakeConfig}; - - -pub fn get_data_dir(tauri_config: Config) -> std::path::PathBuf { - let package_name = tauri_config.package.product_name.unwrap(); - let home_dir = match home::home_dir() { - Some(path1) => path1, - None => panic!("Error, can't found you home dir!!"), - }; - #[cfg(target_os = "windows")] - let data_dir = home_dir.join("AppData").join("Roaming").join(package_name); - #[cfg(target_os = "linux")] - let data_dir = home_dir.join(".config").join(package_name); - if !data_dir.exists() { - std::fs::create_dir(&data_dir) - .unwrap_or_else(|_| panic!("can't create dir {}", data_dir.display())); - } - data_dir -} - - -pub fn get_window(app: & mut App, config: PakeConfig, data_dir: std::path::PathBuf) -> Window { - let window_config = config.windows.first().unwrap(); - let user_agent = config.user_agent; - let url = match window_config.url_type.as_str() { - "web" => WindowUrl::External(window_config.url.parse().unwrap()), - "local" => WindowUrl::App(std::path::PathBuf::from(&window_config.url)), - _ => panic!("url type only can be web or local"), - }; - #[cfg(target_os = "macos")] - let window = WindowBuilder::new( - app, - "pake", - url - ) - .title("") - .user_agent(user_agent.macos.as_str()) - .resizable(window_config.resizable) - .fullscreen(window_config.fullscreen) - .transparent(window_config.transparent) - .inner_size(window_config.width, window_config.height) - .initialization_script(include_str!("pake.js")); - - #[cfg(any(target_os = "linux", target_os = "windows"))] - let window = { - #[cfg(target_os = "linux")] - let user_agent = user_agent.linux.as_str(); - #[cfg(target_os = "windows")] - let user_agent = user_agent.windows.as_str(); - WindowBuilder::new( - app, - "pake", - url - ) - .title("") - .data_directory(data_dir) - .resizable(window_config.resizable) - .fullscreen(window_config.fullscreen) - .user_agent(user_agent) - .inner_size(window_config.width, window_config.height) - .initialization_script(include_str!("pake.js")) - }; - window.build().unwrap() - -} - - -pub fn get_system_tray() -> SystemTray { - let hide = CustomMenuItem::new("hide".to_string(), "Hide"); - let show = CustomMenuItem::new("show".to_string(), "Show"); - let quit = CustomMenuItem::new("quit".to_string(), "Quit"); - let about = CustomMenuItem::new("about".to_string(), "About"); - let tray_menu = SystemTrayMenu::new() - .add_item(hide) - .add_item(show) - .add_item(quit) - .add_item(about); - SystemTray::new().with_menu(tray_menu) -} - - -pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) { - if let SystemTrayEvent::MenuItemClick { tray_id: _, id, .. } = event { - match id.as_str() { - "hide" => { - app.get_window("pake").unwrap().hide().unwrap(); - }, - "show" => { - app.get_window("pake").unwrap().show().unwrap(); - }, - "quit" => { - std::process::exit(0); - }, - "about" => { - let _about_window = WindowBuilder::new( - app, - "about", - WindowUrl::App(std::path::PathBuf::from("about_pake.html")) - ) - .resizable(true) - .title("About") - .inner_size(100.0, 100.0) - .build() - .expect("can't open about!") - ; - } - _ => {}, - } - }; -} +use app::{get_window, get_system_tray, get_data_dir, get_pake_config, system_tray_handle}; pub fn run_app() { @@ -169,8 +56,6 @@ pub fn run_app() { .run(tauri::generate_context!()) .expect("error while running tauri application"); } - - }