feat: add demo, sync theme from scope.

This commit is contained in:
rabbitism
2024-02-06 22:06:52 +08:00
parent 5f35f574a9
commit b297b3f5aa
8 changed files with 124 additions and 6 deletions

View File

@@ -3,11 +3,13 @@ using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Ursa.Common;
namespace Ursa.Controls;
public abstract class ThemeSelectorBase: TemplatedControl
{
private bool _syncFromScope;
private Application? _application;
private ThemeVariantScope? _scope;
@@ -33,17 +35,81 @@ public abstract class ThemeSelectorBase: TemplatedControl
static ThemeSelectorBase()
{
SelectedThemeProperty.Changed.AddClassHandler<ThemeSelectorBase, ThemeVariant?>((s, e) => s.OnSelectedThemeChanged(e));
TargetScopeProperty.Changed.AddClassHandler<ThemeSelectorBase, ThemeVariantScope?>((s, e) => s.OnTargetScopeChanged(e));
}
private void OnTargetScopeChanged(AvaloniaPropertyChangedEventArgs<ThemeVariantScope?> args)
{
var target = args.NewValue.Value;
if (target is not null)
{
SyncThemeFromScope(target.ActualThemeVariant);
target.ActualThemeVariantChanged += OnScopeThemeChanged;
}
}
private void OnScopeThemeChanged(object sender, EventArgs e)
{
_syncFromScope = true;
if (this.TargetScope is { } target)
{
SyncThemeFromScope(target.ActualThemeVariant);
}
else if (this._scope is { } scope)
{
SyncThemeFromScope(scope.ActualThemeVariant);
}
else if (_application is { } app)
{
SyncThemeFromScope(app.ActualThemeVariant);
}
_syncFromScope = false;
}
protected internal virtual void SyncThemeFromScope(ThemeVariant? theme)
{
this.SelectedTheme = theme;
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_application = Application.Current;
if (_application is not null)
{
_application.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(_application.ActualThemeVariant);
}
_scope = this.GetLogicalAncestors().FirstOrDefault(a => a is ThemeVariantScope) as ThemeVariantScope;
if (_scope is not null)
{
_scope.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(_scope.ActualThemeVariant);
}
if (TargetScope is not null)
{
SyncThemeFromScope(TargetScope.ActualThemeVariant);
}
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
if (_application is not null)
{
_application.ActualThemeVariantChanged -= OnScopeThemeChanged;
}
if (_scope is not null)
{
_scope.ActualThemeVariantChanged -= OnScopeThemeChanged;
}
}
protected virtual void OnSelectedThemeChanged(AvaloniaPropertyChangedEventArgs<ThemeVariant?> args)
{
if (_syncFromScope) return;
ThemeVariant? newTheme = args.NewValue.Value;
if (newTheme is null) return;
if (TargetScope is not null)

View File

@@ -28,18 +28,22 @@ public class ThemeToggleButton: ThemeSelectorBase
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
EventHelper.UnregisterEvent(ToggleButton.IsCheckedChangedEvent, OnButtonCheckedChanged, _button);
EventHelper.UnregisterEvent(Button.ClickEvent, OnButtonClickedChanged, _button);
_button = e.NameScope.Get<ToggleButton>(PART_ThemeToggleButton);
EventHelper.RegisterEvent(ToggleButton.IsCheckedChangedEvent, OnButtonCheckedChanged, _button);
EventHelper.RegisterEvent(Button.ClickEvent, OnButtonClickedChanged, _button);
PropertyHelper.SetValue(ToggleButton.IsCheckedProperty, _currentTheme == ThemeVariant.Light, _button);
}
private void OnButtonCheckedChanged(object sender, RoutedEventArgs e)
private void OnButtonClickedChanged(object sender, RoutedEventArgs e)
{
var newTheme = (sender as ToggleButton)!.IsChecked;
if (newTheme is null) return;
SelectedTheme = newTheme.Value ? ThemeVariant.Light : ThemeVariant.Dark;
SetCurrentValue(SelectedThemeProperty, newTheme.Value ? ThemeVariant.Light : ThemeVariant.Dark);
}
protected internal override void SyncThemeFromScope(ThemeVariant? theme)
{
base.SyncThemeFromScope(theme);
PropertyHelper.SetValue(ToggleButton.IsCheckedProperty, theme == ThemeVariant.Light, _button);
}
}