A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Updated clipboard on MacOS
Etienne Baudoux0cd04569616f
diff --git a/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs b/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.csindex f1e26883c..0914f9422 100644--- a/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs+++ b/src/app/dev/platforms/desktop/DevToys.MacOS/Core/Clipboard.cs@@ -1,4 +1,7 @@-using Microsoft.Extensions.Logging;+using System.Collections.Specialized;+using Foundation;+using Microsoft.Extensions.Logging;+using UIKit;using Uno.Extensions;namespace DevToys.MacOS.Core;@@ -18,6 +21,21 @@ internal sealed partial class Clipboard : Api.IClipboard{try{+ if (UIPasteboard.General.HasUrls && UIPasteboard.General.Urls is not null)+ {+ var files = new List<FileInfo>();+ foreach (NSUrl filePath in UIPasteboard.General.Urls)+ {+ if (filePath.AbsoluteString is not null && filePath.Path is not null)+ {+ if (filePath.AbsoluteString.StartsWith("file:///"))+ {+ files.Add(new FileInfo(filePath.Path));+ }+ }+ }+ return files.ToArray();+ }if (Microsoft.Maui.ApplicationModel.DataTransfer.Clipboard.Default.HasText){return await Microsoft.Maui.ApplicationModel.DataTransfer.Clipboard.Default.GetTextAsync();@@ -34,16 +52,47 @@ internal sealed partial class Clipboard : Api.IClipboardreturn null;}- public Task<string?> GetClipboardTextAsync()+ public async Task<string?> GetClipboardTextAsync(){- // TODO- throw new NotImplementedException();+ try+ {+ if (Microsoft.Maui.ApplicationModel.DataTransfer.Clipboard.Default.HasText)+ {+ return await Microsoft.Maui.ApplicationModel.DataTransfer.Clipboard.Default.GetTextAsync();+ }+ }+ catch (Exception ex)+ {+ LogGetClipboardFailed(ex);+ }+ return null;}public Task<FileInfo[]?> GetClipboardFilesAsync(){- // TODO- throw new NotImplementedException();+ try+ {+ if (UIPasteboard.General.HasUrls && UIPasteboard.General.Urls is not null)+ {+ var files = new List<FileInfo>();+ foreach (NSUrl filePath in UIPasteboard.General.Urls)+ {+ if (filePath.AbsoluteString is not null && filePath.Path is not null)+ {+ if (filePath.AbsoluteString.StartsWith("file:///"))+ {+ files.Add(new FileInfo(filePath.Path));+ }+ }+ }+ return Task.FromResult<FileInfo[]?>(files.ToArray());+ }+ }+ catch (Exception ex)+ {+ LogGetClipboardFailed(ex);+ }+ return Task.FromResult<FileInfo[]?>(null);}public Task<string?> GetClipboardBitmapAsync()@@ -60,14 +109,41 @@ internal sealed partial class Clipboard : Api.IClipboardpublic Task SetClipboardFilesAsync(FileInfo[]? data){- // TODO- throw new NotImplementedException();+ try+ {+ NSUrl[]? fileList = null;+ if (data is not null)+ {+ fileList = new NSUrl[data.Length];+ if (data is not null)+ {+ for (int i = 0; i < data.Length; i++)+ {+ fileList[i] = new NSUrl("file://" + data[i].FullName);+ }+ }+ }++ UIPasteboard.General.Urls = fileList;+ }+ catch (Exception ex)+ {+ LogSetClipboardTextFailed(ex);+ }++ return Task.CompletedTask;}- public Task SetClipboardTextAsync(string? data)+ public async Task SetClipboardTextAsync(string? data){- // TODO- throw new NotImplementedException();+ try+ {+ await Microsoft.Maui.ApplicationModel.DataTransfer.Clipboard.Default.SetTextAsync(data);+ }+ catch (Exception ex)+ {+ LogSetClipboardTextFailed(ex);+ }}[LoggerMessage(0, LogLevel.Warning, "Failed to retrieve the clipboard data.")]diff --git a/src/app/dev/platforms/desktop/DevToys.MacOS/Platforms/MacCatalyst/AppDelegate.cs b/src/app/dev/platforms/desktop/DevToys.MacOS/Platforms/MacCatalyst/AppDelegate.csindex 0ca6f7e92..7c3d32588 100644--- a/src/app/dev/platforms/desktop/DevToys.MacOS/Platforms/MacCatalyst/AppDelegate.cs+++ b/src/app/dev/platforms/desktop/DevToys.MacOS/Platforms/MacCatalyst/AppDelegate.cs@@ -31,6 +31,7 @@ public class AppDelegate : MauiUIApplicationDelegate// TODO: Add custom "About" that navigates to Settings page.// TODO: Add a Settings menu that navigates to Settings page.+ // TODO: Add a Edit menu that works well (default one doesn't do much?).// TODO: Add a custom "Help" that offers to open feedback}}diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs b/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.csindex 1ee2db754..7cabdd353 100644--- a/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs+++ b/src/app/dev/platforms/desktop/DevToys.Windows/Core/Clipboard.cs@@ -26,11 +26,7 @@ internal sealed partial class Clipboard : Api.IClipboard{try{- if (System.Windows.Clipboard.ContainsText())- {- return System.Windows.Clipboard.GetText();- }- else if (System.Windows.Clipboard.ContainsFileDropList())+ if (System.Windows.Clipboard.ContainsFileDropList()){var files = new List<FileInfo>();foreach (string? filePath in System.Windows.Clipboard.GetFileDropList())@@ -42,6 +38,10 @@ internal sealed partial class Clipboard : Api.IClipboard}return files.ToArray();}+ else if (System.Windows.Clipboard.ContainsText())+ {+ return System.Windows.Clipboard.GetText();+ }else if (System.Windows.Clipboard.ContainsImage()){// TODO: convert to a cross-platform compatible format?