Skip to content

Added ThemeGenerator script for Fluent and generated combined Fluent theme resource dictionaries #9422

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ static ThemeManager()
// TODO : Temprorary way of checking if setting Fluent theme enabled flag. Provide a property for theme switch.
if (Application.Current != null)
{
string dictionarySource;
foreach (ResourceDictionary mergedDictionary in Application.Current.Resources.MergedDictionaries)
{
if (mergedDictionary.Source != null && mergedDictionary.Source.ToString().EndsWith("Fluent.xaml"))
if (mergedDictionary.Source != null)
{
dictionarySource = mergedDictionary.Source.ToString();

if (dictionarySource.EndsWith("Fluent.Light.xaml", StringComparison.OrdinalIgnoreCase)
|| dictionarySource.EndsWith("Fluent.Dark.xaml", StringComparison.OrdinalIgnoreCase)
|| dictionarySource.EndsWith("Fluent.HC.xaml", StringComparison.OrdinalIgnoreCase)
|| dictionarySource.EndsWith("Fluent.xaml", StringComparison.OrdinalIgnoreCase))

_isFluentThemeEnabled = true;
break;
}
Expand All @@ -40,8 +48,8 @@ internal static void InitializeFluentTheme()
_currentApplicationTheme = GetSystemTheme();
_currentUseLightMode = IsSystemThemeLight();

var themeColorResourceUri = GetFluentWindowThemeColorResourceUri(_currentApplicationTheme, _currentUseLightMode);
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = themeColorResourceUri });
var themeColorResourceUri = GetFluentThemeResourceUri(_currentApplicationTheme, _currentUseLightMode);
AddOrUpdateThemeResources(themeColorResourceUri);

_isFluentThemeInitialized = true;
}
Expand Down Expand Up @@ -103,7 +111,7 @@ private static void ApplyTheme(
requestedAccentColor != _currentSystemAccentColor)
{

Uri dictionaryUri = GetFluentWindowThemeColorResourceUri(requestedTheme, requestedUseLightMode);
Uri dictionaryUri = GetFluentThemeResourceUri(requestedTheme, requestedUseLightMode);
AddOrUpdateThemeResources(dictionaryUri);

foreach(Window window in windows)
Expand Down Expand Up @@ -191,11 +199,11 @@ private static void AddOrUpdateThemeResources(Uri dictionaryUri)

var newDictionary = new ResourceDictionary() { Source = dictionaryUri };

FindFluentThemeAndColorDictionary(out ResourceDictionary fluentDictionary, out ResourceDictionary colorDictionary);
FindFluentThemeResourceDictionary(out ResourceDictionary fluentDictionary);

if (colorDictionary != null)
if (fluentDictionary != null)
{
Application.Current.Resources.MergedDictionaries.Remove(colorDictionary);
Application.Current.Resources.MergedDictionaries.Remove(fluentDictionary);
}

Application.Current.Resources.MergedDictionaries.Add(newDictionary);
Expand All @@ -215,43 +223,37 @@ private static void AddOrUpdateThemeResources(Uri dictionaryUri)

#region Private Methods

private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool useLightMode)
private static Uri GetFluentThemeResourceUri(string systemTheme, bool useLightMode)
{
string themeColorFileName = useLightMode ? "light.xaml" : "dark.xaml";
string themeFileName = "Fluent." + (useLightMode ? "Light" : "Dark") + ".xaml";

if(SystemParameters.HighContrast)
{
themeColorFileName = "hc.xaml";
themeFileName = "Fluent.HC.xaml";
}

return new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/" + themeColorFileName, UriKind.Absolute);
}
return new Uri("pack://application:,,,/PresentationFramework.Fluent;component/Themes/" + themeFileName, UriKind.Absolute);
}

private static void FindFluentThemeAndColorDictionary(out ResourceDictionary fluentThemeDictionary, out ResourceDictionary fluentColorDictionary)
private static void FindFluentThemeResourceDictionary(out ResourceDictionary fluentDictionary)
{
fluentThemeDictionary = null;
fluentColorDictionary = null;
fluentDictionary = null;

if (Application.Current == null) return;

foreach (ResourceDictionary mergedDictionary in Application.Current.Resources.MergedDictionaries)
{
if (mergedDictionary.Source != null)
{
if (mergedDictionary.Source.ToString() == fluentResourceDictionaryUri)
{
fluentThemeDictionary = mergedDictionary;
}
else if (mergedDictionary.Source.ToString().StartsWith(fluentColorResourceUriPart))
if (mergedDictionary.Source.ToString().Contains(fluentThemeResoruceDictionaryUri))
{
fluentColorDictionary = mergedDictionary;
fluentDictionary = mergedDictionary;
break;
}
}
}
}



#endregion

#region Private Members
Expand All @@ -260,8 +262,7 @@ private static void FindFluentThemeAndColorDictionary(out ResourceDictionary flu

private static readonly string _regPersonalizeKeyPath = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";

private static readonly string fluentResourceDictionaryUri = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fluent.xaml";
private static readonly string fluentColorResourceUriPart = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/";
private static readonly string fluentThemeResoruceDictionaryUri = "pack://application:,,,/PresentationFramework.Fluent;component/Themes/";

private static string _currentApplicationTheme;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[CmdletBinding(PositionalBinding=$false)]
Param(
[string][Alias('c')]$themeColor = "Light"
)

$currentDir = Get-Location
$fluentThemeDir = Join-Path $currentDir "..\PresentationFramework.Fluent\"

$outFilePath = Join-Path $fluentThemeDir "Themes\Fluent.$themeColor.xaml"

$styleFilesDir = Join-Path $fluentThemeDir "Styles"
$resouceFilesDir = Join-Path $fluentThemeDir "Resources"
$themeColorFilePath = Join-Path $resouceFilesDir "Theme\$themeColor.xaml"

[xml]$combinedXaml = '<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
xmlns:fluentcontrols="clr-namespace:Fluent.Controls"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:ui="clr-namespace:System.Windows.Documents;assembly=PresentationUI"
xmlns:theme="clr-namespace:Microsoft.Windows.Themes"
xmlns:base="clr-namespace:System.Windows;assembly=WindowsBase">
</ResourceDictionary>'

foreach ($file in Get-ChildItem $resouceFilesDir -Filter "*.xaml") {
if($file.BaseName -eq "Fluent") {
continue
}
[xml]$currentXaml = Get-Content $file

$combinedXaml.ResourceDictionary.InnerXml += $currentXaml.ResourceDictionary.InnerXml
}

[xml]$themeColorXaml = Get-Content $themeColorFilePath
$combinedXaml.ResourceDictionary.InnerXml += $themeColorXaml.ResourceDictionary.InnerXml

foreach ($file in Get-ChildItem $styleFilesDir -Filter "*.xaml") {
[xml]$currentXaml = Get-Content $file

$combinedXaml.ResourceDictionary.InnerXml += $currentXaml.ResourceDictionary.InnerXml
}

([xml]$combinedXaml).Save($outFilePath)
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<Thickness x:Key="ComboBoxEditableTextPadding">10,0,30,0</Thickness>

<system:Double x:Key="ComboBoxMinHeight">24</system:Double>
<Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness>
<!-- <Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness> -->
<system:Double x:Key="NavigationViewItemOnLeftMinHeight">32</system:Double>

</ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=System.Runtime">

<system:String x:Key="CaretUpGlyph">&#xEDDB;</system:String>
<system:String x:Key="CaretDownGlyph">&#xEDDC;</system:String>


<!-- HINT: Day button style -->
<Style x:Key="DefaultCalendarDayButtonStyle" TargetType="CalendarDayButton">
<Setter Property="MinWidth" Value="30" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Fluent.Controls"
xmlns:fluentcontrols="clr-namespace:Fluent.Controls"
xmlns:system="clr-namespace:System;assembly=System.Runtime">


Expand All @@ -37,8 +37,8 @@
<Color x:Key="ControlPressedColor">#FF211AA9</Color>
<Color x:Key="GlyphColor">#FF444444</Color>

<system:String x:Key="CheckBoxCheckedGlyph">&#xE73E;</system:String>
<system:String x:Key="CheckBoxIndeterminateGlyph">&#xE9AE;</system:String>
<system:String x:Key="DataGridCheckBoxCheckedGlyph">&#xE73E;</system:String>
<system:String x:Key="DataGridCheckBoxIndeterminateGlyph">&#xE9AE;</system:String>

<LinearGradientBrush x:Key="MenuPopupBrush" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="{DynamicResource ControlLightColor}" />
Expand Down Expand Up @@ -636,16 +636,16 @@
</Style.Triggers>
</Style>

<controls:FallbackBrushConverter x:Key="FallbackBrushConverter" />
<fluentcontrols:FallbackBrushConverter x:Key="FallbackBrushConverter" />

<Color x:Key="FallbackColor">#FFFF0000</Color>
<!-- <Color x:Key="FallbackColor">#FFFF0000</Color> -->

<Thickness x:Key="CheckBoxPadding">11,5,11,6</Thickness>
<Thickness x:Key="CheckBoxBorderThemeThickness">1</Thickness>
<Thickness x:Key="CheckBoxContentMargin">8,0,0,0</Thickness>
<system:Double x:Key="CheckBoxIconSize">14</system:Double>
<system:Double x:Key="CheckBoxHeight">22</system:Double>
<system:Double x:Key="CheckBoxWidth">22</system:Double>
<Thickness x:Key="DataGridCheckBoxPadding">11,5,11,6</Thickness>
<Thickness x:Key="DataGridCheckBoxBorderThemeThickness">1</Thickness>
<Thickness x:Key="DataGridCheckBoxContentMargin">8,0,0,0</Thickness>
<system:Double x:Key="DataGridCheckBoxIconSize">14</system:Double>
<system:Double x:Key="DataGridCheckBoxHeight">22</system:Double>
<system:Double x:Key="DataGridCheckBoxWidth">22</system:Double>

<Style x:Key="DataGridCheckBoxElementDefaultStyle" TargetType="{x:Type CheckBox}">
<!-- Universal WPF UI focus -->
Expand All @@ -658,8 +658,8 @@
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource CheckBoxBorderThemeThickness}" />
<Setter Property="Padding" Value="{StaticResource CheckBoxPadding}" />
<Setter Property="BorderThickness" Value="{StaticResource DataGridCheckBoxBorderThemeThickness}" />
<Setter Property="Padding" Value="{StaticResource DataGridCheckBoxPadding}" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
Expand All @@ -684,8 +684,8 @@
<BulletDecorator.Bullet>
<Border
x:Name="ControlBorderIconPresenter"
Width="{StaticResource CheckBoxHeight}"
Height="{StaticResource CheckBoxWidth}"
Width="{StaticResource DataGridCheckBoxHeight}"
Height="{StaticResource DataGridCheckBoxWidth}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="{TemplateBinding Background}"
Expand All @@ -698,10 +698,10 @@
Margin="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="{StaticResource CheckBoxIconSize}"
FontSize="{StaticResource DataGridCheckBoxIconSize}"
FontWeight="Bold"
FontFamily="{DynamicResource SegoeFluentIcons}"
Text="{StaticResource CheckBoxCheckedGlyph}"
Text="{StaticResource DataGridCheckBoxCheckedGlyph}"
Visibility="Collapsed">
<TextBlock.Foreground>
<SolidColorBrush Color="{DynamicResource TextOnAccentFillColorPrimary}" />
Expand All @@ -712,7 +712,7 @@
</BulletDecorator.Bullet>
<ContentPresenter
x:Name="ContentPresenter"
Margin="{StaticResource CheckBoxContentMargin}"
Margin="{StaticResource DataGridCheckBoxContentMargin}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True" />
Expand All @@ -725,7 +725,7 @@
<Setter TargetName="ContentPresenter" Property="Margin" Value="0" />
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="ControlIcon" Property="Text" Value="{StaticResource CheckBoxIndeterminateGlyph}" />
<Setter TargetName="ControlIcon" Property="Text" Value="{StaticResource DataGridCheckBoxIndeterminateGlyph}" />
<Setter TargetName="ControlIcon" Property="Visibility" Value="Visible" />
<Setter TargetName="ControlBorderIconPresenter" Property="Background">
<Setter.Value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:controls="clr-namespace:Fluent.Controls"
xmlns:fluentcontrols="clr-namespace:Fluent.Controls"
>

<Thickness x:Key="ExpanderPadding">11,11,11,11</Thickness>
<Thickness x:Key="ExpanderBorderThemeThickness">1</Thickness>
<system:Double x:Key="ExpanderChevronSize">12.0</system:Double>
<controls:AnimationFactorToValueConverter x:Key="AnimationFactorToValueConverter" />
<fluentcontrols:AnimationFactorToValueConverter x:Key="AnimationFactorToValueConverter" />
<system:String x:Key="ExpanderChevronUpGlyph">&#xE70E;</system:String>
<system:String x:Key="ExpanderChevronDownGlyph">&#xE70D;</system:String>
<system:String x:Key="ExpanderChevronLeftGlyph">&#xE76B;</system:String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<sys:Double x:Key="LineButtonHeight">12</sys:Double>
<sys:Double x:Key="LineButtonWidth">12</sys:Double>

<system:String x:Key="CaretUpGlyph">&#xEDDB;</system:String>
<system:String x:Key="CaretDownGlyph">&#xEDDC;</system:String>
<system:String x:Key="CaretLeftGlyph">&#xEDD9;</system:String>
<system:String x:Key="CaretRightGlyph">&#xEDDA;</system:String>
<system:String x:Key="ScrollBarCaretUpGlyph">&#xEDDB;</system:String>
<system:String x:Key="ScrollBarCaretDownGlyph">&#xEDDC;</system:String>
<system:String x:Key="ScrollBarCaretLeftGlyph">&#xEDD9;</system:String>
<system:String x:Key="ScrollBarCaretRightGlyph">&#xEDDA;</system:String>

<Style x:Key="ScrollBarLineButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="Foreground" Value="{DynamicResource ScrollBarButtonArrowForeground}" />
Expand Down Expand Up @@ -137,7 +137,7 @@
Grid.Row="0"
HorizontalContentAlignment="Left"
Command="ScrollBar.LineUpCommand"
Content="{StaticResource CaretUpGlyph}"
Content="{StaticResource ScrollBarCaretUpGlyph}"
Opacity="0"
AutomationProperties.Name="Scroll Up"
Style="{StaticResource ScrollBarLineButtonStyle}" />
Expand Down Expand Up @@ -167,7 +167,7 @@
Grid.Row="2"
HorizontalContentAlignment="Left"
Command="ScrollBar.LineDownCommand"
Content="{StaticResource CaretDownGlyph}"
Content="{StaticResource ScrollBarCaretDownGlyph}"
Opacity="0"
AutomationProperties.Name="Scroll Down"
Style="{StaticResource ScrollBarLineButtonStyle}" />
Expand Down Expand Up @@ -259,7 +259,7 @@
Grid.Column="0"
VerticalAlignment="Center"
Command="ScrollBar.LineLeftCommand"
Content="{StaticResource CaretLeftGlyph}"
Content="{StaticResource ScrollBarCaretLeftGlyph}"
Opacity="0"
AutomationProperties.Name="Scroll Left"
Style="{StaticResource ScrollBarLineButtonStyle}" />
Expand Down Expand Up @@ -287,7 +287,7 @@
Grid.Column="2"
VerticalAlignment="Center"
Command="ScrollBar.LineRightCommand"
Content="{StaticResource CaretRightGlyph}"
Content="{StaticResource ScrollBarCaretRightGlyph}"
Opacity="0"
AutomationProperties.Name="Scroll Right"
Style="{StaticResource ScrollBarLineButtonStyle}" />
Expand Down
Loading
Loading