From 4a4850c9f6a1421c5bd648b170fe966fe6a8d8c4 Mon Sep 17 00:00:00 2001 From: Tlntin Date: Tue, 27 Dec 2022 21:33:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90T?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/about_pake.html | 16 +++++ src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/pake.json | 7 +- src-tauri/src/main.rs | 147 ++++++++++++++++++++++++++++++-------- src-tauri/tauri.conf.json | 2 +- 6 files changed, 143 insertions(+), 31 deletions(-) create mode 100644 dist/about_pake.html diff --git a/dist/about_pake.html b/dist/about_pake.html new file mode 100644 index 0000000..e1655dd --- /dev/null +++ b/dist/about_pake.html @@ -0,0 +1,16 @@ + + + + + + Document + + +
Welcome from Pake!
+

version: 1.0.1

+ Project link
+ Discussions
+ Issues
+

LICENSE: MIT

+ + \ No newline at end of file diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 21f87ef..fa36a96 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -50,6 +50,7 @@ dependencies = [ "tauri-build", "tauri-plugin-window-state", "tauri-utils", + "webkit2gtk", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index da20314..d829449 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -22,6 +22,7 @@ image = "0.24.5" home = "0.5" tauri-utils = "1.2.1" tauri-plugin-window-state = { git = "https://github.com/tauri-apps/tauri-plugin-window-state", branch = "dev"} +webkit2gtk = "0.18.0" # webbrowser = "0.8.2" # wry = "0.23.4" diff --git a/src-tauri/pake.json b/src-tauri/pake.json index b99022f..32b5543 100644 --- a/src-tauri/pake.json +++ b/src-tauri/pake.json @@ -9,5 +9,10 @@ "resizable": true, "url_type": "web" } - ] + ], + "user_agent": { + "macos": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", + "linux": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + "windows": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" + } } \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e853e87..d17ac20 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,7 +6,7 @@ use tauri::MenuItem; use tauri::{ - CustomMenuItem, Menu, Submenu, WindowBuilder, App, + CustomMenuItem, Menu, Submenu, WindowBuilder, App, Config, Window, WindowUrl, WindowMenuEvent, window::PlatformWebview, SystemTrayMenu, SystemTray, SystemTrayEvent, Manager }; @@ -28,27 +28,31 @@ pub mod pake { } - // #[derive(Debug, Deserialize)] - // pub struct SystemTray { - // pub icon_path: String, - // pub icon_as_template: bool, - // } + #[derive(Debug, Deserialize)] + pub struct UserAgent { + pub macos: String, + pub linux: String, + pub windows: String, + } #[derive(Debug, Deserialize)] pub struct PakeConfig { pub windows: Vec, - // pub system_tray: SystemTray, + pub user_agent: UserAgent, } } use pake::PakeConfig; -pub fn get_pake_config() -> PakeConfig{ - let config_path = include_str!("../pake.json"); - let config: PakeConfig = serde_json::from_str(config_path) - .expect("failed to parse common config"); +pub fn get_pake_config() -> (PakeConfig, Config){ + let pake_config_path = include_str!("../pake.json"); + let pake_config: PakeConfig = serde_json::from_str(pake_config_path) + .expect("failed to parse pake config"); // println!("{:#?}", config); - config + let tauri_config_path = include_str!("../tauri.conf.json"); + let tauri_config: Config = serde_json::from_str(tauri_config_path) + .expect("failed to parse tauri config"); + (pake_config, tauri_config) } @@ -118,24 +122,65 @@ pub fn get_menu() -> Menu { } -pub fn get_window(app: & mut App, config: PakeConfig) -> Window { +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() } @@ -204,11 +249,13 @@ pub fn menu_event_handle(event: WindowMenuEvent) { pub fn get_system_tray() -> SystemTray { + let debug = CustomMenuItem::new("debug", "Debug"); 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(debug) .add_item(hide) .add_item(show) .add_item(quit) @@ -220,6 +267,7 @@ pub fn get_system_tray() -> SystemTray { pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) { if let SystemTrayEvent::MenuItemClick { tray_id: _, id, .. } = event { match id.as_str() { + "debug" => app.get_window("pake").unwrap().open_devtools(), "hide" => { app.get_window("pake").unwrap().hide().unwrap(); }, @@ -229,29 +277,70 @@ pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) "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 run_app() { + let system_tray = get_system_tray(); + #[cfg(target_os = "macos")] + { + let (pake_config, _) = get_pake_config(); + let menu = get_menu(); + tauri::Builder::default() + .menu(menu) + .on_menu_event(menu_event_handle) + .system_tray(system_tray) + .on_system_tray_event(system_tray_handle) + .plugin(tauri_plugin_window_state::Builder::default().build()) + .invoke_handler(tauri::generate_handler![]) + .setup(|app| { + let _window = get_window(app, pake_config, std::path::PathBuf::new()); + Ok(()) + }) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); + } + #[cfg(any(target_os = "linux", target_os = "windows"))] + { + let (pake_config, tauri_config) = get_pake_config(); + let data_dir = get_data_dir(tauri_config); + // let menu = get_menu(); + tauri::Builder::default() + // .menu(menu) + // .on_menu_event(menu_event_handle) + .system_tray(system_tray) + .on_system_tray_event(system_tray_handle) + .plugin(tauri_plugin_window_state::Builder::default().build()) + .invoke_handler(tauri::generate_handler![]) + .setup(|app| { + let _window = get_window(app, pake_config, data_dir); + Ok(()) + }) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); + } + + +} + fn main() { - let pake_config = get_pake_config(); - let menu = get_menu(); - let system_tray = get_system_tray(); - tauri::Builder::default() - .menu(menu) - .on_menu_event(menu_event_handle) - .system_tray(system_tray) - .on_system_tray_event(system_tray_handle) - .plugin(tauri_plugin_window_state::Builder::default().build()) - .invoke_handler(tauri::generate_handler![]) - .setup(|app| { - let _window = get_window(app, pake_config); - Ok(()) - }) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + run_app() } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 4ce3f37..35ed045 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -11,7 +11,7 @@ "active": false }, "systemTray": { - "iconPath": "png/weread_32.ico", + "iconPath": "png/weread_512.png", "iconAsTemplate": true } },