Skip to content

Commit

Permalink
Merge pull request #1974 from cwensley/curtis/fix-textbox-select-always
Browse files Browse the repository at this point in the history
TextBox.AutoSelectMode fixes
  • Loading branch information
cwensley committed Jun 21, 2021
2 parents fcbf412 + 252520b commit ec210e5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
31 changes: 28 additions & 3 deletions src/Eto.Mac/Forms/Controls/MacText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static class MacText
{
public static object AutoSelectMode_Key = new object();
public static object HasInitialFocus_Key = new object();
public static object NeedsSelectAll_Key = new object();
}

public abstract class MacText<TControl, TWidget, TCallback> : MacControl<TControl, TWidget, TCallback>, TextControl.IHandler, IMacText
Expand Down Expand Up @@ -169,16 +170,24 @@ protected override void Initialize()
{
base.Initialize();
Widget.GotFocus += Widget_GotFocus;
Widget.LostFocus += Widget_LostFocus;
Widget.MouseDown += Widget_MouseDown;
}

void Widget_MouseDown(object sender, MouseEventArgs e)
{
if (AutoSelectMode == AutoSelectMode.Always)
e.Handled = SelectAllOnMouseDown(e);
}

protected virtual bool SelectAllOnMouseDown(MouseEventArgs e)
{
if (NeedsSelectAll)
{
SelectAll();
e.Handled = true;
NeedsSelectAll = false;
return true;
}
return false;
}


Expand All @@ -188,6 +197,18 @@ bool HasInitialFocus
set { Widget.Properties.Set(MacText.HasInitialFocus_Key, value, false); }
}

protected bool NeedsSelectAll
{
get { return Widget.Properties.Get(MacText.NeedsSelectAll_Key, false); }
set { Widget.Properties.Set(MacText.NeedsSelectAll_Key, value, false); }
}

void Widget_LostFocus(object sender, EventArgs e)
{
if (AutoSelectMode == AutoSelectMode.Always)
NeedsSelectAll = false;
}

void Widget_GotFocus(object sender, EventArgs e)
{
var editor = Control.CurrentEditor;
Expand All @@ -208,9 +229,13 @@ void Widget_GotFocus(object sender, EventArgs e)
else
{
var len = Text?.Length ?? 0;
editor.SelectedRange = new NSRange(0, len);
editor.SelectedRange = new NSRange(len, 0);
}
}
else if (AutoSelectMode == AutoSelectMode.Always)
{
NeedsSelectAll = true;
}
HasInitialFocus = true;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Eto.Mac/Forms/Controls/SearchBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public EtoTextField()
Cell.Wraps = false;
Cell.UsesSingleLineMode = true;
}

[Export("textViewDidChangeSelection:")]
public void TextViewDidChangeSelection(NSNotification notification)
{
var h = Handler;
if (h != null)
{
var textView = (NSTextView)notification.Object;
h.SetLastSelection(textView.SelectedRange.ToEto());
}
}

}

public override bool HasFocus
Expand Down Expand Up @@ -121,6 +133,14 @@ static void HandleTextChanged(object sender, EventArgs e)
handler.Callback.OnTextChanged(handler.Widget, EventArgs.Empty);
}
}

protected override bool SelectAllOnMouseDown(MouseEventArgs e)
{
var cancelRect = Control.Cell.CancelButtonRectForBounds(Control.Bounds).ToEto();
if (!cancelRect.Contains(e.Location))
return base.SelectAllOnMouseDown(e);
return false;
}

public bool ReadOnly
{
Expand Down
5 changes: 5 additions & 0 deletions src/Eto.Mac/Forms/Controls/TextBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public class EtoTextFieldCell : NSTextFieldCell, IColorizeCell
{
ColorizeView colorize;

public EtoTextFieldCell()
{
StringValue = string.Empty;
}

public Color? Color
{
get => colorize?.Color;
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Wpf/Forms/Controls/TextBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Control_GotKeyboardFocus(object sender, sw.Input.KeyboardFocusChangedEventA
initialSelection = false;
return;
}
if (AutoSelectMode == AutoSelectMode.OnFocus)
if (AutoSelectMode == AutoSelectMode.OnFocus || AutoSelectMode == AutoSelectMode.Always)
TextBox.SelectAll();
}

Expand Down
16 changes: 13 additions & 3 deletions test/Eto.Test/Sections/Controls/TextBoxSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
namespace Eto.Test.Sections.Controls
{
[Section("Controls", typeof(TextBox))]
public class TextBoxSection : Scrollable
public class TextBoxSection : TextBoxSection<TextBox>
{
}

[Section("Controls", typeof(SearchBox))]
public class SearchBoxSection : TextBoxSection<SearchBox>
{
}

public class TextBoxSection<T> : Scrollable
where T: TextBox, new()
{
public TextBoxSection()
{
var textBox = new TextBox();
var textBox = new T();
LogEvents(textBox);

var placeholderText = new TextBox();
Expand Down Expand Up @@ -53,7 +63,7 @@ public TextBoxSection()

Control DifferentSize()
{
var control = new TextBox { Text = "Different Size (300x50)", Size = new Size(300, 50) };
var control = new T { Text = "Different Size (300x50)", Size = new Size(300, 50) };
LogEvents(control);
return control;
}
Expand Down

0 comments on commit ec210e5

Please sign in to comment.