Skip to content

Commit

Permalink
Merge pull request #200 from ricaun/dev-changetheme
Browse files Browse the repository at this point in the history
Apply Theme updates each Windows
  • Loading branch information
Nice3point authored Dec 28, 2023
2 parents fbc9ba1 + 4af9ee2 commit 45e5bce
Showing 1 changed file with 97 additions and 83 deletions.
180 changes: 97 additions & 83 deletions source/RevitLookup.UI/Appearance/ApplicationThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,102 +69,116 @@ public static void Apply(ApplicationTheme applicationTheme,
bool updateAccent = true,
bool forceBackground = false)
{
if (updateAccent)
{
ApplicationAccentColorManager.Apply(ApplicationAccentColorManager.GetColorizationColor(),
applicationTheme,
false
);
}
var mainWindow = Application.MainWindow;

if (applicationTheme == ApplicationTheme.Unknown)
foreach (var window in Application.Windows)
{
return;
}

var appDictionaries = new ResourceDictionaryManager(LibraryNamespace);

var themeDictionaryName = "Light";

switch (applicationTheme)
{
case ApplicationTheme.Dark:
themeDictionaryName = "Dark";
break;
case ApplicationTheme.HighContrast:
switch (ApplicationThemeManager.GetSystemTheme())
{
case SystemTheme.HC1:
themeDictionaryName = "HC1";
break;
case SystemTheme.HC2:
themeDictionaryName = "HC2";
break;
case SystemTheme.HCBlack:
themeDictionaryName = "HCBlack";
break;
case SystemTheme.HCWhite:
default:
themeDictionaryName = "HCWhite";
break;
}

break;
}

var isUpdated = appDictionaries.UpdateDictionary("theme",
new Uri(ThemesDictionaryPath + themeDictionaryName + ".xaml", UriKind.Absolute)
);

//var wpfUiDictionary = appDictionaries.GetDictionary("wpf.ui");

// Force reloading ALL dictionaries
// Works but is terrible
//var isCoreUpdated = appDictionaries.UpdateDictionary(
// "wpf.ui",
// new Uri(
// AppearanceData.LibraryDictionariesUri + "Wpf.Ui.xaml",
// UriKind.Absolute
// )
//);

//var isBrushesUpdated = appDictionaries.UpdateDictionary(
// "assets/brushes",
// new Uri(
// AppearanceData.LibraryDictionariesUri + "Assets/Brushes.xaml",
// UriKind.Absolute
// )
// );

#if DEBUG
System
.Diagnostics
.Debug
.WriteLine(
$"INFO | {typeof(ApplicationThemeManager)} tries to update theme to {themeDictionaryName} ({applicationTheme}): {isUpdated}",
nameof(ApplicationThemeManager)
Application.MainWindow = window;

if (updateAccent)
{
ApplicationAccentColorManager.Apply(ApplicationAccentColorManager.GetColorizationColor(),
applicationTheme,
false
);
}

if (applicationTheme == ApplicationTheme.Unknown)
{
return;
}

var appDictionaries = new ResourceDictionaryManager(LibraryNamespace);

var themeDictionaryName = "Light";

switch (applicationTheme)
{
case ApplicationTheme.Dark:
themeDictionaryName = "Dark";
break;
case ApplicationTheme.HighContrast:
switch (ApplicationThemeManager.GetSystemTheme())
{
case SystemTheme.HC1:
themeDictionaryName = "HC1";
break;
case SystemTheme.HC2:
themeDictionaryName = "HC2";
break;
case SystemTheme.HCBlack:
themeDictionaryName = "HCBlack";
break;
case SystemTheme.HCWhite:
default:
themeDictionaryName = "HCWhite";
break;
}

break;
}

var isUpdated = appDictionaries.UpdateDictionary("theme",
new Uri(ThemesDictionaryPath + themeDictionaryName + ".xaml", UriKind.Absolute)
);
#endif
if (!isUpdated)
{
return;

//var wpfUiDictionary = appDictionaries.GetDictionary("wpf.ui");

// Force reloading ALL dictionaries
// Works but is terrible
//var isCoreUpdated = appDictionaries.UpdateDictionary(
// "wpf.ui",
// new Uri(
// AppearanceData.LibraryDictionariesUri + "Wpf.Ui.xaml",
// UriKind.Absolute
// )
//);

//var isBrushesUpdated = appDictionaries.UpdateDictionary(
// "assets/brushes",
// new Uri(
// AppearanceData.LibraryDictionariesUri + "Assets/Brushes.xaml",
// UriKind.Absolute
// )
// );

#if DEBUG
System
.Diagnostics
.Debug
.WriteLine(
$"INFO | {typeof(ApplicationThemeManager)} tries to update theme to {themeDictionaryName} ({applicationTheme}): {isUpdated}",
nameof(ApplicationThemeManager)
);
#endif
if (!isUpdated)
{
return;
}
}
Application.MainWindow = mainWindow; // Restore MainWindow

SystemThemeManager.UpdateSystemThemeCache();

_cachedApplicationTheme = applicationTheme;

Changed?.Invoke(applicationTheme, ApplicationAccentColorManager.SystemAccent);

if (Application.MainWindow is not null)
foreach (var window in Application.Windows)
{
WindowBackgroundManager.UpdateBackground(
Application.MainWindow,
applicationTheme,
backgroundEffect,
forceBackground
);
Application.MainWindow = window;

if (Application.MainWindow is not null)
{
WindowBackgroundManager.UpdateBackground(
Application.MainWindow,
applicationTheme,
backgroundEffect,
forceBackground
);
}
}
Application.MainWindow = mainWindow; // Restore MainWindow
}

public static void ApplySystemTheme()
Expand Down

0 comments on commit 45e5bce

Please sign in to comment.