Skip to content

Commit 7c54978

Browse files
committed
add clip view model.
1 parent db9e1b1 commit 7c54978

3 files changed

Lines changed: 69 additions & 14 deletions

File tree

SharpFM.App/MainWindow.axaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
<TextBox Margin="0" Text="{Binding Name, Mode=TwoWay}" />
4848
<ComboBox
4949
DisplayMemberBinding="{Binding DisplayName}"
50-
ItemsSource="{Binding ClipTypes}"
51-
SelectedValue="{Binding ClipboardFormat, Mode=TwoWay}"
50+
ItemsSource="{Binding Clip.ClipTypes}"
51+
SelectedValue="{Binding ClipType, Mode=TwoWay}"
5252
SelectedValueBinding="{Binding KeyId}" />
53-
<TextBlock MaxLines="1" Text="{Binding ClipboardFormat}" />
53+
<TextBlock MaxLines="1" Text="{Binding ClipType}" />
5454
</StackPanel>
5555
</DataTemplate>
5656
</ListBox.ItemTemplate>
@@ -64,7 +64,7 @@
6464
SyntaxHighlighting="Xml"
6565
WordWrap="False">
6666
<i:Interaction.Behaviors>
67-
<behaviors:DocumentTextBindingBehavior Text="{Binding SelectedClip.XmlData, Mode=TwoWay}" />
67+
<behaviors:DocumentTextBindingBehavior Text="{Binding SelectedClip.ClipXml, Mode=TwoWay}" />
6868
</i:Interaction.Behaviors>
6969
</AvaloniaEdit:TextEditor>
7070

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
3+
using SharpFM.Core;
4+
5+
namespace SharpFM.App.ViewModels;
6+
7+
public partial class ClipViewModel : INotifyPropertyChanged
8+
{
9+
public event PropertyChangedEventHandler? PropertyChanged;
10+
11+
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
12+
{
13+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
14+
}
15+
16+
public FileMakerClip Clip { get; set; }
17+
18+
public ClipViewModel(FileMakerClip clip)
19+
{
20+
Clip = clip;
21+
}
22+
23+
public string ClipType
24+
{
25+
get => Clip.ClipboardFormat;
26+
set
27+
{
28+
Clip.ClipboardFormat = value;
29+
NotifyPropertyChanged();
30+
}
31+
}
32+
33+
public string Name
34+
{
35+
get => Clip.Name;
36+
set
37+
{
38+
Clip.Name = value;
39+
NotifyPropertyChanged();
40+
}
41+
}
42+
43+
public string ClipXml
44+
{
45+
get => Clip.XmlData;
46+
set
47+
{
48+
Clip.XmlData = value;
49+
NotifyPropertyChanged();
50+
}
51+
}
52+
}

SharpFM.App/ViewModels/MainWindowViewModel.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
2323

2424
public MainWindowViewModel()
2525
{
26-
Keys = new ObservableCollection<FileMakerClip>();
26+
Keys = new ObservableCollection<ClipViewModel>();
2727
}
2828

2929
public void ExitApplication()
@@ -38,7 +38,10 @@ public void NewEmptyItem()
3838
{
3939
try
4040
{
41-
Keys.Add(new FileMakerClip("New", FileMakerClip.ClipTypes.First()?.KeyId ?? "", Array.Empty<byte>()));
41+
var clip = new FileMakerClip("New", FileMakerClip.ClipTypes.First()?.KeyId ?? "", Array.Empty<byte>());
42+
var clipVm = new ClipViewModel(clip);
43+
44+
Keys.Add(clipVm);
4245
}
4346
catch (Exception e)
4447
{
@@ -62,7 +65,7 @@ public void CopyAsClass()
6265
desktop.MainWindow?.Clipboard is not { } provider)
6366
throw new NullReferenceException("Missing Clipboard instance.");
6467

65-
var classString = SelectedClip.CreateClass();
68+
var classString = SelectedClip.Clip.CreateClass();
6669
provider.SetTextAsync(classString);
6770
}
6871
catch (Exception e)
@@ -99,12 +102,12 @@ public async Task PasteFileMakerClipData(CancellationToken token)
99102

100103
// don't bother adding a duplicate. For some reason entries were getting entered twice per clip
101104
// this is not the most efficient method to detect it, but it works well enough for now
102-
if (Keys.Any(k => k.XmlData == clip.XmlData))
105+
if (Keys.Any(k => k.ClipXml == clip.XmlData))
103106
{
104107
continue;
105108
}
106109

107-
Keys.Add(clip);
110+
Keys.Add(new ClipViewModel(clip));
108111
}
109112
}
110113
catch (Exception e)
@@ -123,12 +126,12 @@ public async Task CopySelectedToClip(CancellationToken token)
123126

124127
var dp = new DataPackage();
125128

126-
if (SelectedClip is not FileMakerClip data)
129+
if (SelectedClip is not ClipViewModel data)
127130
{
128131
return; // no data
129132
}
130133

131-
dp.SetData(data.ClipboardFormat, data.RawData);
134+
dp.SetData(data.ClipType, data.Clip.RawData);
132135

133136
await provider.SetDataObjectAsync(dp);
134137
}
@@ -137,10 +140,10 @@ public async Task CopySelectedToClip(CancellationToken token)
137140
}
138141
}
139142

140-
public ObservableCollection<FileMakerClip> Keys { get; set; }
143+
public ObservableCollection<ClipViewModel> Keys { get; set; }
141144

142-
private FileMakerClip? _selectedClip;
143-
public FileMakerClip? SelectedClip
145+
private ClipViewModel? _selectedClip;
146+
public ClipViewModel? SelectedClip
144147
{
145148
get => _selectedClip;
146149
set

0 commit comments

Comments
 (0)