refactor ThemeSelectorBase and ThemeToggleButton

This commit is contained in:
lxymahatma
2024-09-07 12:49:02 +08:00
parent 5d3646ed74
commit 1b88a9b23a
2 changed files with 53 additions and 67 deletions

View File

@@ -10,7 +10,6 @@ public abstract class ThemeSelectorBase: TemplatedControl
{
private bool _syncFromScope;
private Application? _application;
private ThemeVariantScope? _scope;
public static readonly StyledProperty<ThemeVariant?> SelectedThemeProperty = AvaloniaProperty.Register<ThemeSelectorBase, ThemeVariant?>(
nameof(SelectedTheme));
@@ -62,18 +61,12 @@ public abstract class ThemeSelectorBase: TemplatedControl
private void OnScopeThemeChanged(object? sender, System.EventArgs e)
{
_syncFromScope = true;
if (this.TargetScope is { } target)
if (TargetScope is { } target)
{
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? target.RequestedThemeVariant
: target.ActualThemeVariant);
}
else if (this._scope is { } scope)
{
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? scope.RequestedThemeVariant
: scope.ActualThemeVariant);
}
else if (_application is { } app)
{
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
@@ -85,7 +78,7 @@ public abstract class ThemeSelectorBase: TemplatedControl
protected virtual void SyncThemeFromScope(ThemeVariant? theme)
{
this.SelectedTheme = theme;
SelectedTheme = theme;
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
@@ -93,27 +86,30 @@ public abstract class ThemeSelectorBase: TemplatedControl
base.OnAttachedToVisualTree(e);
_application = Application.Current;
_syncFromScope = true;
if (_application is not null)
if (TargetScope is not null)
{
TargetScope.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? TargetScope.RequestedThemeVariant
: TargetScope.ActualThemeVariant);
}
else if (this.GetLogicalAncestors().FirstOrDefault(a => a is ThemeVariantScope) is ThemeVariantScope scope)
{
TargetScope = scope;
TargetScope.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? TargetScope.RequestedThemeVariant
: TargetScope.ActualThemeVariant);
}
else if (_application is not null)
{
_application.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? _application.RequestedThemeVariant
: _application.ActualThemeVariant);
}
_scope = this.GetLogicalAncestors().FirstOrDefault(a => a is ThemeVariantScope) as ThemeVariantScope;
if (_scope is not null)
{
_scope.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? _scope.RequestedThemeVariant
: _scope.ActualThemeVariant);
}
if (TargetScope is not null)
{
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
? TargetScope.RequestedThemeVariant
: TargetScope.ActualThemeVariant);
}
_syncFromScope = false;
}
@@ -124,9 +120,10 @@ public abstract class ThemeSelectorBase: TemplatedControl
{
_application.ActualThemeVariantChanged -= OnScopeThemeChanged;
}
if (_scope is not null)
if (TargetScope is not null)
{
_scope.ActualThemeVariantChanged -= OnScopeThemeChanged;
TargetScope.ActualThemeVariantChanged -= OnScopeThemeChanged;
}
}
@@ -139,11 +136,7 @@ public abstract class ThemeSelectorBase: TemplatedControl
TargetScope.RequestedThemeVariant = newTheme;
return;
}
if (_scope is not null)
{
_scope.RequestedThemeVariant = newTheme;
return;
}
if (_application is not null)
{
_application.RequestedThemeVariant = newTheme;

View File

@@ -41,57 +41,50 @@ public class ThemeToggleButton: ThemeSelectorBase
private void OnButtonClicked(object? sender, RoutedEventArgs e)
{
bool? currentState = _state;
if (IsThreeState)
if (Mode == ThemeSelectorMode.Indicator || !IsThreeState)
{
_state = currentState switch
{
true => false,
false => null,
null => true,
};
}
else
{
_state = currentState switch
_state = _state switch
{
true => false,
false => true,
null => true,
null => throw new InvalidOperationException("Invalid state")
};
}
if (_state == true)
{
SelectedTheme = ThemeVariant.Light;
}
else if (_state == false)
{
SelectedTheme = ThemeVariant.Dark;
}
}
else
{
SelectedTheme = ThemeVariant.Default;
_state = _state switch
{
true => false,
false => null,
null => true
};
}
if (Mode == ThemeSelectorMode.Controller)
SelectedTheme = _state switch
{
PseudoClasses.Set(PC_Light, SelectedTheme == ThemeVariant.Light);
PseudoClasses.Set(PC_Dark, SelectedTheme == ThemeVariant.Dark);
PseudoClasses.Set(PC_Default, SelectedTheme == null || SelectedTheme == ThemeVariant.Default);
}
true => ThemeVariant.Light,
false => ThemeVariant.Dark,
null => ThemeVariant.Default
};
PseudoClasses.Set(PC_Light, SelectedTheme == ThemeVariant.Light);
PseudoClasses.Set(PC_Dark, SelectedTheme == ThemeVariant.Dark);
PseudoClasses.Set(PC_Default, SelectedTheme == ThemeVariant.Default);
}
protected override void SyncThemeFromScope(ThemeVariant? theme)
{
base.SyncThemeFromScope(theme);
if (Mode == ThemeSelectorMode.Indicator)
if (!IsThreeState && theme is null)
{
PseudoClasses.Set(PC_Light, theme == ThemeVariant.Light);
PseudoClasses.Set(PC_Dark, theme == ThemeVariant.Dark);
PseudoClasses.Set(PC_Default, theme == null || SelectedTheme == ThemeVariant.Default);
if (theme == ThemeVariant.Dark) _state = false;
else if (theme == ThemeVariant.Light) _state = true;
else _state = null;
theme = ThemeVariant.Light;
}
PseudoClasses.Set(PC_Light, theme == ThemeVariant.Light);
PseudoClasses.Set(PC_Dark, theme == ThemeVariant.Dark);
PseudoClasses.Set(PC_Default, theme == null || SelectedTheme == ThemeVariant.Default);
if (theme == ThemeVariant.Dark) _state = false;
else if (theme == ThemeVariant.Light) _state = true;
else _state = null;
}
}