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;
|
2021-06-29 18:06:12 +08:00
|
|
|
|
using System.IO;
|
2020-04-08 01:19:14 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Helpers;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Services;
|
2022-04-20 04:00:28 +08:00
|
|
|
|
using Microsoft.UI.Xaml.Controls;
|
|
|
|
|
using Microsoft.UI.Xaml.Input;
|
|
|
|
|
using Microsoft.UI.Xaml.Navigation;
|
2020-04-08 01:19:14 +08:00
|
|
|
|
using Windows.System;
|
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;
|
2022-04-20 04:00:28 +08:00
|
|
|
|
private NavigationView navigationView;
|
|
|
|
|
private NavigationViewItem selected;
|
2020-04-08 01:19:14 +08:00
|
|
|
|
private ICommand loadedCommand;
|
|
|
|
|
private ICommand itemInvokedCommand;
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public bool IsBackEnabled
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
get { return isBackEnabled; }
|
|
|
|
|
set { Set(ref isBackEnabled, value); }
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 18:06:12 +08:00
|
|
|
|
public bool IsVideoConferenceBuild
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-07-28 22:26:06 +08:00
|
|
|
|
var mfHandle = NativeMethods.LoadLibrary("mf.dll");
|
2022-04-20 04:00:28 +08:00
|
|
|
|
bool mfAvailable = mfHandle != IntPtr.Zero;
|
2021-07-23 21:59:22 +08:00
|
|
|
|
if (mfAvailable)
|
|
|
|
|
{
|
2021-07-28 22:26:06 +08:00
|
|
|
|
NativeMethods.FreeLibrary(mfHandle);
|
2021-07-23 21:59:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 17:50:46 +08:00
|
|
|
|
return this != null && File.Exists("modules/VideoConference/PowerToys.VideoConferenceModule.dll") && mfAvailable;
|
2021-06-29 18:06:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
public NavigationViewItem Selected
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
get { return selected; }
|
|
|
|
|
set { Set(ref selected, value); }
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-11 06:22:07 +08:00
|
|
|
|
public ICommand LoadedCommand => loadedCommand ?? (loadedCommand = new RelayCommand(OnLoaded));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
public ICommand ItemInvokedCommand => itemInvokedCommand ?? (itemInvokedCommand = new RelayCommand<NavigationViewItemInvokedEventArgs>(OnItemInvoked));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
|
|
|
|
|
public ShellViewModel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
public void Initialize(Frame frame, NavigationView navigationView, IList<KeyboardAccelerator> keyboardAccelerators)
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
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-11 06:22:07 +08:00
|
|
|
|
NavigationService.NavigationFailed += Frame_NavigationFailed;
|
|
|
|
|
NavigationService.Navigated += Frame_Navigated;
|
|
|
|
|
this.navigationView.BackRequested += OnBackRequested;
|
2020-04-08 01:19:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-11 06:22:07 +08:00
|
|
|
|
keyboardAccelerators.Add(altLeftKeyboardAccelerator);
|
|
|
|
|
keyboardAccelerators.Add(backKeyboardAccelerator);
|
2020-10-30 05:24:16 +08:00
|
|
|
|
await Task.CompletedTask.ConfigureAwait(false);
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
private void OnItemInvoked(NavigationViewItemInvokedEventArgs args)
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
var item = navigationView.MenuItems
|
2022-04-20 04:00:28 +08:00
|
|
|
|
.OfType<NavigationViewItem>()
|
2020-03-12 01:43:32 +08:00
|
|
|
|
.First(menuItem => (string)menuItem.Content == (string)args.InvokedItem);
|
|
|
|
|
var pageType = item.GetValue(NavHelper.NavigateToProperty) as Type;
|
|
|
|
|
NavigationService.Navigate(pageType);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
private void OnBackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
|
|
|
|
NavigationService.GoBack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
throw e.Exception;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Frame_Navigated(object sender, NavigationEventArgs e)
|
|
|
|
|
{
|
2020-04-11 06:22:07 +08:00
|
|
|
|
IsBackEnabled = NavigationService.CanGoBack;
|
|
|
|
|
Selected = navigationView.MenuItems
|
2022-04-20 04:00:28 +08:00
|
|
|
|
.OfType<NavigationViewItem>()
|
2020-04-11 06:22:07 +08:00
|
|
|
|
.FirstOrDefault(menuItem => IsMenuItemForPageType(menuItem, e.SourcePageType));
|
2020-03-12 01:43:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 04:00:28 +08:00
|
|
|
|
private static bool IsMenuItemForPageType(NavigationViewItem menuItem, Type sourcePageType)
|
2020-03-12 01:43:32 +08:00
|
|
|
|
{
|
|
|
|
|
var pageType = menuItem.GetValue(NavHelper.NavigateToProperty) as Type;
|
|
|
|
|
return pageType == sourcePageType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|