2020-04-08 01:19:14 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Helpers;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Services;
|
|
|
|
|
using Windows.System;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
using Windows.UI.Xaml.Input;
|
|
|
|
|
using Windows.UI.Xaml.Navigation;
|
|
|
|
|
using WinUI = Microsoft.UI.Xaml.Controls;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class ShellViewModel : Observable
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
private readonly KeyboardAccelerator altLeftKeyboardAccelerator = BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu);
|
2020-03-25 10:55:02 +08:00
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
private readonly KeyboardAccelerator backKeyboardAccelerator = BuildKeyboardAccelerator(VirtualKey.GoBack);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
private bool isBackEnabled;
|
|
|
|
|
private IList<KeyboardAccelerator> keyboardAccelerators;
|
|
|
|
|
private WinUI.NavigationView navigationView;
|
|
|
|
|
private WinUI.NavigationViewItem selected;
|
|
|
|
|
private ICommand loadedCommand;
|
|
|
|
|
private ICommand itemInvokedCommand;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public bool IsBackEnabled
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
get { return this.isBackEnabled; }
|
|
|
|
|
set { this.Set(ref this.isBackEnabled, value); }
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WinUI.NavigationViewItem Selected
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
get { return this.selected; }
|
|
|
|
|
set { this.Set(ref this.selected, value); }
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
public ICommand LoadedCommand => this.loadedCommand ?? (this.loadedCommand = new RelayCommand(this.OnLoaded));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
2020-04-08 01:19:14 +08:00
|
|
|
|
public ICommand ItemInvokedCommand => this.itemInvokedCommand ?? (this.itemInvokedCommand = new RelayCommand<WinUI.NavigationViewItemInvokedEventArgs>(this.OnItemInvoked));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public ShellViewModel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize(Frame frame, WinUI.NavigationView navigationView, IList<KeyboardAccelerator> keyboardAccelerators)
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
this.navigationView = navigationView;
|
|
|
|
|
this.keyboardAccelerators = keyboardAccelerators;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
NavigationService.Frame = frame;
|
2020-04-08 01:19:14 +08:00
|
|
|
|
NavigationService.NavigationFailed += this.Frame_NavigationFailed;
|
|
|
|
|
NavigationService.Navigated += this.Frame_Navigated;
|
|
|
|
|
this.navigationView.BackRequested += this.OnBackRequested;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static KeyboardAccelerator BuildKeyboardAccelerator(VirtualKey key, VirtualKeyModifiers? modifiers = null)
|
|
|
|
|
{
|
|
|
|
|
var keyboardAccelerator = new KeyboardAccelerator() { Key = key };
|
|
|
|
|
if (modifiers.HasValue)
|
|
|
|
|
{
|
|
|
|
|
keyboardAccelerator.Modifiers = modifiers.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyboardAccelerator.Invoked += OnKeyboardAcceleratorInvoked;
|
|
|
|
|
return keyboardAccelerator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnKeyboardAcceleratorInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
var result = NavigationService.GoBack();
|
|
|
|
|
args.Handled = result;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void OnLoaded()
|
|
|
|
|
{
|
|
|
|
|
// Keyboard accelerators are added here to avoid showing 'Alt + left' tooltip on the page.
|
|
|
|
|
// More info on tracking issue https://github.com/Microsoft/microsoft-ui-xaml/issues/8
|
2020-04-08 01:19:14 +08:00
|
|
|
|
this.keyboardAccelerators.Add(this.altLeftKeyboardAccelerator);
|
|
|
|
|
this.keyboardAccelerators.Add(this.backKeyboardAccelerator);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
await Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnItemInvoked(WinUI.NavigationViewItemInvokedEventArgs args)
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
var item = this.navigationView.MenuItems
|
2020-03-12 01:43:32 +08:00
|
|
|
|
.OfType<WinUI.NavigationViewItem>()
|
|
|
|
|
.First(menuItem => (string)menuItem.Content == (string)args.InvokedItem);
|
|
|
|
|
var pageType = item.GetValue(NavHelper.NavigateToProperty) as Type;
|
|
|
|
|
NavigationService.Navigate(pageType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBackRequested(WinUI.NavigationView sender, WinUI.NavigationViewBackRequestedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
NavigationService.GoBack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
throw e.Exception;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Frame_Navigated(object sender, NavigationEventArgs e)
|
|
|
|
|
{
|
2020-04-08 01:19:14 +08:00
|
|
|
|
this.IsBackEnabled = NavigationService.CanGoBack;
|
|
|
|
|
this.Selected = this.navigationView.MenuItems
|
2020-03-12 01:43:32 +08:00
|
|
|
|
.OfType<WinUI.NavigationViewItem>()
|
2020-04-08 01:19:14 +08:00
|
|
|
|
.FirstOrDefault(menuItem => this.IsMenuItemForPageType(menuItem, e.SourcePageType));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsMenuItemForPageType(WinUI.NavigationViewItem menuItem, Type sourcePageType)
|
|
|
|
|
{
|
|
|
|
|
var pageType = menuItem.GetValue(NavHelper.NavigateToProperty) as Type;
|
|
|
|
|
return pageType == sourcePageType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|