refactor ThemeSelectorBase and ThemeToggleButton
This commit is contained in:
@@ -10,7 +10,6 @@ public abstract class ThemeSelectorBase: TemplatedControl
|
|||||||
{
|
{
|
||||||
private bool _syncFromScope;
|
private bool _syncFromScope;
|
||||||
private Application? _application;
|
private Application? _application;
|
||||||
private ThemeVariantScope? _scope;
|
|
||||||
|
|
||||||
public static readonly StyledProperty<ThemeVariant?> SelectedThemeProperty = AvaloniaProperty.Register<ThemeSelectorBase, ThemeVariant?>(
|
public static readonly StyledProperty<ThemeVariant?> SelectedThemeProperty = AvaloniaProperty.Register<ThemeSelectorBase, ThemeVariant?>(
|
||||||
nameof(SelectedTheme));
|
nameof(SelectedTheme));
|
||||||
@@ -62,18 +61,12 @@ public abstract class ThemeSelectorBase: TemplatedControl
|
|||||||
private void OnScopeThemeChanged(object? sender, System.EventArgs e)
|
private void OnScopeThemeChanged(object? sender, System.EventArgs e)
|
||||||
{
|
{
|
||||||
_syncFromScope = true;
|
_syncFromScope = true;
|
||||||
if (this.TargetScope is { } target)
|
if (TargetScope is { } target)
|
||||||
{
|
{
|
||||||
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
||||||
? target.RequestedThemeVariant
|
? target.RequestedThemeVariant
|
||||||
: target.ActualThemeVariant);
|
: target.ActualThemeVariant);
|
||||||
}
|
}
|
||||||
else if (this._scope is { } scope)
|
|
||||||
{
|
|
||||||
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
|
||||||
? scope.RequestedThemeVariant
|
|
||||||
: scope.ActualThemeVariant);
|
|
||||||
}
|
|
||||||
else if (_application is { } app)
|
else if (_application is { } app)
|
||||||
{
|
{
|
||||||
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
||||||
@@ -85,7 +78,7 @@ public abstract class ThemeSelectorBase: TemplatedControl
|
|||||||
|
|
||||||
protected virtual void SyncThemeFromScope(ThemeVariant? theme)
|
protected virtual void SyncThemeFromScope(ThemeVariant? theme)
|
||||||
{
|
{
|
||||||
this.SelectedTheme = theme;
|
SelectedTheme = theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
@@ -93,27 +86,30 @@ public abstract class ThemeSelectorBase: TemplatedControl
|
|||||||
base.OnAttachedToVisualTree(e);
|
base.OnAttachedToVisualTree(e);
|
||||||
_application = Application.Current;
|
_application = Application.Current;
|
||||||
_syncFromScope = true;
|
_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;
|
_application.ActualThemeVariantChanged += OnScopeThemeChanged;
|
||||||
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
SyncThemeFromScope(Mode == ThemeSelectorMode.Controller
|
||||||
? _application.RequestedThemeVariant
|
? _application.RequestedThemeVariant
|
||||||
: _application.ActualThemeVariant);
|
: _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;
|
_syncFromScope = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,9 +120,10 @@ public abstract class ThemeSelectorBase: TemplatedControl
|
|||||||
{
|
{
|
||||||
_application.ActualThemeVariantChanged -= OnScopeThemeChanged;
|
_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;
|
TargetScope.RequestedThemeVariant = newTheme;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (_scope is not null)
|
|
||||||
{
|
|
||||||
_scope.RequestedThemeVariant = newTheme;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_application is not null)
|
if (_application is not null)
|
||||||
{
|
{
|
||||||
_application.RequestedThemeVariant = newTheme;
|
_application.RequestedThemeVariant = newTheme;
|
||||||
|
|||||||
@@ -41,57 +41,50 @@ public class ThemeToggleButton: ThemeSelectorBase
|
|||||||
|
|
||||||
private void OnButtonClicked(object? sender, RoutedEventArgs e)
|
private void OnButtonClicked(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
bool? currentState = _state;
|
if (Mode == ThemeSelectorMode.Indicator || !IsThreeState)
|
||||||
if (IsThreeState)
|
|
||||||
{
|
{
|
||||||
_state = currentState switch
|
_state = _state switch
|
||||||
{
|
|
||||||
true => false,
|
|
||||||
false => null,
|
|
||||||
null => true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_state = currentState switch
|
|
||||||
{
|
{
|
||||||
true => false,
|
true => false,
|
||||||
false => true,
|
false => true,
|
||||||
null => true,
|
null => throw new InvalidOperationException("Invalid state")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (_state == true)
|
|
||||||
{
|
|
||||||
SelectedTheme = ThemeVariant.Light;
|
|
||||||
}
|
|
||||||
else if (_state == false)
|
|
||||||
{
|
|
||||||
SelectedTheme = ThemeVariant.Dark;
|
|
||||||
}
|
|
||||||
else
|
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);
|
true => ThemeVariant.Light,
|
||||||
PseudoClasses.Set(PC_Dark, SelectedTheme == ThemeVariant.Dark);
|
false => ThemeVariant.Dark,
|
||||||
PseudoClasses.Set(PC_Default, SelectedTheme == null || SelectedTheme == ThemeVariant.Default);
|
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)
|
protected override void SyncThemeFromScope(ThemeVariant? theme)
|
||||||
{
|
{
|
||||||
base.SyncThemeFromScope(theme);
|
base.SyncThemeFromScope(theme);
|
||||||
if (Mode == ThemeSelectorMode.Indicator)
|
if (!IsThreeState && theme is null)
|
||||||
{
|
{
|
||||||
PseudoClasses.Set(PC_Light, theme == ThemeVariant.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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user