mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
120 lines
3.6 KiB
C#
120 lines
3.6 KiB
C#
|
|
// 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.Collections.ObjectModel;
|
||
|
|
using CommunityToolkit.Mvvm.Messaging;
|
||
|
|
using Microsoft.CmdPal.UI.Pages;
|
||
|
|
using Microsoft.CmdPal.UI.ViewModels;
|
||
|
|
using Microsoft.UI.Windowing;
|
||
|
|
using Microsoft.UI.Xaml;
|
||
|
|
using Microsoft.UI.Xaml.Controls;
|
||
|
|
using Windows.Graphics;
|
||
|
|
using RS_ = Microsoft.CmdPal.UI.Helpers.ResourceLoaderInstance;
|
||
|
|
|
||
|
|
namespace Microsoft.CmdPal.UI.Settings;
|
||
|
|
|
||
|
|
public sealed partial class SettingsWindow : Window,
|
||
|
|
IRecipient<NavigateToExtensionSettingsMessage>
|
||
|
|
{
|
||
|
|
public ObservableCollection<Crumb> BreadCrumbs { get; } = [];
|
||
|
|
|
||
|
|
public SettingsWindow()
|
||
|
|
{
|
||
|
|
this.InitializeComponent();
|
||
|
|
this.ExtendsContentIntoTitleBar = true;
|
||
|
|
this.AppWindow.SetIcon("ms-appx:///Assets/Icons/StoreLogo.png");
|
||
|
|
this.AppWindow.Title = RS_.GetString("SettingsWindowTitle");
|
||
|
|
this.AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
|
||
|
|
PositionCentered();
|
||
|
|
WeakReferenceMessenger.Default.Register<NavigateToExtensionSettingsMessage>(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void NavView_Loaded(object sender, RoutedEventArgs e)
|
||
|
|
{
|
||
|
|
NavView.SelectedItem = NavView.MenuItems[0];
|
||
|
|
Navigate("General");
|
||
|
|
}
|
||
|
|
|
||
|
|
private void NavView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
|
||
|
|
{
|
||
|
|
var selectedItem = args.InvokedItem;
|
||
|
|
|
||
|
|
if (selectedItem is not null)
|
||
|
|
{
|
||
|
|
Navigate(selectedItem.ToString()!);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Navigate(string page)
|
||
|
|
{
|
||
|
|
var pageType = page switch
|
||
|
|
{
|
||
|
|
"General" => typeof(GeneralPage),
|
||
|
|
"Extensions" => typeof(ExtensionsPage),
|
||
|
|
_ => null,
|
||
|
|
};
|
||
|
|
if (pageType is not null)
|
||
|
|
{
|
||
|
|
BreadCrumbs.Clear();
|
||
|
|
BreadCrumbs.Add(new(page, page));
|
||
|
|
NavFrame.Navigate(pageType);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Navigate(ProviderSettingsViewModel extension)
|
||
|
|
{
|
||
|
|
NavFrame.Navigate(typeof(ExtensionPage), extension);
|
||
|
|
BreadCrumbs.Add(new(extension.DisplayName, string.Empty));
|
||
|
|
}
|
||
|
|
|
||
|
|
private void PositionCentered()
|
||
|
|
{
|
||
|
|
AppWindow.Resize(new SizeInt32 { Width = 1280, Height = 720 });
|
||
|
|
var displayArea = DisplayArea.GetFromWindowId(AppWindow.Id, DisplayAreaFallback.Nearest);
|
||
|
|
if (displayArea is not null)
|
||
|
|
{
|
||
|
|
var centeredPosition = AppWindow.Position;
|
||
|
|
centeredPosition.X = (displayArea.WorkArea.Width - AppWindow.Size.Width) / 2;
|
||
|
|
centeredPosition.Y = (displayArea.WorkArea.Height - AppWindow.Size.Height) / 2;
|
||
|
|
AppWindow.Move(centeredPosition);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Receive(NavigateToExtensionSettingsMessage message) => Navigate(message.ProviderSettingsVM);
|
||
|
|
|
||
|
|
private void NavigationBreadcrumbBar_ItemClicked(BreadcrumbBar sender, BreadcrumbBarItemClickedEventArgs args)
|
||
|
|
{
|
||
|
|
if (args.Item is Crumb crumb)
|
||
|
|
{
|
||
|
|
if (crumb.Data is string data)
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrEmpty(data))
|
||
|
|
{
|
||
|
|
Navigate(data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Window_Activated(object sender, WindowActivatedEventArgs args)
|
||
|
|
{
|
||
|
|
WeakReferenceMessenger.Default.Send<WindowActivatedEventArgs>(args);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public readonly struct Crumb
|
||
|
|
{
|
||
|
|
public Crumb(string label, object data)
|
||
|
|
{
|
||
|
|
Label = label;
|
||
|
|
Data = data;
|
||
|
|
}
|
||
|
|
|
||
|
|
public string Label { get; }
|
||
|
|
|
||
|
|
public object Data { get; }
|
||
|
|
|
||
|
|
public override string ToString() => Label;
|
||
|
|
}
|