diff --git a/Dopamine.Common/Services/Notification/NotificationService.cs b/Dopamine.Common/Services/Notification/NotificationService.cs index c844acf5c..72789f1b8 100644 --- a/Dopamine.Common/Services/Notification/NotificationService.cs +++ b/Dopamine.Common/Services/Notification/NotificationService.cs @@ -20,7 +20,7 @@ public class NotificationService : LegacyNotificationService private SystemMediaTransportControlsDisplayUpdater displayUpdater; private MusicDisplayProperties musicProperties; private InMemoryRandomAccessStream artworkStream; - + public override bool SystemNotificationIsEnabled { get => this.systemNotificationIsEnabled; @@ -31,7 +31,7 @@ public override bool SystemNotificationIsEnabled Application.Current.Dispatcher.InvokeAsync(async () => await SwitchNotificationHandlerAsync(value)); } } - + public NotificationService(IPlaybackService playbackService, ICacheService cacheService, IMetadataService metadataService) : base(playbackService, cacheService, metadataService) { // Pay attention to UPPERCASE property @@ -44,13 +44,35 @@ public NotificationService(IPlaybackService playbackService, ICacheService cache systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Closed; displayUpdater.Update(); } - + + private async void SMCButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs e) + { + switch (e.Button) + { + case SystemMediaTransportControlsButton.Previous: + await this.PlaybackService.PlayPreviousAsync(); + break; + case SystemMediaTransportControlsButton.Next: + await this.PlaybackService.PlayNextAsync(); + break; + case SystemMediaTransportControlsButton.Pause: + await this.PlaybackService.PlayOrPauseAsync(); + break; + case SystemMediaTransportControlsButton.Play: + await this.PlaybackService.PlayOrPauseAsync(); + break; + default: + // Never happens + throw new ArgumentOutOfRangeException(); + } + } + protected override bool CanShowNotification() { if (this.systemNotificationIsEnabled) return false; return base.CanShowNotification(); } - + private void PlaybackResumedSystemNotificationHandler(object _, EventArgs __) { systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Playing; @@ -102,6 +124,7 @@ await Task.Run(() => systemMediaControls.IsFastForwardEnabled = false; systemMediaControls.IsRecordEnabled = false; systemMediaControls.IsStopEnabled = false; + systemMediaControls.ButtonPressed += SMCButtonPressed; this.PlaybackService.PlaybackSuccess += this.PlaybackSuccessSystemNotificationHandler; this.PlaybackService.PlaybackPaused += this.PlaybackPausedSystemNotificationHandler; @@ -117,6 +140,7 @@ await Task.Run(() => if (Constants.IsWindows10) { systemMediaControls.IsEnabled = false; + systemMediaControls.ButtonPressed -= SMCButtonPressed; this.PlaybackService.PlaybackSuccess -= this.PlaybackSuccessSystemNotificationHandler; this.PlaybackService.PlaybackPaused -= this.PlaybackPausedSystemNotificationHandler; diff --git a/Dopamine.Common/Services/Win32Input/IWin32InputService.cs b/Dopamine.Common/Services/Win32Input/IWin32InputService.cs index 004efb06d..fcb80a930 100644 --- a/Dopamine.Common/Services/Win32Input/IWin32InputService.cs +++ b/Dopamine.Common/Services/Win32Input/IWin32InputService.cs @@ -6,8 +6,5 @@ public interface IWin32InputService { void SetKeyboardHook(IntPtr hwnd); void UnhookKeyboard(); - event EventHandler MediaKeyNextPressed; - event EventHandler MediaKeyPreviousPressed; - event EventHandler MediaKeyPlayPressed; } } diff --git a/Dopamine.Common/Services/Win32Input/Win32InputService.cs b/Dopamine.Common/Services/Win32Input/Win32InputService.cs index ce4e8ba06..68a96f356 100644 --- a/Dopamine.Common/Services/Win32Input/Win32InputService.cs +++ b/Dopamine.Common/Services/Win32Input/Win32InputService.cs @@ -1,25 +1,25 @@ -using System; +using Digimezzo.Utilities.Settings; +using Dopamine.Common.Services.Playback; +using System; using System.Timers; namespace Dopamine.Common.Services.Win32Input { public class Win32InputService : IWin32InputService { - private bool canRaiseMediaKeyEvent = true; + private IPlaybackService playbackService; + private bool isMediaKeyJustPressed = false; private Timer canRaiseMediaKeyEventTimer = new Timer(); private IKeyboardHookManager lowLevelManager ; private IKeyboardHookManager appCommandManager; - public Win32InputService() + public Win32InputService(IPlaybackService playbackService) { + this.playbackService = playbackService; this.canRaiseMediaKeyEventTimer.Interval = 250; this.canRaiseMediaKeyEventTimer.Elapsed += CanPressMediaKeyTimer_Elapsed; } - public event EventHandler MediaKeyNextPressed = delegate { }; - public event EventHandler MediaKeyPreviousPressed = delegate { }; - public event EventHandler MediaKeyPlayPressed = delegate { }; - public void SetKeyboardHook(IntPtr hWnd) { #if DEBUG @@ -47,31 +47,31 @@ public void SetKeyboardHook(IntPtr hWnd) this.appCommandManager.SetHook(); } - private void MediaKeyNextPressedHandler(object sender, EventArgs e) + private async void MediaKeyNextPressedHandler(object sender, EventArgs e) { - if (this.canRaiseMediaKeyEvent) + if (this.CanPressMediaKey()) { - this.MediaKeyNextPressed(this, new EventArgs()); + await this.playbackService.PlayNextAsync(); } this.StartCanPressMediaKeyTimer(); } - private void MediaKeyPreviousPressedHandler(object sender, EventArgs e) + private async void MediaKeyPreviousPressedHandler(object sender, EventArgs e) { - if (this.canRaiseMediaKeyEvent) + if (this.CanPressMediaKey()) { - this.MediaKeyPreviousPressed(this, new EventArgs()); + await this.playbackService.PlayPreviousAsync(); } this.StartCanPressMediaKeyTimer(); } - private void MediaKeyPlayPressedHandler(object sender, EventArgs e) + private async void MediaKeyPlayPressedHandler(object sender, EventArgs e) { - if (this.canRaiseMediaKeyEvent) + if (this.CanPressMediaKey()) { - this.MediaKeyPlayPressed(this, new EventArgs()); + await this.playbackService.PlayOrPauseAsync(); } this.StartCanPressMediaKeyTimer(); @@ -94,15 +94,20 @@ public void UnhookKeyboard() this.appCommandManager.Unhook(); } + private bool CanPressMediaKey() + { + return !isMediaKeyJustPressed && SettingsClient.Get<bool>("Behaviour", "EnableSystemNotification"); + } + private void CanPressMediaKeyTimer_Elapsed(object sender, ElapsedEventArgs e) { canRaiseMediaKeyEventTimer.Stop(); - canRaiseMediaKeyEvent = true; + isMediaKeyJustPressed = false; } private void StartCanPressMediaKeyTimer() { - canRaiseMediaKeyEvent = false; + isMediaKeyJustPressed = true; canRaiseMediaKeyEventTimer.Start(); } } diff --git a/Dopamine/Changelog.txt b/Dopamine/Changelog.txt index dcbc3c544..de18bb106 100644 --- a/Dopamine/Changelog.txt +++ b/Dopamine/Changelog.txt @@ -1,4 +1,4 @@ -2017-11-21: Dopamine 1.5.3 +2017-11-22: Dopamine 1.5.3 -------------------------- - [Changed] Updated the German translation @@ -8,6 +8,7 @@ - [Fixed] Some broken files generate faulty artists, genres and albums in the collection during indexing - [Fixed] Portable version update notification forwards the user to the installer instead of download location - [Fixed] Files with special characters fail to play +- [Fixed] Buttons don't work on the system notification 2017-11-21: Dopamine 1.5.2 diff --git a/Dopamine/Views/Shell.xaml.cs b/Dopamine/Views/Shell.xaml.cs index 6436934b2..45a254c47 100644 --- a/Dopamine/Views/Shell.xaml.cs +++ b/Dopamine/Views/Shell.xaml.cs @@ -335,9 +335,6 @@ private void InitializeServices() { // IWin32InputService this.win32InputService.SetKeyboardHook(new WindowInteropHelper(this).EnsureHandle()); // Listen to media keys - this.win32InputService.MediaKeyNextPressed += async (_, __) => await this.playbackService.PlayNextAsync(); - this.win32InputService.MediaKeyPreviousPressed += async (_, __) => await this.playbackService.PlayPreviousAsync(); - this.win32InputService.MediaKeyPlayPressed += async (_, __) => await this.playbackService.PlayOrPauseAsync(); // IAppearanceService this.appearanceService.ThemeChanged += this.ThemeChangedHandler;