Browse Source

Merge branch 'master' into fixes/2680-path-geometry-invalidation

pull/3757/head
Steven Kirk 6 years ago
committed by GitHub
parent
commit
5104c375bc
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/Avalonia.Controls/TextBox.cs
  2. 39
      tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

10
src/Avalonia.Controls/TextBox.cs

@ -473,8 +473,10 @@ namespace Avalonia.Controls
{ {
if (!IsPasswordBox) if (!IsPasswordBox)
{ {
_undoRedoHelper.Snapshot();
Copy(); Copy();
DeleteSelection(); DeleteSelection();
_undoRedoHelper.Snapshot();
} }
handled = true; handled = true;
@ -600,6 +602,7 @@ namespace Avalonia.Controls
break; break;
case Key.Back: case Key.Back:
_undoRedoHelper.Snapshot();
if (hasWholeWordModifiers && SelectionStart == SelectionEnd) if (hasWholeWordModifiers && SelectionStart == SelectionEnd)
{ {
SetSelectionForControlBackspace(); SetSelectionForControlBackspace();
@ -623,11 +626,13 @@ namespace Avalonia.Controls
CaretIndex -= removedCharacters; CaretIndex -= removedCharacters;
SelectionStart = SelectionEnd = CaretIndex; SelectionStart = SelectionEnd = CaretIndex;
} }
_undoRedoHelper.Snapshot();
handled = true; handled = true;
break; break;
case Key.Delete: case Key.Delete:
_undoRedoHelper.Snapshot();
if (hasWholeWordModifiers && SelectionStart == SelectionEnd) if (hasWholeWordModifiers && SelectionStart == SelectionEnd)
{ {
SetSelectionForControlDelete(); SetSelectionForControlDelete();
@ -649,6 +654,7 @@ namespace Avalonia.Controls
SetTextInternal(text.Substring(0, caretIndex) + SetTextInternal(text.Substring(0, caretIndex) +
text.Substring(caretIndex + removedCharacters)); text.Substring(caretIndex + removedCharacters));
} }
_undoRedoHelper.Snapshot();
handled = true; handled = true;
break; break;
@ -656,7 +662,9 @@ namespace Avalonia.Controls
case Key.Enter: case Key.Enter:
if (AcceptsReturn) if (AcceptsReturn)
{ {
_undoRedoHelper.Snapshot();
HandleTextInput(NewLine); HandleTextInput(NewLine);
_undoRedoHelper.Snapshot();
handled = true; handled = true;
} }
@ -665,7 +673,9 @@ namespace Avalonia.Controls
case Key.Tab: case Key.Tab:
if (AcceptsTab) if (AcceptsTab)
{ {
_undoRedoHelper.Snapshot();
HandleTextInput("\t"); HandleTextInput("\t");
_undoRedoHelper.Snapshot();
handled = true; handled = true;
} }
else else

39
tests/Avalonia.Controls.UnitTests/TextBoxTests.cs

@ -1,10 +1,12 @@
using System; using System;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Threading.Tasks;
using Avalonia.Controls.Presenters; using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.UnitTests; using Avalonia.UnitTests;
@ -554,6 +556,34 @@ namespace Avalonia.Controls.UnitTests
} }
} }
[Theory]
[InlineData(Key.X, KeyModifiers.Control)]
[InlineData(Key.Back, KeyModifiers.None)]
[InlineData(Key.Delete, KeyModifiers.None)]
[InlineData(Key.Tab, KeyModifiers.None)]
[InlineData(Key.Enter, KeyModifiers.None)]
public void Keys_Allow_Undo(Key key, KeyModifiers modifiers)
{
using (UnitTestApplication.Start(Services))
{
var target = new TextBox
{
Template = CreateTemplate(),
Text = "0123",
AcceptsReturn = true,
AcceptsTab = true
};
target.SelectionStart = 1;
target.SelectionEnd = 3;
AvaloniaLocator.CurrentMutable
.Bind<Input.Platform.IClipboard>().ToSingleton<ClipboardStub>();
RaiseKeyEvent(target, key, modifiers);
RaiseKeyEvent(target, Key.Z, KeyModifiers.Control); // undo
Assert.True(target.Text == "0123");
}
}
private static TestServices FocusServices => TestServices.MockThreadingInterface.With( private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
focusManager: new FocusManager(), focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice(), keyboardDevice: () => new KeyboardDevice(),
@ -616,5 +646,14 @@ namespace Avalonia.Controls.UnitTests
set { _bar = value; RaisePropertyChanged(); } set { _bar = value; RaisePropertyChanged(); }
} }
} }
private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard
{
public Task<string> GetTextAsync() => Task.FromResult("");
public Task SetTextAsync(string text) => Task.CompletedTask;
public Task ClearAsync() => Task.CompletedTask;
}
} }
} }

Loading…
Cancel
Save