Browse Source
- Removes the `IStyler` service and the `Styler` implementation - Moves the logic for applying styles and control themes into `StyledElement` - Removes the style `TryAttach` method from the public API - Removes style caching for now - this will need to be added backpull/9411/head
30 changed files with 494 additions and 636 deletions
@ -1,8 +0,0 @@ |
|||
|
|||
namespace Avalonia.Styling |
|||
{ |
|||
public interface IStyler |
|||
{ |
|||
void ApplyStyles(IStyleable control); |
|||
} |
|||
} |
|||
@ -1,58 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Avalonia.Styling |
|||
{ |
|||
/// <summary>
|
|||
/// Simple cache for improving performance of applying styles.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// Maps <see cref="IStyleable.StyleKey"/> to a list of styles that are known be be possible
|
|||
/// matches.
|
|||
/// </remarks>
|
|||
internal class StyleCache : Dictionary<Type, List<IStyle>?> |
|||
{ |
|||
public SelectorMatchResult TryAttach(IList<IStyle> styles, IStyleable target, object? host) |
|||
{ |
|||
if (TryGetValue(target.StyleKey, out var cached)) |
|||
{ |
|||
if (cached is object) |
|||
{ |
|||
var result = SelectorMatchResult.NeverThisType; |
|||
|
|||
foreach (var style in cached) |
|||
{ |
|||
var childResult = style.TryAttach(target, host); |
|||
if (childResult > result) |
|||
result = childResult; |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
else |
|||
{ |
|||
return SelectorMatchResult.NeverThisType; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
List<IStyle>? matches = null; |
|||
|
|||
foreach (var child in styles) |
|||
{ |
|||
if (child.TryAttach(target, host) != SelectorMatchResult.NeverThisType) |
|||
{ |
|||
matches ??= new List<IStyle>(); |
|||
matches.Add(child); |
|||
} |
|||
} |
|||
|
|||
Add(target.StyleKey, matches); |
|||
|
|||
return matches is null ? |
|||
SelectorMatchResult.NeverThisType : |
|||
SelectorMatchResult.AlwaysThisType; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,35 +0,0 @@ |
|||
using System; |
|||
|
|||
namespace Avalonia.Styling |
|||
{ |
|||
public class Styler : IStyler |
|||
{ |
|||
public void ApplyStyles(IStyleable target) |
|||
{ |
|||
_ = target ?? throw new ArgumentNullException(nameof(target)); |
|||
|
|||
// Apply the control theme.
|
|||
target.GetEffectiveTheme()?.TryAttach(target, target); |
|||
|
|||
// If the control has a themed templated parent then apply the styles from the
|
|||
// templated parent theme.
|
|||
if (target.TemplatedParent is IStyleable styleableParent) |
|||
styleableParent.GetEffectiveTheme()?.TryAttach(target, styleableParent); |
|||
|
|||
// Apply styles from the rest of the tree.
|
|||
if (target is IStyleHost styleHost) |
|||
ApplyStyles(target, styleHost); |
|||
} |
|||
|
|||
private void ApplyStyles(IStyleable target, IStyleHost host) |
|||
{ |
|||
var parent = host.StylingParent; |
|||
|
|||
if (parent != null) |
|||
ApplyStyles(target, parent); |
|||
|
|||
if (host.IsStylesInitialized) |
|||
host.Styles.TryAttach(target, host); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue