feat: add show menu

This commit is contained in:
椰子 2024-11-01 20:00:29 +08:00
parent 82496dee31
commit 690f2272b9

View File

@ -1,12 +1,49 @@
use tauri::{
menu::{Menu, MenuItem},
menu::{MenuBuilder, MenuEvent, MenuItem},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
App, Manager,
App, AppHandle, Manager,
};
fn menu_event_handle(app: &AppHandle, event: MenuEvent) {
match event.id.as_ref() {
"quit" => {
println!("quit menu item was clicked");
app.exit(0);
}
"show" => {
println!("show menu item was clicked");
show_window(app.app_handle());
}
_ => {
println!("menu item {:?} not handled", event.id);
}
}
}
fn show_window(app_handle: &AppHandle) {
let window = app_handle.get_window("main").unwrap();
if !window.is_visible().unwrap() {
window.show().unwrap();
}
if window.is_minimized().unwrap() {
window.unminimize().unwrap();
}
if !window.is_focused().unwrap() {
window.set_focus().unwrap();
}
}
pub fn tray(app: &mut App) {
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap();
let menu = Menu::with_items(app, &[&quit_i]).unwrap();
let show_i = MenuItem::with_id(app, "show", "Show", true, None::<&str>).unwrap();
let menu = MenuBuilder::new(app)
.item(&show_i)
.separator()
.item(&quit_i)
.build()
.unwrap();
let _tray: tauri::tray::TrayIcon = TrayIconBuilder::new()
.tooltip("Realtime Voice Changer")
.icon(app.default_window_icon().unwrap().clone())
@ -16,31 +53,14 @@ pub fn tray(app: &mut App) {
button_state: MouseButtonState::Up,
..
} => {
let window = icon.app_handle().get_window("main").unwrap();
if !window.is_visible().unwrap() {
window.show().unwrap();
}
if window.is_minimized().unwrap() {
window.unminimize().unwrap();
}
if !window.is_focused().unwrap() {
window.set_focus().unwrap();
}
show_window(icon.app_handle());
println!("Tray icon clicked");
}
_ => {}
})
.menu(&menu)
.menu_on_left_click(false)
.on_menu_event(|app, event| match event.id.as_ref() {
"quit" => {
println!("quit menu item was clicked");
app.exit(0);
}
_ => {
println!("menu item {:?} not handled", event.id);
}
})
.on_menu_event(menu_event_handle)
.build(app)
.unwrap();
}