Skip to content

Commit

Permalink
Adjust diagnostic apis (#17022)
Browse files Browse the repository at this point in the history
* Make base project internals visible to DiagnosticsSupport, just in case for now

* Make ValueStoreDiagnostic diagnostic API more usable with wider range of types

* Add MultiBindingExpression internal members for visibilty
  • Loading branch information
maxkatz6 authored Sep 14, 2024
1 parent e22cfa8 commit 751b273
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Avalonia.Base/Avalonia.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<InternalsVisibleTo Include="Avalonia.Tizen, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.Dialogs, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.Diagnostics, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="AvaloniaUI.DiagnosticsSupport.Avalonia, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.LinuxFramebuffer, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.Base/Data/Core/MultiBindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public MultiBindingExpression(
}

public override string Description => "MultiBinding";
internal UntypedBindingExpressionBase?[] Expressions => _expressions;
internal IMultiValueConverter? Converter => _converter;
internal CultureInfo? ConverterCulture => _converterCulture;
internal object? ConverterParameter => _converterParameter;
internal object? FallbackValue => _fallbackValue;
internal object? TargetNullValue => _targetNullValue;

protected override void StartCore()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Avalonia.Base/Diagnostics/AvaloniaObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ public static AvaloniaPropertyValue GetDiagnostic(this AvaloniaObject o, Avaloni
{
return o.GetDiagnosticInternal(property);
}

/// <summary>
/// Gets a value store diagnostics for a <see cref="AvaloniaObject"/>.
/// </summary>
/// <param name="avaloniaObject">The avalonia object.</param>
public static ValueStoreDiagnostic GetValueStoreDiagnostic(this AvaloniaObject avaloniaObject)
{
return avaloniaObject.GetValueStore().GetStoreDiagnostic();
}
}
}
4 changes: 2 additions & 2 deletions src/Avalonia.Base/Diagnostics/IValueFrameDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public enum FrameType
Style,
Template
}
string? Description { get; }

object? Source { get; }
FrameType Type { get; }
bool IsActive { get; }
BindingPriority Priority { get; }
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.Base/Diagnostics/LocalValueFrameDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public LocalValueFrameDiagnostic(IEnumerable<ValueEntryDiagnostic> values)
{
Values = values;
}
public string? Description => null;

public object? Source => null;
public IValueFrameDiagnostic.FrameType Type => IValueFrameDiagnostic.FrameType.Local;
public bool IsActive => true;
public BindingPriority Priority => BindingPriority.LocalValue;
Expand Down
24 changes: 1 addition & 23 deletions src/Avalonia.Base/Diagnostics/StyleValueFrameDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ internal StyleValueFrameDiagnostic(StyleInstance styleInstance)
_styleInstance = styleInstance;
}

public string? Description => _styleInstance.Source switch
{
Style s => GetFullSelector(s),
ControlTheme t => t.TargetType?.Name,
_ => null
};
public object? Source => _styleInstance.Source;

public IValueFrameDiagnostic.FrameType Type => _styleInstance.Source switch
{
Expand All @@ -45,23 +40,6 @@ public IEnumerable<ValueEntryDiagnostic> Values
}
}

private string GetFullSelector(Style? style)
{
var selectors = new Stack<string>();

while (style is not null)
{
if (style.Selector is not null)
{
selectors.Push(style.Selector.ToString());
}

style = style.Parent as Style;
}

return string.Concat(selectors);
}

[Unstable("Compatibility with 11.x")]
public AppliedStyle AsAppliedStyle() => new AppliedStyle(_styleInstance);
}
11 changes: 1 addition & 10 deletions src/Avalonia.Base/Diagnostics/StyledElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ namespace Avalonia.Diagnostics;
[PrivateApi]
public static class StyledElementExtensions
{
/// <summary>
/// Gets a style diagnostics for a <see cref="StyledElement"/>.
/// </summary>
/// <param name="styledElement">The element.</param>
public static ValueStoreDiagnostic GetValueStoreDiagnostic(this StyledElement styledElement)
{
return styledElement.GetValueStore().GetStoreDiagnostic();
}

[Obsolete("Use StyledElementExtensions.GetValueStoreDiagnostic instead", true)]
[Obsolete("Use AvaloniaObjectExtensions.GetValueStoreDiagnostic instead", true)]
public static StyleDiagnostics GetStyleDiagnostics(this StyledElement styledElement)
{
var diagnostics = styledElement.GetValueStore().GetStoreDiagnostic();
Expand Down
5 changes: 3 additions & 2 deletions src/Avalonia.Base/Diagnostics/ValueFrameDiagnostic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Avalonia.Data;
using Avalonia.PropertyStore;
using Avalonia.Styling;
Expand All @@ -14,7 +15,7 @@ internal ValueFrameDiagnostic(ValueFrame valueFrame)
_valueFrame = valueFrame;
}

public string? Description => (_valueFrame.Owner?.Owner as StyledElement)?.StyleKey.Name;
public object? Source => _valueFrame.Owner?.Owner;

public IValueFrameDiagnostic.FrameType Type => IValueFrameDiagnostic.FrameType.Template;

Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Controls/Avalonia.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<InternalsVisibleTo Include="Avalonia.Markup.UnitTests, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.DesignerSupport, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.Diagnostics, PublicKey=$(AvaloniaPublicKey)"/>
<InternalsVisibleTo Include="AvaloniaUI.DiagnosticsSupport.Avalonia, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.LeakTests, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.Headless, PublicKey=$(AvaloniaPublicKey)" />
<InternalsVisibleTo Include="Avalonia.Native, PublicKey=$(AvaloniaPublicKey)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public ValueFrameViewModel(StyledElement styledElement, IValueFrameDiagnostic va
_valueFrame = valueFrame;
IsVisible = true;

Description = (_valueFrame.Type, _valueFrame.Description) switch
var source = SourceToString(_valueFrame.Source);
Description = (_valueFrame.Type, source) switch
{
(IValueFrameDiagnostic.FrameType.Local, _) => "Local Values " + _valueFrame.Description,
(IValueFrameDiagnostic.FrameType.Template, _) => "Template " + _valueFrame.Description,
(IValueFrameDiagnostic.FrameType.Theme, _) => "Theme " + _valueFrame.Description,
(_, {Length:>0}) => _valueFrame.Description,
(IValueFrameDiagnostic.FrameType.Local, _) => "Local Values " + source,
(IValueFrameDiagnostic.FrameType.Template, _) => "Template " + source,
(IValueFrameDiagnostic.FrameType.Theme, _) => "Theme " + source,
(_, {Length:>0}) => source,
_ => _valueFrame.Priority.ToString()
};

Expand Down Expand Up @@ -113,5 +114,40 @@ private static bool IsBinding(object? value)

return false;
}

private string? SourceToString(object? source)
{
if (source is Style style)
{
StyleBase? currentStyle = style;
var selectors = new Stack<string>();

while (currentStyle is not null)
{
if (currentStyle is Style { Selector: { } selector })
{
selectors.Push(selector.ToString());
}
if (currentStyle is ControlTheme theme)
{
selectors.Push("Theme " + theme.TargetType?.Name);
}

currentStyle = currentStyle.Parent as StyleBase;
}

return string.Concat(selectors).Replace("^", "");
}
else if (source is ControlTheme controlTheme)
{
return controlTheme.TargetType?.Name;
}
else if (source is StyledElement styledElement)
{
return styledElement.StyleKey?.Name;
}

return null;
}
}
}

0 comments on commit 751b273

Please sign in to comment.