From 0191080e14c4e72c76de691fd951d16a6835d862 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sat, 3 Feb 2024 21:47:32 +0000 Subject: [PATCH] More work on MC visuals --- .../IMediaButtonViewModel.cs | 3 +- .../MediaButton.cs | 2 +- .../MediaButtonDisabled.cs | 7 +++ .../MediaButtonEnabled.cs | 7 +++ .../MediaButtonPaused.cs | 7 +++ .../MediaButtonPlaying.cs | 7 +++ .../MediaButtonState.cs | 3 ++ .../MediaButtonStopped.cs | 7 +++ .../MediaButtonView.xaml | 27 ++++++++-- .../MediaButtonViewModel.cs | 12 ++--- .../MediaController.cs | 50 +++++++++++++++---- .../MediaControllerViewModel.cs | 2 +- .../MediaNextButton.cs | 3 +- .../MediaPlayPauseButton.cs | 3 +- .../MediaPreviousButton.cs | 4 +- 15 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 Hyperbar.Widget.MediaController.Windows/MediaButtonDisabled.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/MediaButtonEnabled.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/MediaButtonPaused.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/MediaButtonPlaying.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/MediaButtonState.cs create mode 100644 Hyperbar.Widget.MediaController.Windows/MediaButtonStopped.cs diff --git a/Hyperbar.Widget.MediaController.Windows/IMediaButtonViewModel.cs b/Hyperbar.Widget.MediaController.Windows/IMediaButtonViewModel.cs index 9080668..63dd7bf 100644 --- a/Hyperbar.Widget.MediaController.Windows/IMediaButtonViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/IMediaButtonViewModel.cs @@ -7,7 +7,8 @@ public interface IMediaButtonViewModel : { IRelayCommand? InvokeCommand { get; set; } - bool IsEnabled { get; set; } + string? Button { get; set; } string? State { get; set; } + } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButton.cs b/Hyperbar.Widget.MediaController.Windows/MediaButton.cs index fc507fa..e019cc6 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaButton.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaButton.cs @@ -1,3 +1,3 @@ namespace Hyperbar.Widget.MediaController.Windows; -public record MediaButton(bool IsEnabled); +public record MediaButton(MediaButtonState State); \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonDisabled.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonDisabled.cs new file mode 100644 index 0000000..b2a9523 --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonDisabled.cs @@ -0,0 +1,7 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record MediaButtonDisabled : + MediaButtonState +{ + public override string ToString() => "Disabled"; +} diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonEnabled.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonEnabled.cs new file mode 100644 index 0000000..2d4992e --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonEnabled.cs @@ -0,0 +1,7 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record MediaButtonEnabled : + MediaButtonState +{ + public override string ToString() => "Enabled"; +} \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonPaused.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonPaused.cs new file mode 100644 index 0000000..cbf8c00 --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonPaused.cs @@ -0,0 +1,7 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record MediaButtonPaused : + MediaButtonState +{ + public override string ToString() => "Paused"; +} diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonPlaying.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonPlaying.cs new file mode 100644 index 0000000..990c260 --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonPlaying.cs @@ -0,0 +1,7 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record MediaButtonPlaying : + MediaButtonState +{ + public override string ToString() => "Playing"; +} diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonState.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonState.cs new file mode 100644 index 0000000..5043cfe --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonState.cs @@ -0,0 +1,3 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record MediaButtonState; diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonStopped.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonStopped.cs new file mode 100644 index 0000000..d58b1d8 --- /dev/null +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonStopped.cs @@ -0,0 +1,7 @@ +namespace Hyperbar.Widget.MediaController.Windows; + +public record MediaButtonStopped : + MediaButtonState +{ + public override string ToString() => "Stopped"; +} diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml b/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml index affce04..8a88320 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonView.xaml @@ -23,7 +23,6 @@ Height="{StaticResource ButtonHeight}" Padding="{StaticResource ButtonPadding}" Command="{x:Bind ViewModel.InvokeCommand}" - Content="{x:Bind ViewModel.State}" FontFamily="{StaticResource SymbolThemeFontFamily}" FontSize="16" IsEnabled="False"> @@ -36,7 +35,7 @@ - + @@ -44,7 +43,7 @@ - + @@ -52,14 +51,32 @@ - + - + + + + + + + + + + + + + + + + + + + diff --git a/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs b/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs index f59c1ef..50508f3 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaButtonViewModel.cs @@ -9,24 +9,22 @@ public partial class MediaButtonViewModel(IServiceFactory serviceF ITemplateFactory templateFactory, IRelayCommand invokeCommand) : WidgetComponentViewModel(serviceFactory, mediator, disposer, templateFactory), - INotificationHandler>, + INotificationHandler>>, IMediaButtonViewModel - where TMediaButton : - MediaButton { [ObservableProperty] private IRelayCommand? invokeCommand = invokeCommand; [ObservableProperty] - private bool isEnabled; + private string? state; [ObservableProperty] - private string? state = $"{typeof(TMediaButton).Name}"; + private string? button = $"{typeof(TMediaButton).Name}"; - public Task Handle(Changed args, + public Task Handle(Changed> args, CancellationToken cancellationToken) { - IsEnabled = args.Value is not null && args.Value.IsEnabled; + State = $"{args.Value?.State}"; return Task.CompletedTask; } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaController.cs b/Hyperbar.Widget.MediaController.Windows/MediaController.cs index 4aad3b4..99b4b15 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaController.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaController.cs @@ -1,23 +1,24 @@ -using System.Collections.Concurrent; -using System.Diagnostics; -using Windows.Media.Control; +using Windows.Media.Control; using Windows.Storage.Streams; namespace Hyperbar.Widget.MediaController.Windows; public class MediaController : INotificationHandler>, + INotificationHandler>, INotificationHandler>, INotificationHandler>, INotificationHandler>, + INotificationHandler>, INotificationHandler>, IDisposable { - private readonly AsyncLock asyncLock = new(); private readonly IDisposer disposer; private readonly IMediator mediator; private readonly GlobalSystemMediaTransportControlsSession session; + private bool isNextEnabled; + private bool isPreviousEnabled; private GlobalSystemMediaTransportControlsSessionPlaybackStatus playbackStatus; public MediaController(IMediator mediator, @@ -63,6 +64,28 @@ public class MediaController : public async Task Handle(Request args, CancellationToken cancellationToken) => await UpdateMediaStateAsync(); + public async Task Handle(Request args, + CancellationToken cancellationToken) + { + GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo = + session.GetPlaybackInfo(); + + if (playbackInfo.PlaybackStatus is GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing) + { + await session.TryPauseAsync(); + } + + if (playbackInfo.PlaybackStatus is GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused) + { + await session.TryPlayAsync(); + } + + await UpdateMediaStateAsync(); + } + + public async Task Handle(Request args, + CancellationToken cancellationToken) => await UpdateMediaStateAsync(); + private async void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender, MediaPropertiesChangedEventArgs args) { @@ -103,9 +126,6 @@ public class MediaController : } } - private bool isPreviousEnabled; - private bool isNextEnabled; - private async Task UpdateMediaStateAsync() { try @@ -113,11 +133,18 @@ public class MediaController : GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo = session.GetPlaybackInfo(); + await mediator.PublishAsync(new Changed>(new + MediaButton(playbackInfo.PlaybackStatus is + GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing ? + new MediaButtonPlaying() : + new MediaButtonPaused()))); + bool isPreviousEnabled = playbackInfo.Controls.IsPreviousEnabled; if (this.isPreviousEnabled != isPreviousEnabled) { - await mediator.PublishAsync(new Changed(new - MediaPreviousButton(isPreviousEnabled))); + await mediator.PublishAsync(new Changed>(new + MediaButton(isPreviousEnabled ? new MediaButtonEnabled() : + new MediaButtonDisabled()))); this.isPreviousEnabled = isPreviousEnabled; } @@ -125,8 +152,9 @@ public class MediaController : bool isNextEnabled = playbackInfo.Controls.IsNextEnabled; if (this.isNextEnabled != isNextEnabled) { - await mediator.PublishAsync(new Changed(new - MediaNextButton(isNextEnabled))); + await mediator.PublishAsync(new Changed>(new + MediaButton(isNextEnabled ? new MediaButtonEnabled() : + new MediaButtonDisabled()))); this.isNextEnabled = isNextEnabled; } diff --git a/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs b/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs index bf9188c..203c06f 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaControllerViewModel.cs @@ -22,7 +22,7 @@ public class MediaControllerViewModel : await mediator.PublishAsync>())); Add>(new RelayCommand(async () => - await mediator.PublishAsync>())); + await mediator.PublishAsync>())); Add>(new RelayCommand(async () => await mediator.PublishAsync>())); diff --git a/Hyperbar.Widget.MediaController.Windows/MediaNextButton.cs b/Hyperbar.Widget.MediaController.Windows/MediaNextButton.cs index c512deb..cb8da9b 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaNextButton.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaNextButton.cs @@ -1,4 +1,3 @@ namespace Hyperbar.Widget.MediaController.Windows; -public record MediaNextButton(bool IsEnabled) : - MediaButton(IsEnabled); \ No newline at end of file +public record MediaNextButton; \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/MediaPlayPauseButton.cs b/Hyperbar.Widget.MediaController.Windows/MediaPlayPauseButton.cs index 3002308..8d5f1c3 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaPlayPauseButton.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaPlayPauseButton.cs @@ -1,4 +1,3 @@ namespace Hyperbar.Widget.MediaController.Windows; -public record MediaPlayPauseButton(bool IsEnabled) : - MediaButton(IsEnabled); \ No newline at end of file +public record MediaPlayPauseButton; \ No newline at end of file diff --git a/Hyperbar.Widget.MediaController.Windows/MediaPreviousButton.cs b/Hyperbar.Widget.MediaController.Windows/MediaPreviousButton.cs index 71547db..892c385 100644 --- a/Hyperbar.Widget.MediaController.Windows/MediaPreviousButton.cs +++ b/Hyperbar.Widget.MediaController.Windows/MediaPreviousButton.cs @@ -1,5 +1,3 @@ namespace Hyperbar.Widget.MediaController.Windows; -public record MediaPreviousButton(bool IsEnabled) : - MediaButton(IsEnabled); - +public record MediaPreviousButton; \ No newline at end of file