generated from atc-net/atc-template-dotnet-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from atc-net/feature/ThemeManagerHelper
Feature/theme manager helper
- Loading branch information
Showing
8 changed files
with
198 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// ReSharper disable CheckNamespace | ||
namespace Atc.Wpf.Theming; | ||
|
||
public enum AtcAppsBrushKeyType | ||
{ | ||
Highlight, | ||
AccentBase, | ||
Accent, | ||
Accent2, | ||
Accent3, | ||
Accent4, | ||
ThemeBackground, | ||
ThemeBackground1, | ||
ThemeBackground2, | ||
ThemeBackground3, | ||
ThemeBackground4, | ||
ThemeBackground5, | ||
ThemeBackground6, | ||
ThemeBackground7, | ||
ThemeForeground7, | ||
ThemeForeground6, | ||
ThemeForeground5, | ||
ThemeForeground4, | ||
ThemeForeground3, | ||
ThemeForeground2, | ||
ThemeForeground1, | ||
ThemeForeground, | ||
IdealForeground, | ||
Gray1, | ||
Gray2, | ||
Gray3, | ||
Gray4, | ||
Gray5, | ||
Gray6, | ||
Gray7, | ||
Gray8, | ||
Gray9, | ||
Gray10, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// ReSharper disable CheckNamespace | ||
namespace Atc.Wpf.Theming; | ||
|
||
public enum AtcAppsColorKeyType | ||
{ | ||
Highlight, | ||
AccentBase, | ||
Accent, | ||
Accent2, | ||
Accent3, | ||
Accent4, | ||
ThemeBackground, | ||
ThemeBackground1, | ||
ThemeBackground2, | ||
ThemeBackground3, | ||
ThemeBackground4, | ||
ThemeBackground5, | ||
ThemeBackground6, | ||
ThemeBackground7, | ||
ThemeForeground7, | ||
ThemeForeground6, | ||
ThemeForeground5, | ||
ThemeForeground4, | ||
ThemeForeground3, | ||
ThemeForeground2, | ||
ThemeForeground1, | ||
ThemeForeground, | ||
IdealForeground, | ||
Gray1, | ||
Gray2, | ||
Gray3, | ||
Gray4, | ||
Gray5, | ||
Gray6, | ||
Gray7, | ||
Gray8, | ||
Gray9, | ||
Gray10, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace Atc.Wpf.Theming.Helpers; | ||
|
||
public static class ThemeManagerHelper | ||
{ | ||
private static readonly ConcurrentDictionary<string, Color> CacheColors = new(StringComparer.Ordinal); | ||
private static readonly ConcurrentDictionary<string, SolidColorBrush> CacheBrushes = new(StringComparer.Ordinal); | ||
|
||
public static Color GetColorByResourceKey( | ||
AtcAppsColorKeyType colorKey) | ||
=> GetColorByResourceKey(colorKey.ToString()); | ||
|
||
public static Color GetColorByResourceKey( | ||
string resourceKey) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(resourceKey); | ||
|
||
if (!resourceKey.StartsWith("AtcApps.Colors.", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
resourceKey = $"AtcApps.Colors.{resourceKey}"; | ||
} | ||
|
||
var currentTheme = ThemeManager.Current.DetectTheme(Application.Current)!; | ||
var cacheKey = $"{currentTheme.BaseColorScheme}_{resourceKey}"; | ||
|
||
if (CacheColors.TryGetValue(cacheKey, out var cacheColor)) | ||
{ | ||
return cacheColor; | ||
} | ||
|
||
var color = (Color)currentTheme.Resources[resourceKey]!; | ||
CacheColors.TryAdd(cacheKey, color); | ||
return color; | ||
} | ||
|
||
public static SolidColorBrush GetBrushByResourceKey( | ||
AtcAppsBrushKeyType brushKey) | ||
=> GetBrushByResourceKey(brushKey.ToString()); | ||
|
||
public static SolidColorBrush GetBrushByResourceKey( | ||
string resourceKey) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(resourceKey); | ||
|
||
if (!resourceKey.StartsWith("AtcApps.Brushes.", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
resourceKey = $"AtcApps.Brushes.{resourceKey}"; | ||
} | ||
|
||
var currentTheme = ThemeManager.Current.DetectTheme(Application.Current)!; | ||
var cacheKey = $"{currentTheme.BaseColorScheme}_{resourceKey}"; | ||
|
||
if (CacheBrushes.TryGetValue(cacheKey, out var cacheBrush)) | ||
{ | ||
return cacheBrush; | ||
} | ||
|
||
var brush = (SolidColorBrush)currentTheme.Resources[resourceKey]!; | ||
if (brush.CanFreeze) | ||
{ | ||
brush.Freeze(); | ||
} | ||
|
||
CacheBrushes.TryAdd(cacheKey, brush); | ||
return brush; | ||
} | ||
|
||
public static Color GetPrimaryAccentColor() | ||
{ | ||
var currentTheme = ThemeManager.Current.DetectTheme(Application.Current)!; | ||
return currentTheme.PrimaryAccentColor; | ||
} | ||
|
||
public static SolidColorBrush GetPrimaryAccentBrush() | ||
{ | ||
var color = GetPrimaryAccentColor(); | ||
var brush = new SolidColorBrush(color); | ||
brush.Freeze(); | ||
return brush; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters