Replace Mediator with Messenger

This commit is contained in:
Dan Clark
2024-11-16 13:52:03 +00:00
parent 469a52efaa
commit f16dbaf375
73 changed files with 615 additions and 1345 deletions
+8 -8
View File
@@ -1,13 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Mvvm.Messaging;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using Toolkit.Foundation;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
namespace Toolkit.Windows;
public class PointerMonitor(IPublisher publisher) :
public class PointerMonitor(IMessenger messenger) :
IPointerMonitor
{
private bool isDisposed;
@@ -99,16 +99,16 @@ public class PointerMonitor(IPublisher publisher) :
isPointerDrag = true;
}
publisher.Publish(new PointerDragEventArgs(location));
messenger.Send(new PointerDragEventArgs(location));
}
publisher.Publish(new PointerMovedEventArgs(location));
messenger.Send(new PointerMovedEventArgs(location));
}
private void SendPointerPressed(PointerLocation location, PointerButton button)
{
isPointerPressed = true;
publisher.Publish(new PointerPressedEventArgs(location, button));
messenger.Send(new PointerPressedEventArgs(location, button));
}
private void SendPointerReleased(PointerLocation location, PointerButton button)
@@ -118,11 +118,11 @@ public class PointerMonitor(IPublisher publisher) :
if (isPointerDrag)
{
isPointerDrag = false;
publisher.Publish(new PointerDragReleasedEventArgs(location, button));
messenger.Send(new PointerDragReleasedEventArgs(location, button));
}
isPointerPressed = false;
publisher.Publish(new PointerReleasedEventArgs(location, button));
messenger.Send(new PointerReleasedEventArgs(location, button));
}
}
+19 -25
View File
@@ -1,23 +1,25 @@
using Toolkit.Foundation;
using CommunityToolkit.Mvvm.Messaging;
using Toolkit.Foundation;
using Windows.Win32;
namespace Toolkit.Windows;
public class Taskbar(ISubscriber subscriber,
IPublisher publisher,
public class Taskbar(IMessenger messenger,
IDisposer disposer) :
ITaskbar,
INotificationHandler<WndProcEventArgs>,
INotificationHandler<PointerReleasedEventArgs>,
INotificationHandler<PointerMovedEventArgs>,
INotificationHandler<PointerDragEventArgs>
IRecipient<WndProcEventArgs>,
IRecipient<PointerReleasedEventArgs>,
IRecipient<PointerMovedEventArgs>,
IRecipient<PointerDragEventArgs>
{
private bool isDrag;
private bool isWithinBounds;
public void Dispose()
{
messenger.UnregisterAll(this);
disposer.Dispose(this);
GC.SuppressFinalize(this);
}
@@ -40,29 +42,25 @@ public class Taskbar(ISubscriber subscriber,
public IntPtr GetHandle() => WindowHelper.Find("Shell_TrayWnd");
public Task Handle(WndProcEventArgs args)
public void Receive(WndProcEventArgs args)
{
if (args.Message == PInvoke.WM_TASKBARCREATED ||
args.Message == (int)WndProcMessages.WM_SETTINGCHANGE &&
(int)args.WParam == PInvoke.SPI_SETWORKAREA)
{
publisher.Publish<TaskbarChangedEventArgs>();
messenger.Send<TaskbarChangedEventArgs>();
}
return Task.CompletedTask;
}
public Task Handle(PointerReleasedEventArgs args)
public void Receive(PointerReleasedEventArgs args)
{
if (isDrag)
{
isDrag = false;
}
return Task.CompletedTask;
}
public Task Handle(PointerMovedEventArgs args)
public void Receive(PointerMovedEventArgs args)
{
nint taskbarHandle = GetHandle();
if (WindowHelper.TryGetBounds(taskbarHandle, out Rect? rect))
@@ -71,11 +69,11 @@ public class Taskbar(ISubscriber subscriber,
{
if (isWithinBounds)
{
return Task.CompletedTask;
return;
}
isWithinBounds = true;
publisher.Publish<TaskbarEnteredEventArgs>();
messenger.Send<TaskbarEnteredEventArgs>();
}
else
{
@@ -83,21 +81,19 @@ public class Taskbar(ISubscriber subscriber,
isWithinBounds = false;
}
}
return Task.CompletedTask;
}
public Task Handle(PointerDragEventArgs args)
public void Receive(PointerDragEventArgs args)
{
if (isWithinBounds)
{
if (isDrag)
{
publisher.Publish<TaskbarDragOverEventArgs>();
messenger.Send<TaskbarDragOverEventArgs>();
}
else
{
publisher.Publish<TaskbarDragEnterEventArgs>();
messenger.Send<TaskbarDragEnterEventArgs>();
}
isDrag = true;
@@ -106,9 +102,7 @@ public class Taskbar(ISubscriber subscriber,
{
isDrag = false;
}
return Task.CompletedTask;
}
public void Initialize() => subscriber.Subscribe(this);
public void Initialize() => messenger.RegisterAll(this);
}
+19 -23
View File
@@ -1,31 +1,31 @@
using Toolkit.Foundation;
using CommunityToolkit.Mvvm.Messaging;
using Toolkit.Foundation;
namespace Toolkit.Windows;
public class TaskbarButton :
ITaskbarButton,
INotificationHandler<PointerReleasedEventArgs>,
INotificationHandler<PointerMovedEventArgs>,
INotificationHandler<PointerDragEventArgs>
IRecipient<PointerReleasedEventArgs>,
IRecipient<PointerMovedEventArgs>,
IRecipient<PointerDragEventArgs>
{
private readonly IPublisher publisher;
private readonly IMessenger messenger;
private readonly IDisposer disposer;
private bool isWithinBounds;
private bool isDrag;
public TaskbarButton(string name,
Rect rect,
IPublisher publisher,
ISubscriber subscriber,
IMessenger messenger,
IDisposer disposer)
{
this.publisher = publisher;
this.messenger = messenger;
this.disposer = disposer;
Name = name;
Rect = rect;
subscriber.Subscribe(this);
messenger.RegisterAll(this);
}
public Rect Rect { get; internal set; }
@@ -34,36 +34,36 @@ public class TaskbarButton :
public void Dispose()
{
messenger.UnregisterAll(this);
disposer.Dispose(this);
GC.SuppressFinalize(this);
}
public Task Handle(PointerReleasedEventArgs args)
public void Receive(PointerReleasedEventArgs args)
{
if (!isDrag && isWithinBounds)
{
publisher.Publish(new TaskbarButtonInvokedEventArgs(this));
messenger.Send(new TaskbarButtonInvokedEventArgs(this));
}
if (isDrag)
{
isDrag = false;
}
return Task.CompletedTask;
}
public Task Handle(PointerDragEventArgs args)
public void Receive(PointerDragEventArgs args)
{
if (isWithinBounds)
{
if (isDrag)
{
publisher.Publish(new TaskbarButtonDragOverEventArgs(this));
messenger.Send(new TaskbarButtonDragOverEventArgs(this));
}
else
{
publisher.Publish(new TaskbarButtonDragEnterEventArgs(this));
messenger.Send(new TaskbarButtonDragEnterEventArgs(this));
}
isDrag = true;
@@ -72,28 +72,24 @@ public class TaskbarButton :
{
isDrag = false;
}
return Task.CompletedTask;
}
public Task Handle(PointerMovedEventArgs args)
public void Receive(PointerMovedEventArgs args)
{
if (args.Location.IsWithinBounds(Rect))
{
if (isWithinBounds)
{
return Task.CompletedTask;
return;
}
isWithinBounds = true;
publisher.Publish(new TaskbarButtonEnteredEventArgs(this));
messenger.Send(new TaskbarButtonEnteredEventArgs(this));
}
else
{
isDrag = false;
isWithinBounds = false;
}
return Task.CompletedTask;
}
}
+7 -13
View File
@@ -1,4 +1,4 @@
using System.Diagnostics;
using CommunityToolkit.Mvvm.Messaging;
using Toolkit.Foundation;
using UIAutomationClient;
@@ -10,7 +10,7 @@ public class TaskbarButtonMonitor :
private readonly IDispatcherTimer dispatcherTimer;
private readonly IDispatcherTimerFactory dispatcherTimerFactory;
private readonly IDisposer disposer;
private readonly IPublisher publisher;
private readonly IMessenger messenger;
private readonly IServiceFactory serviceFactory;
private readonly Dictionary<string, TaskbarButton> taskbarButtons = [];
private readonly ITaskbarList taskbarList;
@@ -20,13 +20,13 @@ public class TaskbarButtonMonitor :
private IntPtr taskListHandle;
public TaskbarButtonMonitor(ITaskbarList taskbarList,
IPublisher publisher,
IMessenger messenger,
IDispatcherTimerFactory dispatcherTimerFactory,
IServiceFactory serviceFactory,
IDisposer disposer)
{
this.taskbarList = taskbarList;
this.publisher = publisher;
this.messenger = messenger;
this.dispatcherTimerFactory = dispatcherTimerFactory;
this.serviceFactory = serviceFactory;
this.disposer = disposer;
@@ -121,10 +121,8 @@ public class TaskbarButtonMonitor :
string key = buttonToRemove.Key;
TaskbarButton button = buttonToRemove.Value;
Debug.WriteLine($"{key} button removed");
taskbarButtons.Remove(key);
publisher.Publish(new TaskbarButtonRemovedEventArgs(button));
messenger.Send(new TaskbarButtonRemovedEventArgs(button));
button.Dispose();
}
@@ -141,17 +139,13 @@ public class TaskbarButtonMonitor :
if (taskbarButtons.TryGetValue(name, out TaskbarButton? taskbarButton))
{
Debug.WriteLine($"{name} button updated");
taskbarButtons[name].Rect = rect;
publisher.Publish(new TaskbarButtonUpdatedEventArgs(taskbarButtons[name]));
messenger.Send(new TaskbarButtonUpdatedEventArgs(taskbarButtons[name]));
}
else
{
Debug.WriteLine($"{name} button added");
taskbarButtons.Add(name, serviceFactory.Create<TaskbarButton>(name, rect));
publisher.Publish(new TaskbarButtonCreatedEventArgs(taskbarButtons[name]));
messenger.Send(new TaskbarButtonCreatedEventArgs(taskbarButtons[name]));
}
}
}
+4 -4
View File
@@ -1,4 +1,4 @@
using Toolkit.Foundation;
using CommunityToolkit.Mvvm.Messaging;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
@@ -6,16 +6,16 @@ using Windows.Win32.UI.WindowsAndMessaging;
namespace Toolkit.Windows;
public class WndProcMonitor(IPublisher publisher) :
public class WndProcMonitor(IMessenger messenger) :
IWndProcMonitor
{
private WNDPROC? handler;
private readonly IPublisher publisher = publisher;
public IntPtr Handle { get; private set; }
public void Dispose()
{
GC.SuppressFinalize(this);
PInvoke.DestroyWindow((HWND)Handle);
}
@@ -54,7 +54,7 @@ public class WndProcMonitor(IPublisher publisher) :
private LRESULT Wndproc(HWND param0, uint param1, WPARAM param2, LPARAM param3)
{
publisher.Publish(new WndProcEventArgs(param1, (uint)param2.Value, (uint)param3.Value));
messenger.Send(new WndProcEventArgs(param1, (uint)param2.Value, (uint)param3.Value));
return PInvoke.DefWindowProc(param0, param1, param2, param3);
}