Skip to content

Commit

Permalink
Fix nav buttons not using accent color
Browse files Browse the repository at this point in the history
Adapt to disable titlebar color during color change
  • Loading branch information
Mike Corsaro authored and PureWeen committed Sep 26, 2023
1 parent 5829ec0 commit fe425cf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
33 changes: 31 additions & 2 deletions src/Core/src/Platform/Windows/MauiWinUIWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using System.Runtime.InteropServices;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Devices;
using Microsoft.Maui.LifecycleEvents;
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Windows.Graphics;
using ViewManagement = Windows.UI.ViewManagement;

namespace Microsoft.Maui
{
Expand All @@ -21,10 +22,12 @@ public class MauiWinUIWindow : UI.Xaml.Window, IPlatformSizeRestrictedWindow
IntPtr _windowIcon;
bool _enableResumeEvent;
bool _isActivated;
ViewManagement.UISettings _viewSettings;

public MauiWinUIWindow()
{
_windowManager = WindowMessageManager.Get(this);
_viewSettings = new ViewManagement.UISettings();

Activated += OnActivated;
Closed += OnClosedPrivate;
Expand All @@ -34,7 +37,16 @@ public MauiWinUIWindow()
// set to false we know the user toggled this to false
// and then we can react accordingly
if (AppWindowTitleBar.IsCustomizationSupported())
{
base.AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
_viewSettings.ColorValuesChanged += _viewSettings_ColorValuesChanged;
SetTileBarButtonColors();
}

if (MicaController.IsSupported())
{
base.SystemBackdrop = new MicaBackdrop() { Kind = MicaKind.Base };
}

SubClassingWin32();
SetIcon();
Expand Down Expand Up @@ -69,6 +81,8 @@ private void OnClosedPrivate(object sender, UI.Xaml.WindowEventArgs args)
{
OnClosed(sender, args);

_viewSettings.ColorValuesChanged -= _viewSettings_ColorValuesChanged;

if (_windowIcon != IntPtr.Zero)
{
DestroyIcon(_windowIcon);
Expand Down Expand Up @@ -179,6 +193,21 @@ void SetIcon()
}
}

private void _viewSettings_ColorValuesChanged(ViewManagement.UISettings sender, object args)
{
DispatcherQueue.TryEnqueue(SetTileBarButtonColors);
}

private void SetTileBarButtonColors()
{
if (AppWindowTitleBar.IsCustomizationSupported())
{
base.AppWindow.TitleBar.ButtonBackgroundColor = Colors.Transparent;
base.AppWindow.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
base.AppWindow.TitleBar.ButtonForegroundColor = _viewSettings.GetColorValue(ViewManagement.UIColorType.Foreground);
}
}

SizeInt32 IPlatformSizeRestrictedWindow.MinimumSize { get; set; } = DefaultMinimumSize;

SizeInt32 IPlatformSizeRestrictedWindow.MaximumSize { get; set; } = DefaultMaximumSize;
Expand Down
10 changes: 0 additions & 10 deletions src/Core/src/Platform/Windows/Styles/WindowRootViewStyle.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui.Platform">

<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="TitleBarBackgroundColor" Color="#FF000000" />
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="TitleBarBackgroundColor" Color="#FFFFFFFF" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

<maui:DefaultOrUserDataTemplateSelector
UserTemplateName="MauiAppTitleBarTemplate"
DefaultTemplateName="MauiAppTitleBarTemplateDefault"
Expand All @@ -26,7 +17,6 @@
<Border
Canvas.ZIndex="1"
VerticalAlignment="Stretch"
Background="{ThemeResource TitleBarBackgroundColor}"
Margin="0,0,0,0">
<StackPanel Orientation="Horizontal" Margin="12, 0, 0, 0" x:Name="RootStackPanel">
<Image
Expand Down
25 changes: 13 additions & 12 deletions src/Core/src/Platform/Windows/WindowRootView.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
using Microsoft.Win32;
using Windows.Foundation;
using WThickness = Microsoft.UI.Xaml.Thickness;
using ViewManagement = Windows.UI.ViewManagement;
using Microsoft.UI.Windowing;
using WThickness = Microsoft.UI.Xaml.Thickness;

namespace Microsoft.Maui.Platform
{
Expand Down Expand Up @@ -243,17 +240,21 @@ void ApplyTitlebarColorPrevalence()
{
try
{
// Figure out if the "show accent color on title bars" setting is enabled
using var dwmSubKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\DWM\");
var enableAccentColor = dwmSubKey?.GetValue("ColorPrevalence");
if (enableAccentColor != null && int.TryParse(enableAccentColor.ToString(), out var enableValue))
if (enableAccentColor != null &&
int.TryParse(enableAccentColor.ToString(), out var enableValue) &&
_appTitleBar is Border border)
{
if (enableValue == 1 && _appTitleBar is Border border)
{
// `ColorValuesChanged` doesn't fire on the UI thread
DispatcherQueue.TryEnqueue(() => {
border.Background = new SolidColorBrush(_viewSettings.GetColorValue(ViewManagement.UIColorType.Accent));
});
}
DispatcherQueue.TryEnqueue(() => {
border.Background = enableValue == 1 ?
new SolidColorBrush(_viewSettings.GetColorValue(ViewManagement.UIColorType.Accent)) :
new SolidColorBrush(UI.Colors.Transparent);

if (NavigationViewControl != null && NavigationViewControl.ButtonHolderGrid != null)
NavigationViewControl.ButtonHolderGrid.Background = border.Background;
});
}
}
catch (Exception) { }
Expand Down

0 comments on commit fe425cf

Please sign in to comment.