Skip to content

Commit c1f4c14

Browse files
committed
add copy command and button
1 parent 9269e93 commit c1f4c14

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

SharpFM.App/MainWindow.axaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<Button Command="{Binding PasteTextCommand}">
2626
Paste
2727
</Button>
28+
29+
<Button Command="{Binding CopySelectedToClipCommand}">
30+
Copy
31+
</Button>
2832
</StackPanel>
2933
<ListBox
3034
Grid.Column="1"

SharpFM.App/ViewModels/MainWindowViewModel.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Avalonia.Controls.ApplicationLifetimes;
88
using CommunityToolkit.Mvvm.ComponentModel;
99
using CommunityToolkit.Mvvm.Input;
10+
using FluentAvalonia.UI.Data;
1011
using SharpFM.Core;
1112

1213
namespace SharpFM.App.ViewModels;
@@ -47,6 +48,41 @@ private async Task PasteText(CancellationToken token)
4748
ErrorMessages?.Add(e.Message);
4849
}
4950
}
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+
}
5086
private async Task DoGetClipboardDataAsync()
5187
{
5288
// For learning purposes, we opted to directly get the reference

0 commit comments

Comments
 (0)