|
|
@ -3,6 +3,7 @@ using System.Collections.Generic; |
|
|
using Avalonia.Animation; |
|
|
using Avalonia.Animation; |
|
|
using Avalonia.Controls; |
|
|
using Avalonia.Controls; |
|
|
using Avalonia.Metadata; |
|
|
using Avalonia.Metadata; |
|
|
|
|
|
using Avalonia.Styling.Activators; |
|
|
|
|
|
|
|
|
namespace Avalonia.Styling |
|
|
namespace Avalonia.Styling |
|
|
{ |
|
|
{ |
|
|
@ -64,43 +65,14 @@ namespace Avalonia.Styling |
|
|
bool IResourceNode.HasResources => _resources?.Count > 0; |
|
|
bool IResourceNode.HasResources => _resources?.Count > 0; |
|
|
IReadOnlyList<IStyle> IStyle.Children => (IReadOnlyList<IStyle>?)_children ?? Array.Empty<IStyle>(); |
|
|
IReadOnlyList<IStyle> IStyle.Children => (IReadOnlyList<IStyle>?)_children ?? Array.Empty<IStyle>(); |
|
|
|
|
|
|
|
|
internal abstract bool HasSelector { get; } |
|
|
internal bool HasSettersOrAnimations => _setters?.Count > 0 || _animations?.Count > 0; |
|
|
|
|
|
|
|
|
public void Add(ISetter setter) => Setters.Add(setter); |
|
|
public void Add(ISetter setter) => Setters.Add(setter); |
|
|
public void Add(IStyle style) => Children.Add(style); |
|
|
public void Add(IStyle style) => Children.Add(style); |
|
|
|
|
|
|
|
|
public event EventHandler? OwnerChanged; |
|
|
public event EventHandler? OwnerChanged; |
|
|
|
|
|
|
|
|
public SelectorMatchResult TryAttach(IStyleable target, object? host) |
|
|
public abstract SelectorMatchResult TryAttach(IStyleable target, object? host); |
|
|
{ |
|
|
|
|
|
target = target ?? throw new ArgumentNullException(nameof(target)); |
|
|
|
|
|
|
|
|
|
|
|
var result = SelectorMatchResult.NeverThisType; |
|
|
|
|
|
|
|
|
|
|
|
if (_setters?.Count > 0 || _animations?.Count > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
var match = Match(target, host, subscribe: true); |
|
|
|
|
|
|
|
|
|
|
|
if (match.IsMatch) |
|
|
|
|
|
{ |
|
|
|
|
|
var instance = new StyleInstance(this, target, _setters, _animations, match.Activator); |
|
|
|
|
|
target.StyleApplied(instance); |
|
|
|
|
|
instance.Start(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
result = match.Result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (_children is not null) |
|
|
|
|
|
{ |
|
|
|
|
|
_childCache ??= new StyleCache(); |
|
|
|
|
|
var childResult = _childCache.TryAttach(_children, target, host); |
|
|
|
|
|
if (childResult > result) |
|
|
|
|
|
result = childResult; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool TryGetResource(object key, out object? result) |
|
|
public bool TryGetResource(object key, out object? result) |
|
|
{ |
|
|
{ |
|
|
@ -108,19 +80,20 @@ namespace Avalonia.Styling |
|
|
return _resources?.TryGetResource(key, out result) ?? false; |
|
|
return _resources?.TryGetResource(key, out result) ?? false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
internal void Attach(IStyleable target, IStyleActivator? activator) |
|
|
/// Evaluates the style's selector against the specified target element.
|
|
|
{ |
|
|
/// </summary>
|
|
|
var instance = new StyleInstance(this, target, _setters, _animations, activator); |
|
|
/// <param name="control">The control.</param>
|
|
|
target.StyleApplied(instance); |
|
|
/// <param name="host">The element that hosts the style.</param>
|
|
|
instance.Start(); |
|
|
/// <param name="subscribe">
|
|
|
} |
|
|
/// Whether the match should subscribe to changes in order to track the match over time,
|
|
|
|
|
|
/// or simply return an immediate result.
|
|
|
internal SelectorMatchResult TryAttachChildren(IStyleable target, object? host) |
|
|
/// </param>
|
|
|
{ |
|
|
/// <returns>
|
|
|
if (_children is null || _children.Count == 0) |
|
|
/// A <see cref="SelectorMatchResult"/> describing how the style matches the control.
|
|
|
return SelectorMatchResult.NeverThisType; |
|
|
/// </returns>
|
|
|
_childCache ??= new StyleCache(); |
|
|
internal abstract SelectorMatch Match(IStyleable control, object? host, bool subscribe); |
|
|
return _childCache.TryAttach(_children, target, host); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
internal virtual void SetParent(StyleBase? parent) => Parent = parent; |
|
|
internal virtual void SetParent(StyleBase? parent) => Parent = parent; |
|
|
|
|
|
|
|
|
|