Skip to content

Commit

Permalink
handle event keyup in the URL entry so that users of hardware keyboar…
Browse files Browse the repository at this point in the history
…ds can navigate with the Enter key

BubbleID #4
  • Loading branch information
aschuhardt committed May 18, 2023
1 parent 6a80913 commit 6515d92
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Input;
using Android.Views;
using CommunityToolkit.Maui.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Handlers;
Expand All @@ -14,8 +15,8 @@ namespace RosyCrow.Views;
public partial class MainPage : ContentPage
{
private readonly IBrowsingDatabase _browsingDatabase;
private readonly ISettingsDatabase _settingsDatabase;
private readonly ILogger<MainPage> _logger;
private readonly ISettingsDatabase _settingsDatabase;

private ICommand _expandMenu;
private ICommand _findInPage;
Expand All @@ -34,9 +35,9 @@ public partial class MainPage : ContentPage
private ICommand _openSettings;
private ICommand _print;
private ICommand _setHomeUrl;
private ICommand _showPageCertificate;
private ICommand _toggleBookmarked;
private ICommand _toggleMenuExpanded;
private ICommand _showPageCertificate;

public MainPage(ISettingsDatabase settingsDatabase, IBrowsingDatabase browsingDatabase, ILogger<MainPage> logger)
{
Expand Down Expand Up @@ -79,6 +80,8 @@ public MainPage(ISettingsDatabase settingsDatabase, IBrowsingDatabase browsingDa
button.GestureRecognizers.Add(new TapGestureRecognizer { Command = button.Command });
}

UrlEntry.HandlerChanged += SetupUrlEnterHandling;

WebViewHandler.Mapper.AppendToMapping("WebViewScrollingAware", (handler, _) =>
{
#if ANDROID
Expand Down Expand Up @@ -295,6 +298,25 @@ public ICommand ShowPageCertificate
}
}

private void SetupUrlEnterHandling(object sender, EventArgs e)
{
if (sender is not Entry entry || entry.Handler is not EntryHandler handler)
return;

#if ANDROID
handler.PlatformView.KeyPress += (_, args) =>
{
if (args.Event is { KeyCode: Keycode.Enter or Keycode.NumpadEnter, Action: KeyEventActions.Up })
{
entry.ReturnCommand?.Execute(entry.ReturnCommandParameter);
args.Handled = true;
}

args.Handled = false;
};
#endif
}

private void PerformNavBarAnimations()
{
try
Expand Down Expand Up @@ -465,7 +487,6 @@ private void TryToggleBookmarked()
{
if (_browsingDatabase.IsBookmark(Browser.Location, out var bookmark))
{

_browsingDatabase.Bookmarks.Remove(bookmark);
Browser.SimulateLocationChanged(); // force buttons to update

Expand All @@ -481,7 +502,7 @@ private void TryToggleBookmarked()
Url = Browser.Location.ToString()
});
Browser.SimulateLocationChanged(); // force buttons to update

_logger.LogInformation("Set bookmarked location {URI}", bookmark.Url);

this.ShowToast(Text.MainPage_TryToggleBookmarked_Bookmark_added, ToastDuration.Short);
Expand All @@ -495,7 +516,8 @@ private void TryToggleBookmarked()

private double GetExpandedMenuHeight()
{
return ExpandableMenu.Sum(e => (double.IsNaN(e.MinimumHeight) ? 0 : e.MinimumHeight) + e.Margin.VerticalThickness);
return ExpandableMenu.Sum(e =>
(double.IsNaN(e.MinimumHeight) ? 0 : e.MinimumHeight) + e.Margin.VerticalThickness);
}

private void AddMenuAnimations()
Expand Down

0 comments on commit 6515d92

Please sign in to comment.