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

[Android] TimePicker focus & unfocus events #26949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26908.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Maui.Controls.Sample.Issues
{

[Issue(IssueTracker.Github, 26908, "Time Picker Focus Unfocus is not working", PlatformAffected.Android)]
public class Issue26908 : TestContentPage
{
protected override void Init()
{
var timePicker = new TimePicker();
var focusedLabel = new Label
kubaflo marked this conversation as resolved.
Show resolved Hide resolved
{
AutomationId = "FocusedLabel",
Text = "The time picker was focused",
IsVisible = false,
};

var unfocusedLabel = new Label
{
AutomationId = "UnfocusedLabel",
Text = "The time picker was unfocused",
IsVisible = false,
};

timePicker.Focused += (s, e) =>
{
focusedLabel.IsVisible = true;
kubaflo marked this conversation as resolved.
Show resolved Hide resolved
timePicker.Unfocus();
};

timePicker.Unfocused += (s, e) => unfocusedLabel.IsVisible = true;

Content = new StackLayout
{
Children =
{
timePicker,
focusedLabel,
unfocusedLabel,
new Button()
{
Text = "Focus",
AutomationId = "FocusButton",
Command = new Command(() => timePicker.Focus())
}
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue26908 : _IssuesUITest
{
public Issue26908(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Time Picker Focus Unfocus is not working";

[Test]
[Category(UITestCategories.TimePicker)]
public void FocusAndUnfocusEventsShouldWork()
{
App.WaitForElement("FocusButton");
App.Click("FocusButton");
App.WaitForElement("FocusedLabel");
App.WaitForElement("UnfocusedLabel");
}
}
}
23 changes: 21 additions & 2 deletions src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ public static void MapFormat(ITimePickerHandler handler, ITimePicker timePicker)
handler.PlatformView?.UpdateFormat(timePicker);
}

static void MapFocus(ITimePickerHandler handler, ITimePicker timePicker, object? args)
{
if (args is FocusRequest request && handler is TimePickerHandler timePickerHandler)
{
timePickerHandler.ShowPickerDialog();
request.TrySetResult(true);
}
}

static void MapUnfocus(ITimePickerHandler handler, ITimePicker timePicker, object? args)
{
if (handler is TimePickerHandler timePickerHandler)
{
timePickerHandler.HidePickerDialog();
}
}

public static void MapTime(ITimePickerHandler handler, ITimePicker timePicker)
{
handler.PlatformView?.UpdateTime(timePicker);
Expand All @@ -85,7 +102,7 @@ public static void MapTextColor(ITimePickerHandler handler, ITimePicker timePick
handler.PlatformView?.UpdateTextColor(timePicker);
}

void ShowPickerDialog()
internal void ShowPickerDialog()
kubaflo marked this conversation as resolved.
Show resolved Hide resolved
{
if (VirtualView == null)
return;
Expand All @@ -101,15 +118,17 @@ void ShowPickerDialog(int hour, int minute)
{
_dialog = CreateTimePickerDialog(hour, minute);
_dialog.Show();
VirtualView.IsFocused = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed here? The maui time p;icker is a text box, so that should work with normal focus commands?

Entry does this:

static void MapFocus(IEntryHandler handler, IEntry entry, object? args)
{
if (args is FocusRequest request)
handler.PlatformView.Focus(request);
}

I also see we double-override it in Controls:

#if IOS || ANDROID
EntryHandler.Mapper.AppendToMapping(nameof(VisualElement.IsFocused), InputView.MapIsFocused);
#endif
#if ANDROID
EntryHandler.CommandMapper.PrependToMapping(nameof(IEntry.Focus), InputView.MapFocus);
#endif

So basically, Focus support is all strage...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added it so that focus is activated when the dialog is opened. Without it the control never recognises its focus status

}

void HidePickerDialog()
internal void HidePickerDialog()
{
if (_dialog != null)
{
_dialog.Hide();
}

VirtualView.IsFocused = false;
_dialog = null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Core/src/Handlers/TimePicker/TimePickerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public partial class TimePickerHandler : ITimePickerHandler

public static CommandMapper<ITimePicker, ITimePickerHandler> CommandMapper = new(ViewCommandMapper)
{
#if ANDROID
[nameof(ITimePicker.Focus)] = MapFocus,
[nameof(ITimePicker.Unfocus)] = MapUnfocus,
#endif
};

public TimePickerHandler() : base(Mapper, CommandMapper)
Expand Down
Loading