From bf20c9d5ec38bcebd3092ad527fb62418dd3c654 Mon Sep 17 00:00:00 2001 From: Tlntin Date: Tue, 27 Dec 2022 22:06:58 +0800 Subject: [PATCH 1/3] =?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 | 176 +++++++++++++++++++++++++++++++++++++++++ src-tauri/src/main.rs | 177 +----------------------------------------- 2 files changed, 180 insertions(+), 173 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e69de29..d552858 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -0,0 +1,176 @@ +#[cfg(target_os = "macos")] +use tauri::MenuItem; + +use tauri::{ + CustomMenuItem, Menu, Submenu, WindowMenuEvent, window::PlatformWebview, Config +}; + + +pub fn get_menu() -> Menu { + // first menu + let hide = CustomMenuItem::new("hide", "Hide"); + let close = CustomMenuItem::new("close", "Close"); + let quit = CustomMenuItem::new("quit", "Quit"); + #[cfg(target_os = "macos")] + let first_menu = Menu::new() + .add_native_item(MenuItem::EnterFullScreen) + .add_native_item(MenuItem::Minimize) + .add_native_item(MenuItem::Separator) + .add_native_item(MenuItem::Copy) + .add_native_item(MenuItem::Cut) + .add_native_item(MenuItem::Paste) + .add_native_item(MenuItem::Undo) + .add_native_item(MenuItem::Redo) + .add_native_item(MenuItem::SelectAll) + .add_native_item(MenuItem::Separator) + .add_item(hide) + .add_item(close) + .add_item(quit); + #[cfg(any(target_os = "linux", target_os = "windows"))] + let first_menu = Menu::new() + .add_item(hide) + .add_item(close) + .add_item(quit); + let first_menu = Submenu::new("File", first_menu); + // Hot Key + // let top = CustomMenuItem::new("top", "Top (↑)"); + // let buttom = CustomMenuItem::new("buttom", "Bottom (↓)"); + // let previous = CustomMenuItem::new("previous", "Previous (←)"); + // let next = CustomMenuItem::new("next", "next (→)"); + // let refresh = CustomMenuItem::new("refresh", "Refresh"); + let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)"); + let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)"); + let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset"); + let hot_key = Menu::new() + // .add_item(top) + // .add_item(buttom) + // .add_item(previous) + // .add_item(next) + // .add_item(refresh) + .add_item(zoom_in) + .add_item(zoom_out) + .add_item(zoom_reset); + let hot_key_menu = Submenu::new("Hot Key", hot_key); + + // Help + // let instructions = CustomMenuItem::new("instruction", "Instruction"); + // let about = CustomMenuItem::new("about", "About"); + // let help = Menu::new() + // .add_item(instructions) + // .add_item(about); + // let help_menu = Submenu::new("Help", help); + let menu = Menu::new() + .add_submenu(first_menu) + .add_submenu(hot_key_menu); + // .add_submenu(help_menu); + menu + + +} + + +pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) { + #[cfg(target_os = "linux")] + { + // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html + // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html + use webkit2gtk::traits::WebViewExt; + webview.inner().set_zoom_level(zoom_value); + } + + #[cfg(windows)] + unsafe { + // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html + webview.controller().SetZoomFactor(zoom_value).unwrap(); + } + + #[cfg(target_os = "macos")] + unsafe { + let () = msg_send![webview.inner(), setPageZoom: zoom_value]; + let () = msg_send![webview.controller(), removeAllUserScripts]; + let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.]; + let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color]; + } +} + +pub fn set_zoom_out(webview: PlatformWebview) { + set_zoom(webview, 1.25); +} + + +pub fn set_zoom_in(webview: PlatformWebview) { + set_zoom(webview, 0.75); +} + + +pub fn zoom_reset(webview: PlatformWebview) { + set_zoom(webview, 1.0); +} + +pub fn menu_event_handle(event: WindowMenuEvent) { + match event.menu_item_id() { + "hide" => event.window().hide().expect("can't hide window"), + "close" => event.window().close().expect("can't close window"), + "quit" => std::process::exit(0), + "zoom_out" => { + event.window() + .with_webview(set_zoom_out) + .expect("can't set zoom out"); + }, + "zoom_in" => { + event.window() + .with_webview(set_zoom_in) + .expect("can't set zoom in"); + }, + "reset" => { + event.window() + .with_webview(zoom_reset) + .expect("can't reset zoom"); + }, + _ => {}, + } +} + + +pub mod pake { + use serde::Deserialize; + + #[derive(Debug, Deserialize)] + pub struct WindowConfig { + pub url: String, + pub transparent: bool, + pub fullscreen: bool, + pub width: f64, + pub height: f64, + pub resizable: bool, + pub url_type: String + } + + + #[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 user_agent: UserAgent, + } +} + + +use pake::PakeConfig; + +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); + 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) +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 611f93a..1cd23e2 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,122 +2,15 @@ all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] -#[cfg(target_os = "macos")] -use tauri::MenuItem; use tauri::{ - CustomMenuItem, Menu, Submenu, WindowBuilder, App, Config, - Window, WindowUrl, WindowMenuEvent, window::PlatformWebview, + CustomMenuItem, WindowBuilder, App, Config, Window, WindowUrl, SystemTrayMenu, SystemTray, SystemTrayEvent, Manager }; -// use tauri::Config; - -pub mod pake { - use serde::Deserialize; - - #[derive(Debug, Deserialize)] - pub struct WindowConfig { - pub url: String, - pub transparent: bool, - pub fullscreen: bool, - pub width: f64, - pub height: f64, - pub resizable: bool, - pub url_type: String - } - - - #[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 user_agent: UserAgent, - } -} - -use pake::PakeConfig; - -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); - 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) -} - - -pub fn get_menu() -> Menu { - // first menu - let hide = CustomMenuItem::new("hide", "Hide"); - let close = CustomMenuItem::new("close", "Close"); - let quit = CustomMenuItem::new("quit", "Quit"); - #[cfg(target_os = "macos")] - let first_menu = Menu::new() - .add_native_item(MenuItem::EnterFullScreen) - .add_native_item(MenuItem::Minimize) - .add_native_item(MenuItem::Separator) - .add_native_item(MenuItem::Copy) - .add_native_item(MenuItem::Cut) - .add_native_item(MenuItem::Paste) - .add_native_item(MenuItem::Undo) - .add_native_item(MenuItem::Redo) - .add_native_item(MenuItem::SelectAll) - .add_native_item(MenuItem::Separator) - .add_item(debug) - .add_item(hide) - .add_item(close) - .add_item(quit); - #[cfg(any(target_os = "linux", target_os = "windows"))] - let first_menu = Menu::new() - .add_item(hide) - .add_item(close) - .add_item(quit); - let first_menu = Submenu::new("File", first_menu); - // Hot Key - // let top = CustomMenuItem::new("top", "Top (↑)"); - // let buttom = CustomMenuItem::new("buttom", "Bottom (↓)"); - // let previous = CustomMenuItem::new("previous", "Previous (←)"); - // let next = CustomMenuItem::new("next", "next (→)"); - // let refresh = CustomMenuItem::new("refresh", "Refresh"); - let zoom_out = CustomMenuItem::new("zoom_out", "Zoom Out (125%)"); - let zoom_in = CustomMenuItem::new("zoom_in", "Zoom In (75%)"); - let zoom_reset = CustomMenuItem::new("reset", "Zoom Reset"); - let hot_key = Menu::new() - // .add_item(top) - // .add_item(buttom) - // .add_item(previous) - // .add_item(next) - // .add_item(refresh) - .add_item(zoom_in) - .add_item(zoom_out) - .add_item(zoom_reset); - let hot_key_menu = Submenu::new("Hot Key", hot_key); - - // Help - // let instructions = CustomMenuItem::new("instruction", "Instruction"); - // let about = CustomMenuItem::new("about", "About"); - // let help = Menu::new() - // .add_item(instructions) - // .add_item(about); - // let help_menu = Submenu::new("Help", help); - let menu = Menu::new() - .add_submenu(first_menu) - .add_submenu(hot_key_menu); - // .add_submenu(help_menu); - menu - - -} +#[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 { @@ -183,68 +76,6 @@ pub fn get_window(app: & mut App, config: PakeConfig, data_dir: std::path::PathB } -pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) { - #[cfg(target_os = "linux")] - { - // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html - // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html - use webkit2gtk::traits::WebViewExt; - webview.inner().set_zoom_level(zoom_value); - } - - #[cfg(windows)] - unsafe { - // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html - webview.controller().SetZoomFactor(zoom_value).unwrap(); - } - - #[cfg(target_os = "macos")] - unsafe { - let () = msg_send![webview.inner(), setPageZoom: zoom_value]; - let () = msg_send![webview.controller(), removeAllUserScripts]; - let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.]; - let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color]; - } -} - -pub fn set_zoom_out(webview: PlatformWebview) { - set_zoom(webview, 1.25); -} - - -pub fn set_zoom_in(webview: PlatformWebview) { - set_zoom(webview, 0.75); -} - - -pub fn zoom_reset(webview: PlatformWebview) { - set_zoom(webview, 1.0); -} - -pub fn menu_event_handle(event: WindowMenuEvent) { - match event.menu_item_id() { - "hide" => event.window().hide().expect("can't hide window"), - "close" => event.window().close().expect("can't close window"), - "quit" => std::process::exit(0), - "zoom_out" => { - event.window() - .with_webview(set_zoom_out) - .expect("can't set zoom out"); - }, - "zoom_in" => { - event.window() - .with_webview(set_zoom_in) - .expect("can't set zoom in"); - }, - "reset" => { - event.window() - .with_webview(zoom_reset) - .expect("can't reset zoom"); - }, - _ => {}, - } -} - pub fn get_system_tray() -> SystemTray { let hide = CustomMenuItem::new("hide".to_string(), "Hide"); From 2d7278a44d00c48f3d3b08a6e220a2cf388acb25 Mon Sep 17 00:00:00 2001 From: Tlntin Date: Tue, 27 Dec 2022 22:15:23 +0800 Subject: [PATCH 2/3] =?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"); } - - } From c6d8495f2bef0f7c2aa20022ad60a94bf8c355ea Mon Sep 17 00:00:00 2001 From: Tlntin Date: Tue, 27 Dec 2022 22:35:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/lib.rs | 152 ++++++++++++++++++------------------------ src-tauri/src/main.rs | 8 +-- 2 files changed, 66 insertions(+), 94 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index ace0516..6bd2441 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2,12 +2,10 @@ use tauri::MenuItem; use tauri::{ - CustomMenuItem, Menu, Submenu, WindowMenuEvent, window::PlatformWebview, - Config, SystemTrayMenu, SystemTray, SystemTrayEvent, Manager, - WindowBuilder, WindowUrl, App, Window, + window::PlatformWebview, App, Config, CustomMenuItem, Manager, Menu, Submenu, SystemTray, + SystemTrayEvent, SystemTrayMenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl, }; - pub fn get_menu() -> Menu { // first menu let hide = CustomMenuItem::new("hide", "Hide"); @@ -29,12 +27,9 @@ pub fn get_menu() -> Menu { .add_item(close) .add_item(quit); #[cfg(any(target_os = "linux", target_os = "windows"))] - let first_menu = Menu::new() - .add_item(hide) - .add_item(close) - .add_item(quit); + let first_menu = Menu::new().add_item(hide).add_item(close).add_item(quit); let first_menu = Submenu::new("File", first_menu); - // Hot Key + // Hot Key // let top = CustomMenuItem::new("top", "Top (↑)"); // let buttom = CustomMenuItem::new("buttom", "Bottom (↓)"); // let previous = CustomMenuItem::new("previous", "Previous (←)"); @@ -52,7 +47,7 @@ pub fn get_menu() -> Menu { .add_item(zoom_in) .add_item(zoom_out) .add_item(zoom_reset); - let hot_key_menu = Submenu::new("Hot Key", hot_key); + let hot_key_menu = Submenu::new("Hot Key", hot_key); // Help // let instructions = CustomMenuItem::new("instruction", "Instruction"); @@ -64,47 +59,42 @@ pub fn get_menu() -> Menu { let menu = Menu::new() .add_submenu(first_menu) .add_submenu(hot_key_menu); - // .add_submenu(help_menu); menu - - } - pub fn set_zoom(webview: PlatformWebview, zoom_value: f64) { #[cfg(target_os = "linux")] - { - // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html - // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html - use webkit2gtk::traits::WebViewExt; - webview.inner().set_zoom_level(zoom_value); - } + { + // see https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/struct.WebView.html + // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html + use webkit2gtk::traits::WebViewExt; + webview.inner().set_zoom_level(zoom_value); + } - #[cfg(windows)] - unsafe { - // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html - webview.controller().SetZoomFactor(zoom_value).unwrap(); - } + #[cfg(windows)] + unsafe { + // see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html + webview.controller().SetZoomFactor(zoom_value).unwrap(); + } - #[cfg(target_os = "macos")] - unsafe { - let () = msg_send![webview.inner(), setPageZoom: zoom_value]; - let () = msg_send![webview.controller(), removeAllUserScripts]; - let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.]; - let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color]; - } + #[cfg(target_os = "macos")] + unsafe { + let () = msg_send![webview.inner(), setPageZoom: zoom_value]; + let () = msg_send![webview.controller(), removeAllUserScripts]; + let bg_color: cocoa::base::id = + msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.]; + let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color]; + } } pub fn set_zoom_out(webview: PlatformWebview) { set_zoom(webview, 1.25); } - pub fn set_zoom_in(webview: PlatformWebview) { set_zoom(webview, 0.75); } - pub fn zoom_reset(webview: PlatformWebview) { set_zoom(webview, 1.0); } @@ -115,25 +105,27 @@ pub fn menu_event_handle(event: WindowMenuEvent) { "close" => event.window().close().expect("can't close window"), "quit" => std::process::exit(0), "zoom_out" => { - event.window() + event + .window() .with_webview(set_zoom_out) .expect("can't set zoom out"); - }, + } "zoom_in" => { - event.window() + event + .window() .with_webview(set_zoom_in) .expect("can't set zoom in"); - }, + } "reset" => { - event.window() + event + .window() .with_webview(zoom_reset) .expect("can't reset zoom"); - }, - _ => {}, + } + _ => {} } } - pub mod pake { use serde::Deserialize; @@ -145,17 +137,16 @@ pub mod pake { pub width: f64, pub height: f64, pub resizable: bool, - pub url_type: String + pub url_type: String, } - #[derive(Debug, Deserialize)] pub struct UserAgent { - pub macos: String, + pub macos: String, pub linux: String, pub windows: String, } - + #[derive(Debug, Deserialize)] pub struct PakeConfig { pub windows: Vec, @@ -163,21 +154,19 @@ pub mod pake { } } - use pake::PakeConfig; -pub fn get_pake_config() -> (PakeConfig, 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"); + let pake_config: PakeConfig = + serde_json::from_str(pake_config_path).expect("failed to parse pake config"); // println!("{:#?}", 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"); + 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"); @@ -191,40 +180,37 @@ pub fn get_system_tray() -> SystemTray { 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, + app, "about", - WindowUrl::App(std::path::PathBuf::from("about_pake.html")) + 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!") - ; + .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 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!!"), @@ -240,21 +226,16 @@ pub fn get_data_dir(tauri_config: Config) -> std::path::PathBuf { data_dir } - -pub fn get_window(app: & mut App, config: PakeConfig, data_dir: std::path::PathBuf) -> Window { +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"), + "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 - ) + let window = WindowBuilder::new(app, "pake", url) .title("") .user_agent(user_agent.macos.as_str()) .resizable(window_config.resizable) @@ -269,19 +250,14 @@ pub fn get_window(app: & mut App, config: PakeConfig, data_dir: std::path::PathB 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")) + 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 cba98ff..0ac2093 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,11 +3,9 @@ windows_subsystem = "windows" )] - +use app::{get_data_dir, get_pake_config, get_system_tray, get_window, system_tray_handle}; #[cfg(target_os = "macos")] use app::{get_menu, menu_event_handle}; -use app::{get_window, get_system_tray, get_data_dir, get_pake_config, system_tray_handle}; - pub fn run_app() { let system_tray = get_system_tray(); @@ -58,8 +56,6 @@ pub fn run_app() { } } - fn main() { - run_app() - + run_app() }