Skip to content

Commit

Permalink
修复:windows端开启混合模式服务器后无法同步其他客户端的剪贴板(#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Oct 26, 2024
1 parent 73a0f34 commit 2c197e3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/SyncClipboard.Core/Clipboard/Profile/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ public async Task SetLocalClipboard(bool notify, CancellationToken ctk, bool mut

try
{
await ClipboardSetter.SetLocalClipboard(MetaInfomation, ctk);
var dispather = AppCore.Current.Services.GetService<IThreadDispatcher>();
if (dispather is null)
{
await ClipboardSetter.SetLocalClipboard(MetaInfomation, ctk);
}
else
{
await dispather.RunOnMainThreadAsync(() => ClipboardSetter.SetLocalClipboard(MetaInfomation, ctk));
}

if (notify && EnableNotify)
{
Logger.Write("System notification has sent.");
Expand Down
7 changes: 7 additions & 0 deletions src/SyncClipboard.Core/Interfaces/IThreadDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SyncClipboard.Core.Interfaces;

public interface IThreadDispatcher
{
Task<T> RunOnMainThreadAsync<T>(Func<Task<T>> func);
Task RunOnMainThreadAsync(Func<Task> func);
}
3 changes: 3 additions & 0 deletions src/SyncClipboard.WinUI3/AppServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using SyncClipboard.WinUI3.Views;
using SyncClipboard.WinUI3.Win32;
using SyncClipboard.Core.Utilities.Fake;
using SyncClipboard.WinUI3.Utilities;

namespace SyncClipboard.WinUI3;

Expand Down Expand Up @@ -37,6 +38,8 @@ public static ServiceCollection ConfigureServices()
services.AddSingleton((sp) => FakeFactory.Create<INotification, NotificationManager, FakeNotification>("Windows Notification"));
services.AddSingleton<INativeHotkeyRegistry>(sp => new NativeHotkeyRegistry((MainWindow)sp.GetRequiredService<IMainWindow>()));

services.AddTransient<IThreadDispatcher>(sp => new ThreadDispatcher(((MainWindow)sp.GetRequiredService<IMainWindow>()).DispatcherQueue));

services.AddTransient<IClipboardSetter<TextProfile>, TextClipboardSetter>();
services.AddTransient<IClipboardSetter<FileProfile>, FileClipboardSetter>();
services.AddTransient<IClipboardSetter<ImageProfile>, ImageClipboardSetter>();
Expand Down
13 changes: 10 additions & 3 deletions src/SyncClipboard.WinUI3/ClipboardWinUI/ClipboardFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ImageMagick;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using SyncClipboard.Core.Clipboard;
using SyncClipboard.Core.Interfaces;
using SyncClipboard.Core.Models;
Expand All @@ -25,6 +24,8 @@ internal partial class ClipboardFactory : ClipboardFactoryBase
protected override IServiceProvider ServiceProvider { get; set; }
protected override IWebDav WebDav { get; set; }

private readonly IThreadDispatcher _dispatcher;

private const string LOG_TAG = nameof(ClipboardFactory);

private delegate Task FormatHandler(DataPackageView ClipboardData, ClipboardMetaInfomation meta, CancellationToken ctk);
Expand Down Expand Up @@ -127,9 +128,15 @@ public ClipboardFactory(IServiceProvider serviceProvider)
ServiceProvider = serviceProvider;
Logger = ServiceProvider.GetRequiredService<ILogger>();
WebDav = ServiceProvider.GetRequiredService<IWebDav>();
_dispatcher = ServiceProvider.GetRequiredService<IThreadDispatcher>();
}

public override Task<ClipboardMetaInfomation> GetMetaInfomation(CancellationToken ctk)
{
return _dispatcher.RunOnMainThreadAsync(() => GetMetaInfomationCurrentThread(ctk));
}

public override async Task<ClipboardMetaInfomation> GetMetaInfomation(CancellationToken ctk)
private async Task<ClipboardMetaInfomation> GetMetaInfomationCurrentThread(CancellationToken ctk)
{
ClipboardMetaInfomation meta = new();
DataPackageView ClipboardData = Clipboard.GetContent();
Expand Down
26 changes: 26 additions & 0 deletions src/SyncClipboard.WinUI3/Utilities/ThreadDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CommunityToolkit.WinUI;
using Microsoft.UI.Dispatching;
using SyncClipboard.Core.Interfaces;
using System;
using System.Threading.Tasks;

namespace SyncClipboard.WinUI3.Utilities;

internal class ThreadDispatcher : IThreadDispatcher
{
private readonly DispatcherQueue _dispatcherQueue;
public ThreadDispatcher(DispatcherQueue dispatcherQueue)
{
_dispatcherQueue = dispatcherQueue;
}

public Task<T> RunOnMainThreadAsync<T>(Func<Task<T>> func)
{
return _dispatcherQueue.EnqueueAsync(func);
}

public Task RunOnMainThreadAsync(Func<Task> func)
{
return _dispatcherQueue.EnqueueAsync(func);
}
}

0 comments on commit 2c197e3

Please sign in to comment.