Browse Source

Add TextBox UndoLimit

pull/6023/head
Deadpikle 5 years ago
parent
commit
c6eb57c535
  1. 20
      src/Avalonia.Controls/TextBox.cs
  2. 11
      src/Avalonia.Controls/Utils/UndoRedoHelper.cs

20
src/Avalonia.Controls/TextBox.cs

@ -138,6 +138,13 @@ namespace Avalonia.Controls
nameof(IsUndoEnabled),
defaultValue: true);
public static readonly DirectProperty<TextBox, int> UndoLimitProperty =
AvaloniaProperty.RegisterDirect<TextBox, int>(
nameof(UndoLimit),
o => o._undoRedoHelper.Limit,
(o, v) => o._undoRedoHelper.Limit = v,
unsetValue: -1);
struct UndoRedoState : IEquatable<UndoRedoState>
{
public string Text { get; }
@ -472,6 +479,19 @@ namespace Avalonia.Controls
}
}
public int UndoLimit
{
get { return GetValue(UndoLimitProperty); }
set
{
SetValue(UndoLimitProperty, value);
// from docs at
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.textboxbase.isundoenabled:
// "Setting UndoLimit clears the undo queue."
_undoRedoHelper.Clear();
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
_presenter = e.NameScope.Get<TextPresenter>("PART_TextPresenter");

11
src/Avalonia.Controls/Utils/UndoRedoHelper.cs

@ -22,6 +22,10 @@ namespace Avalonia.Controls.Utils
private LinkedListNode<TState> _currentNode;
/// <summary>
/// Maximum number of states this helper can store for undo/redo.
/// If -1, no limit is imposed.
/// </summary>
public int Limit { get; set; } = 10;
public UndoRedoHelper(IUndoRedoHost host)
@ -54,7 +58,10 @@ namespace Avalonia.Controls.Utils
public bool HasState => _currentNode != null;
public void UpdateLastState(TState state)
{
_states.Last.Value = state;
if (_states.Last != null)
{
_states.Last.Value = state;
}
}
public void UpdateLastState()
@ -86,7 +93,7 @@ namespace Avalonia.Controls.Utils
DiscardRedo();
_states.AddLast(current);
_currentNode = _states.Last;
if (_states.Count > Limit)
if (Limit != -1 && _states.Count > Limit)
_states.RemoveFirst();
}
}

Loading…
Cancel
Save