From 66f4bb87576e95d25b51272a08f36e9a590e7214 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sat, 13 Jan 2024 13:47:26 +0000 Subject: [PATCH] UI threading work --- Hyperbar.Windows/App.xaml.cs | 5 ++++- Hyperbar.Windows/Lifecycles/AppConfiguration.cs | 8 ++++++++ Hyperbar.Windows/Lifecycles/AppInitializer.cs | 5 +++-- .../Lifecycles/ConfigurationChangedHandler.cs | 14 ++++++++++++++ Hyperbar/Mediators/Mediator.cs | 8 ++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 Hyperbar.Windows/Lifecycles/AppConfiguration.cs create mode 100644 Hyperbar.Windows/Lifecycles/ConfigurationChangedHandler.cs diff --git a/Hyperbar.Windows/App.xaml.cs b/Hyperbar.Windows/App.xaml.cs index f4b0f78..13cf47e 100644 --- a/Hyperbar.Windows/App.xaml.cs +++ b/Hyperbar.Windows/App.xaml.cs @@ -39,14 +39,17 @@ public partial class App : services.AddSingleton(); services.AddHostedService(); + services.AddConfiguration(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddSingleton(); services.AddContentTemplate(); + services.AddHandler(); + //services.AddWidgetProvider(); services.AddWidgetProvider(); diff --git a/Hyperbar.Windows/Lifecycles/AppConfiguration.cs b/Hyperbar.Windows/Lifecycles/AppConfiguration.cs new file mode 100644 index 0000000..2ea0375 --- /dev/null +++ b/Hyperbar.Windows/Lifecycles/AppConfiguration.cs @@ -0,0 +1,8 @@ +using Hyperbar.Windows.Controls; + +namespace Hyperbar.Windows; + +public class AppConfiguration +{ + public DesktopFlyoutPlacement Placement { get; set; } +} diff --git a/Hyperbar.Windows/Lifecycles/AppInitializer.cs b/Hyperbar.Windows/Lifecycles/AppInitializer.cs index 5236278..2958e48 100644 --- a/Hyperbar.Windows/Lifecycles/AppInitializer.cs +++ b/Hyperbar.Windows/Lifecycles/AppInitializer.cs @@ -5,14 +5,15 @@ namespace Hyperbar.Windows; public class AppInitializer([FromKeyedServices(nameof(WidgetBarViewModel))] WidgetBarView view, [FromKeyedServices(nameof(WidgetBarViewModel))] WidgetBarViewModel viewModel, - DesktopFlyout desktopFlyout) : + DesktopFlyout desktopFlyout, + AppConfiguration configuration) : IInitializer { public Task InitializeAsync() { view.DataContext = viewModel; - desktopFlyout.Placement = DesktopFlyoutPlacement.Top; + desktopFlyout.Placement = configuration.Placement; desktopFlyout.Content = view; return Task.CompletedTask; diff --git a/Hyperbar.Windows/Lifecycles/ConfigurationChangedHandler.cs b/Hyperbar.Windows/Lifecycles/ConfigurationChangedHandler.cs new file mode 100644 index 0000000..8537e05 --- /dev/null +++ b/Hyperbar.Windows/Lifecycles/ConfigurationChangedHandler.cs @@ -0,0 +1,14 @@ +using Hyperbar.Windows.Controls; + +namespace Hyperbar.Windows.Primary; + +public class AppConfigurationChangedHandler(DesktopFlyout desktopFlyout, + AppConfiguration configuration) : + INotificationHandler> +{ + public ValueTask Handle(ConfigurationChanged notification, CancellationToken cancellationToken) + { + desktopFlyout.Placement = configuration.Placement; + return ValueTask.CompletedTask; + } +} diff --git a/Hyperbar/Mediators/Mediator.cs b/Hyperbar/Mediators/Mediator.cs index cf1e045..96e5135 100644 --- a/Hyperbar/Mediators/Mediator.cs +++ b/Hyperbar/Mediators/Mediator.cs @@ -6,9 +6,11 @@ namespace Hyperbar; public class Mediator(IServiceProvider provider) : IMediator { + private readonly SynchronizationContext? context = SynchronizationContext.Current; + private readonly ConditionalWeakTable subjects = []; - public async ValueTask PublishAsync(TNotification notification, + public ValueTask PublishAsync(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification @@ -26,8 +28,10 @@ public class Mediator(IServiceProvider provider) : foreach (INotificationHandler handler in handlers) { - await handler.Handle(notification, cancellationToken); + context?.Post(async state => await handler.Handle(notification, cancellationToken), null); } + + return ValueTask.CompletedTask; } public ValueTask SendAsync(IRequest request,