mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
This PR introduces user settings for app mode themes (dark, light, or
system) and background customization options, including custom colors,
system accent colors, or custom images.
- Adds a new page to the Settings window with new appearance settings
and moves some existing settings there as well.
- Introduces a new core-level service abstraction, `IThemeService`, that
holds the state for the current theme.
- Uses the helper class `ResourceSwapper` to update application-level
XAML resources. The way WinUI / XAML handles these is painful, and XAML
Hot Reload is pain². Initialization must be lazy, as XAML resources can
only be accessed after the window is activated.
- `ThemeService` takes app and system settings and selects one of the
registered `IThemeProvider`s to calculate visuals and choose the
appropriate XAML resources.
- At the moment, there are two:
- `NormalThemeProvider`
- Provides the current uncolorized light and dark styles
- `ms-appx:///Styles/Theme.Normal.xaml`
- `ColorfulThemeProvider`
- Style that matches the Windows 11 visual style (based on the Start
menu) and colors
- `ms-appx:///Styles/Theme.Colorful.xaml`
- Applied when the background is colorized or a background image is
selected
- The app theme is applied only on the main window
(`WindowThemeSynchronizer` helper class can be used to synchronize other
windows if needed).
- Adds a new dependency on `Microsoft.Graphics.Win2D`.
- Adds a custom color picker popup; the one from the Community Toolkit
occasionally loses the selected color.
- Flyby: separates the keyword tag and localizable label for pages in
the Settings window navigation.
## Pictures? Pictures!
<img width="2027" height="1276" alt="image"
src="https://github.com/user-attachments/assets/e3485c71-7faa-495b-b455-b313ea6046ee"
/>
<img width="3776" height="2025" alt="image"
src="https://github.com/user-attachments/assets/820fa823-34d4-426d-b066-b1049dc3266f"
/>
Matching Windows accent color and tint:
<img width="3840" height="2160" alt="image"
src="https://github.com/user-attachments/assets/65f3b608-e282-4894-b7c8-e014a194f11f"
/>
<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
- [x] Closes: #38444
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx
<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments
<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
---------
Co-authored-by: Niels Laute <niels.laute@live.nl>
292 lines
9.7 KiB
C#
292 lines
9.7 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 ManagedCommon;
|
|
using Microsoft.CmdPal.UI.Helpers;
|
|
using Microsoft.CmdPal.UI.Messages;
|
|
using Microsoft.CmdPal.UI.ViewModels;
|
|
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
|
using Microsoft.UI.Input;
|
|
using Microsoft.UI.Windowing;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Input;
|
|
using Microsoft.UI.Xaml.Navigation;
|
|
using Windows.System;
|
|
using Windows.UI.Core;
|
|
using WinUIEx;
|
|
using RS_ = Microsoft.CmdPal.UI.Helpers.ResourceLoaderInstance;
|
|
using TitleBar = Microsoft.UI.Xaml.Controls.TitleBar;
|
|
|
|
namespace Microsoft.CmdPal.UI.Settings;
|
|
|
|
public sealed partial class SettingsWindow : WindowEx,
|
|
IDisposable,
|
|
IRecipient<NavigateToExtensionSettingsMessage>,
|
|
IRecipient<QuitMessage>
|
|
{
|
|
private readonly LocalKeyboardListener _localKeyboardListener;
|
|
|
|
public ObservableCollection<Crumb> BreadCrumbs { get; } = [];
|
|
|
|
// Gets or sets optional action invoked after NavigationView is loaded.
|
|
public Action NavigationViewLoaded { get; set; } = () => { };
|
|
|
|
public SettingsWindow()
|
|
{
|
|
this.InitializeComponent();
|
|
this.ExtendsContentIntoTitleBar = true;
|
|
this.SetIcon();
|
|
var title = RS_.GetString("SettingsWindowTitle");
|
|
this.AppWindow.Title = title;
|
|
this.AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
|
|
this.AppTitleBar.Title = title;
|
|
PositionCentered();
|
|
|
|
WeakReferenceMessenger.Default.Register<NavigateToExtensionSettingsMessage>(this);
|
|
WeakReferenceMessenger.Default.Register<QuitMessage>(this);
|
|
|
|
_localKeyboardListener = new LocalKeyboardListener();
|
|
_localKeyboardListener.KeyPressed += LocalKeyboardListener_OnKeyPressed;
|
|
_localKeyboardListener.Start();
|
|
Closed += SettingsWindow_Closed;
|
|
RootElement.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(RootElement_OnPointerPressed), true);
|
|
}
|
|
|
|
private void SettingsWindow_Closed(object sender, WindowEventArgs args)
|
|
{
|
|
Dispose();
|
|
}
|
|
|
|
// Handles NavigationView loaded event.
|
|
// Sets up initial navigation and accessibility notifications.
|
|
private void NavView_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
// Delay necessary to ensure NavigationView visual state can match navigation
|
|
Task.Delay(500).ContinueWith(_ => this.NavigationViewLoaded?.Invoke(), TaskScheduler.FromCurrentSynchronizationContext());
|
|
|
|
NavView.SelectedItem = NavView.MenuItems[0];
|
|
Navigate("General");
|
|
|
|
if (sender is NavigationView navigationView)
|
|
{
|
|
// Register for pane open/close changes to announce to screen readers
|
|
navigationView.RegisterPropertyChangedCallback(NavigationView.IsPaneOpenProperty, AnnounceNavigationPaneStateChanged);
|
|
}
|
|
}
|
|
|
|
// Announces navigation pane open/close state to screen readers for accessibility.
|
|
private void AnnounceNavigationPaneStateChanged(DependencyObject sender, DependencyProperty dp)
|
|
{
|
|
if (sender is NavigationView navigationView)
|
|
{
|
|
UIHelper.AnnounceActionForAccessibility(
|
|
ue: (UIElement)sender,
|
|
(sender as NavigationView)?.IsPaneOpen == true ? RS_.GetString("NavigationPaneOpened") : RS_.GetString("NavigationPaneClosed"),
|
|
"NavigationViewPaneIsOpenChangeNotificationId");
|
|
}
|
|
}
|
|
|
|
private void NavView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
|
|
{
|
|
var selectedItem = args.InvokedItemContainer;
|
|
Navigate((selectedItem.Tag as string)!);
|
|
}
|
|
|
|
private void Navigate(string page)
|
|
{
|
|
var pageType = page switch
|
|
{
|
|
"General" => typeof(GeneralPage),
|
|
"Appearance" => typeof(AppearancePage),
|
|
"Extensions" => typeof(ExtensionsPage),
|
|
_ => null,
|
|
};
|
|
if (pageType is not null)
|
|
{
|
|
NavFrame.Navigate(pageType);
|
|
}
|
|
}
|
|
|
|
private void Navigate(ProviderSettingsViewModel extension)
|
|
{
|
|
NavFrame.Navigate(typeof(ExtensionPage), extension);
|
|
}
|
|
|
|
private void PositionCentered()
|
|
{
|
|
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, Microsoft.UI.Xaml.WindowActivatedEventArgs args)
|
|
{
|
|
WeakReferenceMessenger.Default.Send<Microsoft.UI.Xaml.WindowActivatedEventArgs>(args);
|
|
}
|
|
|
|
private void Window_Closed(object sender, WindowEventArgs args)
|
|
{
|
|
WeakReferenceMessenger.Default.Send<SettingsWindowClosedMessage>();
|
|
|
|
WeakReferenceMessenger.Default.UnregisterAll(this);
|
|
}
|
|
|
|
private void NavView_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)
|
|
{
|
|
if (args.DisplayMode is NavigationViewDisplayMode.Compact or NavigationViewDisplayMode.Minimal)
|
|
{
|
|
AppTitleBar.IsPaneToggleButtonVisible = true;
|
|
WorkAroundIcon.Margin = new Thickness(8, 0, 16, 0); // Required for workaround, see XAML comment
|
|
}
|
|
else
|
|
{
|
|
AppTitleBar.IsPaneToggleButtonVisible = false;
|
|
WorkAroundIcon.Margin = new Thickness(16, 0, 8, 0); // Required for workaround, see XAML comment
|
|
}
|
|
}
|
|
|
|
public void Receive(QuitMessage message)
|
|
{
|
|
// This might come in on a background thread
|
|
DispatcherQueue.TryEnqueue(() => Close());
|
|
}
|
|
|
|
private void AppTitleBar_PaneToggleRequested(TitleBar sender, object args)
|
|
{
|
|
NavView.IsPaneOpen = !NavView.IsPaneOpen;
|
|
}
|
|
|
|
private void TryGoBack()
|
|
{
|
|
if (NavFrame.CanGoBack)
|
|
{
|
|
NavFrame.GoBack();
|
|
}
|
|
}
|
|
|
|
private void TitleBar_BackRequested(TitleBar sender, object args)
|
|
{
|
|
TryGoBack();
|
|
}
|
|
|
|
private void LocalKeyboardListener_OnKeyPressed(object? sender, LocalKeyboardListenerKeyPressedEventArgs e)
|
|
{
|
|
switch (e.Key)
|
|
{
|
|
case VirtualKey.GoBack:
|
|
case VirtualKey.XButton1:
|
|
TryGoBack();
|
|
break;
|
|
|
|
case VirtualKey.Left:
|
|
var altPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);
|
|
if (altPressed)
|
|
{
|
|
TryGoBack();
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void RootElement_OnPointerPressed(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse)
|
|
{
|
|
var ptrPt = e.GetCurrentPoint(RootElement);
|
|
if (ptrPt.Properties.IsXButton1Pressed)
|
|
{
|
|
TryGoBack();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError("Error handling mouse button press event", ex);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_localKeyboardListener?.Dispose();
|
|
}
|
|
|
|
private void NavFrame_OnNavigated(object sender, NavigationEventArgs e)
|
|
{
|
|
BreadCrumbs.Clear();
|
|
|
|
if (e.SourcePageType == typeof(GeneralPage))
|
|
{
|
|
NavView.SelectedItem = GeneralPageNavItem;
|
|
var pageType = RS_.GetString("Settings_PageTitles_GeneralPage");
|
|
BreadCrumbs.Add(new(pageType, pageType));
|
|
}
|
|
else if (e.SourcePageType == typeof(AppearancePage))
|
|
{
|
|
NavView.SelectedItem = AppearancePageNavItem;
|
|
var pageType = RS_.GetString("Settings_PageTitles_AppearancePage");
|
|
BreadCrumbs.Add(new(pageType, pageType));
|
|
}
|
|
else if (e.SourcePageType == typeof(ExtensionsPage))
|
|
{
|
|
NavView.SelectedItem = ExtensionPageNavItem;
|
|
var pageType = RS_.GetString("Settings_PageTitles_ExtensionsPage");
|
|
BreadCrumbs.Add(new(pageType, pageType));
|
|
}
|
|
else if (e.SourcePageType == typeof(ExtensionPage) && e.Parameter is ProviderSettingsViewModel vm)
|
|
{
|
|
NavView.SelectedItem = ExtensionPageNavItem;
|
|
var extensionsPageType = RS_.GetString("Settings_PageTitles_ExtensionsPage");
|
|
BreadCrumbs.Add(new(extensionsPageType, extensionsPageType));
|
|
BreadCrumbs.Add(new(vm.DisplayName, vm));
|
|
}
|
|
else
|
|
{
|
|
BreadCrumbs.Add(new($"[{e.SourcePageType?.Name}]", string.Empty));
|
|
Logger.LogError($"Unknown breadcrumb for page type '{e.SourcePageType}'");
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|