|
7 | 7 | using Avalonia.Controls.ApplicationLifetimes; |
8 | 8 | using CommunityToolkit.Mvvm.ComponentModel; |
9 | 9 | using CommunityToolkit.Mvvm.Input; |
| 10 | +using FluentAvalonia.UI.Data; |
10 | 11 | using SharpFM.Core; |
11 | 12 |
|
12 | 13 | namespace SharpFM.App.ViewModels; |
@@ -47,6 +48,41 @@ private async Task PasteText(CancellationToken token) |
47 | 48 | ErrorMessages?.Add(e.Message); |
48 | 49 | } |
49 | 50 | } |
| 51 | + [RelayCommand] |
| 52 | + private async Task CopySelectedToClip(CancellationToken token) |
| 53 | + { |
| 54 | + ErrorMessages?.Clear(); |
| 55 | + try |
| 56 | + { |
| 57 | + // See DepInject project for a sample of how to accomplish this. |
| 58 | + if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop || |
| 59 | + desktop.MainWindow?.Clipboard is not { } provider) |
| 60 | + throw new NullReferenceException("Missing Clipboard instance."); |
| 61 | + |
| 62 | + |
| 63 | + var dp = new DataPackage(); |
| 64 | + |
| 65 | + if (!(SelectedClip is FileMakerClip data)) |
| 66 | + { |
| 67 | + return; // no data |
| 68 | + } |
| 69 | + |
| 70 | + // recalculate the length of the original text and make sure that is the first four bytes in the stream |
| 71 | + //var code = data.RawData;// XamlCodeRenderer.Text; |
| 72 | + //byte[] byteList = Encoding.UTF8.GetBytes(code); |
| 73 | + //int bl = byteList.Length; |
| 74 | + //byte[] intBytes = BitConverter.GetBytes(bl); |
| 75 | + |
| 76 | + //dp.SetData("Mac-XMSS", intBytes.Concat(byteList).ToArray().AsBuffer().AsStream().AsRandomAccessStream()); |
| 77 | + dp.SetData(data.ClipboardFormat, data.RawData); |
| 78 | + |
| 79 | + await provider.SetDataObjectAsync(dp); |
| 80 | + } |
| 81 | + catch (Exception e) |
| 82 | + { |
| 83 | + ErrorMessages?.Add(e.Message); |
| 84 | + } |
| 85 | + } |
50 | 86 | private async Task DoGetClipboardDataAsync() |
51 | 87 | { |
52 | 88 | // For learning purposes, we opted to directly get the reference |
|
0 commit comments