Skip to content

Commit

Permalink
Fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelwgn committed Sep 27, 2023
1 parent 7b9a6ec commit 5a1a2cc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion WinUIGallery/ControlPages/TitleBarPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

<TextBlock> Foreground Color</TextBlock>
<SplitButton x:Name="myFgColorButton" AutomationProperties.Name="Foreground color" Padding="0" MinHeight="0" MinWidth="0" VerticalAlignment="Top">
<Border x:Name="ForegroundColorElement" Width="{StaticResource SwatchSize}" Height="{StaticResource SwatchSize}" Background="Black" Margin="0" CornerRadius="4,0,0,4"/>
<Border x:Name="ForegroundColorElement" Width="{StaticResource SwatchSize}" Height="{StaticResource SwatchSize}" Background="Transparent" Margin="0" CornerRadius="4,0,0,4"/>
<SplitButton.Flyout>
<Flyout Placement="Auto">
<GridView ItemClick="FgGridView_ItemClick" IsItemClickEnabled="True">
Expand Down
26 changes: 25 additions & 1 deletion WinUIGallery/ControlPages/TitleBarPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace AppUIBasics.ControlPages
public sealed partial class TitleBarPage : Page
{
private Windows.UI.Color currentBgColor = Colors.Transparent;
private Windows.UI.Color currentFgColor = Colors.Black;
private Windows.UI.Color currentFgColor = Colors.Transparent;

public TitleBarPage()
{
Expand All @@ -45,14 +45,18 @@ public TitleBarPage()
private void SetTitleBar(UIElement titlebar)
{
var window = WindowHelper.GetWindowForElement(this as UIElement);
var titleBarElement = UIHelper.FindElementByName(this, "AppTitleBar");

if (!window.ExtendsContentIntoTitleBar)
{
titleBarElement.Visibility = Visibility.Visible;
window.ExtendsContentIntoTitleBar = true;
window.SetTitleBar(titlebar);
TitleBarHelper.SetCaptionButtonBackgroundColors(window, Colors.Transparent);
}
else
{
titleBarElement.Visibility = Visibility.Collapsed;
window.ExtendsContentIntoTitleBar = false;
window.SetTitleBar(null);
TitleBarHelper.SetCaptionButtonBackgroundColors(window, null);
Expand All @@ -64,6 +68,7 @@ private void SetTitleBar(UIElement titlebar)
public void UpdateButtonText()
{
var window = WindowHelper.GetWindowForElement(this as UIElement);

if (window.ExtendsContentIntoTitleBar)
{
customTitleBar.Content = "Reset to system TitleBar";
Expand Down Expand Up @@ -109,9 +114,28 @@ public void UpdateTitleBarColor()
{
var window = WindowHelper.GetWindowForElement(this);
var titleBarElement = UIHelper.FindElementByName(this, "AppTitleBar");
var titleBarAppNameElement = UIHelper.FindElementByName(this, "AppTitle");

(titleBarElement as Border).Background = new SolidColorBrush(currentBgColor); // changing titlebar uielement's color
if(currentFgColor != Colors.Transparent)
{
(titleBarAppNameElement as TextBlock).Foreground = new SolidColorBrush(currentFgColor);
}
else
{
(titleBarAppNameElement as TextBlock).Foreground = Application.Current.Resources["TextFillColorPrimaryBrush"] as SolidColorBrush;
}
TitleBarHelper.SetCaptionButtonColors(window, currentFgColor);
if(currentBgColor == Colors.Transparent)
{
// If the current background is null, we want to revert to the default titlebar which is achieved using null as color
TitleBarHelper.SetBackgroundColor(window, null);
}
else
{
TitleBarHelper.SetBackgroundColor(window, currentBgColor);
}
TitleBarHelper.SetForegroundColor(window, currentFgColor);
}

private void customTitleBar_Click(object sender, RoutedEventArgs e)
Expand Down
14 changes: 14 additions & 0 deletions WinUIGallery/Helper/TitleBarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,19 @@ public static void SetCaptionButtonBackgroundColors(Window window, Windows.UI.Co
titleBar.ButtonBackgroundColor = color;
triggerTitleBarRepaint(window);
}

public static void SetForegroundColor(Window window, Windows.UI.Color? color)
{
var titleBar = window.AppWindow.TitleBar;
titleBar.ForegroundColor = color;
triggerTitleBarRepaint(window);
}

public static void SetBackgroundColor(Window window, Windows.UI.Color? color)
{
var titleBar = window.AppWindow.TitleBar;
titleBar.BackgroundColor = color;
triggerTitleBarRepaint(window);
}
}
}

0 comments on commit 5a1a2cc

Please sign in to comment.