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

Support mouse and forward navigation #2164

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions common/Services/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ string DefaultPage
bool NavigateTo(string pageKey, object? parameter = null, bool clearNavigation = false);

bool GoBack();

bool GoForward();
}
20 changes: 20 additions & 0 deletions src/Services/NavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public string DefaultPage
[MemberNotNullWhen(true, nameof(Frame), nameof(_frame))]
public bool CanGoBack => Frame != null && Frame.CanGoBack;

[MemberNotNullWhen(true, nameof(Frame), nameof(_frame))]
public bool CanGoForward => Frame != null && Frame.CanGoForward;

public NavigationService(IPageService pageService)
{
_pageService = pageService;
Expand Down Expand Up @@ -93,6 +96,23 @@ public bool GoBack()
return false;
}

public bool GoForward()
{
if (CanGoForward)
{
var vmBeforeNavigation = _frame.GetPageViewModel();
_frame.GoForward();
if (vmBeforeNavigation is INavigationAware navigationAware)
{
navigationAware.OnNavigatedFrom();
}

return true;
}

return false;
}

public bool NavigateTo(string pageKey, object? parameter = null, bool clearNavigation = false)
{
var pageType = _pageService.GetPageType(pageKey);
Expand Down
32 changes: 29 additions & 3 deletions src/Views/ShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public ShellPage(ShellViewModel viewModel)
App.MainWindow.Activated += MainWindow_Activated;

ActualThemeChanged += OnActualThemeChanged;

PointerPressed += OnPointerPressed;
}

private async void OnLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
Expand All @@ -46,6 +48,8 @@ private async void OnLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)

KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoBack));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Right, VirtualKeyModifiers.Menu));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoForward));

await ViewModel.OnLoaded();
}
Expand Down Expand Up @@ -89,11 +93,33 @@ private static KeyboardAccelerator BuildKeyboardAccelerator(VirtualKey key, Virt

private static void OnKeyboardAcceleratorInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
var navigationService = Application.Current.GetService<INavigationService>();
if ((sender.Key == VirtualKey.GoBack) ||
((sender.Key == VirtualKey.Left) && sender.Modifiers.HasFlag(VirtualKeyModifiers.Menu)))
{
args.Handled = Application.Current.GetService<INavigationService>().GoBack();
}
else if ((sender.Key == VirtualKey.GoForward) ||
((sender.Key == VirtualKey.Right) && sender.Modifiers.HasFlag(VirtualKeyModifiers.Menu)))
{
args.Handled = Application.Current.GetService<INavigationService>().GoForward();
}
}

private static void OnPointerPressed(object sender, PointerRoutedEventArgs args)
{
var handled = false;

var result = navigationService.GoBack();
// Handle mouse forward and back navigation.
if (args.GetCurrentPoint(null).Properties.IsXButton1Pressed)
{
handled = Application.Current.GetService<INavigationService>().GoBack();
}
else if (args.GetCurrentPoint(null).Properties.IsXButton2Pressed)
{
handled = Application.Current.GetService<INavigationService>().GoForward();
}

args.Handled = result;
args.Handled = handled;
}

public static readonly DependencyProperty ExperimentalFeatureProperty = DependencyProperty.Register(
Expand Down
Loading