Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Fix TextBox.Text so it clears the undo buffer when set #2713

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 37 additions & 39 deletions src/Eto.Wpf/Forms/Controls/TextBoxHandler.cs
Original file line number Diff line number Diff line change
@@ -298,55 +298,53 @@ public string Text
var oldText = Text;
CurrentText = null;
var newText = value ?? string.Empty;
TextBox.BeginChange();
if (newText != oldText)
{
var args = new TextChangingEventArgs(oldText, newText, false);
Callback.OnTextChanging(Widget, args);
if (args.Cancel)
{
TextBox.EndChange();
return;
}
if (newText == oldText)
return;

var needsTextChanged = TextBox.Text == newText;
var args = new TextChangingEventArgs(oldText, newText, false);
Callback.OnTextChanging(Widget, args);
if (args.Cancel)
return;

// Improve performance when setting text often
// See https://github.com/dotnet/wpf/issues/5887#issuecomment-1604577981
var endNoGCRegion = EnableNoGCRegion
&& GCSettings.LatencyMode != GCLatencyMode.NoGCRegion;
var needsTextChanged = TextBox.Text == newText;

try
{
endNoGCRegion &= GC.TryStartNoGCRegion(1000000); // is this magic number reasonable??
}
catch
{
// Ignore any exceptions, they can apparently still happen even though we check the LatencyMode above
endNoGCRegion = false;
}
// Improve performance when setting text often
// See https://github.com/dotnet/wpf/issues/5887#issuecomment-1604577981
var endNoGCRegion = EnableNoGCRegion
&& GCSettings.LatencyMode != GCLatencyMode.NoGCRegion;

try
{
TextBox.Text = newText;
}
finally
{
if (endNoGCRegion && GCSettings.LatencyMode == GCLatencyMode.NoGCRegion)
GC.EndNoGCRegion();
}

if (needsTextChanged)
{
Callback.OnTextChanged(Widget, EventArgs.Empty);
}
try
{
endNoGCRegion &= GC.TryStartNoGCRegion(1000000); // is this magic number reasonable??
}
catch
{
// Ignore any exceptions, they can apparently still happen even though we check the LatencyMode above
endNoGCRegion = false;
}

try
{
TextBox.Text = newText;
}
finally
{
if (endNoGCRegion && GCSettings.LatencyMode == GCLatencyMode.NoGCRegion)
GC.EndNoGCRegion();
}

if (value != null && AutoSelectMode == AutoSelectMode.Never && !HasFocus)
{
TextBox.BeginChange();
TextBox.SelectionStart = value.Length;
TextBox.SelectionLength = 0;
TextBox.EndChange();
}

if (needsTextChanged)
{
Callback.OnTextChanged(Widget, EventArgs.Empty);
}
TextBox.EndChange();
}
}

28 changes: 28 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/TextBoxTests.cs
Original file line number Diff line number Diff line change
@@ -6,6 +6,34 @@ namespace Eto.Test.UnitTests.Forms.Controls
public abstract class TextBoxBase<T> : TestBase
where T: TextBox, new()
{
[Test, ManualTest]
public void SettingTextShouldClearUndoBuffer()
{
ManualForm("Try typing and undo/redo, then press the button to reset. After reset, it should not undo to previous values", form =>
{
var textBox = new T();
textBox.Text = "Hello";
textBox.SelectAll();

var button = new Button { Text = "Click Me" };
button.Click += (sender, e) =>
{
textBox.Text = "Thanks, now try to undo";
textBox.Focus();
};

return new TableLayout
{
Spacing = new Size(5, 5),
Padding = 10,
Rows = {
textBox,
button
}
};
});
}

[Test, ManualTest]
public void CaretIndexShouldStartInInitialPosition()
{

Unchanged files with check annotations Beta

var mask = Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask;
#if GTK3
var grabbed = window.Display.DeviceManager.ClientPointer.Grab(window.GetWindow(), Gdk.GrabOwnership.Window, true, mask, null, 0);

Check warning on line 14 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-mac

'Display.DeviceManager' is obsolete

Check warning on line 14 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-mac

'DeviceManager.ClientPointer' is obsolete

Check warning on line 14 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-mac

'Device.Grab(Window, GrabOwnership, bool, EventMask, Cursor, uint)' is obsolete

Check warning on line 14 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-windows

'Display.DeviceManager' is obsolete

Check warning on line 14 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-windows

'DeviceManager.ClientPointer' is obsolete

Check warning on line 14 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-windows

'Device.Grab(Window, GrabOwnership, bool, EventMask, Cursor, uint)' is obsolete
if (grabbed != Gdk.GrabStatus.Success)
{
Gtk.Grab.Remove(window);
{
Gtk.Grab.Remove(window);
#if GTK3
window.Display.DeviceManager.ClientPointer.Ungrab(0);

Check warning on line 42 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-mac

'Display.DeviceManager' is obsolete

Check warning on line 42 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-mac

'DeviceManager.ClientPointer' is obsolete

Check warning on line 42 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-mac

'Device.Ungrab(uint)' is obsolete

Check warning on line 42 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-windows

'Display.DeviceManager' is obsolete

Check warning on line 42 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-windows

'DeviceManager.ClientPointer' is obsolete

Check warning on line 42 in src/Eto.Gtk/CustomControls/WindowExtensions.cs

GitHub Actions / build-windows

'Device.Ungrab(uint)' is obsolete
#else
Gdk.Pointer.Ungrab(0);
Gdk.Keyboard.Ungrab(0);
public static Pango.FontDescription GetFont(this Gtk.Widget widget, GtkStateFlags state = GtkStateFlags.Normal)
{
return widget.StyleContext.GetFont(state.ToGtk());

Check warning on line 172 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-mac

'StyleContext.GetFont(StateFlags)' is obsolete

Check warning on line 172 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-windows

'StyleContext.GetFont(StateFlags)' is obsolete
}
public static void SetFont(this Gtk.Widget widget, Pango.FontDescription font)
{
widget.OverrideFont(font);

Check warning on line 177 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-mac

'Widget.OverrideFont(FontDescription)' is obsolete

Check warning on line 177 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-windows

'Widget.OverrideFont(FontDescription)' is obsolete
}
public static Color GetBackground(this Gtk.Widget widget, GtkStateFlags state = GtkStateFlags.Normal)
{
return widget.StyleContext.GetBackgroundColor(state.ToGtk()).ToEto();

Check warning on line 182 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-mac

'StyleContext.GetBackgroundColor(StateFlags)' is obsolete

Check warning on line 182 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-windows

'StyleContext.GetBackgroundColor(StateFlags)' is obsolete
}
public static void SetBackground(this Gtk.Widget widget, Color color, GtkStateFlags state = GtkStateFlags.Normal)
{
widget.OverrideBackgroundColor(state.ToGtk(), color.ToRGBA());

Check warning on line 187 in src/Eto.Gtk/Gtk3Compatibility.cs

GitHub Actions / build-windows

'Widget.OverrideBackgroundColor(StateFlags, RGBA)' is obsolete
}
public static void ClearBackground(this Gtk.Widget widget, GtkStateFlags state = GtkStateFlags.Normal)