8000 Implemented Application.ThemeMode and Window.ThemeMode properties by dipeshmsft · Pull Request #9436 · dotnet/wpf · GitHub
[go: up one dir, main page]

Skip to content

Implemented Application.ThemeMode and Window.ThemeMode properties #9436

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 15 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added caching of app theme state to avoid multiple reloads on same th…
…eme change
  • Loading branch information
dipeshmsft committed Jul 22, 2024
commit a865345fb29fcc41c1451a4ac761bb429a8d4a46
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@
<Compile Include="System\Windows\TemplateVisualStateAttribute.cs" />
<Compile Include="System\Windows\TextPanelProperties.cs" />
<Compile Include="System\Windows\ThemeManager3.cs" />
<Compile Include="System\Windows\FluentThemeState.cs" />
<Compile Include="System\Windows\ThemeMode.cs" />
<Compile Include="System\Windows\ThemeDictionaryExtension.cs" />
<Compile Include="System\Windows\ThemeInfoAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using System.Security;
using MS.Internal;
using MS.Utility;
Expand Down Expand Up @@ -35,9 +36,9 @@ public FluentThemeState(string themeName, bool useLightColors)
public bool UseLightColors => _useLightColors;
public Color AccentColor => _accentColor;

private string _themeName;
private bool _useLightColors;
private Color _accentColor;
private readonly string _themeName;
private readonly bool _useLightColors;
private readonly Color _accentColor;


public bool Equals(FluentThemeState other)
Expand All @@ -47,6 +48,11 @@ public bool Equals(FluentThemeState other)
AccentColor == other.AccentColor;
}

public override bool Equals(object obj)
{
return obj is FluentThemeState other && Equals(other);
}

public static bool operator ==(FluentThemeState left, FluentThemeState right)
{
return left.Equals(right);
Expand All @@ -57,6 +63,12 @@ public bool Equals(FluentThemeState other)
return !left.Equals(right);
}

public override int GetHashCode()
{
return StringComparer.Ordinal.GetHashCode(ToString());
}


public override string ToString()
{
return $"ThemeName: {ThemeName}, UseLightColors: {UseLightColors}, AccentColor: {AccentColor}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ internal static void OnSystemThemeChanged()

bool useLightColors = GetUseLightColors(Application.Current.ThemeMode);
var fluentThemeResourceUri = GetFluentThemeResourceUri(useLightColors);

FluentThemeState newFluentThemeState = new FluentThemeState(Application.Current.ThemeMode.Value, useLightColors);

if(_currentFluentThemeState == newFluentThemeState)
{
return;
}

AddOrUpdateThemeResources(Application.Current.Resources, fluentThemeResourceUri);

foreach(Window window in Application.Current.Windows)
Expand All @@ -38,6 +46,7 @@ internal static void OnSystemThemeChanged()
}
}

_currentFluentThemeState = newFluentThemeState;
IgnoreAppResourcesChange = false;
}
else
Expand Down Expand Up @@ -69,6 +78,7 @@ internal static void OnApplicationThemeChanged(ThemeMode oldThemeMode, ThemeMode
if(oldThemeMode != newThemeMode)
{
RemoveFluentFromApplication();
_currentFluentThemeState = new FluentThemeState("None", false);
}
return;
}
Expand All @@ -85,6 +95,8 @@ internal static void OnApplicationThemeChanged(ThemeMode oldThemeMode, ThemeMode
ApplyStyleOnWindow(window, useLightColors);
}
}

_currentFluentThemeState= new FluentThemeState(newThemeMode.Value, useLightColors);
}
finally
{
Expand Down Expand Up @@ -446,7 +458,7 @@ private static bool IsSystemThemeLight()
#region Private Fields
private static readonly string fluentThemeResoruceDictionaryUri = "pack://application:,,,/PresentationFramework.Fluent;component/Themes/";
private static readonly string _regPersonalizeKeyPath = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";

private static FluentThemeState _currentFluentThemeState = new FluentThemeState("None", false, SystemColors.AccentColor);

#endregion
}
Loading
0