refactoring enums

This commit is contained in:
Holger Börchers 2020-07-11 15:46:20 +02:00
parent 94f5eced01
commit b1e70f09b1
7 changed files with 68 additions and 51 deletions

View File

@ -1,10 +0,0 @@
namespace ModernWpfPlayground
{
public enum AccentColor
{
Green,
Yellow,
Blue,
Purple
}
}

View File

@ -1,6 +1,6 @@
using System.Windows; using System.Windows;
using System.Windows.Media;
using ModernWpf; using ModernWpf;
using ModernWpfPlayground.Types;
using Prism.Ioc; using Prism.Ioc;
namespace ModernWpfPlayground namespace ModernWpfPlayground
@ -13,10 +13,11 @@ namespace ModernWpfPlayground
/// <inheritdoc /> /// <inheritdoc />
protected override void OnStartup(StartupEventArgs e) protected override void OnStartup(StartupEventArgs e)
{ {
ThemeManager.Current.AccentColor = Color.FromArgb(255, 0, 86, 76);
base.OnStartup(e); base.OnStartup(e);
ThemeManager.Current.AccentColor = AccentColors.Green.ToWindowsColor();
} }
protected override void RegisterTypes(IContainerRegistry containerRegistry) protected override void RegisterTypes(IContainerRegistry containerRegistry)
{ {
containerRegistry.Register<MainWindow>(); containerRegistry.Register<MainWindow>();

View File

@ -246,7 +246,7 @@
<ui:SimpleStackPanel Margin="5" Spacing="10"> <ui:SimpleStackPanel Margin="5" Spacing="10">
<controls:PropertyPresenter Label="Theme Mode" Value="{Binding ThemeMode}" /> <controls:PropertyPresenter Label="Theme Mode" Value="{Binding ThemeMode}" />
<controls:PropertyPresenter Label="Accent color" Value="{Binding AccentColor}" /> <controls:PropertyPresenter Label="Accent color" Value="{Binding AccentColors}" />
<controls:PropertyPresenter <controls:PropertyPresenter
Command="{Binding ShowDialogCommand}" Command="{Binding ShowDialogCommand}"
Label="Hello" Label="Hello"

View File

@ -1,15 +1,14 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Win32; using Microsoft.Win32;
using ModernWpf;
using ModernWpfPlayground.MvvmStuff; using ModernWpfPlayground.MvvmStuff;
using ModernWpfPlayground.Types;
using Prism.Commands; using Prism.Commands;
using YamlDotNet.Serialization; using YamlDotNet.Serialization;
using static ModernWpf.ThemeManager;
namespace ModernWpfPlayground namespace ModernWpfPlayground
{ {
@ -96,23 +95,13 @@ namespace ModernWpfPlayground
set => SetProperty(value, SetTheme); set => SetProperty(value, SetTheme);
} }
public AccentColor AccentColor public AccentColors AccentColors
{ {
get => GetProperty(AccentColor.Green); get => GetProperty(AccentColors.Green);
set => SetProperty(value, SetAccentColor); set => SetProperty(value, SetAccentColor);
} }
private static void SetAccentColor(AccentColor accentColor) private static void SetAccentColor(AccentColors accentColors) => Current.AccentColor = accentColors.ToWindowsColor();
{
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)
};
}
public int WindowWidth public int WindowWidth
{ {
@ -132,16 +121,7 @@ namespace ModernWpfPlayground
set => SetProperty(value); set => SetProperty(value);
} }
private static void SetTheme(ThemeMode themeMode) private static void SetTheme(ThemeMode themeMode) => Current.ApplicationTheme = themeMode.ToApplicationTheme();
{
ThemeManager.Current.ApplicationTheme = themeMode switch
{
ThemeMode.Light => ApplicationTheme.Light,
ThemeMode.Dark => ApplicationTheme.Dark,
ThemeMode.UseSystemSetting => default,
_ => ThemeManager.Current.ApplicationTheme
};
}
private void ShowDialog() private void ShowDialog()
{ {

View File

@ -1,11 +0,0 @@
using System.ComponentModel;
namespace ModernWpfPlayground
{
public enum ThemeMode
{
[Description("Light")] Light,
[Description("Dark")] Dark,
[Description("Use system setting")] UseSystemSetting
}
}

30
App/Types/AccentColors.cs Normal file
View File

@ -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)
};
}
}
}

27
App/Types/ThemeMode.cs Normal file
View File

@ -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)
};
}
}
}