From b1e70f09b11768a342357eea7421f30b1a4fc52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Sat, 11 Jul 2020 15:46:20 +0200 Subject: [PATCH] refactoring enums --- App/AccentColor.cs | 10 ---------- App/App.xaml.cs | 5 +++-- App/MainWindow.xaml | 2 +- App/MainWindowViewModel.cs | 34 +++++++--------------------------- App/ThemeMode.cs | 11 ----------- App/Types/AccentColors.cs | 30 ++++++++++++++++++++++++++++++ App/Types/ThemeMode.cs | 27 +++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 51 deletions(-) delete mode 100644 App/AccentColor.cs delete mode 100644 App/ThemeMode.cs create mode 100644 App/Types/AccentColors.cs create mode 100644 App/Types/ThemeMode.cs diff --git a/App/AccentColor.cs b/App/AccentColor.cs deleted file mode 100644 index a01bfa5..0000000 --- a/App/AccentColor.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ModernWpfPlayground -{ - public enum AccentColor - { - Green, - Yellow, - Blue, - Purple - } -} \ No newline at end of file diff --git a/App/App.xaml.cs b/App/App.xaml.cs index d45232b..f81f2d0 100644 --- a/App/App.xaml.cs +++ b/App/App.xaml.cs @@ -1,6 +1,6 @@ using System.Windows; -using System.Windows.Media; using ModernWpf; +using ModernWpfPlayground.Types; using Prism.Ioc; namespace ModernWpfPlayground @@ -13,10 +13,11 @@ namespace ModernWpfPlayground /// protected override void OnStartup(StartupEventArgs e) { - ThemeManager.Current.AccentColor = Color.FromArgb(255, 0, 86, 76); base.OnStartup(e); + ThemeManager.Current.AccentColor = AccentColors.Green.ToWindowsColor(); } + protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.Register(); diff --git a/App/MainWindow.xaml b/App/MainWindow.xaml index e458782..f217f38 100644 --- a/App/MainWindow.xaml +++ b/App/MainWindow.xaml @@ -246,7 +246,7 @@ - + SetProperty(value, SetTheme); } - public AccentColor AccentColor + public AccentColors AccentColors { - get => GetProperty(AccentColor.Green); + get => GetProperty(AccentColors.Green); set => SetProperty(value, SetAccentColor); } - private static void SetAccentColor(AccentColor accentColor) - { - ThemeManager.Current.AccentColor = accentColor switch - { - AccentColor.Green => Color.FromArgb(255, 0, 86, 76), - AccentColor.Yellow => Color.FromArgb(255, 164, 144, 0), - AccentColor.Blue => Color.FromArgb(255, 0, 120, 215), - AccentColor.Purple => Color.FromArgb(255, 104, 33, 122), - _ => throw new ArgumentOutOfRangeException(nameof(accentColor), accentColor, null) - }; - } + private static void SetAccentColor(AccentColors accentColors) => Current.AccentColor = accentColors.ToWindowsColor(); public int WindowWidth { @@ -132,16 +121,7 @@ namespace ModernWpfPlayground set => SetProperty(value); } - private static void SetTheme(ThemeMode themeMode) - { - ThemeManager.Current.ApplicationTheme = themeMode switch - { - ThemeMode.Light => ApplicationTheme.Light, - ThemeMode.Dark => ApplicationTheme.Dark, - ThemeMode.UseSystemSetting => default, - _ => ThemeManager.Current.ApplicationTheme - }; - } + private static void SetTheme(ThemeMode themeMode) => Current.ApplicationTheme = themeMode.ToApplicationTheme(); private void ShowDialog() { diff --git a/App/ThemeMode.cs b/App/ThemeMode.cs deleted file mode 100644 index ac959f4..0000000 --- a/App/ThemeMode.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.ComponentModel; - -namespace ModernWpfPlayground -{ - public enum ThemeMode - { - [Description("Light")] Light, - [Description("Dark")] Dark, - [Description("Use system setting")] UseSystemSetting - } -} \ No newline at end of file diff --git a/App/Types/AccentColors.cs b/App/Types/AccentColors.cs new file mode 100644 index 0000000..b4215aa --- /dev/null +++ b/App/Types/AccentColors.cs @@ -0,0 +1,30 @@ +using System; +using System.Windows.Media; + +namespace ModernWpfPlayground.Types +{ + public enum AccentColors + { + Green, + Yellow, + Blue, + Purple, + Red + } + + public static class AccentColorExtension + { + public static Color ToWindowsColor(this AccentColors accentColor) + { + return accentColor switch + { + AccentColors.Green => Color.FromRgb(0, 86, 76), + AccentColors.Yellow => Color.FromRgb(164, 144, 0), + AccentColors.Blue => Color.FromRgb(0, 120, 215), + AccentColors.Purple => Color.FromRgb(104, 33, 122), + AccentColors.Red => Color.FromRgb(183, 71, 42), + _ => throw new ArgumentOutOfRangeException(nameof(accentColor), accentColor, null) + }; + } + } +} \ No newline at end of file diff --git a/App/Types/ThemeMode.cs b/App/Types/ThemeMode.cs new file mode 100644 index 0000000..329eb85 --- /dev/null +++ b/App/Types/ThemeMode.cs @@ -0,0 +1,27 @@ +using System; +using System.ComponentModel; +using ModernWpf; + +namespace ModernWpfPlayground.Types +{ + public enum ThemeMode + { + [Description("Light")] Light, + [Description("Dark")] Dark, + [Description("Use system setting")] UseSystemSetting + } + + public static class ThemeModeExtension + { + public static ApplicationTheme? ToApplicationTheme(this ThemeMode themeMode) + { + return themeMode switch + { + ThemeMode.Light => ApplicationTheme.Light, + ThemeMode.Dark => ApplicationTheme.Dark, + ThemeMode.UseSystemSetting => default, + _ => throw new ArgumentOutOfRangeException(nameof(themeMode), themeMode, null) + }; + } + } +} \ No newline at end of file