fix: unregister from old scope.

This commit is contained in:
rabbitism
2024-02-06 22:12:16 +08:00
parent b297b3f5aa
commit 95321030c8
2 changed files with 9 additions and 8 deletions

View File

@@ -40,11 +40,14 @@ public abstract class ThemeSelectorBase: TemplatedControl
private void OnTargetScopeChanged(AvaloniaPropertyChangedEventArgs<ThemeVariantScope?> args)
{
var target = args.NewValue.Value;
if (target is not null)
if (args.OldValue.Value is { } oldTarget)
{
SyncThemeFromScope(target.ActualThemeVariant);
target.ActualThemeVariantChanged += OnScopeThemeChanged;
oldTarget.ActualThemeVariantChanged -= OnScopeThemeChanged;
}
if (args.NewValue.Value is { } newTarget)
{
newTarget.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(newTarget.ActualThemeVariant);
}
}
@@ -66,7 +69,7 @@ public abstract class ThemeSelectorBase: TemplatedControl
_syncFromScope = false;
}
protected internal virtual void SyncThemeFromScope(ThemeVariant? theme)
protected virtual void SyncThemeFromScope(ThemeVariant? theme)
{
this.SelectedTheme = theme;
}
@@ -86,7 +89,6 @@ public abstract class ThemeSelectorBase: TemplatedControl
_scope.ActualThemeVariantChanged += OnScopeThemeChanged;
SyncThemeFromScope(_scope.ActualThemeVariant);
}
if (TargetScope is not null)
{
SyncThemeFromScope(TargetScope.ActualThemeVariant);
@@ -100,7 +102,6 @@ public abstract class ThemeSelectorBase: TemplatedControl
{
_application.ActualThemeVariantChanged -= OnScopeThemeChanged;
}
if (_scope is not null)
{
_scope.ActualThemeVariantChanged -= OnScopeThemeChanged;

View File

@@ -41,7 +41,7 @@ public class ThemeToggleButton: ThemeSelectorBase
SetCurrentValue(SelectedThemeProperty, newTheme.Value ? ThemeVariant.Light : ThemeVariant.Dark);
}
protected internal override void SyncThemeFromScope(ThemeVariant? theme)
protected override void SyncThemeFromScope(ThemeVariant? theme)
{
base.SyncThemeFromScope(theme);
PropertyHelper.SetValue(ToggleButton.IsCheckedProperty, theme == ThemeVariant.Light, _button);