增加托盘,菜单栏控制是否开启选项

This commit is contained in:
Tlntin
2022-12-28 12:49:02 +08:00
parent 60f8da8e83
commit c48d48363c
4 changed files with 92 additions and 38 deletions

View File

@@ -14,5 +14,15 @@
"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"
},
"menu": {
"macos": true,
"linux": true,
"windows": false
},
"system_tray": {
"macos": false,
"linux": false,
"windows": true
}
}

View File

@@ -9,6 +9,8 @@ use tauri::{
window::PlatformWebview, App, Config, CustomMenuItem, Manager, Menu, Submenu, SystemTray,
SystemTrayEvent, SystemTrayMenu, Window, WindowBuilder, WindowMenuEvent, WindowUrl,
};
mod pake;
use pake::pake::PakeConfig;
pub fn get_menu() -> Menu {
// first menu
@@ -137,36 +139,6 @@ pub fn menu_event_handle(event: WindowMenuEvent) {
}
}
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<WindowConfig>,
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 =
@@ -178,21 +150,24 @@ pub fn get_pake_config() -> (PakeConfig, Config) {
(pake_config, tauri_config)
}
pub fn get_system_tray() -> SystemTray {
pub fn get_system_tray(show_menu: bool) -> SystemTray {
let hide_app = CustomMenuItem::new("hide_app".to_string(), "Hide App");
let show_app = CustomMenuItem::new("show_app".to_string(), "Show App");
let hide_menu = CustomMenuItem::new("hide_menu".to_string(), "Hide Menu");
let show_menu = CustomMenuItem::new("show_menu".to_string(), "Show Menu");
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let about = CustomMenuItem::new("about".to_string(), "About");
let tray_menu = SystemTrayMenu::new()
.add_item(hide_app)
.add_item(show_app)
.add_item(hide_menu)
.add_item(show_menu)
.add_item(quit)
.add_item(about);
SystemTray::new().with_menu(tray_menu)
if show_menu {
let hide_menu = CustomMenuItem::new("hide_menu".to_string(), "Hide Menu");
let show_menu = CustomMenuItem::new("show_menu".to_string(), "Show Menu");
let tray_menu = tray_menu.add_item(hide_menu).add_item(show_menu);
SystemTray::new().with_menu(tray_menu)
} else {
SystemTray::new().with_menu(tray_menu)
}
}
pub fn system_tray_handle(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) {

View File

@@ -9,15 +9,26 @@ use app::{
};
pub fn run_app() {
let system_tray = get_system_tray();
let (pake_config, tauri_config) = get_pake_config();
let show_menu = pake_config.show_menu();
let show_system_tray = pake_config.show_system_tray();
let system_tray = get_system_tray(show_menu);
let data_dir = get_data_dir(tauri_config);
let menu = get_menu();
tauri::Builder::default()
let tauri_app = if show_menu && ! show_system_tray {
tauri::Builder::default().menu(menu).on_menu_event(menu_event_handle)
} else if !show_menu && show_system_tray {
tauri::Builder::default().system_tray(system_tray).on_system_tray_event(system_tray_handle)
} else if show_menu && show_system_tray {
tauri::Builder::default()
.menu(menu)
.on_menu_event(menu_event_handle)
.system_tray(system_tray)
.on_system_tray_event(system_tray_handle)
} else {
tauri::Builder::default()
};
tauri_app
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![])
.setup(|app| {

58
src-tauri/src/pake.rs Normal file
View File

@@ -0,0 +1,58 @@
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 FunctionON {
pub macos: bool,
pub linux: bool,
pub windows: bool,
}
#[derive(Debug, Deserialize)]
pub struct PakeConfig {
pub windows: Vec<WindowConfig>,
pub user_agent: UserAgent,
pub menu: FunctionON,
pub system_tray: FunctionON,
}
impl PakeConfig {
pub fn show_menu(&self) -> bool {
#[cfg(target_os = "macos")]
let menu_status = self.menu.macos;
#[cfg(target_os = "linux")]
let menu_status = self.menu.linux;
#[cfg(target_os = "windows")]
let menu_status = self.menu.windows;
menu_status
}
pub fn show_system_tray(&self) -> bool {
#[cfg(target_os = "macos")]
let tray_status = self.system_tray.macos;
#[cfg(target_os = "linux")]
let tary_status = self.system_tray.linux;
#[cfg(target_os = "windows")]
let tary_status = self.system_tray.windows;
tary_status
}
}
}