From 715b53ba82ee524f9afecd62c0ecf69661e4e579 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Wed, 24 Dec 2025 20:23:15 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=9C=80=E5=B0=8F=E5=8C=96=E6=97=B6=E6=89=98=E7=9B=98?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/tray.rs | 60 +++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 64186342..3d24dfae 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -1,12 +1,23 @@ -//! @file src/tray.rs -//! @desc 系统托盘模块,负责创建和管理系统托盘图标 -//! @since Beta v0.8.8 +// 系统托盘模块,负责创建和管理系统托盘图标 +// @since Beta v0.9.1 use tauri::image::Image; use tauri::menu::{MenuBuilder, MenuItemBuilder, PredefinedMenuItem}; use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; use tauri::{AppHandle, Manager, Runtime}; +/// 显示主窗口(带最小化判断) +fn show_main_window(app: &AppHandle) { + if let Some(window) = app.get_webview_window("TeyvatGuide") { + // 如果窗口处于最小化状态,则恢复 + if let Ok(true) = window.is_minimized() { + let _ = window.unminimize(); + } + let _ = window.show(); + let _ = window.set_focus(); + } +} + /// 创建系统托盘图标 pub fn create_tray(app: &AppHandle) -> tauri::Result<()> { // 创建托盘菜单 @@ -17,14 +28,7 @@ pub fn create_tray(app: &AppHandle) -> tauri::Result<()> { let menu = MenuBuilder::new(app).item(&show_item).item(&separator).item(&quit_item).build()?; // 加载托盘图标 - // 在不同操作系统上,托盘图标的显示效果可能有所不同: - // - Windows: 使用 .ico 格式获得最佳效果 - // - macOS: 支持 .icns 和 .png 格式 - // - Linux: 通常使用 .png 格式 let icon_bytes = include_bytes!("../icons/32x32.png"); - - // 使用 image crate 解码 PNG 图标 - // Tauri 的 Image 结构只接受原始 RGBA 数据,因此需要使用 image crate 解码 PNG let img = image::load_from_memory(icon_bytes).map_err(|e| { tauri::Error::InvalidIcon(std::io::Error::new(std::io::ErrorKind::InvalidData, e)) })?; @@ -37,26 +41,19 @@ pub fn create_tray(app: &AppHandle) -> tauri::Result<()> { .icon(icon) .tooltip("Teyvat Guide") .menu(&menu) - .on_menu_event(|app, event| { - match event.id.as_ref() { - "show" => { - if let Some(window) = app.get_webview_window("TeyvatGuide") { - let _ = window.show(); - let _ = window.set_focus(); - } - } - "quit" => { - // 关闭所有子窗口 - for label in crate::SUB_WINDOW_LABELS.iter() { - if let Some(sub) = app.get_webview_window(label) { - let _ = sub.destroy(); - } - } - // 退出应用 - app.exit(0); - } - _ => {} + .on_menu_event(|app, event| match event.id.as_ref() { + "show" => { + show_main_window(app); } + "quit" => { + for label in crate::SUB_WINDOW_LABELS.iter() { + if let Some(sub) = app.get_webview_window(label) { + let _ = sub.destroy(); + } + } + app.exit(0); + } + _ => {} }) .on_tray_icon_event(|tray, event| { if let TrayIconEvent::Click { @@ -66,10 +63,7 @@ pub fn create_tray(app: &AppHandle) -> tauri::Result<()> { } = event { let app = tray.app_handle(); - if let Some(window) = app.get_webview_window("TeyvatGuide") { - let _ = window.show(); - let _ = window.set_focus(); - } + show_main_window(&app); } }) .build(app)?;