Skip to content

Commit

Permalink
Fix control catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Aug 15, 2022
1 parent 7dfceb1 commit a75fe22
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions samples/ControlCatalog/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Themes.Fluent;

using ControlCatalog.Models;
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Controls/ResourceNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static bool TryFindResource(this IResourceHost control, object key, Theme
return false;
}

/// <inheritdoc cref="IResourceHost.TryGetResource" />
/// <inheritdoc cref="IResourceNode.TryGetResource" />
public static bool TryGetResource(this IResourceHost control, object key, out object? value)
{
control = control ?? throw new ArgumentNullException(nameof(control));
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Diagnostics/Diagnostics/DevToolsOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Avalonia.Input;
using Avalonia.Styling;

namespace Avalonia.Diagnostics
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public object ProvideValue(IServiceProvider serviceProvider)
}

var previousWasControlTheme = false;
var firstParentVisited = false;

// Look upwards though the ambient context for IResourceNodes
// which might be able to give us the resource.
Expand All @@ -56,22 +55,24 @@ public object ProvideValue(IServiceProvider serviceProvider)
return ColorToBrushConverter.Convert(value, targetType);
}

// To get a fallback theme variant, check if static resource was invoked inside of the ResourceDictionary.ThemeDictionaries.
if (themeVariant is null && firstParentVisited && containingDictionary is not null)
// To get a fallback theme variant, check if static resource was invoked inside of the ResourceDictionary.ThemeDictionaries.
if (themeVariant is null)
{
if (parent is IResourceDictionary parentDictionary
&& parentDictionary.ThemeDictionaries
.FirstOrDefault(p => p.Value == containingDictionary).Key is { } key)
if (containingDictionary is not null)
{
themeVariant = key;
if (parent is ResourceDictionary parentDictionary
&& parentDictionary.ThemeDictionaries
.FirstOrDefault(p => p.Value == containingDictionary).Key is { } key)
{
themeVariant = key;
}

containingDictionary = null;
}
else
{
containingDictionary = parent as ResourceDictionary;
}
containingDictionary = null;
}

if (!firstParentVisited)
{
firstParentVisited = true;
containingDictionary = parent as IResourceDictionary;
}

// HACK: Temporary fix for #8678. Hard-coded to only work for the DevTools main
Expand Down
2 changes: 1 addition & 1 deletion tests/Avalonia.Benchmarks/Styling/ResourceBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static Styles CreateTheme()
return new Styles
{
preHost,
new TestStyles(50, 3, 5),
new TestStyles(50, 3, 5, 0),
postHost
};
}
Expand Down
19 changes: 16 additions & 3 deletions tests/Avalonia.Benchmarks/TestStyles.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Avalonia.Styling;
using Avalonia.Controls;
using Avalonia.Styling;

namespace Avalonia.Benchmarks
{
public class TestStyles : Styles
{
public TestStyles(int childStylesCount, int childInnerStyleCount, int childResourceCount)
public TestStyles(int childStylesCount, int childInnerStyleCount, int childResourceCount, int childThemeResourcesCount)
{
for (int i = 0; i < childStylesCount; i++)
{
Expand All @@ -18,7 +19,19 @@ public TestStyles(int childStylesCount, int childInnerStyleCount, int childResou
{
childStyle.Resources.Add($"resource.{i}.{j}.{k}", null);
}


if (childThemeResourcesCount > 0)
{
ResourceDictionary darkTheme, lightTheme;
childStyle.Resources.ThemeDictionaries[ThemeVariant.Dark] = darkTheme = new ResourceDictionary();
childStyle.Resources.ThemeDictionaries[ThemeVariant.Light] = lightTheme = new ResourceDictionary();
for (int k = 0; k < childThemeResourcesCount; k++)
{
darkTheme.Add($"resource.theme.{i}.{j}.{k}", null);
lightTheme.Add($"resource.theme.{i}.{j}.{k}", null);
}
}

childStyles.Add(childStyle);
}

Expand Down

0 comments on commit a75fe22

Please sign in to comment.