Fix Entry Completed invocation logic for macOS (#3201) fixes #1650

The Completed event should be invoked when the user presses the return key only. Using NSTextField DidEndEditing is not favorable because pressing the Tab key will cause this method as well
This commit is contained in:
John VanSickle 2018-08-06 12:54:54 -04:00 коммит произвёл Rui Marinho
Родитель fa1df7bdcd
Коммит a447b5b1af
3 изменённых файлов: 89 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,69 @@
using System;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
#if UITEST
using NUnit.Framework;
#endif
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 1650, "[macOS] Completed event of Entry raised on Tab key", PlatformAffected.macOS)]
public class GitHub1650 : TestContentPage
{
Label _completedCountLabel = new Label
{
Text = "Completed count: 0",
AutomationId = "CompletedCountLabel"
};
int _completedCount;
public int CompletedCount
{
get { return _completedCount; }
set
{
_completedCount = value;
_completedCountLabel.Text = $"Completed count: {value}";
}
}
protected override void Init()
{
// Setup our completed entry
var entry = new Entry
{
Placeholder = "Press enter here!",
AutomationId = "CompletedTargetEntry"
};
entry.Completed += (sender, e) =>
{
CompletedCount++;
};
StackLayout layout = new StackLayout();
layout.Children.Add(_completedCountLabel);
layout.Children.Add(entry);
Content = layout;
}
#if UITEST
#if __MACOS__
[Test]
public void GitHub1650Test()
{
RunningApp.WaitForElement(q => q.Marked("CompletedTargetEntry"));
RunningApp.Tap(q => q.Marked("CompletedTargetEntry"));
Assert.AreEqual(0, _completedCount, "Completed should not have been fired");
RunningApp.PressEnter();
Assert.AreEqual(1, _completedCount, "Completed should have been fired once");
}
#endif
#endif
}
}

Просмотреть файл

@ -773,6 +773,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue2728.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1667.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3012.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GitHub1650.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GitHub3216.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GitHub1776.cs" />
</ItemGroup>

Просмотреть файл

@ -11,6 +11,8 @@ namespace Xamarin.Forms.Platform.MacOS
{
public EventHandler<BoolEventArgs> FocusChanged;
public EventHandler Completed;
bool _windowEventsSet;
bool _disposed;
@ -40,10 +42,18 @@ namespace Xamarin.Forms.Platform.MacOS
{
if (CurrentEditor != Window.FirstResponder)
FocusChanged?.Invoke(this, new BoolEventArgs(false));
base.DidEndEditing(notification);
}
public override void KeyUp(NSEvent theEvent)
{
base.KeyUp(theEvent);
if (theEvent.KeyCode == (ushort)NSKey.Return)
Completed?.Invoke(this, EventArgs.Empty);
}
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
@ -92,6 +102,7 @@ namespace Xamarin.Forms.Platform.MacOS
{
textField = new FormsNSTextField();
(textField as FormsNSTextField).FocusChanged += TextFieldFocusChanged;
(textField as FormsNSTextField).Completed += OnCompleted;
}
SetNativeControl(textField);
@ -167,7 +178,10 @@ namespace Xamarin.Forms.Platform.MacOS
Control.EditingEnded -= OnEditingEnded;
var formsNSTextField = (Control as FormsNSTextField);
if (formsNSTextField != null)
{
formsNSTextField.FocusChanged -= TextFieldFocusChanged;
formsNSTextField.Completed -= OnCompleted;
}
}
}
@ -193,6 +207,10 @@ namespace Xamarin.Forms.Platform.MacOS
void OnEditingEnded(object sender, EventArgs e)
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}
void OnCompleted(object sender, EventArgs e)
{
EntryController?.SendCompleted();
}