Skip to content

Commit

Permalink
Fixes buttons not working on system notification
Browse files Browse the repository at this point in the history
  • Loading branch information
raphgodart committed Nov 22, 2017
1 parent f5e0f2d commit 1c8d527
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
32 changes: 28 additions & 4 deletions Dopamine.Common/Services/Notification/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class NotificationService : LegacyNotificationService
private SystemMediaTransportControlsDisplayUpdater displayUpdater;
private MusicDisplayProperties musicProperties;
private InMemoryRandomAccessStream artworkStream;

public override bool SystemNotificationIsEnabled
{
get => this.systemNotificationIsEnabled;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions Dopamine.Common/Services/Win32Input/IWin32InputService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ public interface IWin32InputService
{
void SetKeyboardHook(IntPtr hwnd);
void UnhookKeyboard();
event EventHandler MediaKeyNextPressed;
event EventHandler MediaKeyPreviousPressed;
event EventHandler MediaKeyPlayPressed;
}
}
41 changes: 23 additions & 18 deletions Dopamine.Common/Services/Win32Input/Win32InputService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Dopamine/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2017-11-21: Dopamine 1.5.3
2017-11-22: Dopamine 1.5.3
--------------------------

- [Changed] Updated the German translation
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions Dopamine/Views/Shell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1c8d527

Please sign in to comment.