WIP: undo redo

This commit is contained in:
rabbitism
2024-01-31 01:11:25 +08:00
parent 93029b47e3
commit 553d413202
2 changed files with 23 additions and 11 deletions

View File

@@ -10,5 +10,12 @@
<u:TagInput HorizontalAlignment="Stretch" AllowDuplicates="False" Tags="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TargetClasses, Mode=TwoWay}" /> <u:TagInput HorizontalAlignment="Stretch" AllowDuplicates="False" Tags="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TargetClasses, Mode=TwoWay}" />
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Setter Property="ContextFlyout">
<MenuFlyout>
<MenuItem Header="Undo" Command="{Binding $parent[u:ControlClassesInput].UnDo}" />
<MenuItem Header="Redo" Command="{Binding $parent[u:ControlClassesInput].Redo}" />
<MenuItem Header="Clear" Command="{Binding $parent[u:ControlClassesInput].Clear}" />
</MenuFlyout>
</Setter>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -74,13 +74,13 @@ public class ControlClassesInput: TemplatedControl
var newValue = args.NewValue.Value; var newValue = args.NewValue.Value;
if (newValue is null) if (newValue is null)
{ {
SaveHistory(new List<string>()); SaveHistory(new List<string>(), true);
return; return;
} }
else else
{ {
var classes = newValue.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct().ToList(); var classes = newValue.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct().ToList();
SaveHistory(classes); SaveHistory(classes, true);
if (newValue is INotifyCollectionChanged incc) if (newValue is INotifyCollectionChanged incc)
{ {
incc.CollectionChanged+=InccOnCollectionChanged; incc.CollectionChanged+=InccOnCollectionChanged;
@@ -91,20 +91,20 @@ public class ControlClassesInput: TemplatedControl
private void InccOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) private void InccOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ {
SaveHistory(TargetClasses?.ToList() ?? new List<string>()); SaveHistory(TargetClasses?.ToList() ?? new List<string>(), true);
} }
private void SaveHistory(List<string> strings) private void SaveHistory(List<string> strings, bool fromInput)
{ {
_history.AddLast(strings); _history.AddLast(strings);
if (_history.Count > CountOfHistoricalRecord) if (_history.Count > CountOfHistoricalRecord)
{ {
_history.RemoveFirst(); _history.RemoveFirst();
} }
SetClassesToTarget(); SetClassesToTarget(fromInput);
} }
private void SetClassesToTarget() private void SetClassesToTarget(bool fromInput)
{ {
List<string> strings; List<string> strings;
if (_history.Count == 0) if (_history.Count == 0)
@@ -115,6 +115,11 @@ public class ControlClassesInput: TemplatedControl
{ {
strings = _history.Last.Value; strings = _history.Last.Value;
} }
if (!fromInput)
{
SetCurrentValue(TargetClassesProperty, new ObservableCollection<string>(strings));
}
if (Target is not null) if (Target is not null)
{ {
Target.Classes.Replace(strings); Target.Classes.Replace(strings);
@@ -130,8 +135,8 @@ public class ControlClassesInput: TemplatedControl
var node = _history.Last; var node = _history.Last;
_history.RemoveLast(); _history.RemoveLast();
_undoHistory.AddFirst(node); _undoHistory.AddFirst(node);
SetCurrentValue(TargetClassesProperty, new AvaloniaList<string>(node.Value)); SetCurrentValue(TargetClassesProperty, new ObservableCollection<string>(node.Value));
SetClassesToTarget(); SetClassesToTarget(false);
} }
public void Redo() public void Redo()
@@ -139,12 +144,12 @@ public class ControlClassesInput: TemplatedControl
var node = _undoHistory.First; var node = _undoHistory.First;
_undoHistory.RemoveFirst(); _undoHistory.RemoveFirst();
_history.AddLast(node); _history.AddLast(node);
SetCurrentValue(TargetClassesProperty, new AvaloniaList<string>(node.Value)); SetCurrentValue(TargetClassesProperty, new ObservableCollection<string>(node.Value));
SetClassesToTarget(); SetClassesToTarget(false);
} }
public void Clear() public void Clear()
{ {
SaveHistory(new List<string>()); SaveHistory(new List<string>(), false);
} }
} }