use tauri::{ menu::{MenuBuilder, MenuItemBuilder}, tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}, AppHandle, Manager, RunEvent, }; fn format_main_window_title(version: &str) -> String { format!("GoldenChart (v{version})") } fn apply_main_window_title(app: &AppHandle) { let title = format_main_window_title(&app.package_info().version.to_string()); if let Some(w) = app.get_webview_window("main") { let _ = w.set_title(&title); } } fn show_main_window(app: &AppHandle) { if let Some(w) = app.get_webview_window("main") { let _ = w.show(); let _ = w.unminimize(); let _ = w.set_focus(); } apply_main_window_title(app); } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_notification::init()) .plugin(tauri_plugin_http::init()) .plugin(tauri_plugin_updater::Builder::new().build()) .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_opener::init()) .setup(|app| { apply_main_window_title(app.handle()); let show_i = MenuItemBuilder::with_id("show", "GoldenChart 열기").build(app)?; let quit_i = MenuItemBuilder::with_id("quit", "종료").build(app)?; let menu = MenuBuilder::new(app) .items(&[&show_i, &quit_i]) .build()?; let icon = app .default_window_icon() .expect("missing tray icon") .clone(); let _tray = TrayIconBuilder::new() .icon(icon) .menu(&menu) .tooltip("GoldenChart") .show_menu_on_left_click(false) .on_menu_event(|app, event| match event.id.as_ref() { "show" => show_main_window(app), "quit" => app.exit(0), _ => {} }) .on_tray_icon_event(|tray, event| { if let TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } = event { show_main_window(tray.app_handle()); } }) .build(app)?; show_main_window(app.handle()); Ok(()) }) .on_page_load(|webview, _payload| { if webview.label() != "main" { return; } apply_main_window_title(webview.app_handle()); }) .on_window_event(|window, event| { if window.label() != "main" { return; } if matches!(event, tauri::WindowEvent::Focused(true)) { apply_main_window_title(window.app_handle()); } if let tauri::WindowEvent::CloseRequested { api, .. } = event { api.prevent_close(); let _ = window.hide(); } }) .build(tauri::generate_context!()) .expect("error while building tauri application") .run(|app_handle, event| match event { RunEvent::Ready => apply_main_window_title(&app_handle), #[cfg(target_os = "macos")] RunEvent::Reopen { .. } => show_main_window(&app_handle), RunEvent::ExitRequested { api, .. } => { api.prevent_exit(); if let Some(w) = app_handle.get_webview_window("main") { let _ = w.hide(); } } _ => {} }); }