add --maximize and --start-to-tray options
This commit is contained in:
@@ -15,7 +15,9 @@
|
||||
"hide_on_close": true,
|
||||
"incognito": false,
|
||||
"enable_wasm": false,
|
||||
"enable_drag_drop": false
|
||||
"enable_drag_drop": false,
|
||||
"maximize": false,
|
||||
"start_to_tray": false
|
||||
}
|
||||
],
|
||||
"user_agent": {
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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)?;
|
||||
|
||||
@@ -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(())
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user