Skip to content

Commit

Permalink
Update for focus navigation (dotnet#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot committed Aug 25, 2022
1 parent a6e9870 commit 5fd60db
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 82 deletions.
117 changes: 70 additions & 47 deletions src/Controls/src/Core/Platform/AlertManager/AlertManager.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ internal partial class AlertManager
internal void Subscribe(Window window)
{
var nativeWindow = window?.MauiContext.GetPlatformWindow();

var modalStack = window?.MauiContext.GetModalStack();
if (Subscriptions.Any(s => s.Window == nativeWindow))
return;

Subscriptions.Add(new AlertRequestHelper(nativeWindow));
Subscriptions.Add(new AlertRequestHelper(nativeWindow, modalStack));
}

internal void Unsubscribe(Window window)
Expand All @@ -42,14 +42,17 @@ internal sealed class AlertRequestHelper : IDisposable
int _busyCount;
Popup _busyPopup;

internal AlertRequestHelper(NWindow window)
NavigationStack _modalStack;

internal AlertRequestHelper(NWindow window, NavigationStack modalStack)
{
Window = window;

MessagingCenter.Subscribe<Page, bool>(Window, Page.BusySetSignalName, OnBusySetRequest);
MessagingCenter.Subscribe<Page, AlertArguments>(Window, Page.AlertSignalName, OnAlertRequest);
MessagingCenter.Subscribe<Page, ActionSheetArguments>(Window, Page.ActionSheetSignalName, OnActionSheetRequest);
MessagingCenter.Subscribe<Page, PromptArguments>(Window, Page.PromptSignalName, OnPromptRequested);
_modalStack = modalStack;
}

public NWindow Window { get; }
Expand All @@ -73,25 +76,10 @@ void OnBusySetRequest(Page sender, bool enabled)

if (null == _busyPopup)
{
_busyPopup = new Popup
{
BackgroundColor = new Tizen.NUI.Color(0.1f, 0.1f, 0.1f, 0.5f),
Layout = new LinearLayout
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
},
Content = new Tizen.UIExtensions.NUI.GraphicsView.ActivityIndicator
{
// TODO. need to fix
SizeWidth = 100,
SizeHeight = 100,
IsRunning = true,
}
};
_busyPopup = new BusyPopup();
}

if (_busyCount > 0)
if (_busyCount > 0 && !_busyPopup.IsOpen)
{
_busyPopup.Open();
}
Expand All @@ -118,14 +106,19 @@ async void OnAlertRequest(Page sender, AlertArguments arguments)
{
alert = new MessagePopup(arguments.Title, arguments.Message, arguments.Cancel);
}
try
{
arguments.SetResult(await alert.Open());
}
catch (TaskCanceledException)
{
arguments.SetResult(false);
}

await _modalStack.PushDummyPopupPage(async () => {
try
{
arguments.SetResult(await alert.Open());
}
catch (TaskCanceledException)
{
arguments.SetResult(false);
}
});

alert?.Dispose();
}

async void OnActionSheetRequest(Page sender, ActionSheetArguments arguments)
Expand All @@ -134,15 +127,17 @@ async void OnActionSheetRequest(Page sender, ActionSheetArguments arguments)
if (!PageIsInThisWindow(sender))
return;

try
{
var popup = new ActionSheetPopup(arguments.Title, arguments.Cancel, destruction: arguments.Destruction, buttons: arguments.Buttons);
arguments.SetResult(await popup.Open());
}
catch (TaskCanceledException)
{
arguments.SetResult(arguments.Cancel);
}
await _modalStack.PushDummyPopupPage(async () => {
try
{
using var popup = new ActionSheetPopup(arguments.Title, arguments.Cancel, destruction: arguments.Destruction, buttons: arguments.Buttons);
arguments.SetResult(await popup.Open());
}
catch (TaskCanceledException)
{
arguments.SetResult(arguments.Cancel);
}
});
}

async void OnPromptRequested(Page sender, PromptArguments args)
Expand All @@ -151,22 +146,50 @@ async void OnPromptRequested(Page sender, PromptArguments args)
if (!PageIsInThisWindow(sender))
return;

try
{
// placeholder should not be empty string, if not layout is broken
var popup = new PromptPopup(args.Title, args.Message, args.Accept, args.Cancel, args.Placeholder ?? " ", args.MaxLength, args.Keyboard.ToPlatform(), args.InitialValue);
args.SetResult(await popup.Open());
}
catch (TaskCanceledException)
{
args.SetResult(null);
}

await _modalStack.PushDummyPopupPage(async () => {
try
{
// placeholder should not be empty string, if not layout is broken
using var popup = new PromptPopup(args.Title, args.Message, args.Accept, args.Cancel, args.Placeholder ?? " ", args.MaxLength, args.Keyboard.ToPlatform(), args.InitialValue);
args.SetResult(await popup.Open());
}
catch (TaskCanceledException)
{
args.SetResult(null);
}
});
}

bool PageIsInThisWindow(IView sender)
{
var window = sender.Handler?.MauiContext?.GetPlatformWindow() ?? null;
return window == Window;
}

class BusyPopup : Popup
{
public BusyPopup()
{
BackgroundColor = new Color(0.1f, 0.1f, 0.1f, 0.5f);
Layout = new LinearLayout
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
Content = new Tizen.UIExtensions.NUI.GraphicsView.ActivityIndicator
{

SizeWidth = 100,
SizeHeight = 100,
IsRunning = true,
};
}

protected override bool OnBackButtonPressed()
{
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Tizen.NUI.BaseComponents;
using Tizen.UIExtensions.NUI;
Expand Down Expand Up @@ -110,17 +111,22 @@ public static void UpdateMenuItems(this MauiToolbar platformToolbar, Toolbar too
{
var actions = secondaryActions.ToList();
var actionTexts = actions.Select(i => i.Text).ToList();
using (var popup = new ActionSheetPopup("", "Cancel", null, buttons: actionTexts))
var modalStack = toolbar?.Handler.MauiContext?.GetModalStack();
if (modalStack != null)
{
try
await modalStack.PushDummyPopupPage(async () =>
{
var select = actionTexts.IndexOf(await popup.Open());
actions[select].Command.Execute(actions[select].CommandParameter);
}
catch
{
// Cancel
}
try
{
using var popup = new ActionSheetPopup("", "Cancel", null, buttons: actionTexts);
var select = actionTexts.IndexOf(await popup.Open());
actions[select].Command.Execute(actions[select].CommandParameter);
}
catch
{
// Cancel
}
});
}
};
platformToolbar.Actions.Add(more);
Expand Down
34 changes: 26 additions & 8 deletions src/Core/src/Handlers/Picker/PickerHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public partial class PickerHandler : ViewHandler<IPicker, NEntry>
protected override void ConnectHandler(NEntry platformView)
{
platformView.TouchEvent += OnTouch;
platformView.KeyEvent += OnKeyEvent;
base.ConnectHandler(platformView);
}

protected override void DisconnectHandler(NEntry platformView)
{
platformView.TouchEvent -= OnTouch;
platformView.KeyEvent -= OnKeyEvent;
base.DisconnectHandler(platformView);
}

Expand Down Expand Up @@ -87,21 +89,37 @@ bool OnTouch(object source, Tizen.NUI.BaseComponents.View.TouchEventArgs e)
return true;
}

bool OnKeyEvent(object source, Tizen.NUI.BaseComponents.View.KeyEventArgs e)
{
if (e.Key.State == Tizen.NUI.Key.StateType.Up && (e.Key.KeyPressedName == "Return" || e.Key.KeyPressedName == "Enter"))
{
OpenPopupAsync();
return true;
}
return false;
}


async void OpenPopupAsync()
{
if (VirtualView == null)
return;

using (var popup = new ActionSheetPopup(VirtualView.Title, "Cancel", null, VirtualView.GetItemsAsArray()))
var modalStack = MauiContext?.GetModalStack();
if (modalStack != null)
{
try
{
VirtualView.SelectedIndex = VirtualView.GetItemsAsArray().IndexOf(await popup.Open());
}
catch
await modalStack.PushDummyPopupPage(async () =>
{
// Cancel
}
try
{
using var popup = new ActionSheetPopup(VirtualView.Title, "Cancel", null, VirtualView.GetItemsAsArray());
VirtualView.SelectedIndex = VirtualView.GetItemsAsArray().IndexOf(await popup.Open());
}
catch
{
// Cancel
}
});
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/Core/src/Handlers/SearchBar/SearchBarHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : ViewHandler<ISearchBar, MauiSearchBar>
{
protected override MauiSearchBar CreatePlatformView() => new()
{
Focusable = true,
FocusableInTouch = true,
};
protected override MauiSearchBar CreatePlatformView() => new();

protected override void ConnectHandler(MauiSearchBar platformView)
{
Expand Down
34 changes: 26 additions & 8 deletions src/Core/src/Handlers/TimePicker/TimePickerHandler.Tizen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Tizen.NUI;
using Tizen.UIExtensions.NUI;
using NEntry = Tizen.UIExtensions.NUI.Entry;

namespace Microsoft.Maui.Handlers
Expand All @@ -15,12 +16,14 @@ public partial class TimePickerHandler : ViewHandler<ITimePicker, NEntry>
protected override void ConnectHandler(NEntry platformView)
{
platformView.TouchEvent += OnTouch;
platformView.KeyEvent += OnKeyEvent;
base.ConnectHandler(platformView);
}

protected override void DisconnectHandler(NEntry platformView)
{
platformView.TouchEvent -= OnTouch;
platformView.KeyEvent -= OnKeyEvent;
base.DisconnectHandler(platformView);
}

Expand Down Expand Up @@ -60,21 +63,36 @@ bool OnTouch(object source, Tizen.NUI.BaseComponents.View.TouchEventArgs e)
return true;
}

bool OnKeyEvent(object source, Tizen.NUI.BaseComponents.View.KeyEventArgs e)
{
if (e.Key.IsAcceptKeyEvent())
{
OpenPopupAsync();
return true;
}
return false;
}

async void OpenPopupAsync()
{
if (VirtualView == null)
return;

using (var popup = new MauiTimePicker(VirtualView.Time))
var modalStack = MauiContext?.GetModalStack();
if (modalStack != null)
{
try
{
VirtualView.Time = await popup.Open();
}
catch
await modalStack.PushDummyPopupPage(async () =>
{
// Cancel
}
try
{
using var popup = new MauiTimePicker(VirtualView.Time);
VirtualView.Time = await popup.Open();
}
catch
{
// Cancel
}
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Platform/Tizen/MauiApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ protected MauiApplication()
protected override void OnPreCreate()
{
base.OnPreCreate();
FocusManager.Instance.EnableDefaultAlgorithm(true);

var mauiApp = CreateMauiApp();

var rootContext = new MauiContext(mauiApp.Services);

var platformWindow = CoreAppExtensions.GetDefaultWindow();
Expand Down
Loading

0 comments on commit 5fd60db

Please sign in to comment.