More work on MC visuals
This commit is contained in:
@@ -7,7 +7,8 @@ public interface IMediaButtonViewModel :
|
|||||||
{
|
{
|
||||||
IRelayCommand? InvokeCommand { get; set; }
|
IRelayCommand? InvokeCommand { get; set; }
|
||||||
|
|
||||||
bool IsEnabled { get; set; }
|
string? Button { get; set; }
|
||||||
|
|
||||||
string? State { get; set; }
|
string? State { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
namespace Hyperbar.Widget.MediaController.Windows;
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
public record MediaButton(bool IsEnabled);
|
public record MediaButton<TMediaButton>(MediaButtonState State);
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
|
public record MediaButtonDisabled :
|
||||||
|
MediaButtonState
|
||||||
|
{
|
||||||
|
public override string ToString() => "Disabled";
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
|
public record MediaButtonEnabled :
|
||||||
|
MediaButtonState
|
||||||
|
{
|
||||||
|
public override string ToString() => "Enabled";
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
|
public record MediaButtonPaused :
|
||||||
|
MediaButtonState
|
||||||
|
{
|
||||||
|
public override string ToString() => "Paused";
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
|
public record MediaButtonPlaying :
|
||||||
|
MediaButtonState
|
||||||
|
{
|
||||||
|
public override string ToString() => "Playing";
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
|
public record MediaButtonState;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
|
public record MediaButtonStopped :
|
||||||
|
MediaButtonState
|
||||||
|
{
|
||||||
|
public override string ToString() => "Stopped";
|
||||||
|
}
|
||||||
@@ -23,7 +23,6 @@
|
|||||||
Height="{StaticResource ButtonHeight}"
|
Height="{StaticResource ButtonHeight}"
|
||||||
Padding="{StaticResource ButtonPadding}"
|
Padding="{StaticResource ButtonPadding}"
|
||||||
Command="{x:Bind ViewModel.InvokeCommand}"
|
Command="{x:Bind ViewModel.InvokeCommand}"
|
||||||
Content="{x:Bind ViewModel.State}"
|
|
||||||
FontFamily="{StaticResource SymbolThemeFontFamily}"
|
FontFamily="{StaticResource SymbolThemeFontFamily}"
|
||||||
FontSize="16"
|
FontSize="16"
|
||||||
IsEnabled="False">
|
IsEnabled="False">
|
||||||
@@ -36,7 +35,7 @@
|
|||||||
<VisualStateGroup>
|
<VisualStateGroup>
|
||||||
<VisualState x:Name="MediaPreviousButtonState">
|
<VisualState x:Name="MediaPreviousButtonState">
|
||||||
<VisualState.StateTriggers>
|
<VisualState.StateTriggers>
|
||||||
<triggers:IsEqualStateTrigger Value="{Binding State}" To="MediaPreviousButton" />
|
<triggers:IsEqualStateTrigger Value="{Binding Button}" To="MediaPreviousButton" />
|
||||||
</VisualState.StateTriggers>
|
</VisualState.StateTriggers>
|
||||||
<VisualState.Setters>
|
<VisualState.Setters>
|
||||||
<Setter Target="Button.Content" Value="" />
|
<Setter Target="Button.Content" Value="" />
|
||||||
@@ -44,7 +43,7 @@
|
|||||||
</VisualState>
|
</VisualState>
|
||||||
<VisualState x:Name="MediaNextButtonState">
|
<VisualState x:Name="MediaNextButtonState">
|
||||||
<VisualState.StateTriggers>
|
<VisualState.StateTriggers>
|
||||||
<triggers:IsEqualStateTrigger Value="{Binding State}" To="MediaNextButton" />
|
<triggers:IsEqualStateTrigger Value="{Binding Button}" To="MediaNextButton" />
|
||||||
</VisualState.StateTriggers>
|
</VisualState.StateTriggers>
|
||||||
<VisualState.Setters>
|
<VisualState.Setters>
|
||||||
<Setter Target="Button.Content" Value="" />
|
<Setter Target="Button.Content" Value="" />
|
||||||
@@ -52,14 +51,32 @@
|
|||||||
</VisualState>
|
</VisualState>
|
||||||
</VisualStateGroup>
|
</VisualStateGroup>
|
||||||
<VisualStateGroup>
|
<VisualStateGroup>
|
||||||
<VisualState x:Name="IsEnabledState">
|
<VisualState x:Name="EnabledState">
|
||||||
<VisualState.StateTriggers>
|
<VisualState.StateTriggers>
|
||||||
<triggers:IsEqualStateTrigger Value="{Binding IsEnabled}" To="True" />
|
<triggers:IsEqualStateTrigger Value="{Binding State}" To="Enabled" />
|
||||||
</VisualState.StateTriggers>
|
</VisualState.StateTriggers>
|
||||||
<VisualState.Setters>
|
<VisualState.Setters>
|
||||||
<Setter Target="Button.IsEnabled" Value="True" />
|
<Setter Target="Button.IsEnabled" Value="True" />
|
||||||
</VisualState.Setters>
|
</VisualState.Setters>
|
||||||
</VisualState>
|
</VisualState>
|
||||||
|
<VisualState x:Name="PlayingState">
|
||||||
|
<VisualState.StateTriggers>
|
||||||
|
<triggers:IsEqualStateTrigger Value="{Binding State}" To="Playing" />
|
||||||
|
</VisualState.StateTriggers>
|
||||||
|
<VisualState.Setters>
|
||||||
|
<Setter Target="Button.Content" Value="" />
|
||||||
|
<Setter Target="Button.IsEnabled" Value="True" />
|
||||||
|
</VisualState.Setters>
|
||||||
|
</VisualState>
|
||||||
|
<VisualState x:Name="PausedState">
|
||||||
|
<VisualState.StateTriggers>
|
||||||
|
<triggers:IsEqualStateTrigger Value="{Binding State}" To="Paused" />
|
||||||
|
</VisualState.StateTriggers>
|
||||||
|
<VisualState.Setters>
|
||||||
|
<Setter Target="Button.Content" Value="" />
|
||||||
|
<Setter Target="Button.IsEnabled" Value="True" />
|
||||||
|
</VisualState.Setters>
|
||||||
|
</VisualState>
|
||||||
</VisualStateGroup>
|
</VisualStateGroup>
|
||||||
</VisualStateManager.VisualStateGroups>
|
</VisualStateManager.VisualStateGroups>
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -9,24 +9,22 @@ public partial class MediaButtonViewModel<TMediaButton>(IServiceFactory serviceF
|
|||||||
ITemplateFactory templateFactory,
|
ITemplateFactory templateFactory,
|
||||||
IRelayCommand invokeCommand) :
|
IRelayCommand invokeCommand) :
|
||||||
WidgetComponentViewModel(serviceFactory, mediator, disposer, templateFactory),
|
WidgetComponentViewModel(serviceFactory, mediator, disposer, templateFactory),
|
||||||
INotificationHandler<Changed<TMediaButton>>,
|
INotificationHandler<Changed<MediaButton<TMediaButton>>>,
|
||||||
IMediaButtonViewModel
|
IMediaButtonViewModel
|
||||||
where TMediaButton :
|
|
||||||
MediaButton
|
|
||||||
{
|
{
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private IRelayCommand? invokeCommand = invokeCommand;
|
private IRelayCommand? invokeCommand = invokeCommand;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private bool isEnabled;
|
private string? state;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private string? state = $"{typeof(TMediaButton).Name}";
|
private string? button = $"{typeof(TMediaButton).Name}";
|
||||||
|
|
||||||
public Task Handle(Changed<TMediaButton> args,
|
public Task Handle(Changed<MediaButton<TMediaButton>> args,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
IsEnabled = args.Value is not null && args.Value.IsEnabled;
|
State = $"{args.Value?.State}";
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
using System.Collections.Concurrent;
|
using Windows.Media.Control;
|
||||||
using System.Diagnostics;
|
|
||||||
using Windows.Media.Control;
|
|
||||||
using Windows.Storage.Streams;
|
using Windows.Storage.Streams;
|
||||||
|
|
||||||
namespace Hyperbar.Widget.MediaController.Windows;
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
public class MediaController :
|
public class MediaController :
|
||||||
INotificationHandler<Request<MediaPrevious>>,
|
INotificationHandler<Request<MediaPrevious>>,
|
||||||
|
INotificationHandler<Request<MediaPlayPause>>,
|
||||||
INotificationHandler<Request<MediaNext>>,
|
INotificationHandler<Request<MediaNext>>,
|
||||||
INotificationHandler<Request<MediaInformation>>,
|
INotificationHandler<Request<MediaInformation>>,
|
||||||
INotificationHandler<Request<MediaPreviousButton>>,
|
INotificationHandler<Request<MediaPreviousButton>>,
|
||||||
|
INotificationHandler<Request<MediaPlayPauseButton>>,
|
||||||
INotificationHandler<Request<MediaNextButton>>,
|
INotificationHandler<Request<MediaNextButton>>,
|
||||||
IDisposable
|
IDisposable
|
||||||
{
|
{
|
||||||
private readonly AsyncLock asyncLock = new();
|
|
||||||
private readonly IDisposer disposer;
|
private readonly IDisposer disposer;
|
||||||
private readonly IMediator mediator;
|
private readonly IMediator mediator;
|
||||||
private readonly GlobalSystemMediaTransportControlsSession session;
|
private readonly GlobalSystemMediaTransportControlsSession session;
|
||||||
|
|
||||||
|
private bool isNextEnabled;
|
||||||
|
private bool isPreviousEnabled;
|
||||||
private GlobalSystemMediaTransportControlsSessionPlaybackStatus playbackStatus;
|
private GlobalSystemMediaTransportControlsSessionPlaybackStatus playbackStatus;
|
||||||
|
|
||||||
public MediaController(IMediator mediator,
|
public MediaController(IMediator mediator,
|
||||||
@@ -63,6 +64,28 @@ public class MediaController :
|
|||||||
public async Task Handle(Request<MediaNextButton> args,
|
public async Task Handle(Request<MediaNextButton> args,
|
||||||
CancellationToken cancellationToken) => await UpdateMediaStateAsync();
|
CancellationToken cancellationToken) => await UpdateMediaStateAsync();
|
||||||
|
|
||||||
|
public async Task Handle(Request<MediaPlayPause> 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<MediaPlayPauseButton> args,
|
||||||
|
CancellationToken cancellationToken) => await UpdateMediaStateAsync();
|
||||||
|
|
||||||
private async void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender,
|
private async void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender,
|
||||||
MediaPropertiesChangedEventArgs args)
|
MediaPropertiesChangedEventArgs args)
|
||||||
{
|
{
|
||||||
@@ -103,9 +126,6 @@ public class MediaController :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool isPreviousEnabled;
|
|
||||||
private bool isNextEnabled;
|
|
||||||
|
|
||||||
private async Task UpdateMediaStateAsync()
|
private async Task UpdateMediaStateAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -113,11 +133,18 @@ public class MediaController :
|
|||||||
GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo =
|
GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo =
|
||||||
session.GetPlaybackInfo();
|
session.GetPlaybackInfo();
|
||||||
|
|
||||||
|
await mediator.PublishAsync(new Changed<MediaButton<MediaPlayPauseButton>>(new
|
||||||
|
MediaButton<MediaPlayPauseButton>(playbackInfo.PlaybackStatus is
|
||||||
|
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing ?
|
||||||
|
new MediaButtonPlaying() :
|
||||||
|
new MediaButtonPaused())));
|
||||||
|
|
||||||
bool isPreviousEnabled = playbackInfo.Controls.IsPreviousEnabled;
|
bool isPreviousEnabled = playbackInfo.Controls.IsPreviousEnabled;
|
||||||
if (this.isPreviousEnabled != isPreviousEnabled)
|
if (this.isPreviousEnabled != isPreviousEnabled)
|
||||||
{
|
{
|
||||||
await mediator.PublishAsync(new Changed<MediaPreviousButton>(new
|
await mediator.PublishAsync(new Changed<MediaButton<MediaPreviousButton>>(new
|
||||||
MediaPreviousButton(isPreviousEnabled)));
|
MediaButton<MediaPreviousButton>(isPreviousEnabled ? new MediaButtonEnabled() :
|
||||||
|
new MediaButtonDisabled())));
|
||||||
|
|
||||||
this.isPreviousEnabled = isPreviousEnabled;
|
this.isPreviousEnabled = isPreviousEnabled;
|
||||||
}
|
}
|
||||||
@@ -125,8 +152,9 @@ public class MediaController :
|
|||||||
bool isNextEnabled = playbackInfo.Controls.IsNextEnabled;
|
bool isNextEnabled = playbackInfo.Controls.IsNextEnabled;
|
||||||
if (this.isNextEnabled != isNextEnabled)
|
if (this.isNextEnabled != isNextEnabled)
|
||||||
{
|
{
|
||||||
await mediator.PublishAsync(new Changed<MediaNextButton>(new
|
await mediator.PublishAsync(new Changed<MediaButton<MediaNextButton>>(new
|
||||||
MediaNextButton(isNextEnabled)));
|
MediaButton<MediaNextButton>(isNextEnabled ? new MediaButtonEnabled() :
|
||||||
|
new MediaButtonDisabled())));
|
||||||
|
|
||||||
this.isNextEnabled = isNextEnabled;
|
this.isNextEnabled = isNextEnabled;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class MediaControllerViewModel :
|
|||||||
await mediator.PublishAsync<Request<MediaPrevious>>()));
|
await mediator.PublishAsync<Request<MediaPrevious>>()));
|
||||||
|
|
||||||
Add<MediaButtonViewModel<MediaPlayPauseButton>>(new RelayCommand(async () =>
|
Add<MediaButtonViewModel<MediaPlayPauseButton>>(new RelayCommand(async () =>
|
||||||
await mediator.PublishAsync<Request<MediaPlayPauseButton>>()));
|
await mediator.PublishAsync<Request<MediaPlayPause>>()));
|
||||||
|
|
||||||
Add<MediaButtonViewModel<MediaNextButton>>(new RelayCommand(async () =>
|
Add<MediaButtonViewModel<MediaNextButton>>(new RelayCommand(async () =>
|
||||||
await mediator.PublishAsync<Request<MediaNext>>()));
|
await mediator.PublishAsync<Request<MediaNext>>()));
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
namespace Hyperbar.Widget.MediaController.Windows;
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
public record MediaNextButton(bool IsEnabled) :
|
public record MediaNextButton;
|
||||||
MediaButton(IsEnabled);
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
namespace Hyperbar.Widget.MediaController.Windows;
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
public record MediaPlayPauseButton(bool IsEnabled) :
|
public record MediaPlayPauseButton;
|
||||||
MediaButton(IsEnabled);
|
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
namespace Hyperbar.Widget.MediaController.Windows;
|
namespace Hyperbar.Widget.MediaController.Windows;
|
||||||
|
|
||||||
public record MediaPreviousButton(bool IsEnabled) :
|
public record MediaPreviousButton;
|
||||||
MediaButton(IsEnabled);
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user