feat(settings): add system tray settings
Add tray icon configuration options: - Enable/disable tray icon (requires restart) - Minimize to tray: hide window when clicking minimize - Close to tray: hide window instead of quitting Rust backend: TraySettingsState with SQLite persistence Frontend: SettingsView tray subsection with toggles Related to #15 (title bar settings)
This commit is contained in:
1 parent
3fb597f3fb
commit
077aa01356
7 files changed
+369
-8
No files matched your search
@@ -14,6 +14,7 @@ pub mod download_settings;
|
||||
pub mod playback_preferences;
|
||||
pub mod favorites_preferences;
|
||||
pub mod subscription_state;
|
||||
pub mod tray_settings;
|
||||
|
||||
pub use audio_settings::{
|
||||
AudioSettings,
|
||||
@@ -55,3 +56,12 @@ pub use subscription_state::{
|
||||
SubscriptionStateStore,
|
||||
create_subscription_state,
|
||||
};
|
||||
|
||||
pub use tray_settings::{
|
||||
TraySettings,
|
||||
TraySettingsState,
|
||||
get_tray_settings,
|
||||
set_enable_tray,
|
||||
set_minimize_to_tray,
|
||||
set_close_to_tray,
|
||||
};
|
||||
@@ -0,0 +1,186 @@
|
||||
//! Tray icon settings
|
||||
//!
|
||||
//! Stores user preferences for system tray behavior:
|
||||
//! - enable_tray: Show tray icon (requires restart)
|
||||
//! - minimize_to_tray: Hide to tray when minimizing
|
||||
//! - close_to_tray: Hide to tray when closing window
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use log::info;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TraySettings {
|
||||
/// Show system tray icon (requires restart to take effect)
|
||||
pub enable_tray: bool,
|
||||
/// Hide window to tray when clicking minimize
|
||||
pub minimize_to_tray: bool,
|
||||
/// Hide window to tray instead of quitting when clicking close
|
||||
pub close_to_tray: bool,
|
||||
}
|
||||
|
||||
impl Default for TraySettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enable_tray: true,
|
||||
minimize_to_tray: false,
|
||||
close_to_tray: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TraySettingsStore {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl TraySettingsStore {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let data_dir = dirs::data_dir()
|
||||
.ok_or("Could not determine data directory")?
|
||||
.join("qbz");
|
||||
|
||||
std::fs::create_dir_all(&data_dir)
|
||||
.map_err(|e| format!("Failed to create data directory: {}", e))?;
|
||||
|
||||
let db_path = data_dir.join("tray_settings.db");
|
||||
let conn = Connection::open(&db_path)
|
||||
.map_err(|e| format!("Failed to open tray settings database: {}", e))?;
|
||||
|
||||
// Create table
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE IF NOT EXISTS tray_settings (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
enable_tray INTEGER NOT NULL DEFAULT 1,
|
||||
minimize_to_tray INTEGER NOT NULL DEFAULT 0,
|
||||
close_to_tray INTEGER NOT NULL DEFAULT 0
|
||||
);"
|
||||
).map_err(|e| format!("Failed to create tray settings table: {}", e))?;
|
||||
|
||||
// Insert default row if it doesn't exist
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO tray_settings (id, enable_tray, minimize_to_tray, close_to_tray)
|
||||
VALUES (1, 1, 0, 0)",
|
||||
[]
|
||||
).map_err(|e| format!("Failed to insert default tray settings: {}", e))?;
|
||||
|
||||
info!("[TraySettings] Database initialized");
|
||||
|
||||
Ok(Self { conn })
|
||||
}
|
||||
|
||||
pub fn get_settings(&self) -> Result<TraySettings, String> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT enable_tray, minimize_to_tray, close_to_tray FROM tray_settings WHERE id = 1",
|
||||
[],
|
||||
|row| {
|
||||
let enable_tray: i32 = row.get(0)?;
|
||||
let minimize_to_tray: i32 = row.get(1)?;
|
||||
let close_to_tray: i32 = row.get(2)?;
|
||||
Ok(TraySettings {
|
||||
enable_tray: enable_tray != 0,
|
||||
minimize_to_tray: minimize_to_tray != 0,
|
||||
close_to_tray: close_to_tray != 0,
|
||||
})
|
||||
},
|
||||
)
|
||||
.map_err(|e| format!("Failed to get tray settings: {}", e))
|
||||
}
|
||||
|
||||
pub fn set_enable_tray(&self, value: bool) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE tray_settings SET enable_tray = ?1 WHERE id = 1",
|
||||
params![if value { 1 } else { 0 }],
|
||||
)
|
||||
.map_err(|e| format!("Failed to set enable_tray: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_minimize_to_tray(&self, value: bool) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE tray_settings SET minimize_to_tray = ?1 WHERE id = 1",
|
||||
params![if value { 1 } else { 0 }],
|
||||
)
|
||||
.map_err(|e| format!("Failed to set minimize_to_tray: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_close_to_tray(&self, value: bool) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE tray_settings SET close_to_tray = ?1 WHERE id = 1",
|
||||
params![if value { 1 } else { 0 }],
|
||||
)
|
||||
.map_err(|e| format!("Failed to set close_to_tray: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Global state wrapper for thread-safe access
|
||||
pub struct TraySettingsState {
|
||||
store: Arc<Mutex<TraySettingsStore>>,
|
||||
}
|
||||
|
||||
impl TraySettingsState {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let store = TraySettingsStore::new()?;
|
||||
Ok(Self {
|
||||
store: Arc::new(Mutex::new(store)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_settings(&self) -> Result<TraySettings, String> {
|
||||
self.store.lock().unwrap().get_settings()
|
||||
}
|
||||
|
||||
pub fn set_enable_tray(&self, value: bool) -> Result<(), String> {
|
||||
self.store.lock().unwrap().set_enable_tray(value)
|
||||
}
|
||||
|
||||
pub fn set_minimize_to_tray(&self, value: bool) -> Result<(), String> {
|
||||
self.store.lock().unwrap().set_minimize_to_tray(value)
|
||||
}
|
||||
|
||||
pub fn set_close_to_tray(&self, value: bool) -> Result<(), String> {
|
||||
self.store.lock().unwrap().set_close_to_tray(value)
|
||||
}
|
||||
}
|
||||
|
||||
// Tauri commands
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_tray_settings(
|
||||
state: tauri::State<TraySettingsState>,
|
||||
) -> Result<TraySettings, String> {
|
||||
state.get_settings()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_enable_tray(
|
||||
value: bool,
|
||||
state: tauri::State<TraySettingsState>,
|
||||
) -> Result<(), String> {
|
||||
info!("[TraySettings] Setting enable_tray to {} (restart required)", value);
|
||||
state.set_enable_tray(value)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_minimize_to_tray(
|
||||
value: bool,
|
||||
state: tauri::State<TraySettingsState>,
|
||||
) -> Result<(), String> {
|
||||
info!("[TraySettings] Setting minimize_to_tray to {}", value);
|
||||
state.set_minimize_to_tray(value)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_close_to_tray(
|
||||
value: bool,
|
||||
state: tauri::State<TraySettingsState>,
|
||||
) -> Result<(), String> {
|
||||
info!("[TraySettings] Setting close_to_tray to {}", value);
|
||||
state.set_close_to_tray(value)
|
||||
}
|
||||
+40
-4
@@ -186,6 +186,9 @@ pub fn run() {
|
||||
// Initialize favorites preferences state
|
||||
let favorites_prefs_state = config::favorites_preferences::FavoritesPreferencesState::new()
|
||||
.expect("Failed to initialize favorites preferences");
|
||||
// Initialize tray settings state
|
||||
let tray_settings_state = config::tray_settings::TraySettingsState::new()
|
||||
.expect("Failed to initialize tray settings");
|
||||
// Initialize subscription validity tracking (for offline download compliance)
|
||||
let subscription_state = config::create_subscription_state()
|
||||
.expect("Failed to initialize subscription state");
|
||||
@@ -215,15 +218,32 @@ pub fn run() {
|
||||
audio_settings.preferred_sample_rate
|
||||
);
|
||||
|
||||
// Read tray settings for initialization
|
||||
let tray_settings = tray_settings_state.get_settings().unwrap_or_default();
|
||||
log::info!(
|
||||
"Tray settings: enable={}, minimize_to_tray={}, close_to_tray={}",
|
||||
tray_settings.enable_tray,
|
||||
tray_settings.minimize_to_tray,
|
||||
tray_settings.close_to_tray
|
||||
);
|
||||
|
||||
// Clone settings for use in closures
|
||||
let enable_tray = tray_settings.enable_tray;
|
||||
let close_to_tray = tray_settings.close_to_tray;
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_clipboard_manager::init())
|
||||
.manage(AppState::with_device_and_settings(saved_device, audio_settings))
|
||||
.setup(|app| {
|
||||
// Initialize system tray icon
|
||||
if let Err(e) = tray::init_tray(app.handle()) {
|
||||
log::error!("Failed to initialize tray icon: {}", e);
|
||||
.setup(move |app| {
|
||||
// Initialize system tray icon (only if enabled)
|
||||
if enable_tray {
|
||||
if let Err(e) = tray::init_tray(app.handle()) {
|
||||
log::error!("Failed to initialize tray icon: {}", e);
|
||||
}
|
||||
} else {
|
||||
log::info!("System tray icon disabled by user setting");
|
||||
}
|
||||
|
||||
// Initialize media controls (MPRIS) now that we have an AppHandle
|
||||
@@ -355,6 +375,16 @@ pub fn run() {
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.on_window_event(move |window, event| {
|
||||
// Handle close to tray
|
||||
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
|
||||
if close_to_tray {
|
||||
log::info!("Close to tray: hiding window instead of closing");
|
||||
let _ = window.hide();
|
||||
api.prevent_close();
|
||||
}
|
||||
}
|
||||
})
|
||||
.manage(library_state)
|
||||
.manage(cast_state)
|
||||
.manage(dlna_state)
|
||||
@@ -370,6 +400,7 @@ pub fn run() {
|
||||
.manage(offline_state)
|
||||
.manage(playback_prefs_state)
|
||||
.manage(favorites_prefs_state)
|
||||
.manage(tray_settings_state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
// Auth commands
|
||||
commands::init_client,
|
||||
@@ -693,6 +724,11 @@ pub fn run() {
|
||||
// Flatpak detection commands
|
||||
flatpak::is_running_in_flatpak,
|
||||
flatpak::get_flatpak_help_text,
|
||||
// Tray settings commands
|
||||
config::tray_settings::get_tray_settings,
|
||||
config::tray_settings::set_enable_tray,
|
||||
config::tray_settings::set_minimize_to_tray,
|
||||
config::tray_settings::set_close_to_tray,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in new issue
Block a user