-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure DevDiagnostics' owned windows honor theme changes #3790
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
546f420
Ensure DevDiagnostics' owned windows honor theme changes
andreww-msft 4ad0371
Merge branch 'main' into user/andreww/ToolWindow-themes
andreww-msft be362d3
Fix whitespace
timkur 4c12757
Merge branch 'main' into user/andreww/ToolWindow-themes
andreww-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
183 changes: 183 additions & 0 deletions
183
tools/DevDiagnostics/DevHome.DevDiagnostics/Controls/ThemeAwareWindow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.UI; | ||
using Microsoft.UI.Dispatching; | ||
using Microsoft.UI.Windowing; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Media; | ||
using Windows.UI; | ||
using Windows.UI.ViewManagement; | ||
using WinUIEx; | ||
|
||
namespace DevHome.DevDiagnostics.Controls; | ||
|
||
/* This class encapsulates the theme-awareness logic, including support for custom chrome buttons. | ||
* The main BarWindow derives from ThemeAwareWindow. All internal tool windows and any other owned windows | ||
* must follow the theme of the main BarWindow. To achieve this: | ||
* 1. Derive the tool window from ThemeAwareWindow. | ||
* 2. Define a grid or panel for the icon and title (because this window sets ExtendsContentIntoTitleBar | ||
* and HideIconAndSystemMenu). | ||
* 3. Add the tool window to the list of related windows for the BarWindow by calling AddRelatedWindow. | ||
* 4. Remove the tool window from the list of related windows for the BarWindow when the tool window is closed. | ||
* 5. See ClipboardMonitoringWindow for an example. | ||
*/ | ||
|
||
public class ThemeAwareWindow : WindowEx | ||
{ | ||
private readonly SolidColorBrush _darkModeActiveCaptionBrush; | ||
private readonly SolidColorBrush _darkModeInactiveCaptionBrush; | ||
private readonly SolidColorBrush _nonDarkModeActiveCaptionBrush; | ||
private readonly SolidColorBrush _nonDarkModeInactiveCaptionBrush; | ||
private readonly UISettings _uiSettings = new(); | ||
private readonly DispatcherQueue _dispatcher; | ||
|
||
private WindowActivationState _currentActivationState = WindowActivationState.Deactivated; | ||
|
||
private List<ThemeAwareWindow> RelatedWindows { get; set; } = []; | ||
|
||
internal List<Button> CustomTitleBarButtons { get; private set; } = []; | ||
|
||
internal ElementTheme Theme { get; set; } | ||
|
||
public ThemeAwareWindow() | ||
{ | ||
_dispatcher = DispatcherQueue.GetForCurrentThread(); | ||
|
||
// Precreate the brushes for the caption buttons. | ||
// In Dark Mode, the active state is white, and the inactive state is translucent white. | ||
// In Light Mode, the active state is black, and the inactive state is translucent black. | ||
var color = Colors.White; | ||
_darkModeActiveCaptionBrush = new SolidColorBrush(color); | ||
color.A = 0x66; | ||
_darkModeInactiveCaptionBrush = new SolidColorBrush(color); | ||
|
||
color = Colors.Black; | ||
_nonDarkModeActiveCaptionBrush = new SolidColorBrush(color); | ||
color.A = 0x66; | ||
_nonDarkModeInactiveCaptionBrush = new SolidColorBrush(color); | ||
|
||
ExtendsContentIntoTitleBar = true; | ||
AppWindow.TitleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu; | ||
|
||
Closed += ThemeAwareWindow_Closed; | ||
Activated += Window_Activated; | ||
_uiSettings.ColorValuesChanged += UiSettings_ColorValuesChanged; | ||
} | ||
|
||
// Invoked when the user changes system-wide personalization settings. | ||
private void UiSettings_ColorValuesChanged(UISettings sender, object args) | ||
{ | ||
_dispatcher.TryEnqueue(ApplySystemThemeToCaptionButtons); | ||
} | ||
|
||
private void ThemeAwareWindow_Closed(object sender, WindowEventArgs args) | ||
{ | ||
Activated -= Window_Activated; | ||
_uiSettings.ColorValuesChanged -= UiSettings_ColorValuesChanged; | ||
} | ||
|
||
internal void Window_Activated(object sender, WindowActivatedEventArgs args) | ||
{ | ||
// This follows the design guidance of dimming our title bar elements when the window isn't activated. | ||
// https://learn.microsoft.com/en-us/windows/apps/develop/title-bar#dim-the-title-bar-when-the-window-is-inactive | ||
_currentActivationState = args.WindowActivationState; | ||
|
||
if (CustomTitleBarButtons.Count > 0) | ||
{ | ||
UpdateCustomTitleBarButtonsTextColor(); | ||
} | ||
} | ||
|
||
// Invoked when the user changes theme preference in this app's settings. | ||
internal void SetRequestedTheme(ElementTheme theme) | ||
{ | ||
Theme = theme; | ||
|
||
if (Content is FrameworkElement rootElement) | ||
{ | ||
rootElement.RequestedTheme = theme; | ||
|
||
if (theme == ElementTheme.Dark) | ||
{ | ||
SetCaptionButtonColors(Colors.White); | ||
} | ||
else if (theme == ElementTheme.Light) | ||
{ | ||
SetCaptionButtonColors(Colors.Black); | ||
} | ||
else | ||
{ | ||
ApplySystemThemeToCaptionButtons(); | ||
} | ||
} | ||
|
||
foreach (var window in RelatedWindows) | ||
{ | ||
window.SetRequestedTheme(theme); | ||
} | ||
} | ||
|
||
// AppWindow.TitleBar doesn't update caption button colors correctly when changed while app is running. | ||
// https://task.ms/44172495 | ||
jaholme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
internal void ApplySystemThemeToCaptionButtons() | ||
{ | ||
if (Content is FrameworkElement rootElement) | ||
{ | ||
Color foregroundColor; | ||
if (rootElement.ActualTheme == ElementTheme.Dark) | ||
{ | ||
foregroundColor = Colors.White; | ||
} | ||
else | ||
{ | ||
foregroundColor = Colors.Black; | ||
} | ||
|
||
SetCaptionButtonColors(foregroundColor); | ||
} | ||
} | ||
|
||
internal void SetCaptionButtonColors(Color color) | ||
{ | ||
AppWindow.TitleBar.ButtonForegroundColor = color; | ||
if (CustomTitleBarButtons.Count > 0) | ||
{ | ||
UpdateCustomTitleBarButtonsTextColor(); | ||
} | ||
} | ||
|
||
internal void UpdateCustomTitleBarButtonsTextColor() | ||
{ | ||
var rootElement = Content as FrameworkElement; | ||
Debug.Assert(rootElement != null, "Expected Content to be a FrameworkElement"); | ||
|
||
foreach (var button in CustomTitleBarButtons) | ||
{ | ||
if (_currentActivationState == WindowActivationState.Deactivated) | ||
{ | ||
var brush = (rootElement.ActualTheme == ElementTheme.Dark) ? _darkModeInactiveCaptionBrush : _nonDarkModeInactiveCaptionBrush; | ||
button.Foreground = brush; | ||
} | ||
else | ||
{ | ||
var brush = (rootElement.ActualTheme == ElementTheme.Dark) ? _darkModeActiveCaptionBrush : _nonDarkModeActiveCaptionBrush; | ||
button.Foreground = brush; | ||
} | ||
} | ||
} | ||
|
||
internal void AddRelatedWindow(ThemeAwareWindow window) | ||
{ | ||
RelatedWindows.Add(window); | ||
window.SetRequestedTheme(Theme); | ||
} | ||
|
||
internal void RemoveRelatedWindow(ThemeAwareWindow window) | ||
{ | ||
RelatedWindows.Remove(window); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of these seem like candidates for code reusability. Maybe the base class should do this in some manner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue here is that both the main BarWindow and the tool windows derive from the same base class. You could argue that I should separate the common theme behavior from the related-window behavior - and I wouldn't disagree, but we can't derive a window class from multiple classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could hardcode the base class to recognize BarWindow, since it is only "one" whereas the others are dynamically "many". Also, you could look into Generics and include the BarWindow class as a type argument to do something similar but less hardcoded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I see this PR as temporary. It fixes the issues, but I will update it, incorporating this feedback, once we move to WAS 1.6, per new issue: #3803