add --maximize and --start-to-tray options

This commit is contained in:
Tw93
2025-10-17 18:00:28 +08:00
parent fcf807a88d
commit fabebdbd99
12 changed files with 109 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ pub struct WindowConfig {
pub url: String,
pub hide_title_bar: bool,
pub fullscreen: bool,
pub maximize: bool,
pub width: f64,
pub height: f64,
pub resizable: bool,
@@ -18,6 +19,7 @@ pub struct WindowConfig {
pub title: Option<String>,
pub enable_wasm: bool,
pub enable_drag_drop: bool,
pub start_to_tray: bool,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@@ -3,13 +3,13 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tauri::{
menu::{MenuBuilder, MenuItemBuilder},
tray::TrayIconBuilder,
tray::{TrayIconBuilder, TrayIconEvent},
AppHandle, Manager,
};
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut};
use tauri_plugin_window_state::{AppHandleExt, StateFlags};
pub fn set_system_tray(app: &AppHandle, show_system_tray: bool) -> tauri::Result<()> {
pub fn set_system_tray(app: &AppHandle, show_system_tray: bool, tray_icon_path: &str) -> tauri::Result<()> {
if !show_system_tray {
app.remove_tray_by_id("pake-tray");
return Ok(());
@@ -44,7 +44,30 @@ pub fn set_system_tray(app: &AppHandle, show_system_tray: bool) -> tauri::Result
}
_ => (),
})
.icon(app.default_window_icon().unwrap().clone())
.on_tray_icon_event(|tray, event| match event {
TrayIconEvent::Click { button, .. } => {
if button == tauri::tray::MouseButton::Left {
if let Some(window) = tray.app_handle().get_webview_window("pake") {
let is_visible = window.is_visible().unwrap_or(false);
if is_visible {
window.hide().unwrap();
} else {
window.show().unwrap();
window.set_focus().unwrap();
}
}
}
}
_ => {}
})
.icon(if tray_icon_path.is_empty() {
app.default_window_icon().unwrap_or_else(|| panic!("Failed to get default window icon")).clone()
} else {
tauri::image::Image::from_path(tray_icon_path).unwrap_or_else(|_| {
// If custom tray icon fails to load, fallback to default
app.default_window_icon().unwrap_or_else(|| panic!("Failed to get default window icon")).clone()
})
})
.build(app)?;
tray.set_icon_as_template(false)?;

View File

@@ -24,6 +24,7 @@ pub fn run_app() {
let hide_on_close = pake_config.windows[0].hide_on_close;
let activation_shortcut = pake_config.windows[0].activation_shortcut.clone();
let init_fullscreen = pake_config.windows[0].fullscreen;
let start_to_tray = pake_config.windows[0].start_to_tray && show_system_tray; // Only valid when tray is enabled
let multi_instance = pake_config.multi_instance;
let window_state_plugin = WindowStatePlugin::default()
@@ -62,15 +63,18 @@ pub fn run_app() {
])
.setup(move |app| {
let window = set_window(app, &pake_config, &tauri_config);
set_system_tray(app.app_handle(), show_system_tray).unwrap();
set_system_tray(app.app_handle(), show_system_tray, &pake_config.system_tray_path).unwrap();
set_global_shortcut(app.app_handle(), activation_shortcut).unwrap();
// Show window after state restoration to prevent position flashing
let window_clone = window.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
window_clone.show().unwrap();
});
// Unless start_to_tray is enabled, then keep it hidden
if !start_to_tray {
let window_clone = window.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
window_clone.show().unwrap();
});
}
Ok(())
})