Skip to content

Commit 35cbf71

Browse files
committed
feat(gen): copy as class
1 parent 31422bc commit 35cbf71

2 files changed

Lines changed: 46 additions & 13 deletions

File tree

SharpFM.App/MainWindow.axaml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:AvaloniaEdit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit"
77
mc:Ignorable="d" d:DesignWidth="700" d:DesignHeight="500"
8+
Width="700" Height="500"
89
xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity"
910
xmlns:behaviors="clr-namespace:SharpFM.App.Behaviors;assembly=SharpFM.App"
1011
x:DataType="vm:MainWindowViewModel"
1112
x:Class="SharpFM.App.MainWindow"
1213
Title="SharpFM">
1314
<DockPanel>
1415
<Menu DockPanel.Dock="Top">
15-
<MenuItem Header="_File">
16-
<MenuItem Header="_New"
17-
Command="{Binding NewEmptyItemCommand}"/>
18-
<Separator/>
19-
<MenuItem Header="_Exit" Command="{Binding ExitApplicationCommand}" />
20-
</MenuItem>
21-
<MenuItem Header="_Edit">
22-
<MenuItem Header="Copy"
23-
Command="{Binding CopySelectedToClipCommand}"/>
24-
<MenuItem Header="Paste"
25-
Command="{Binding PasteTextCommand}"/>
26-
</MenuItem>
16+
<MenuItem Header="_File">
17+
<MenuItem Header="_New"
18+
Command="{Binding NewEmptyItemCommand}"/>
19+
<Separator/>
20+
<MenuItem Header="_Exit" Command="{Binding ExitApplicationCommand}" />
21+
</MenuItem>
22+
<MenuItem Header="_Edit">
23+
<MenuItem
24+
Header="Copy"
25+
Command="{Binding CopySelectedToClipCommand}"/>
26+
<MenuItem
27+
Header="Paste"
28+
Command="{Binding PasteTextCommand}"/>
29+
</MenuItem>
30+
<MenuItem Header="Transform">
31+
<MenuItem Header="Copy as C# Class" Command="{Binding CopyAsClassCommand}"/>
32+
</MenuItem>
2733
</Menu>
2834
<TextBlock/>
2935
<Grid>

SharpFM.App/ViewModels/MainWindowViewModel.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ private void NewEmptyItem()
4444
}
4545
}
4646

47+
[RelayCommand]
48+
private void CopyAsClass()
49+
{
50+
ErrorMessages?.Clear();
51+
try
52+
{
53+
if (SelectedClip == null)
54+
{
55+
// no clip selected;
56+
return;
57+
}
58+
59+
// TODO: improve the UX of this whole thing. This works as a hack for proving the concept, but it could be so much better.
60+
// See DepInject project for a sample of how to accomplish this.
61+
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
62+
desktop.MainWindow?.Clipboard is not { } provider)
63+
throw new NullReferenceException("Missing Clipboard instance.");
64+
65+
var classString = SelectedClip.CreateClass();
66+
provider.SetTextAsync(classString);
67+
}
68+
catch (Exception e)
69+
{
70+
ErrorMessages?.Add(e.Message);
71+
}
72+
}
73+
4774
[RelayCommand]
4875
private async Task PasteText(CancellationToken token)
4976
{
@@ -57,6 +84,7 @@ private async Task PasteText(CancellationToken token)
5784
ErrorMessages?.Add(e.Message);
5885
}
5986
}
87+
6088
[RelayCommand]
6189
private async Task CopySelectedToClip(CancellationToken token)
6290
{
@@ -68,7 +96,6 @@ private async Task CopySelectedToClip(CancellationToken token)
6896
desktop.MainWindow?.Clipboard is not { } provider)
6997
throw new NullReferenceException("Missing Clipboard instance.");
7098

71-
7299
var dp = new DataPackage();
73100

74101
if (!(SelectedClip is FileMakerClip data))

0 commit comments

Comments
 (0)