2025-11-15 14:52:01 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CommunityToolkit.WinUI;
|
|
|
|
|
using Microsoft.UI.Dispatching;
|
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace Wino.Mail.WinUI;
|
|
|
|
|
|
|
|
|
|
public class WinUIDispatcher : IDispatcher
|
|
|
|
|
{
|
2026-03-01 09:47:05 +01:00
|
|
|
private DispatcherQueue? _coreDispatcher;
|
|
|
|
|
|
|
|
|
|
public WinUIDispatcher()
|
|
|
|
|
{
|
|
|
|
|
}
|
2025-11-15 14:52:01 +01:00
|
|
|
|
|
|
|
|
public WinUIDispatcher(DispatcherQueue coreDispatcher)
|
|
|
|
|
{
|
|
|
|
|
_coreDispatcher = coreDispatcher;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 09:47:05 +01:00
|
|
|
public bool HasThreadAccess => _coreDispatcher?.HasThreadAccess == true;
|
|
|
|
|
|
|
|
|
|
public void Initialize(DispatcherQueue coreDispatcher)
|
|
|
|
|
{
|
|
|
|
|
_coreDispatcher ??= coreDispatcher ?? throw new ArgumentNullException(nameof(coreDispatcher));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task ExecuteOnUIThread(Action action)
|
|
|
|
|
{
|
|
|
|
|
if (action == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(action));
|
|
|
|
|
|
|
|
|
|
if (_coreDispatcher == null)
|
|
|
|
|
throw new InvalidOperationException("UI dispatcher is not initialized.");
|
|
|
|
|
|
|
|
|
|
if (_coreDispatcher.HasThreadAccess)
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _coreDispatcher.EnqueueAsync(action, DispatcherQueuePriority.Normal);
|
|
|
|
|
}
|
2025-11-15 14:52:01 +01:00
|
|
|
}
|