diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index c44fe2ef1da..fd535f21a6e 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -159,7 +159,19 @@ public string Theme public string ResultSubFontStretch { get; set; } public bool UseGlyphIcons { get; set; } = true; public bool UseAnimation { get; set; } = true; - public bool UseSound { get; set; } = true; + private bool _useSound = true; + public bool UseSound + { + get => _useSound; + set + { + if (_useSound != value) + { + _useSound = value; + OnPropertyChanged(); + } + } + } public double SoundVolume { get; set; } = 50; public bool ShowBadges { get; set; } = false; public bool ShowBadgesGlobalOnly { get; set; } = false; diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 60517db9736..70273d616c3 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -95,7 +95,7 @@ public MainWindow() InitializeComponent(); UpdatePosition(); - InitSoundEffects(); + SyncSoundEffectsState(); RegisterSoundEffectsEvent(); DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste); _viewModel.ActualApplicationThemeChanged += ViewModel_ActualApplicationThemeChanged; @@ -328,6 +328,9 @@ private void OnLoaded(object sender, RoutedEventArgs e) case nameof(Settings.ShowAtTopmost): Topmost = _settings.ShowAtTopmost; break; + case nameof(Settings.UseSound): + SyncSoundEffectsState(); + break; } }; @@ -707,17 +710,67 @@ private void SoundPlay() { if (_settings.WMPInstalled) { + if (_animationSoundWMP == null) + { + return; + } + _animationSoundWMP.Position = TimeSpan.Zero; _animationSoundWMP.Volume = _settings.SoundVolume / 100.0; _animationSoundWMP.Play(); } else { + if (_animationSoundWPF == null) + { + return; + } + _animationSoundWPF.Play(); } } } + private bool IsSoundEffectsInitialized() + { + lock (_soundLock) + { + return _animationSoundWMP != null || _animationSoundWPF != null; + } + } + + private void DisposeSoundEffects() + { + lock (_soundLock) + { + _animationSoundWMP?.Stop(); + _animationSoundWMP?.Close(); + _animationSoundWMP = null; + + _animationSoundWPF?.Stop(); + _animationSoundWPF?.Dispose(); + _animationSoundWPF = null; + } + } + + private void SyncSoundEffectsState(bool forceReinitializeWhenEnabled = false) + { + if (!_settings.UseSound) + { + if (IsSoundEffectsInitialized()) + { + DisposeSoundEffects(); + } + + return; + } + + if (forceReinitializeWhenEnabled || !IsSoundEffectsInitialized()) + { + InitSoundEffects(); + } + } + private void RegisterSoundEffectsEvent() { // Fix for sound not playing after sleep / hibernate for both modern standby and legacy standby @@ -731,14 +784,14 @@ private void RegisterSoundEffectsEvent() return; } - // We must run InitSoundEffects on UI thread because MediaPlayer is a DispatcherObject + // We must run SyncSoundEffectsState on UI thread because MediaPlayer is a DispatcherObject if (!Application.Current.Dispatcher.CheckAccess()) { - Application.Current.Dispatcher.Invoke(InitSoundEffects); + Application.Current.Dispatcher.Invoke(() => SyncSoundEffectsState(forceReinitializeWhenEnabled: true)); return; } - InitSoundEffects(); + SyncSoundEffectsState(forceReinitializeWhenEnabled: true); }); } catch (Exception e) @@ -1480,10 +1533,9 @@ protected virtual void Dispose(bool disposing) { _hwndSource?.Dispose(); _notifyIcon?.Dispose(); - _animationSoundWMP?.Close(); - _animationSoundWPF?.Dispose(); - _viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged; UnregisterSoundEffectsEvent(); + DisposeSoundEffects(); + _viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged; } _disposed = true; diff --git a/SolutionAssemblyInfo.cs b/SolutionAssemblyInfo.cs index 2938bcbbedd..365cfc0d26b 100644 --- a/SolutionAssemblyInfo.cs +++ b/SolutionAssemblyInfo.cs @@ -10,6 +10,7 @@ [assembly: AssemblyDescription("Release build, https://github.com/Flow-Launcher/Flow.Launcher")] #endif +[assembly: AssemblyTitle("Flow Launcher")] [assembly: AssemblyCompany("Flow Launcher")] [assembly: AssemblyProduct("Flow Launcher")] [assembly: AssemblyCopyright("The MIT License (MIT)")]