diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 5bb6bf753..15836f065 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -350,6 +350,7 @@ pub fn run() { library::commands::library_get_folders, library::commands::library_scan, library::commands::library_get_scan_progress, + library::commands::library_stop_scan, library::commands::library_get_albums, library::commands::library_get_album_tracks, library::commands::library_get_artists, diff --git a/src-tauri/src/library/commands.rs b/src-tauri/src/library/commands.rs index 11e2c06ef..db356d754 100644 --- a/src-tauri/src/library/commands.rs +++ b/src-tauri/src/library/commands.rs @@ -2,6 +2,7 @@ use std::fs; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use tauri::State; use tokio::sync::Mutex; @@ -17,6 +18,7 @@ use crate::library::{ pub struct LibraryState { pub db: Arc>, pub scan_progress: Arc>, + pub scan_cancel: Arc, } fn normalize_library_path(path: &Path) -> PathBuf { @@ -82,7 +84,8 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String> return Err("No library folders configured".to_string()); } - // Reset progress + // Reset cancel flag and progress + state.scan_cancel.store(false, Ordering::Relaxed); { let mut progress = state.scan_progress.lock().await; *progress = ScanProgress { @@ -122,6 +125,15 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String> // Process CUE files first (they create multiple tracks from one file) for cue_path in &scan_result.cue_files { + // Check for cancellation + if state.scan_cancel.load(Ordering::Relaxed) { + let mut progress = state.scan_progress.lock().await; + progress.status = ScanStatus::Cancelled; + progress.current_file = None; + log::info!("Library scan cancelled by user"); + return Ok(()); + } + { let mut progress = state.scan_progress.lock().await; progress.current_file = Some(cue_path.to_string_lossy().to_string()); @@ -157,6 +169,15 @@ pub async fn library_scan(state: State<'_, LibraryState>) -> Result<(), String> .collect(); for audio_path in &scan_result.audio_files { + // Check for cancellation + if state.scan_cancel.load(Ordering::Relaxed) { + let mut progress = state.scan_progress.lock().await; + progress.status = ScanStatus::Cancelled; + progress.current_file = None; + log::info!("Library scan cancelled by user"); + return Ok(()); + } + // Skip if this file is referenced by a CUE sheet let canonical_path = normalize_library_path(audio_path); let path_str = canonical_path.to_string_lossy().to_string(); @@ -300,6 +321,13 @@ pub async fn library_get_scan_progress( Ok(progress.clone()) } +#[tauri::command] +pub async fn library_stop_scan(state: State<'_, LibraryState>) -> Result<(), String> { + log::info!("Command: library_stop_scan"); + state.scan_cancel.store(true, Ordering::Relaxed); + Ok(()) +} + // === Queries === #[tauri::command] diff --git a/src-tauri/src/library/mod.rs b/src-tauri/src/library/mod.rs index 5151360ac..5f4f7042f 100644 --- a/src-tauri/src/library/mod.rs +++ b/src-tauri/src/library/mod.rs @@ -20,6 +20,7 @@ pub use models::*; pub use scanner::{LibraryScanner, ScanResult}; use std::path::PathBuf; +use std::sync::atomic::AtomicBool; use std::sync::Arc; use tokio::sync::Mutex; @@ -50,5 +51,6 @@ pub fn init_library_state() -> Result { Ok(LibraryState { db: Arc::new(Mutex::new(db)), scan_progress: Arc::new(Mutex::new(ScanProgress::default())), + scan_cancel: Arc::new(AtomicBool::new(false)), }) } diff --git a/src-tauri/src/library/models.rs b/src-tauri/src/library/models.rs index cce3af511..c97439750 100644 --- a/src-tauri/src/library/models.rs +++ b/src-tauri/src/library/models.rs @@ -156,6 +156,7 @@ pub enum ScanStatus { Idle, Scanning, Complete, + Cancelled, Error, } diff --git a/src/lib/app/bootstrap.ts b/src/lib/app/bootstrap.ts index edec95b86..fa7ac6b0b 100644 --- a/src/lib/app/bootstrap.ts +++ b/src/lib/app/bootstrap.ts @@ -8,6 +8,8 @@ import { invoke } from '@tauri-apps/api/core'; import { getCurrentWebview } from '@tauri-apps/api/webview'; import { goBack, goForward } from '$lib/stores/navigationStore'; +import { loadToastsPreference } from '$lib/stores/toastStore'; +import { loadSystemNotificationsPreference } from '$lib/services/playbackService'; // ============ Theme Management ============ @@ -106,6 +108,10 @@ export function bootstrapApp(): BootstrapResult { loadSavedTheme(); void applySavedZoom(); + // Load notification preferences + loadToastsPreference(); + loadSystemNotificationsPreference(); + // Setup mouse navigation const cleanupMouse = setupMouseNavigation(); diff --git a/src/lib/components/views/LocalLibraryView.svelte b/src/lib/components/views/LocalLibraryView.svelte index 61652c0f0..8313ca650 100644 --- a/src/lib/components/views/LocalLibraryView.svelte +++ b/src/lib/components/views/LocalLibraryView.svelte @@ -79,7 +79,7 @@ } interface ScanProgress { - status: 'Idle' | 'Scanning' | 'Complete' | 'Error'; + status: 'Idle' | 'Scanning' | 'Complete' | 'Cancelled' | 'Error'; total_files: number; processed_files: number; current_file?: string; @@ -289,7 +289,7 @@ const pollInterval = setInterval(async () => { try { scanProgress = await invoke('library_get_scan_progress'); - if (scanProgress.status === 'Complete' || scanProgress.status === 'Error') { + if (scanProgress.status === 'Complete' || scanProgress.status === 'Cancelled' || scanProgress.status === 'Error') { clearInterval(pollInterval); scanning = false; await loadLibraryData(); @@ -310,6 +310,14 @@ } } + async function handleStopScan() { + try { + await invoke('library_stop_scan'); + } catch (err) { + console.error('Failed to stop scan:', err); + } + } + async function handleClearLibrary() { if (!confirm('Clear entire library? This will remove all indexed tracks. Your files will not be deleted.')) { return; @@ -981,6 +989,10 @@ {#if scanProgress.current_file} {scanProgress.current_file.split('/').pop()} {/if} + {/if} @@ -1614,6 +1626,26 @@ white-space: nowrap; } + .stop-scan-btn { + display: flex; + align-items: center; + gap: 4px; + padding: 4px 10px; + background: var(--bg-tertiary); + border: 1px solid var(--border-subtle); + border-radius: 6px; + color: var(--text-secondary); + font-size: 12px; + cursor: pointer; + transition: all 0.15s ease; + } + + .stop-scan-btn:hover { + background: var(--error); + border-color: var(--error); + color: white; + } + /* Settings Panel */ .settings-panel { background: var(--bg-secondary); diff --git a/src/lib/components/views/SettingsView.svelte b/src/lib/components/views/SettingsView.svelte index ff947f2dc..fc71a47d0 100644 --- a/src/lib/components/views/SettingsView.svelte +++ b/src/lib/components/views/SettingsView.svelte @@ -17,10 +17,15 @@ import { clearCache as clearLyricsCache } from '$lib/stores/lyricsStore'; import { getDevicePrettyName } from '$lib/utils/audioDeviceNames'; import { - getNotificationsEnabled, - setNotificationsEnabled, - loadNotificationsPreference + getToastsEnabled, + setToastsEnabled, + loadToastsPreference } from '$lib/stores/toastStore'; + import { + getSystemNotificationsEnabled, + setSystemNotificationsEnabled, + loadSystemNotificationsPreference + } from '$lib/services/playbackService'; interface Props { onBack?: () => void; @@ -143,7 +148,8 @@ // Appearance settings let theme = $state('Dark'); let compactMode = $state(false); - let notificationsEnabled = $state(true); + let toastsEnabled = $state(true); + let systemNotificationsEnabled = $state(true); let language = $state('Auto'); // Last.fm integration state @@ -216,9 +222,11 @@ // Load API keys state loadApiKeysState(); - // Load notifications preference - loadNotificationsPreference(); - notificationsEnabled = getNotificationsEnabled(); + // Load notification preferences + loadToastsPreference(); + toastsEnabled = getToastsEnabled(); + loadSystemNotificationsPreference(); + systemNotificationsEnabled = getSystemNotificationsEnabled(); }); async function loadLastfmState() { @@ -854,9 +862,13 @@ Compact Mode (compactMode = v)} /> +
+ In-app Toasts + { toastsEnabled = v; setToastsEnabled(v); }} /> +
- Notifications - { notificationsEnabled = v; setNotificationsEnabled(v); }} /> + System Notifications + { systemNotificationsEnabled = v; setSystemNotificationsEnabled(v); }} />
diff --git a/src/lib/services/playbackService.ts b/src/lib/services/playbackService.ts index b6b830f42..be747623d 100644 --- a/src/lib/services/playbackService.ts +++ b/src/lib/services/playbackService.ts @@ -206,6 +206,33 @@ function normalizeCoverUrlForMetadata(coverUrl?: string | null): string | null { // ============ System Notifications ============ +let systemNotificationsEnabled = true; + +/** + * Load system notifications preference from localStorage + */ +export function loadSystemNotificationsPreference(): void { + const saved = localStorage.getItem('qbz-system-notifications-enabled'); + if (saved !== null) { + systemNotificationsEnabled = saved === 'true'; + } +} + +/** + * Set system notifications enabled/disabled + */ +export function setSystemNotificationsEnabled(enabled: boolean): void { + systemNotificationsEnabled = enabled; + localStorage.setItem('qbz-system-notifications-enabled', String(enabled)); +} + +/** + * Get system notifications enabled state + */ +export function getSystemNotificationsEnabled(): boolean { + return systemNotificationsEnabled; +} + /** * Show system notification for track change */ @@ -217,6 +244,11 @@ export async function showTrackNotification( bitDepth?: number, sampleRate?: number ): Promise { + // Skip if system notifications are disabled + if (!systemNotificationsEnabled) { + return; + } + try { await invoke('show_track_notification', { title, diff --git a/src/lib/stores/toastStore.ts b/src/lib/stores/toastStore.ts index a829a6702..39004a53a 100644 --- a/src/lib/stores/toastStore.ts +++ b/src/lib/stores/toastStore.ts @@ -18,35 +18,43 @@ let currentToast: Toast | null = null; // Track buffering toast specifically so we can dismiss it let bufferingToastActive = false; -// Global enable/disable for notifications -let notificationsEnabled = true; +// Global enable/disable for toasts (in-app notifications) +let toastsEnabled = true; /** - * Load notifications preference from localStorage + * Load toasts preference from localStorage */ -export function loadNotificationsPreference(): void { - const saved = localStorage.getItem('qbz-notifications-enabled'); +export function loadToastsPreference(): void { + const saved = localStorage.getItem('qbz-toasts-enabled'); if (saved !== null) { - notificationsEnabled = saved === 'true'; + toastsEnabled = saved === 'true'; + } else { + // Migrate from old key if exists + const oldSaved = localStorage.getItem('qbz-notifications-enabled'); + if (oldSaved !== null) { + toastsEnabled = oldSaved === 'true'; + localStorage.setItem('qbz-toasts-enabled', oldSaved); + localStorage.removeItem('qbz-notifications-enabled'); + } } } /** - * Set notifications enabled/disabled + * Set toasts enabled/disabled */ -export function setNotificationsEnabled(enabled: boolean): void { - notificationsEnabled = enabled; - localStorage.setItem('qbz-notifications-enabled', String(enabled)); +export function setToastsEnabled(enabled: boolean): void { + toastsEnabled = enabled; + localStorage.setItem('qbz-toasts-enabled', String(enabled)); if (!enabled) { hideToast(); } } /** - * Get notifications enabled state + * Get toasts enabled state */ -export function getNotificationsEnabled(): boolean { - return notificationsEnabled; +export function getToastsEnabled(): boolean { + return toastsEnabled; } // Auto-hide timeout @@ -76,8 +84,8 @@ export function isBufferingActive(): boolean { * @param duration How long to show the toast in ms (default: varies by type) */ export function showToast(message: string, type: ToastType = 'info', duration?: number): void { - // Skip if notifications are disabled (except errors which are always shown) - if (!notificationsEnabled && type !== 'error') { + // Skip if toasts are disabled (except errors which are always shown) + if (!toastsEnabled && type !== 'error') { return; }