Browse Source

Merge IResourceHost2 into IResourceHost (#20576)

* Merge IResourceHost2 into IResourceHost

* Update API suppressions
pull/18827/merge
Julien Lebosquain 7 days ago
committed by GitHub
parent
commit
5483217944
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 48
      api/Avalonia.nupkg.xml
  2. 8
      src/Avalonia.Base/Controls/IResourceHost.cs
  3. 4
      src/Avalonia.Base/Controls/ResourceDictionary.cs
  4. 22
      src/Avalonia.Base/Controls/ResourceNodeExtensions.cs
  5. 6
      src/Avalonia.Base/Controls/ResourceProvider.cs
  6. 28
      src/Avalonia.Base/Controls/ResourcesChangedEventArgs.cs
  7. 45
      src/Avalonia.Base/Controls/ResourcesChangedHelper.cs
  8. 11
      src/Avalonia.Base/Controls/ResourcesChangedToken.cs
  9. 39
      src/Avalonia.Base/StyledElement.cs
  10. 2
      src/Avalonia.Base/Styling/StyleBase.cs
  11. 16
      src/Avalonia.Controls/Application.cs
  12. 6
      src/Avalonia.Controls/Primitives/TemplatedControl.cs
  13. 6
      src/Avalonia.Controls/Primitives/VisualLayerManager.cs
  14. 23
      src/Avalonia.Controls/TopLevel.cs
  15. 2
      src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs
  16. 2
      src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs
  17. 6
      src/Markup/Avalonia.Markup.Xaml/Data/DynamicResourceExpression.cs

48
api/Avalonia.nupkg.xml

@ -97,6 +97,12 @@
<Left>baseline/Avalonia/lib/netstandard2.0/Avalonia.Base.dll</Left> <Left>baseline/Avalonia/lib/netstandard2.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/netstandard2.0/Avalonia.Base.dll</Right> <Right>current/Avalonia/lib/netstandard2.0/Avalonia.Base.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:Avalonia.Controls.ResourcesChangedEventArgs.Empty</Target>
<Left>baseline/Avalonia/lib/net10.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net10.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0002</DiagnosticId> <DiagnosticId>CP0002</DiagnosticId>
<Target>F:Avalonia.Media.DrawingImage.ViewboxProperty</Target> <Target>F:Avalonia.Media.DrawingImage.ViewboxProperty</Target>
@ -109,6 +115,12 @@
<Left>baseline/Avalonia/lib/net10.0/Avalonia.Base.dll</Left> <Left>baseline/Avalonia/lib/net10.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net10.0/Avalonia.Base.dll</Right> <Right>current/Avalonia/lib/net10.0/Avalonia.Base.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Avalonia.Controls.ResourcesChangedEventArgs.#ctor</Target>
<Left>baseline/Avalonia/lib/net10.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net10.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0002</DiagnosticId> <DiagnosticId>CP0002</DiagnosticId>
<Target>M:Avalonia.Input.IKeyboardNavigationHandler.Move(Avalonia.Input.IInputElement,Avalonia.Input.NavigationDirection,Avalonia.Input.KeyModifiers)</Target> <Target>M:Avalonia.Input.IKeyboardNavigationHandler.Move(Avalonia.Input.IInputElement,Avalonia.Input.NavigationDirection,Avalonia.Input.KeyModifiers)</Target>
@ -487,6 +499,12 @@
<Left>baseline/Avalonia/lib/net6.0/Avalonia.Dialogs.dll</Left> <Left>baseline/Avalonia/lib/net6.0/Avalonia.Dialogs.dll</Left>
<Right>current/Avalonia/lib/net6.0/Avalonia.Dialogs.dll</Right> <Right>current/Avalonia/lib/net6.0/Avalonia.Dialogs.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>F:Avalonia.Controls.ResourcesChangedEventArgs.Empty</Target>
<Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0002</DiagnosticId> <DiagnosticId>CP0002</DiagnosticId>
<Target>F:Avalonia.Media.DrawingImage.ViewboxProperty</Target> <Target>F:Avalonia.Media.DrawingImage.ViewboxProperty</Target>
@ -499,6 +517,12 @@
<Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left> <Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right> <Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Avalonia.Controls.ResourcesChangedEventArgs.#ctor</Target>
<Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0002</DiagnosticId> <DiagnosticId>CP0002</DiagnosticId>
<Target>M:Avalonia.Input.IKeyboardNavigationHandler.Move(Avalonia.Input.IInputElement,Avalonia.Input.NavigationDirection,Avalonia.Input.KeyModifiers)</Target> <Target>M:Avalonia.Input.IKeyboardNavigationHandler.Move(Avalonia.Input.IInputElement,Avalonia.Input.NavigationDirection,Avalonia.Input.KeyModifiers)</Target>
@ -1201,6 +1225,18 @@
<Left>baseline/Avalonia/lib/netstandard2.0/Avalonia.OpenGL.dll</Left> <Left>baseline/Avalonia/lib/netstandard2.0/Avalonia.OpenGL.dll</Left>
<Right>current/Avalonia/lib/netstandard2.0/Avalonia.OpenGL.dll</Right> <Right>current/Avalonia/lib/netstandard2.0/Avalonia.OpenGL.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0007</DiagnosticId>
<Target>T:Avalonia.Controls.ResourcesChangedEventArgs</Target>
<Left>baseline/Avalonia/lib/net10.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net10.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0007</DiagnosticId>
<Target>T:Avalonia.Controls.ResourcesChangedEventArgs</Target>
<Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0008</DiagnosticId> <DiagnosticId>CP0008</DiagnosticId>
<Target>T:Avalonia.Media.StreamGeometryContext</Target> <Target>T:Avalonia.Media.StreamGeometryContext</Target>
@ -1225,12 +1261,24 @@
<Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left> <Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right> <Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0009</DiagnosticId>
<Target>T:Avalonia.Controls.ResourcesChangedEventArgs</Target>
<Left>baseline/Avalonia/lib/net10.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net10.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0009</DiagnosticId> <DiagnosticId>CP0009</DiagnosticId>
<Target>T:Avalonia.Platform.Screen</Target> <Target>T:Avalonia.Platform.Screen</Target>
<Left>baseline/Avalonia/lib/net10.0/Avalonia.Controls.dll</Left> <Left>baseline/Avalonia/lib/net10.0/Avalonia.Controls.dll</Left>
<Right>current/Avalonia/lib/net10.0/Avalonia.Controls.dll</Right> <Right>current/Avalonia/lib/net10.0/Avalonia.Controls.dll</Right>
</Suppression> </Suppression>
<Suppression>
<DiagnosticId>CP0009</DiagnosticId>
<Target>T:Avalonia.Controls.ResourcesChangedEventArgs</Target>
<Left>baseline/Avalonia/lib/net8.0/Avalonia.Base.dll</Left>
<Right>current/Avalonia/lib/net8.0/Avalonia.Base.dll</Right>
</Suppression>
<Suppression> <Suppression>
<DiagnosticId>CP0009</DiagnosticId> <DiagnosticId>CP0009</DiagnosticId>
<Target>T:Avalonia.Platform.Screen</Target> <Target>T:Avalonia.Platform.Screen</Target>

8
src/Avalonia.Base/Controls/IResourceHost.cs

@ -29,12 +29,4 @@ namespace Avalonia.Controls
/// </remarks> /// </remarks>
void NotifyHostedResourcesChanged(ResourcesChangedEventArgs e); void NotifyHostedResourcesChanged(ResourcesChangedEventArgs e);
} }
// TODO12: merge with IResourceHost
internal interface IResourceHost2 : IResourceHost
{
event EventHandler<ResourcesChangedToken> ResourcesChanged2;
void NotifyHostedResourcesChanged(ResourcesChangedToken token);
}
} }

4
src/Avalonia.Base/Controls/ResourceDictionary.cs

@ -358,7 +358,7 @@ namespace Avalonia.Controls
if (hasResources) if (hasResources)
{ {
owner.NotifyHostedResourcesChanged(ResourcesChangedToken.Create()); owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }
@ -385,7 +385,7 @@ namespace Avalonia.Controls
if (hasResources) if (hasResources)
{ {
owner.NotifyHostedResourcesChanged(ResourcesChangedToken.Create()); owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }

22
src/Avalonia.Base/Controls/ResourceNodeExtensions.cs

@ -158,7 +158,7 @@ namespace Avalonia.Controls
protected override void Initialize() protected override void Initialize()
{ {
_target.SubscribeToResourcesChanged(ResourcesChanged, ResourcesChanged2); _target.ResourcesChanged += ResourcesChanged;
if (_target is IThemeVariantHost themeVariantHost) if (_target is IThemeVariantHost themeVariantHost)
{ {
@ -168,7 +168,7 @@ namespace Avalonia.Controls
protected override void Deinitialize() protected override void Deinitialize()
{ {
_target.UnsubscribeFromResourcesChanged(ResourcesChanged, ResourcesChanged2); _target.ResourcesChanged -= ResourcesChanged;
if (_target is IThemeVariantHost themeVariantHost) if (_target is IThemeVariantHost themeVariantHost)
{ {
@ -186,11 +186,6 @@ namespace Avalonia.Controls
PublishNext(GetValue()); PublishNext(GetValue());
} }
private void ResourcesChanged2(object? sender, ResourcesChangedToken token)
{
PublishNext(GetValue());
}
private void ActualThemeVariantChanged(object? sender, EventArgs e) private void ActualThemeVariantChanged(object? sender, EventArgs e)
{ {
PublishNext(GetValue()); PublishNext(GetValue());
@ -230,7 +225,7 @@ namespace Avalonia.Controls
_target.OwnerChanged += OwnerChanged; _target.OwnerChanged += OwnerChanged;
_owner = _target.Owner; _owner = _target.Owner;
_owner?.SubscribeToResourcesChanged(ResourcesChanged, ResourcesChanged2); _owner?.ResourcesChanged += ResourcesChanged;
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost)
{ {
@ -242,7 +237,7 @@ namespace Avalonia.Controls
{ {
_target.OwnerChanged -= OwnerChanged; _target.OwnerChanged -= OwnerChanged;
_owner?.UnsubscribeFromResourcesChanged(ResourcesChanged, ResourcesChanged2); _owner?.ResourcesChanged -= ResourcesChanged;
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost)
{ {
@ -270,7 +265,7 @@ namespace Avalonia.Controls
private void OwnerChanged(object? sender, EventArgs e) private void OwnerChanged(object? sender, EventArgs e)
{ {
_owner?.UnsubscribeFromResourcesChanged(ResourcesChanged, ResourcesChanged2); _owner?.ResourcesChanged -= ResourcesChanged;
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost) if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost)
{ {
@ -279,7 +274,7 @@ namespace Avalonia.Controls
_owner = _target.Owner; _owner = _target.Owner;
_owner?.SubscribeToResourcesChanged(ResourcesChanged, ResourcesChanged2); _owner?.ResourcesChanged += ResourcesChanged;
if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost2) if (_overrideThemeVariant is null && _owner is IThemeVariantHost themeVariantHost2)
{ {
@ -299,11 +294,6 @@ namespace Avalonia.Controls
PublishNext(); PublishNext();
} }
private void ResourcesChanged2(object? sender, ResourcesChangedToken token)
{
PublishNext();
}
private object? GetValue() private object? GetValue()
{ {
var theme = _overrideThemeVariant ?? (_target.Owner as IThemeVariantHost)?.ActualThemeVariant; var theme = _overrideThemeVariant ?? (_target.Owner as IThemeVariantHost)?.ActualThemeVariant;

6
src/Avalonia.Base/Controls/ResourceProvider.cs

@ -45,7 +45,7 @@ public abstract class ResourceProvider : AvaloniaObject, IResourceProvider
protected void RaiseResourcesChanged() protected void RaiseResourcesChanged()
{ {
Owner?.NotifyHostedResourcesChanged(ResourcesChangedToken.Create()); Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
/// <summary> /// <summary>
@ -57,7 +57,7 @@ public abstract class ResourceProvider : AvaloniaObject, IResourceProvider
{ {
if (HasResources) if (HasResources)
{ {
owner.NotifyHostedResourcesChanged(ResourcesChangedToken.Create()); owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }
@ -70,7 +70,7 @@ public abstract class ResourceProvider : AvaloniaObject, IResourceProvider
{ {
if (HasResources) if (HasResources)
{ {
owner.NotifyHostedResourcesChanged(ResourcesChangedToken.Create()); owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }

28
src/Avalonia.Base/Controls/ResourcesChangedEventArgs.cs

@ -1,10 +1,24 @@
using System; using System.Threading;
namespace Avalonia.Controls namespace Avalonia.Controls;
/// <summary>
/// Represents the event arguments of <see cref="IResourceHost.ResourcesChanged"/>.
/// The <see cref="SequenceNumber"/> identifies the changes.
/// </summary>
/// <param name="SequenceNumber">The sequence number used to identify the changes.</param>
/// <remarks>
/// For performance reasons, this type is a struct.
/// Avoid using a default instance of this type or its default constructor, call <see cref="Create"/> instead.
/// </remarks>
public readonly record struct ResourcesChangedEventArgs(int SequenceNumber)
{ {
// TODO12: change this to be a struct, remove ResourcesChangedToken private static int s_lastSequenceNumber;
public class ResourcesChangedEventArgs : EventArgs
{ /// <summary>
public static new readonly ResourcesChangedEventArgs Empty = new ResourcesChangedEventArgs(); /// Creates a new instance of <see cref="ResourcesChangedEventArgs"/> with an auto-incremented sequence number.
} /// </summary>
/// <returns></returns>
public static ResourcesChangedEventArgs Create()
=> new(Interlocked.Increment(ref s_lastSequenceNumber));
} }

45
src/Avalonia.Base/Controls/ResourcesChangedHelper.cs

@ -1,45 +0,0 @@
using System;
using Avalonia.LogicalTree;
namespace Avalonia.Controls;
internal static class ResourcesChangedHelper
{
internal static void NotifyHostedResourcesChanged(this IResourceHost host, ResourcesChangedToken token)
{
if (host is IResourceHost2 host2)
host2.NotifyHostedResourcesChanged(token);
else
host.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
}
internal static void NotifyResourcesChanged(this ILogical logical, ResourcesChangedToken token)
{
if (logical is StyledElement styledElement)
styledElement.NotifyResourcesChanged(token);
else
logical.NotifyResourcesChanged(ResourcesChangedEventArgs.Empty);
}
internal static void SubscribeToResourcesChanged(
this IResourceHost host,
EventHandler<ResourcesChangedEventArgs> handler,
EventHandler<ResourcesChangedToken> handler2)
{
if (host is IResourceHost2 host2)
host2.ResourcesChanged2 += handler2;
else
host.ResourcesChanged += handler;
}
internal static void UnsubscribeFromResourcesChanged(
this IResourceHost host,
EventHandler<ResourcesChangedEventArgs> handler,
EventHandler<ResourcesChangedToken> handler2)
{
if (host is IResourceHost2 host2)
host2.ResourcesChanged2 -= handler2;
else
host.ResourcesChanged -= handler;
}
}

11
src/Avalonia.Base/Controls/ResourcesChangedToken.cs

@ -1,11 +0,0 @@
using System.Threading;
namespace Avalonia.Controls;
internal record struct ResourcesChangedToken(int SequenceNumber)
{
private static int s_lastSequenceNumber;
public static ResourcesChangedToken Create()
=> new(Interlocked.Increment(ref s_lastSequenceNumber));
}

39
src/Avalonia.Base/StyledElement.cs

@ -29,7 +29,7 @@ namespace Avalonia
IDataContextProvider, IDataContextProvider,
ILogical, ILogical,
IThemeVariantHost, IThemeVariantHost,
IResourceHost2, IResourceHost,
IStyleHost, IStyleHost,
ISetLogicalParent, ISetLogicalParent,
ISetInheritanceParent, ISetInheritanceParent,
@ -94,8 +94,7 @@ namespace Avalonia
private AvaloniaObject? _templatedParent; private AvaloniaObject? _templatedParent;
private bool _dataContextUpdating; private bool _dataContextUpdating;
private ControlTheme? _implicitTheme; private ControlTheme? _implicitTheme;
private EventHandler<ResourcesChangedToken>? _resourcesChanged2; private ResourcesChangedEventArgs _lastResourcesChangedEventArgs;
private ResourcesChangedToken _lastResourcesChangedToken;
/// <summary> /// <summary>
/// Initializes static members of the <see cref="StyledElement"/> class. /// Initializes static members of the <see cref="StyledElement"/> class.
@ -150,12 +149,6 @@ namespace Avalonia
/// </summary> /// </summary>
public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged; public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged;
event EventHandler<ResourcesChangedToken>? IResourceHost2.ResourcesChanged2
{
add => _resourcesChanged2 += value;
remove => _resourcesChanged2 -= value;
}
/// <inheritdoc /> /// <inheritdoc />
public event EventHandler? ActualThemeVariantChanged; public event EventHandler? ActualThemeVariantChanged;
@ -437,14 +430,11 @@ namespace Avalonia
/// <inheritdoc/> /// <inheritdoc/>
void ILogical.NotifyResourcesChanged(ResourcesChangedEventArgs e) void ILogical.NotifyResourcesChanged(ResourcesChangedEventArgs e)
=> NotifyResourcesChanged(ResourcesChangedToken.Create()); => NotifyResourcesChanged(e);
/// <inheritdoc/> /// <inheritdoc/>
void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e) void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e)
=> NotifyResourcesChanged(ResourcesChangedToken.Create()); => NotifyResourcesChanged(e);
void IResourceHost2.NotifyHostedResourcesChanged(ResourcesChangedToken token)
=> NotifyResourcesChanged(token);
/// <inheritdoc/> /// <inheritdoc/>
public bool TryGetResource(object key, ThemeVariant? theme, out object? value) public bool TryGetResource(object key, ThemeVariant? theme, out object? value)
@ -499,7 +489,7 @@ namespace Avalonia
// non-rooted control beacuse it's unlikely that dynamic resources need to be // non-rooted control beacuse it's unlikely that dynamic resources need to be
// correct until the control is added to the tree, and it causes a *lot* of // correct until the control is added to the tree, and it causes a *lot* of
// notifications. // notifications.
NotifyResourcesChanged(ResourcesChangedToken.Create()); NotifyResourcesChanged(ResourcesChangedEventArgs.Create());
} }
RaisePropertyChanged(ParentProperty, old, Parent); RaisePropertyChanged(ParentProperty, old, Parent);
@ -552,8 +542,8 @@ namespace Avalonia
/// <summary> /// <summary>
/// Notifies child controls that a change has been made to resources that apply to them. /// Notifies child controls that a change has been made to resources that apply to them.
/// </summary> /// </summary>
/// <param name="token">The change token.</param> /// <param name="e">The change token.</param>
internal virtual void NotifyChildResourcesChanged(ResourcesChangedToken token) internal virtual void NotifyChildResourcesChanged(ResourcesChangedEventArgs e)
{ {
if (_logicalChildren is object) if (_logicalChildren is object)
{ {
@ -563,7 +553,7 @@ namespace Avalonia
{ {
for (var i = 0; i < count; ++i) for (var i = 0; i < count; ++i)
{ {
_logicalChildren[i].NotifyResourcesChanged(token); _logicalChildren[i].NotifyResourcesChanged(e);
} }
} }
} }
@ -895,7 +885,7 @@ namespace Avalonia
ReevaluateImplicitTheme(); ReevaluateImplicitTheme();
ApplyStyling(); ApplyStyling();
NotifyResourcesChanged(ResourcesChangedToken.Create(), propagate: false); NotifyResourcesChanged(ResourcesChangedEventArgs.Create(), propagate: false);
OnAttachedToLogicalTree(e); OnAttachedToLogicalTree(e);
AttachedToLogicalTree?.Invoke(this, e); AttachedToLogicalTree?.Invoke(this, e);
@ -999,23 +989,22 @@ namespace Avalonia
} }
internal void NotifyResourcesChanged( internal void NotifyResourcesChanged(
ResourcesChangedToken token, ResourcesChangedEventArgs e,
bool propagate = true) bool propagate = true)
{ {
// We already got a notification for this element, ignore. // We already got a notification for this element, ignore.
if (token.Equals(_lastResourcesChangedToken)) if (e.Equals(_lastResourcesChangedEventArgs))
{ {
return; return;
} }
_lastResourcesChangedToken = token; _lastResourcesChangedEventArgs = e;
_resourcesChanged2?.Invoke(this, token); ResourcesChanged?.Invoke(this, e);
ResourcesChanged?.Invoke(this, ResourcesChangedEventArgs.Empty);
if (propagate) if (propagate)
{ {
NotifyChildResourcesChanged(token); NotifyChildResourcesChanged(e);
} }
} }

2
src/Avalonia.Base/Styling/StyleBase.cs

@ -54,7 +54,7 @@ namespace Avalonia.Styling
if (hadResources || _resources.HasResources) if (hadResources || _resources.HasResources)
{ {
Owner.NotifyHostedResourcesChanged(ResourcesChangedToken.Create()); Owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }
} }

16
src/Avalonia.Controls/Application.cs

@ -30,7 +30,7 @@ namespace Avalonia
/// method. /// method.
/// - Tracks the lifetime of the application. /// - Tracks the lifetime of the application.
/// </remarks> /// </remarks>
public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemplates, IGlobalStyles, IThemeVariantHost, IResourceHost2, IApplicationPlatformEvents, IOptionalFeatureProvider public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemplates, IGlobalStyles, IThemeVariantHost, IResourceHost, IApplicationPlatformEvents, IOptionalFeatureProvider
{ {
/// <summary> /// <summary>
/// The application-global data templates. /// The application-global data templates.
@ -43,7 +43,6 @@ namespace Avalonia
private Action<IReadOnlyList<IStyle>>? _stylesRemoved; private Action<IReadOnlyList<IStyle>>? _stylesRemoved;
private IApplicationLifetime? _applicationLifetime; private IApplicationLifetime? _applicationLifetime;
private bool _setupCompleted; private bool _setupCompleted;
private EventHandler<ResourcesChangedToken>? _resourcesChanged2;
/// <summary> /// <summary>
/// Defines the <see cref="DataContext"/> property. /// Defines the <see cref="DataContext"/> property.
@ -62,12 +61,6 @@ namespace Avalonia
/// <inheritdoc/> /// <inheritdoc/>
public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged; public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged;
event EventHandler<ResourcesChangedToken>? IResourceHost2.ResourcesChanged2
{
add => _resourcesChanged2 += value;
remove => _resourcesChanged2 -= value;
}
[Obsolete("Use Application.Current.TryGetFeature<IActivatableLifetime>() instead.")] [Obsolete("Use Application.Current.TryGetFeature<IActivatableLifetime>() instead.")]
public event EventHandler<UrlOpenedEventArgs>? UrlsOpened; public event EventHandler<UrlOpenedEventArgs>? UrlsOpened;
@ -239,16 +232,9 @@ namespace Avalonia
void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e) void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e)
{ {
_resourcesChanged2?.Invoke(this, ResourcesChangedToken.Create());
ResourcesChanged?.Invoke(this, e); ResourcesChanged?.Invoke(this, e);
} }
void IResourceHost2.NotifyHostedResourcesChanged(ResourcesChangedToken token)
{
_resourcesChanged2?.Invoke(this, token);
ResourcesChanged?.Invoke(this, ResourcesChangedEventArgs.Empty);
}
void IStyleHost.StylesAdded(IReadOnlyList<IStyle> styles) void IStyleHost.StylesAdded(IReadOnlyList<IStyle> styles)
{ {
_stylesAdded?.Invoke(styles); _stylesAdded?.Invoke(styles);

6
src/Avalonia.Controls/Primitives/TemplatedControl.cs

@ -362,7 +362,7 @@ namespace Avalonia.Controls.Primitives
} }
/// <inheritdoc /> /// <inheritdoc />
internal sealed override void NotifyChildResourcesChanged(ResourcesChangedToken token) internal sealed override void NotifyChildResourcesChanged(ResourcesChangedEventArgs e)
{ {
var count = VisualChildren.Count; var count = VisualChildren.Count;
@ -370,11 +370,11 @@ namespace Avalonia.Controls.Primitives
{ {
if (VisualChildren[i] is ILogical logical) if (VisualChildren[i] is ILogical logical)
{ {
logical.NotifyResourcesChanged(token); logical.NotifyResourcesChanged(e);
} }
} }
base.NotifyChildResourcesChanged(token); base.NotifyChildResourcesChanged(e);
} }
/// <inheritdoc/> /// <inheritdoc/>

6
src/Avalonia.Controls/Primitives/VisualLayerManager.cs

@ -118,12 +118,12 @@ namespace Avalonia.Controls.Primitives
} }
/// <inheritdoc /> /// <inheritdoc />
internal override void NotifyChildResourcesChanged(ResourcesChangedToken token) internal override void NotifyChildResourcesChanged(ResourcesChangedEventArgs e)
{ {
foreach (var l in _layers) foreach (var l in _layers)
l.NotifyResourcesChanged(token); l.NotifyResourcesChanged(e);
base.NotifyChildResourcesChanged(token); base.NotifyChildResourcesChanged(e);
} }
/// <inheritdoc /> /// <inheritdoc />

23
src/Avalonia.Controls/TopLevel.cs

@ -121,12 +121,6 @@ namespace Avalonia.Controls
(s, h) => s.ResourcesChanged -= h (s, h) => s.ResourcesChanged -= h
); );
private static readonly WeakEvent<IResourceHost2, ResourcesChangedToken>
ResourcesChanged2WeakEvent = WeakEvent.Register<IResourceHost2, ResourcesChangedToken>(
(s, h) => s.ResourcesChanged2 += h,
(s, h) => s.ResourcesChanged2 -= h
);
private readonly IInputManager? _inputManager; private readonly IInputManager? _inputManager;
private readonly IToolTipService? _tooltipService; private readonly IToolTipService? _tooltipService;
private readonly IAccessKeyHandler? _accessKeyHandler; private readonly IAccessKeyHandler? _accessKeyHandler;
@ -144,7 +138,6 @@ namespace Avalonia.Controls
private ILayoutManager? _layoutManager; private ILayoutManager? _layoutManager;
private Border? _transparencyFallbackBorder; private Border? _transparencyFallbackBorder;
private TargetWeakEventSubscriber<TopLevel, ResourcesChangedEventArgs>? _resourcesChangesSubscriber; private TargetWeakEventSubscriber<TopLevel, ResourcesChangedEventArgs>? _resourcesChangesSubscriber;
private TargetWeakEventSubscriber<TopLevel, ResourcesChangedToken>? _resourcesChangesSubscriber2;
private IStorageProvider? _storageProvider; private IStorageProvider? _storageProvider;
private Screens? _screens; private Screens? _screens;
private LayoutDiagnosticBridge? _layoutDiagnosticBridge; private LayoutDiagnosticBridge? _layoutDiagnosticBridge;
@ -270,25 +263,15 @@ namespace Avalonia.Controls
var stylingParent = ((IStyleHost)this).StylingParent; var stylingParent = ((IStyleHost)this).StylingParent;
if (stylingParent is IResourceHost2 applicationResources2) if (stylingParent is IResourceHost applicationResources2)
{ {
_resourcesChangesSubscriber2 = new TargetWeakEventSubscriber<TopLevel, ResourcesChangedToken>( _resourcesChangesSubscriber = new TargetWeakEventSubscriber<TopLevel, ResourcesChangedEventArgs>(
this, static (target, _, _, token) => this, static (target, _, _, token) =>
{ {
target.NotifyResourcesChanged(token); target.NotifyResourcesChanged(token);
}); });
ResourcesChanged2WeakEvent.Subscribe(applicationResources2, _resourcesChangesSubscriber2); ResourcesChangedWeakEvent.Subscribe(applicationResources2, _resourcesChangesSubscriber);
}
else if (stylingParent is IResourceHost applicationResources)
{
_resourcesChangesSubscriber = new TargetWeakEventSubscriber<TopLevel, ResourcesChangedEventArgs>(
this, static (target, _, _, _) =>
{
target.NotifyResourcesChanged(ResourcesChangedToken.Create());
});
ResourcesChangedWeakEvent.Subscribe(applicationResources, _resourcesChangesSubscriber);
} }
impl.LostFocus += PlatformImpl_LostFocus; impl.LostFocus += PlatformImpl_LostFocus;

2
src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs

@ -154,6 +154,6 @@ internal sealed class SystemAccentColors : ResourceProvider
private void PlatformSettingsOnColorValuesChanged(object? sender, PlatformColorValues e) private void PlatformSettingsOnColorValuesChanged(object? sender, PlatformColorValues e)
{ {
_invalidateColors = true; _invalidateColors = true;
Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }

2
src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs

@ -63,7 +63,7 @@ namespace Avalonia.Themes.Fluent
if (change.Property == DensityStyleProperty) if (change.Property == DensityStyleProperty)
{ {
Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Create());
} }
} }

6
src/Markup/Avalonia.Markup.Xaml/Data/DynamicResourceExpression.cs

@ -84,8 +84,6 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
private void ResourcesChanged(object? sender, ResourcesChangedEventArgs e) => PublishValue(); private void ResourcesChanged(object? sender, ResourcesChangedEventArgs e) => PublishValue();
private void ResourcesChanged2(object? sender, ResourcesChangedToken token) => PublishValue();
private void ActualThemeVariantChanged(object? sender, EventArgs e) private void ActualThemeVariantChanged(object? sender, EventArgs e)
{ {
if (!IsRunning) if (!IsRunning)
@ -134,7 +132,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
{ {
if (host is not null) if (host is not null)
{ {
host.SubscribeToResourcesChanged(ResourcesChanged, ResourcesChanged2); host.ResourcesChanged += ResourcesChanged;
if (!_overrideThemeVariant && _host is IThemeVariantHost themeVariantHost) if (!_overrideThemeVariant && _host is IThemeVariantHost themeVariantHost)
{ {
@ -148,7 +146,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
{ {
if (host is not null) if (host is not null)
{ {
host.UnsubscribeFromResourcesChanged(ResourcesChanged, ResourcesChanged2); host.ResourcesChanged -= ResourcesChanged;
if (!_overrideThemeVariant && _host is IThemeVariantHost themeVariantHost) if (!_overrideThemeVariant && _host is IThemeVariantHost themeVariantHost)
themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged; themeVariantHost.ActualThemeVariantChanged -= ActualThemeVariantChanged;

Loading…
Cancel
Save