using System.Runtime.CompilerServices; using Avalonia.Controls; using Avalonia.PropertyStore; using Avalonia.Styling; using Avalonia.UnitTests; using BenchmarkDotNet.Attributes; #nullable enable namespace Avalonia.Benchmarks.Styling { [MemoryDiagnoser] public class Style_Activation { private TestClass _target = null!; public Style_Activation() { RuntimeHelpers.RunClassConstructor(typeof(TestClass).TypeHandle); } [GlobalSetup] public void Setup() { _target = new TestClass(); var style = new Style(x => x.OfType().Class("foo")) { Setters = { new Setter(TestClass.StringProperty, "foo") } }; StyleHelpers.TryAttach(style, _target); } [Benchmark] public void Toggle_Style_Activation_Via_Class() { for (var i = 0; i < 100; ++i) { _target.Classes.Add("foo"); _target.Classes.Remove("foo"); } } private class TestClass : Control { public static readonly StyledProperty StringProperty = AvaloniaProperty.Register("String"); public static readonly StyledProperty Struct1Property = AvaloniaProperty.Register("Struct1"); public static readonly StyledProperty Struct2Property = AvaloniaProperty.Register("Struct2"); public static readonly StyledProperty Struct3Property = AvaloniaProperty.Register("Struct3"); public static readonly StyledProperty Struct4Property = AvaloniaProperty.Register("Struct4"); public static readonly StyledProperty Struct5Property = AvaloniaProperty.Register("Struct5"); public static readonly StyledProperty Struct6Property = AvaloniaProperty.Register("Struct6"); public static readonly StyledProperty Struct7Property = AvaloniaProperty.Register("Struct7"); public static readonly StyledProperty Struct8Property = AvaloniaProperty.Register("Struct8"); } } }