Skip to content

Commit

Permalink
[Windows] fixed a bug where title bar's background color was misalign…
Browse files Browse the repository at this point in the history
…ed (#1299)
  • Loading branch information
veler authored Jul 17, 2024
1 parent b071d39 commit a9de87b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
<Grid Margin="-1">
<AdornerDecorator>
<Grid Margin="{TemplateBinding MarginMaximized}">
<Border
x:Name="WindowTopBorder"
VerticalAlignment="Top"
Height="48"/>

<ContentPresenter />

<controls:OverlayControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
using DevToys.Windows.Core;
using DevToys.Windows.Core.Helpers;
using DevToys.Windows.Native;
using Microsoft.Win32;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Dwm;
using Windows.Win32.UI.Controls;
using Windows.Win32.UI.WindowsAndMessaging;
using Application = System.Windows.Application;
using Button = System.Windows.Controls.Button;
using Color = System.Windows.Media.Color;
Expand All @@ -34,6 +34,7 @@ public abstract partial class MicaWindowWithOverlay : Window
private Button? _maximizeButton;
private Button? _minimizeButton;
private StackPanel? _windowStateButtonsStackPanel;
private Border? _windowTopBorder;
private bool _isMouseButtonDownOnDraggableTitleBarArea;
private Point _mouseDownPositionOnDraggableTitleBarArea;

Expand Down Expand Up @@ -124,6 +125,7 @@ public override void OnApplyTemplate()
_maximizeButton = (Button)Template.FindName("MaximizeButton", this);
var draggableTitleBarArea = (Border)Template.FindName("DraggableTitleBarArea", this);
var overlayControl = (OverlayControl)Template.FindName("TitleBar", this);
_windowTopBorder = (Border)Template.FindName("WindowTopBorder", this);

_windowStateButtonsStackPanel.SizeChanged += WindowStateButtonsStackPanel_SizeChanged;
closeButton.Click += CloseButton_Click;
Expand Down Expand Up @@ -483,6 +485,16 @@ private void UpdateTheme()
dictionaries.Remove(resourceDictionaryToRemove);
dictionaries.Add(resourceDictionary);
UpdateLayout();

Guard.IsNotNull(_windowTopBorder);
if (IsAccentColorOnTitleBarEnabled())
{
_windowTopBorder.Background = SystemParameters.WindowGlassBrush;
}
else
{
_windowTopBorder.Background = null;
}
}

private nint ShowSnapLayout(nint lParam, ref bool handled)
Expand Down Expand Up @@ -560,4 +572,22 @@ private static void OnForbidMinimizeAndMaximizePropertyChangedCallback(Dependenc
NativeMethods.EnableMinimizeAndMaximizeCapabilities(windowHandle);
}
}

public static bool IsAccentColorOnTitleBarEnabled()
{
const string key = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\DWM";
const string name = "ColorPrevalence";

object? value = Registry.GetValue(key, name, null);
if (value is null)
{
// Key doesn't exist, assume default (OFF)
return false;
}
else
{
// If the key value is 1, the setting is ON. Otherwise, it's OFF.
return ((int)value == 1);
}
}
}

0 comments on commit a9de87b

Please sign in to comment.