Add event handlers for Accept to match Leave events

This commit is contained in:
tznind
2024-08-31 15:23:33 +01:00
parent 56d9c59484
commit 6bcda35384
2 changed files with 68 additions and 23 deletions

View File

@@ -63,7 +63,8 @@ public class ColorPicker : View
Y = y,
Width = textFieldWidth
};
tfValue.HasFocusChanged += UpdateSingleBarValueFromTextField;
tfValue.HasFocusChanged += (s,_)=> UpdateSingleBarValueFromTextField(s);
tfValue.Accept += (s, _)=>UpdateSingleBarValueFromTextField(s);
_textFields.Add (bar, tfValue);
}
@@ -152,7 +153,8 @@ public class ColorPicker : View
};
_tfName.Autocomplete = auto;
_tfName.HasFocusChanged += UpdateValueFromName;
_tfName.HasFocusChanged += (s,_)=> UpdateValueFromName(s);
_tfName.Accept += (s, _) => UpdateValueFromName (s);
}
private void CreateTextField ()
@@ -181,7 +183,8 @@ public class ColorPicker : View
Add (_lbHex);
Add (_tfHex);
_tfHex.HasFocusChanged += UpdateValueFromTextField;
_tfHex.HasFocusChanged += (s, _) => UpdateValueFromTextField(s);
_tfHex.Accept += (s, _) => UpdateValueFromTextField (s);
}
private void DisposeOldViews ()
@@ -192,7 +195,6 @@ public class ColorPicker : View
if (_textFields.TryGetValue (bar, out TextField? tf))
{
tf.HasFocusChanged -= UpdateSingleBarValueFromTextField;
Remove (tf);
tf.Dispose ();
}
@@ -214,7 +216,6 @@ public class ColorPicker : View
if (_tfHex != null)
{
Remove (_tfHex);
_tfHex.HasFocusChanged -= UpdateValueFromTextField;
_tfHex.Dispose ();
_tfHex = null;
}
@@ -229,7 +230,6 @@ public class ColorPicker : View
if (_tfName != null)
{
Remove (_tfName);
_tfName.HasFocusChanged -= UpdateValueFromName;
_tfName.Dispose ();
_tfName = null;
}
@@ -277,12 +277,8 @@ public class ColorPicker : View
}
}
private void UpdateSingleBarValueFromTextField (object? sender, HasFocusEventArgs e)
private void UpdateSingleBarValueFromTextField (object? sender)
{
if (e.NewValue)
{
return;
}
foreach (KeyValuePair<IColorBar, TextField> kvp in _textFields)
{
@@ -296,13 +292,8 @@ public class ColorPicker : View
}
}
private void UpdateValueFromName (object? sender, HasFocusEventArgs e)
private void UpdateValueFromName (object? sender)
{
if (e.NewValue)
{
return;
}
if (_tfName == null)
{
return;
@@ -319,13 +310,9 @@ public class ColorPicker : View
}
}
private void UpdateValueFromTextField (object? sender, HasFocusEventArgs e)
private void UpdateValueFromTextField (object? sender)
{
if (e.NewValue)
{
return;
}
if (_tfHex == null)
{
return;

View File

@@ -790,6 +790,64 @@ public class ColorPickerTests
Application.ResetState ();
}
/// <summary>
/// In this version we use the Enter button to accept the typed text instead
/// of tabbing to the next view.
/// </summary>
[Fact]
[SetupFakeDriver]
public void ColorPicker_EnterHexFor_ColorName_AcceptVariation ()
{
var cp = GetColorPicker (ColorModel.RGB, true, true);
Application.Navigation = new ();
Application.Current = new ();
Application.Current.Add (cp);
cp.Draw ();
var name = GetTextField (cp, ColorPickerPart.ColorName);
var hex = GetTextField (cp, ColorPickerPart.Hex);
hex.SetFocus ();
Assert.True (hex.HasFocus);
Assert.Same (hex, cp.Focused);
hex.Text = "";
name.Text = "";
Assert.Empty (hex.Text);
Assert.Empty (name.Text);
Application.OnKeyDown ('#');
Assert.Empty (name.Text);
//7FFFD4
Assert.Equal ("#", hex.Text);
Application.OnKeyDown ('7');
Application.OnKeyDown ('F');
Application.OnKeyDown ('F');
Application.OnKeyDown ('F');
Application.OnKeyDown ('D');
Assert.Empty (name.Text);
Application.OnKeyDown ('4');
Assert.True (hex.HasFocus);
// Should stay in the hex field (because accept not tab)
Application.OnKeyDown (Key.Enter);
Assert.True (hex.HasFocus);
Assert.Same (hex, cp.Focused);
// But still, Color name should be recognised as a known string and populated
Assert.Equal ("#7FFFD4", hex.Text);
Assert.Equal ("Aquamarine", name.Text);
Application.Current?.Dispose ();
Application.ResetState ();
}
[Fact]
public void TestColorNames ()
{