Skip to content

Commit

Permalink
Remove ColorZone dependencies to MaterialTheme in application resourc…
Browse files Browse the repository at this point in the history
…es. Fix #312
  • Loading branch information
SKProCH committed Nov 2, 2023
1 parent d908647 commit d41f04b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 126 deletions.
40 changes: 40 additions & 0 deletions Material.Styles/Controls/ColorZone.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@
</ControlTemplate>
</Setter.Value>
</Setter>

<Style Selector="^[Mode=Standard]">
<Setter Property="Background" Value="{DynamicResource MaterialPaperBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialBodyBrush}" />
</Style>

<Style Selector="^[Mode=Inverted]">
<Setter Property="Background" Value="{DynamicResource MaterialBodyBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialPaperBrush}" />
</Style>

<Style Selector="^[Mode=PrimaryLight]">
<Setter Property="Background" Value="{DynamicResource MaterialPrimaryLightBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialPrimaryLightForegroundBrush}" />
</Style>

<Style Selector="^[Mode=PrimaryMid]">
<Setter Property="Background" Value="{DynamicResource MaterialPrimaryMidBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialPrimaryMidForegroundBrush}" />
</Style>

<Style Selector="^[Mode=PrimaryDark]">
<Setter Property="Background" Value="{DynamicResource MaterialPrimaryDarkBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialPrimaryForegroundBrush}" />
</Style>

<Style Selector="^[Mode=Accent]">
<Setter Property="Background" Value="{DynamicResource MaterialSecondaryMidBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialSecondaryMidForegroundBrush}" />
</Style>

<Style Selector="^[Mode=Light]">
<Setter Property="Background" Value="{DynamicResource MaterialLightBackgroundBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialLightForegroundBrush}" />
</Style>

<Style Selector="^[Mode=Dark]">
<Setter Property="Background" Value="{DynamicResource MaterialDarkBackgroundBrush}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialDarkForegroundBrush}" />
</Style>
</ControlTheme>

<ControlTheme x:Key="{x:Type controls:ColorZone}" TargetType="controls:ColorZone"
Expand Down
127 changes: 1 addition & 126 deletions Material.Styles/Controls/ColorZone.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Media;
using Avalonia.Threading;
using Material.Styles.Themes;

namespace Material.Styles.Controls {
public enum ColorZoneMode {
Expand All @@ -24,128 +18,9 @@ public class ColorZone : ContentControl {
public static readonly StyledProperty<ColorZoneMode> ModeProperty =
AvaloniaProperty.Register<ColorZone, ColorZoneMode>(nameof(Mode));

private IDisposable? _subscription;

static ColorZone() {
ModeProperty.Changed.Subscribe(OnNext);
}

public ColorZoneMode Mode {
get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value);
}

private static void OnNext(AvaloniaPropertyChangedEventArgs<ColorZoneMode> arg) {
if (arg.Sender is not ColorZone control)
return;

var resources = Application.Current!.LocateMaterialTheme<MaterialTheme>();
control.OnThemeChanged(resources);
}

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) {
OnModeChanged(null);

DisposeSubscription();

var resources = Application.Current!.LocateMaterialTheme<MaterialTheme>();
_subscription = resources.ThemeChangedEndObservable.Subscribe(OnThemeChanged);

base.OnAttachedToVisualTree(e);
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) {
DisposeSubscription();

base.OnDetachedFromVisualTree(e);
}

private void DisposeSubscription() {
_subscription?.Dispose();
_subscription = null;
}

private void OnThemeChanged(MaterialThemeBase theme) {
Dispatcher.UIThread.InvokeAsync(delegate { OnModeChanged(theme); });
}

private void OnModeChanged(MaterialThemeBase? theme) {
var colorZone = this;
theme ??= Application.Current!.LocateMaterialTheme<MaterialTheme>();

var resources = theme;

var foregroundProperty = TemplatedControl.ForegroundProperty;

switch (colorZone.Mode) {
case ColorZoneMode.Standard: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialPaperBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialBodyBrush"));
}
break;

case ColorZoneMode.Inverted: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialBodyBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialPaperBrush"));
}
break;

case ColorZoneMode.Light: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialLightBackgroundBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialLightForegroundBrush"));
}
break;

case ColorZoneMode.Dark: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialDarkBackgroundBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialDarkForegroundBrush"));
}
break;

case ColorZoneMode.PrimaryLight: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialPrimaryLightBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialPrimaryLightForegroundBrush"));
}
break;

case ColorZoneMode.PrimaryMid: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialPrimaryMidBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialPrimaryMidForegroundBrush"));
}
break;

case ColorZoneMode.PrimaryDark: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialPrimaryDarkBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialPrimaryForegroundBrush"));
}
break;

case ColorZoneMode.Accent: {
SetValueInternal(BackgroundProperty, GetBrushResource(resources, "MaterialSecondaryMidBrush"));
SetValueInternal(foregroundProperty, GetBrushResource(resources, "MaterialSecondaryMidForegroundBrush"));
}
break;

case ColorZoneMode.Custom:
break;

default:
throw new ArgumentOutOfRangeException();
}
}

private void SetValueInternal(AvaloniaProperty property, object? value) {
SetValue(property, value, BindingPriority.Style);
}

private static IBrush? GetBrushResource(IResourceNode theme, string name) {
if (!theme.TryGetResource(name, null, out var resource))
return null;

if (resource is IBrush brush)
return brush;

return null;
}
}
}
}

0 comments on commit d41f04b

Please sign in to comment.