mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +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>
262 lines
8.5 KiB
C#
262 lines
8.5 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 CommunityToolkit.WinUI;
|
|
using ManagedCommon;
|
|
using Microsoft.CmdPal.UI.Helpers;
|
|
using Microsoft.CmdPal.UI.ViewModels;
|
|
using Microsoft.CmdPal.UI.ViewModels.Services;
|
|
using Microsoft.UI;
|
|
using Microsoft.UI.Dispatching;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Media;
|
|
using Microsoft.UI.Xaml.Media.Imaging;
|
|
using Windows.UI.ViewManagement;
|
|
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
|
|
|
|
namespace Microsoft.CmdPal.UI.Services;
|
|
|
|
/// <summary>
|
|
/// ThemeService is a hub that translates user settings and system preferences into concrete
|
|
/// theme resources and notifies listeners of changes.
|
|
/// </summary>
|
|
internal sealed partial class ThemeService : IThemeService, IDisposable
|
|
{
|
|
private static readonly TimeSpan ReloadDebounceInterval = TimeSpan.FromMilliseconds(500);
|
|
|
|
private readonly UISettings _uiSettings;
|
|
private readonly SettingsModel _settings;
|
|
private readonly ResourceSwapper _resourceSwapper;
|
|
private readonly NormalThemeProvider _normalThemeProvider;
|
|
private readonly ColorfulThemeProvider _colorfulThemeProvider;
|
|
|
|
private DispatcherQueue? _dispatcherQueue;
|
|
private DispatcherQueueTimer? _dispatcherQueueTimer;
|
|
private bool _isInitialized;
|
|
private bool _disposed;
|
|
private InternalThemeState _currentState;
|
|
|
|
public event EventHandler<ThemeChangedEventArgs>? ThemeChanged;
|
|
|
|
public ThemeSnapshot Current => Volatile.Read(ref _currentState).Snapshot;
|
|
|
|
/// <summary>
|
|
/// Initializes the theme service. Must be called after the application window is activated and on UI thread.
|
|
/// </summary>
|
|
public void Initialize()
|
|
{
|
|
if (_isInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
|
if (_dispatcherQueue is null)
|
|
{
|
|
throw new InvalidOperationException("Failed to get DispatcherQueue for the current thread. Ensure Initialize is called on the UI thread after window activation.");
|
|
}
|
|
|
|
_dispatcherQueueTimer = _dispatcherQueue.CreateTimer();
|
|
|
|
_resourceSwapper.Initialize();
|
|
_isInitialized = true;
|
|
Reload();
|
|
}
|
|
|
|
private void Reload()
|
|
{
|
|
if (!_isInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// provider selection
|
|
var intensity = Math.Clamp(_settings.CustomThemeColorIntensity, 0, 100);
|
|
IThemeProvider provider = intensity > 0 && _settings.ColorizationMode is ColorizationMode.CustomColor or ColorizationMode.WindowsAccentColor or ColorizationMode.Image
|
|
? _colorfulThemeProvider
|
|
: _normalThemeProvider;
|
|
|
|
// Calculate values
|
|
var tint = _settings.ColorizationMode switch
|
|
{
|
|
ColorizationMode.CustomColor => _settings.CustomThemeColor,
|
|
ColorizationMode.WindowsAccentColor => _uiSettings.GetColorValue(UIColorType.Accent),
|
|
ColorizationMode.Image => _settings.CustomThemeColor,
|
|
_ => Colors.Transparent,
|
|
};
|
|
var effectiveTheme = GetElementTheme((ElementTheme)_settings.Theme);
|
|
var imageSource = _settings.ColorizationMode == ColorizationMode.Image
|
|
? LoadImageSafe(_settings.BackgroundImagePath)
|
|
: null;
|
|
var stretch = _settings.BackgroundImageFit switch
|
|
{
|
|
BackgroundImageFit.Fill => Stretch.Fill,
|
|
_ => Stretch.UniformToFill,
|
|
};
|
|
var opacity = Math.Clamp(_settings.BackgroundImageOpacity, 0, 100) / 100.0;
|
|
|
|
// create context and offload to actual theme provider
|
|
var context = new ThemeContext
|
|
{
|
|
Tint = tint,
|
|
ColorIntensity = intensity,
|
|
Theme = effectiveTheme,
|
|
BackgroundImageSource = imageSource,
|
|
BackgroundImageStretch = stretch,
|
|
BackgroundImageOpacity = opacity,
|
|
};
|
|
var backdrop = provider.GetAcrylicBackdrop(context);
|
|
var blur = _settings.BackgroundImageBlurAmount;
|
|
var brightness = _settings.BackgroundImageBrightness;
|
|
|
|
// Create public snapshot (no provider!)
|
|
var snapshot = new ThemeSnapshot
|
|
{
|
|
Tint = tint,
|
|
TintIntensity = intensity / 100f,
|
|
Theme = effectiveTheme,
|
|
BackgroundImageSource = imageSource,
|
|
BackgroundImageStretch = stretch,
|
|
BackgroundImageOpacity = opacity,
|
|
BackdropParameters = backdrop,
|
|
BlurAmount = blur,
|
|
BackgroundBrightness = brightness / 100f,
|
|
};
|
|
|
|
// Bundle with provider for internal use
|
|
var newState = new InternalThemeState
|
|
{
|
|
Snapshot = snapshot,
|
|
Provider = provider,
|
|
};
|
|
|
|
// Atomic swap
|
|
Interlocked.Exchange(ref _currentState, newState);
|
|
|
|
_resourceSwapper.TryActivateTheme(provider.ThemeKey);
|
|
ThemeChanged?.Invoke(this, new ThemeChangedEventArgs());
|
|
}
|
|
|
|
private static BitmapImage? LoadImageSafe(string? path)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
// If it looks like a file path and exists, prefer absolute file URI
|
|
if (!Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out var uri))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!uri.IsAbsoluteUri && File.Exists(path))
|
|
{
|
|
uri = new Uri(Path.GetFullPath(path));
|
|
}
|
|
|
|
return new BitmapImage(uri);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogWarning($"Failed to load background image '{path}'. {ex.Message}");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ThemeService(SettingsModel settings, ResourceSwapper resourceSwapper)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(settings);
|
|
ArgumentNullException.ThrowIfNull(resourceSwapper);
|
|
|
|
_settings = settings;
|
|
_settings.SettingsChanged += SettingsOnSettingsChanged;
|
|
|
|
_resourceSwapper = resourceSwapper;
|
|
|
|
_uiSettings = new UISettings();
|
|
_uiSettings.ColorValuesChanged += UiSettings_ColorValuesChanged;
|
|
|
|
_normalThemeProvider = new NormalThemeProvider(_uiSettings);
|
|
_colorfulThemeProvider = new ColorfulThemeProvider(_uiSettings);
|
|
List<IThemeProvider> providers = [_normalThemeProvider, _colorfulThemeProvider];
|
|
|
|
foreach (var provider in providers)
|
|
{
|
|
_resourceSwapper.RegisterTheme(provider.ThemeKey, provider.ResourcePath);
|
|
}
|
|
|
|
_currentState = new InternalThemeState
|
|
{
|
|
Snapshot = new ThemeSnapshot
|
|
{
|
|
Tint = Colors.Transparent,
|
|
Theme = ElementTheme.Light,
|
|
BackdropParameters = new AcrylicBackdropParameters(Colors.Black, Colors.Black, 0.5f, 0.5f),
|
|
BackgroundImageOpacity = 1,
|
|
BackgroundImageSource = null,
|
|
BackgroundImageStretch = Stretch.Fill,
|
|
BlurAmount = 0,
|
|
TintIntensity = 1.0f,
|
|
BackgroundBrightness = 0,
|
|
},
|
|
Provider = _normalThemeProvider,
|
|
};
|
|
}
|
|
|
|
private void RequestReload()
|
|
{
|
|
if (!_isInitialized || _dispatcherQueueTimer is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_dispatcherQueueTimer.Debounce(Reload, ReloadDebounceInterval);
|
|
}
|
|
|
|
private ElementTheme GetElementTheme(ElementTheme theme)
|
|
{
|
|
return theme switch
|
|
{
|
|
ElementTheme.Light => ElementTheme.Light,
|
|
ElementTheme.Dark => ElementTheme.Dark,
|
|
_ => _uiSettings.GetColorValue(UIColorType.Background).CalculateBrightness() < 0.5
|
|
? ElementTheme.Dark
|
|
: ElementTheme.Light,
|
|
};
|
|
}
|
|
|
|
private void SettingsOnSettingsChanged(SettingsModel sender, object? args)
|
|
{
|
|
RequestReload();
|
|
}
|
|
|
|
private void UiSettings_ColorValuesChanged(UISettings sender, object args)
|
|
{
|
|
RequestReload();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_disposed = true;
|
|
_dispatcherQueueTimer?.Stop();
|
|
_uiSettings.ColorValuesChanged -= UiSettings_ColorValuesChanged;
|
|
_settings.SettingsChanged -= SettingsOnSettingsChanged;
|
|
}
|
|
|
|
private sealed class InternalThemeState
|
|
{
|
|
public required ThemeSnapshot Snapshot { get; init; }
|
|
|
|
public required IThemeProvider Provider { get; init; }
|
|
}
|
|
}
|