bunch ov fixes

This commit is contained in:
TheXamlGuy
2024-01-28 14:57:56 +00:00
parent 9f6cc35bc1
commit 6d40220412
28 changed files with 238 additions and 249 deletions
@@ -1,3 +1,3 @@
namespace Hyperbar.Widget.MediaController.Windows;
public record Foward : INotification;
public record Forward : INotification;
@@ -1,9 +1,11 @@
using Microsoft.UI.Xaml.Controls;
using Hyperbar.UI.Windows;
namespace Hyperbar.Widget.MediaController.Windows;
public sealed partial class MediaButtonView :
UserControl
{
public MediaButtonView() => InitializeComponent();
public MediaButtonView() =>
this.InitializeComponent(ref _contentLoaded);
}
@@ -1,21 +1,47 @@
using CommunityToolkit.Mvvm.Input;
using Hyperbar.Widget;
using System.Windows.Input;
namespace Hyperbar.Widget.MediaController.Windows;
public class MediaButtonViewModel(IServiceFactory serviceFactory,
IMediator mediator,
IDisposer disposer,
ITemplateFactory templateFactory,
Guid guid = default,
string? text = null,
string? icon = null,
RelayCommand? command = null) :
WidgetButtonViewModel(serviceFactory, mediator, disposer, templateFactory, guid, text, icon, command),
IInitialization
public class MediaButtonViewModel :
WidgetButtonViewModel,
IInitialization,
INotificationHandler<Changed<PlaybackInformation>>
{
public ICommand Initialize => new AsyncRelayCommand(InitializeAsync);
private readonly PlaybackButtonType buttonType;
public async Task InitializeAsync() => await Mediator.PublishAsync<Request<Playback>>();
public MediaButtonViewModel(IServiceFactory serviceFactory,
IMediator mediator,
IDisposer disposer,
ITemplateFactory templateFactory,
PlaybackButtonType buttonType,
Guid guid = default,
string? text = null,
string? icon = null,
RelayCommand? command = null) : base (serviceFactory, mediator, disposer, templateFactory, guid, text, icon, command)
{
this.buttonType = buttonType;
mediator.Subscribe(this);
}
public Task Handle(Changed<PlaybackInformation> notification,
CancellationToken cancellationToken)
{
if (notification.Value is PlaybackInformation information)
{
switch (buttonType)
{
case PlaybackButtonType.Play:
Visible = information.Status is PlaybackStatus.Playing;
break;
case PlaybackButtonType.Pause:
Visible = information.Status is PlaybackStatus.Paused;
break;
}
}
return Task.CompletedTask;
}
public override async Task InitializeAsync() =>
await Mediator.PublishAsync<Request<PlaybackInformation>>();
}
@@ -2,17 +2,17 @@
namespace Hyperbar.Widget.MediaController.Windows;
public class MediaController :
public class MediaController :
INotificationHandler<Play>,
INotificationHandler<Pause>,
INotificationHandler<Request<Playback>>,
INotificationHandler<Request<PlaybackInformation>>,
INotificationHandler<Request<MediaInformation>>,
IDisposable
{
private readonly IMediator mediator;
private readonly IDisposer disposer;
private readonly GlobalSystemMediaTransportControlsSession session;
private readonly AsyncLock asyncLock = new();
private readonly IDisposer disposer;
private readonly IMediator mediator;
private readonly GlobalSystemMediaTransportControlsSession session;
public MediaController(IMediator mediator,
IDisposer disposer,
@@ -31,51 +31,46 @@ public class MediaController :
public void Dispose()
{
GC.SuppressFinalize(this);
disposer.Dispose(this);
}
public async Task Handle(Play notification,
CancellationToken cancellationToken) =>
public async Task Handle(Play notification,
CancellationToken cancellationToken)
{
await session.TryPlayAsync();
await UpdateMediaPlaybackPropertiesAsync();
}
public async Task Handle(Pause notification,
CancellationToken cancellationToken) =>
public async Task Handle(Pause notification,
CancellationToken cancellationToken)
{
await session.TryPauseAsync();
public async Task Handle(Request<Playback> notification,
CancellationToken cancellationToken)
{
await mediator.PublishAsync(new Changed<Playback>(), cancellationToken);
await UpdateMediaPlaybackPropertiesAsync();
}
public async Task Handle(Request<MediaInformation> _,
CancellationToken cancellationToken)
{
using (await asyncLock)
{
try
{
GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await session.TryGetMediaPropertiesAsync();
await mediator.PublishAsync(new Changed<MediaInformation>(new MediaInformation(mediaProperties.Title,
mediaProperties.Subtitle)), cancellationToken);
}
catch
{
public async Task Handle(Request<PlaybackInformation> args,
CancellationToken cancellationToken) => await UpdateMediaPlaybackPropertiesAsync();
}
}
}
public async Task Handle(Request<MediaInformation> args,
CancellationToken cancellationToken) => await UpdateMediaPropertiesAsync();
private async void OnMediaPropertiesChanged(GlobalSystemMediaTransportControlsSession sender,
MediaPropertiesChangedEventArgs args)
MediaPropertiesChangedEventArgs args) => await UpdateMediaPropertiesAsync();
private async void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender,
PlaybackInfoChangedEventArgs args) => await UpdateMediaPlaybackPropertiesAsync();
private async Task UpdateMediaPlaybackPropertiesAsync()
{
using (await asyncLock)
{
try
{
GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await session.TryGetMediaPropertiesAsync();
await mediator.PublishAsync(new Changed<MediaInformation>(new MediaInformation(mediaProperties.Title,
mediaProperties.Artist)));
GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo = session.GetPlaybackInfo();
await mediator.PublishAsync(new Changed<PlaybackInformation>(
new PlaybackInformation((PlaybackStatus)playbackInfo.PlaybackStatus)));
}
catch
{
@@ -84,9 +79,23 @@ public class MediaController :
}
}
private async void OnPlaybackInfoChanged(GlobalSystemMediaTransportControlsSession sender,
PlaybackInfoChangedEventArgs args)
private async Task UpdateMediaPropertiesAsync()
{
await mediator.PublishAsync(new Changed<Playback>());
using (await asyncLock)
{
try
{
GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties =
await session.TryGetMediaPropertiesAsync();
await mediator.PublishAsync(new Changed<MediaInformation>(new MediaInformation(mediaProperties.Title,
mediaProperties.Artist)));
}
catch
{
}
}
}
}
@@ -22,8 +22,8 @@ public class MediaControllerHandler(IMediator mediator,
if (factory.Create(mediaController) is MediaControllerViewModel mediaControllerViewModel)
{
cache.Add(mediaController, mediaControllerViewModel);
//await mediator.PublishAsync(new Created<MediaControllerViewModel>(mediaControllerViewModel),
// cancellationToken);
await mediator.PublishAsync(new Created<MediaControllerViewModel>(mediaControllerViewModel),
cancellationToken);
}
}
}
@@ -1,9 +1,11 @@
using Microsoft.UI.Xaml.Controls;
using Hyperbar.UI.Windows;
namespace Hyperbar.Widget.MediaController.Windows;
public sealed partial class MediaControllerView :
UserControl
{
public MediaControllerView() => InitializeComponent();
public MediaControllerView() =>
this.InitializeComponent(ref _contentLoaded);
}
@@ -1,5 +1,4 @@
using CommunityToolkit.Mvvm.Input;
using Hyperbar.Widget;
namespace Hyperbar.Widget.MediaController.Windows;
@@ -15,10 +14,24 @@ public class MediaControllerViewModel :
TemplateFactory = templateFactory;
Add<MediaInformationViewModel>();
Add<MediaButtonViewModel>("Backward", "\uEB9E");
Add<MediaButtonViewModel>("Play", "\uE768", new RelayCommand(async () => await mediator.PublishAsync<Play>()));
Add<MediaButtonViewModel>("Pause", "\uE769", new RelayCommand(async () => await mediator.PublishAsync<Pause>()));
Add<MediaButtonViewModel>("Forward", "\uEB9D");
Add<MediaButtonViewModel>(PlaybackButtonType.Previous,
"Previous", "\uEB9E",
new RelayCommand(async () => await mediator.PublishAsync<Previous>()));
Add<MediaButtonViewModel>(PlaybackButtonType.Play,
"Play", "\uE768",
new RelayCommand(async () => await mediator.PublishAsync<Play>()));
Add<MediaButtonViewModel>(PlaybackButtonType.Pause,
"Pause", "\uE769",
new RelayCommand(async () => await mediator.PublishAsync<Pause>()));
Add<MediaButtonViewModel>(PlaybackButtonType.Forward,
"Forward", "\uEB9D",
new RelayCommand(async () => await mediator.PublishAsync<Forward>()));
mediator.Subscribe(this);
}
public ITemplateFactory TemplateFactory { get; set; }
@@ -2,6 +2,12 @@
<UserControl
x:Class="Hyperbar.Widget.MediaController.Windows.MediaControllerWidgetView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button>sdfsdfsdg</Button>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="using:Hyperbar.UI.Windows">
<FlipView
x:Name="FlipView"
Width="400"
Background="Transparent"
ItemTemplateSelector="{Binding Converter={ui:DataTemplateConverter}}"
ItemsSource="{Binding}" />
</UserControl>
@@ -1,74 +1,11 @@
using CustomExtensions.WinUI;
using Microsoft.UI.Xaml.Controls;
using System.Reflection;
using System.Runtime.CompilerServices;
using Hyperbar.UI.Windows;
namespace Hyperbar.Widget.MediaController.Windows;
public sealed partial class MediaControllerWidgetView :
UserControl
{
public MediaControllerWidgetView()
{
Foo(@"C:\Users\dan_c\AppData\Local\Packages\24ccddba-447f-4d37-891d-523e8d820f45_rmhrgjnfy8he0\LocalCache\Local\Hyperbar.Windows\Extensions\Hyperbar.Windows.MediaController\Hyperbar.Windows.MediaController.dll");
LocateResourcePath(this);
}
public Assembly ForeignAssembly { get; set; }
private string ForeignAssemblyDir;
private string ForeignAssemblyName;
private bool? IsHotReloadAvailable;
private DisposableCollection Disposables = new();
private bool IsDisposed;
internal static readonly Assembly? EntryAssembly;
internal static readonly string HostingProcessDir;
static MediaControllerWidgetView()
{
EntryAssembly = Assembly.GetEntryAssembly();
HostingProcessDir = Path.GetDirectoryName(EntryAssembly.Location);
}
public void Foo(string assemblyPath)
{
// TODO: For some reason WinUI gets very angry when loading via AssemblyLoadContext,
// even if using AssemblyLoadContext.Default which *should* have no difference than
// Assembly.LoadFrom(), but it does.
//
// ExtensionContext = new(assemblyPath);
// ForeignAssembly = ExtensionContext.LoadFromAssemblyPath(assemblyPath);
ForeignAssembly = Assembly.LoadFrom(assemblyPath);
ForeignAssemblyDir = Path.GetDirectoryName(ForeignAssembly.Location);
ForeignAssemblyName = ForeignAssembly.GetName().Name;
}
private string LocateResourcePath(object component, [CallerFilePath] string callerFilePath = "")
{
if (component.GetType().Assembly != ForeignAssembly)
{
throw new InvalidProgramException();
}
string resourceName = Path.GetFileName(callerFilePath)[..^3];
string[] pathParts = callerFilePath.Split('\\')[..^1];
for (int i = pathParts.Length - 1; i > 1; i++)
{
string pathCandidate = Path.Join(pathParts[i..pathParts.Length].Append(resourceName).Prepend(ForeignAssemblyName).ToArray());
FileInfo sourceResource = new(Path.Combine(ForeignAssemblyDir, pathCandidate));
FileInfo colocatedResource = new(Path.Combine(HostingProcessDir, pathCandidate));
if (colocatedResource.Exists)
{
return pathCandidate;
}
if (sourceResource.Exists)
{
return sourceResource.FullName;
}
throw new FileNotFoundException("Could not find resource", resourceName);
}
throw new FileNotFoundException("Could not find resource", resourceName);
}
public MediaControllerWidgetView() =>
this.InitializeComponent(ref _contentLoaded);
}
@@ -9,7 +9,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="8" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<Border
Grid.Column="0"
@@ -6,5 +6,6 @@ namespace Hyperbar.Widget.MediaController.Windows;
public sealed partial class MediaInformationView :
UserControl
{
public MediaInformationView() => this.InitializeComponent(ref _contentLoaded);
public MediaInformationView() =>
this.InitializeComponent(ref _contentLoaded);
}
@@ -1,3 +0,0 @@
namespace Hyperbar.Widget.MediaController.Windows;
public record Playback : INotification;
@@ -0,0 +1,9 @@
namespace Hyperbar.Widget.MediaController.Windows;
public enum PlaybackButtonType
{
Previous,
Play,
Pause,
Forward
}
@@ -0,0 +1,4 @@
namespace Hyperbar.Widget.MediaController.Windows;
public record PlaybackInformation(PlaybackStatus Status) :
INotification;
@@ -0,0 +1,11 @@
namespace Hyperbar.Widget.MediaController.Windows;
public enum PlaybackStatus
{
Closed,
Opened,
Changing,
Stopped,
Playing,
Paused
}
@@ -1,3 +1,3 @@
namespace Hyperbar.Widget.MediaController.Windows;
public record Backward : INotification;
public record Previous : INotification;