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)
|
||||
}
|
||||
Reference in new issue
Block a user