-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from zadjii-msft/dev/crutkas/cleanpu
media player plugin
- Loading branch information
Showing
5 changed files
with
168 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/modules/cmdpal/extensions/MediaControlsExtension/MediaListItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Windows.CommandPalette.Extensions.Helpers; | ||
using Windows.Media.Control; | ||
|
||
namespace MediaControlsExtension; | ||
|
||
internal sealed class MediaListItem : ListItem | ||
{ | ||
private GlobalSystemMediaTransportControlsSession _mediaSession; | ||
|
||
public MediaListItem() | ||
: base(new TogglePlayMediaAction()) | ||
{ | ||
var task = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().AsTask(); | ||
task.ContinueWith(async t => | ||
{ | ||
var manager = t.Result; | ||
_mediaSession = manager.GetCurrentSession(); | ||
((TogglePlayMediaAction)this.Command).MediaSession = _mediaSession; | ||
|
||
_mediaSession.MediaPropertiesChanged += MediaSession_MediaPropertiesChanged; | ||
_mediaSession.PlaybackInfoChanged += MediaSession_PlaybackInfoChanged; | ||
|
||
// mediaSession.TimelinePropertiesChanged += MediaSession_TimelinePropertiesChanged; | ||
await this.UpdateProperties(); | ||
}); | ||
|
||
// task.Start(); | ||
this._MoreCommands = null; | ||
} | ||
|
||
private async Task UpdateProperties() | ||
{ | ||
var properties = await this._mediaSession.TryGetMediaPropertiesAsync().AsTask(); | ||
|
||
if (properties == null) | ||
{ | ||
var a = (TogglePlayMediaAction)this.Command; | ||
a.Icon = new(string.Empty); | ||
a.Name = "No media playing"; | ||
|
||
return; | ||
} | ||
|
||
this.Title = properties.Title; | ||
|
||
// hack | ||
((TogglePlayMediaAction)this.Command).Name = this.Title; | ||
this.Subtitle = properties.Artist; | ||
var status = _mediaSession.GetPlaybackInfo().PlaybackStatus; | ||
|
||
var internalAction = (TogglePlayMediaAction)this.Command; | ||
if (status == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused) | ||
{ | ||
internalAction.Icon = new("\ue768"); // play | ||
internalAction.Name = "Paused"; | ||
} | ||
else if (status == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing) | ||
{ | ||
internalAction.Icon = new("\ue769"); // pause | ||
internalAction.Name = "Playing"; | ||
} | ||
|
||
MoreCommands = [ | ||
new CommandContextItem(new PrevNextTrackAction(true, _mediaSession)), | ||
new CommandContextItem(new PrevNextTrackAction(false, _mediaSession)) | ||
]; | ||
} | ||
|
||
private void MediaSession_TimelinePropertiesChanged(GlobalSystemMediaTransportControlsSession sender, TimelinePropertiesChangedEventArgs args) | ||
{ | ||
_ = UpdateProperties(); | ||
} | ||
|
||
private void MediaSession_PlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender, PlaybackInfoChangedEventArgs args) | ||
{ | ||
_ = UpdateProperties(); | ||
} | ||
|
||
private void MediaSession_MediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args) | ||
{ | ||
_ = UpdateProperties(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/modules/cmdpal/extensions/MediaControlsExtension/PrevNextTrackAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Windows.CommandPalette.Extensions; | ||
using Microsoft.Windows.CommandPalette.Extensions.Helpers; | ||
using Windows.Media.Control; | ||
|
||
namespace MediaControlsExtension; | ||
|
||
internal sealed class PrevNextTrackAction : InvokableCommand | ||
{ | ||
private readonly GlobalSystemMediaTransportControlsSession _mediaSession; | ||
private readonly bool _previous; | ||
|
||
public PrevNextTrackAction(bool previous, GlobalSystemMediaTransportControlsSession s) | ||
{ | ||
_mediaSession = s; | ||
_previous = previous; | ||
|
||
if (previous) | ||
{ | ||
Name = "Previous track"; | ||
Icon = new("\ue892"); | ||
} | ||
else | ||
{ | ||
Name = "Next track"; | ||
Icon = new("\ue893"); | ||
} | ||
} | ||
|
||
public override ICommandResult Invoke() | ||
{ | ||
if (_previous) | ||
{ | ||
_ = _mediaSession.TrySkipPreviousAsync(); | ||
} | ||
else | ||
{ | ||
_ = _mediaSession.TrySkipNextAsync(); | ||
} | ||
|
||
return ActionResult.KeepOpen(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/modules/cmdpal/extensions/MediaControlsExtension/TogglePlayMediaAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Windows.CommandPalette.Extensions.Helpers; | ||
using Windows.Media.Control; | ||
|
||
namespace MediaControlsExtension; | ||
|
||
public sealed class TogglePlayMediaAction : InvokableCommand | ||
{ | ||
public GlobalSystemMediaTransportControlsSession MediaSession { get; set; } | ||
|
||
public TogglePlayMediaAction() | ||
{ | ||
Name = "No media playing"; | ||
Icon = new(string.Empty); | ||
} | ||
|
||
public override ActionResult Invoke() | ||
{ | ||
if (MediaSession != null) | ||
{ | ||
_ = MediaSession.TryTogglePlayPauseAsync(); | ||
} | ||
|
||
return ActionResult.KeepOpen(); | ||
} | ||
} |