refactor: 升级 tauri 到 2.x
This commit is contained in:
@@ -1,31 +1,30 @@
|
||||
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
|
||||
use tauri::{
|
||||
menu::{MenuBuilder, MenuItemBuilder}, tray::TrayIconBuilder, AppHandle, Manager
|
||||
};
|
||||
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
|
||||
|
||||
pub fn get_system_tray() -> SystemTray {
|
||||
let hide_app = CustomMenuItem::new("hide_app".to_string(), "Hide");
|
||||
let show_app = CustomMenuItem::new("show_app".to_string(), "Show");
|
||||
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
|
||||
let tray_menu = SystemTrayMenu::new()
|
||||
.add_item(show_app)
|
||||
.add_item(hide_app)
|
||||
.add_item(quit);
|
||||
SystemTray::new().with_menu(tray_menu)
|
||||
}
|
||||
pub fn set_system_tray(app: &AppHandle) -> tauri::Result<()> {
|
||||
let hide_app = MenuItemBuilder::with_id("hide_app", "Hide").build(app)?;
|
||||
let show_app = MenuItemBuilder::with_id("show_app", "Show").build(app)?;
|
||||
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
|
||||
let menu = MenuBuilder::new(app).items(&[&hide_app, &show_app, &quit]).build()?;
|
||||
|
||||
pub fn system_tray_handle(app: &tauri::AppHandle, event: SystemTrayEvent) {
|
||||
if let SystemTrayEvent::MenuItemClick { tray_id: _, id, .. } = event {
|
||||
match id.as_str() {
|
||||
TrayIconBuilder::new()
|
||||
.menu(&menu)
|
||||
.on_menu_event(move |app, event| match event.id().as_ref() {
|
||||
"hide_app" => {
|
||||
app.get_window("pake").unwrap().minimize().unwrap();
|
||||
app.get_webview_window("pake").unwrap().minimize().unwrap();
|
||||
}
|
||||
"show_app" => {
|
||||
app.get_window("pake").unwrap().show().unwrap();
|
||||
app.get_webview_window("pake").unwrap().show().unwrap();
|
||||
}
|
||||
"quit" => {
|
||||
let _res = app.save_window_state(StateFlags::all());
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
_ => (),
|
||||
})
|
||||
.build(app)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user