Skip to content

Commit

Permalink
Fixed crash issue when changing theme (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Feb 28, 2024
1 parent 3642974 commit 3873414
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
56 changes: 30 additions & 26 deletions ColorPicker/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public static class Global
]
};

internal static Action RefreshButton { get; set; }

internal static string SynethiaPath => $@"{FileSys.AppDataPath}\Léo Corporation\ColorPicker Max\SynethiaConfig.json";
internal static string BookmarksPath => $@"{FileSys.AppDataPath}\Léo Corporation\ColorPicker Max\Bookmarks.xml";
Expand Down Expand Up @@ -278,36 +277,41 @@ public static AppPages PageInfoToAppPages(PageInfo pageInfo)
/// </summary>
public static void ChangeTheme(bool reload = false)
{
App.Current.Resources.MergedDictionaries.Clear();
ResourceDictionary resourceDictionary = []; // Create a resource dictionary

bool isDark = Settings.Theme == Themes.Dark;
if (Settings.Theme == Themes.System)
try
{
isDark = IsSystemThemeDark(); // Set
}
App.Current.Resources.MergedDictionaries.Clear();
ResourceDictionary resourceDictionary = []; // Create a resource dictionary

if (isDark) // If the dark theme is on
{
resourceDictionary.Source = new Uri("..\\Themes\\Dark.xaml", UriKind.Relative); // Add source
bool isDark = Settings.Theme == Themes.Dark;
if (Settings.Theme == Themes.System)
{
isDark = IsSystemThemeDark(); // Set
}

if (isDark) // If the dark theme is on
{
resourceDictionary.Source = new Uri("..\\Themes\\Dark.xaml", UriKind.Relative); // Add source
}
else
{
resourceDictionary.Source = new Uri("..\\Themes\\Light.xaml", UriKind.Relative); // Add source
}

App.Current.Resources.MergedDictionaries.Add(resourceDictionary); // Add the dictionary

if (!reload) return;
BookmarksPage.CheckButton(BookmarksPage.CheckedButton);
ConverterPage.CheckButton(ConverterPage.SelectedColorBtn);
ChromaticWheelPage.CheckButton(ChromaticWheelPage.CheckedButton);
HarmoniesPage.CheckButton(HarmoniesPage.SelectedColorBtn);
PalettePage.CheckButton(PalettePage.SelectedColorBtn);
ContrastPage.CheckButton(ContrastPage.RgbBtn);
ContrastPage.InitGrid(ContrastPage.contrastLimit);
}
else
catch (Exception ex)
{
resourceDictionary.Source = new Uri("..\\Themes\\Light.xaml", UriKind.Relative); // Add source
MessageBox.Show(ex.StackTrace, Properties.Resources.ColorPickerMax, MessageBoxButton.OK, MessageBoxImage.Error);
}

App.Current.Resources.MergedDictionaries.Add(resourceDictionary); // Add the dictionary

if (!reload) return;
AiGenPage.CheckButton(AiGenPage.CheckedButton);
BookmarksPage.CheckButton(BookmarksPage.CheckedButton);
ConverterPage.CheckButton(ConverterPage.SelectedColorBtn);
ChromaticWheelPage.CheckButton(ChromaticWheelPage.CheckedButton);
HarmoniesPage.CheckButton(HarmoniesPage.SelectedColorBtn);
PalettePage.CheckButton(PalettePage.SelectedColorBtn);
ContrastPage.CheckButton(ContrastPage.RgbBtn);
ContrastPage.InitGrid(ContrastPage.contrastLimit);
RefreshButton();
}

public static bool IsSystemThemeDark()
Expand Down
1 change: 0 additions & 1 deletion ColorPicker/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public MainWindow()
{
InitializeComponent();
InitUI();
Global.RefreshButton = () => SettingsPageBtn.Background = Global.GetColorFromResource("Background1");
GC.Collect();
}

Expand Down
2 changes: 1 addition & 1 deletion ColorPicker/Pages/ContrastPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal void RgbBtn_Click(object sender, RoutedEventArgs? e)
Global.SynethiaConfig.PagesInfo[9].InteractionCount++;
}

internal static void CheckButton(Button button) => button.Background = Global.GetColorFromResource("LightAccentColor");
internal void CheckButton(Button button) => button.Background = Global.GetColorFromResource("LightAccentColor");

private void HideAllInput()
{
Expand Down

0 comments on commit 3873414

Please sign in to comment.