Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions MainDemo.Wpf/ColorTool.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,9 @@
</UserControl.Resources>

<DockPanel>
<StackPanel
<local:ThemeSettings
DockPanel.Dock="Top"
Orientation="Horizontal"
Margin="8">
<TextBlock
VerticalAlignment="Center"
Text="Light"/>

<ToggleButton
Margin="8 0 16 0"
IsChecked="{Binding IsDarkTheme}"/>

<TextBlock
VerticalAlignment="Center"
Text="Dark"/>
</StackPanel>
Margin="8" />

<DockPanel>
<!-- Selection controls -->
Expand Down
23 changes: 0 additions & 23 deletions MainDemo.Wpf/Domain/ColorToolViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ public Color? SelectedColor
}
}

private bool _isDarkTheme;
public bool IsDarkTheme
{
get => _isDarkTheme;
set
{
if (SetProperty(ref _isDarkTheme, value))
{
ApplyBase(value);
}
}
}

public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;

public ICommand ChangeCustomHueCommand { get; }
Expand Down Expand Up @@ -105,16 +92,6 @@ public ColorToolViewModel()
_secondaryColor = theme.SecondaryMid.Color;

SelectedColor = _primaryColor;

IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;

if (_paletteHelper.GetThemeManager() is { } themeManager)
{
themeManager.ThemeChanged += (_, e) =>
{
IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
};
}
}

private void ChangeCustomColor(object obj)
Expand Down
118 changes: 0 additions & 118 deletions MainDemo.Wpf/Domain/PaletteSelectorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
Expand All @@ -12,123 +11,6 @@ public class PaletteSelectorViewModel : ViewModelBase
public PaletteSelectorViewModel()
{
Swatches = new SwatchesProvider().Swatches;

PaletteHelper paletteHelper = new PaletteHelper();
ITheme theme = paletteHelper.GetTheme();

IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;

if (theme is Theme internalTheme)
{
_isColorAdjusted = internalTheme.ColorAdjustment is not null;

var colorAdjustment = internalTheme.ColorAdjustment ?? new ColorAdjustment();
_desiredContrastRatio = colorAdjustment.DesiredContrastRatio;
_contrastValue = colorAdjustment.Contrast;
_colorSelectionValue = colorAdjustment.Colors;
}

if (paletteHelper.GetThemeManager() is { } themeManager)
{
themeManager.ThemeChanged += (_, e) =>
{
IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
};
}
}

private bool _isDarkTheme;
public bool IsDarkTheme
{
get => _isDarkTheme;
set
{
if (SetProperty(ref _isDarkTheme, value))
{
ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
}
}
}

private bool _isColorAdjusted;
public bool IsColorAdjusted
{
get => _isColorAdjusted;
set
{
if (SetProperty(ref _isColorAdjusted, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme)
{
internalTheme.ColorAdjustment = value
? new ColorAdjustment
{
DesiredContrastRatio = DesiredContrastRatio,
Contrast = ContrastValue,
Colors = ColorSelectionValue
}
: null;
}
});
}
}
}

private float _desiredContrastRatio = 4.5f;
public float DesiredContrastRatio
{
get => _desiredContrastRatio;
set
{
if (SetProperty(ref _desiredContrastRatio, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
internalTheme.ColorAdjustment.DesiredContrastRatio = value;
});
}
}
}

public IEnumerable<Contrast> ContrastValues => Enum.GetValues(typeof(Contrast)).Cast<Contrast>();

private Contrast _contrastValue;
public Contrast ContrastValue
{
get => _contrastValue;
set
{
if (SetProperty(ref _contrastValue, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
internalTheme.ColorAdjustment.Contrast = value;
});
}
}
}

public IEnumerable<ColorSelection> ColorSelectionValues => Enum.GetValues(typeof(ColorSelection)).Cast<ColorSelection>();

private ColorSelection _colorSelectionValue;
public ColorSelection ColorSelectionValue
{
get => _colorSelectionValue;
set
{
if (SetProperty(ref _colorSelectionValue, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
internalTheme.ColorAdjustment.Colors = value;
});
}
}
}

public IEnumerable<Swatch> Swatches { get; }
Expand Down
140 changes: 140 additions & 0 deletions MainDemo.Wpf/Domain/ThemeSettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MaterialDesignThemes.Wpf;

namespace MaterialDesignDemo.Domain
{
public class ThemeSettingsViewModel : ViewModelBase
{
public ThemeSettingsViewModel()
{
PaletteHelper paletteHelper = new PaletteHelper();
ITheme theme = paletteHelper.GetTheme();

IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;

if (theme is Theme internalTheme)
{
_isColorAdjusted = internalTheme.ColorAdjustment is not null;

var colorAdjustment = internalTheme.ColorAdjustment ?? new ColorAdjustment();
_desiredContrastRatio = colorAdjustment.DesiredContrastRatio;
_contrastValue = colorAdjustment.Contrast;
_colorSelectionValue = colorAdjustment.Colors;
}

if (paletteHelper.GetThemeManager() is { } themeManager)
{
themeManager.ThemeChanged += (_, e) =>
{
IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
};
}
}

private bool _isDarkTheme;
public bool IsDarkTheme
{
get => _isDarkTheme;
set
{
if (SetProperty(ref _isDarkTheme, value))
{
ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
}
}
}

private bool _isColorAdjusted;
public bool IsColorAdjusted
{
get => _isColorAdjusted;
set
{
if (SetProperty(ref _isColorAdjusted, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme)
{
internalTheme.ColorAdjustment = value
? new ColorAdjustment
{
DesiredContrastRatio = DesiredContrastRatio,
Contrast = ContrastValue,
Colors = ColorSelectionValue
}
: null;
}
});
}
}
}

private float _desiredContrastRatio = 4.5f;
public float DesiredContrastRatio
{
get => _desiredContrastRatio;
set
{
if (SetProperty(ref _desiredContrastRatio, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
internalTheme.ColorAdjustment.DesiredContrastRatio = value;
});
}
}
}

public IEnumerable<Contrast> ContrastValues => Enum.GetValues(typeof(Contrast)).Cast<Contrast>();

private Contrast _contrastValue;
public Contrast ContrastValue
{
get => _contrastValue;
set
{
if (SetProperty(ref _contrastValue, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
internalTheme.ColorAdjustment.Contrast = value;
});
}
}
}

public IEnumerable<ColorSelection> ColorSelectionValues => Enum.GetValues(typeof(ColorSelection)).Cast<ColorSelection>();

private ColorSelection _colorSelectionValue;
public ColorSelection ColorSelectionValue
{
get => _colorSelectionValue;
set
{
if (SetProperty(ref _colorSelectionValue, value))
{
ModifyTheme(theme =>
{
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
internalTheme.ColorAdjustment.Colors = value;
});
}
}
}

private static void ModifyTheme(Action<ITheme> modificationAction)
{
var paletteHelper = new PaletteHelper();
ITheme theme = paletteHelper.GetTheme();

modificationAction?.Invoke(theme);

paletteHelper.SetTheme(theme);
}
}
}
Loading