diff --git a/src/Avalonia.Controls/Primitives/ScrollBar.cs b/src/Avalonia.Controls/Primitives/ScrollBar.cs index 4d3ae3c5b8..161c7121c5 100644 --- a/src/Avalonia.Controls/Primitives/ScrollBar.cs +++ b/src/Avalonia.Controls/Primitives/ScrollBar.cs @@ -2,8 +2,6 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Reactive; -using System.Reactive.Linq; using Avalonia.Data; using Avalonia.Interactivity; using Avalonia.Input; @@ -64,13 +62,6 @@ namespace Avalonia.Controls.Primitives /// public ScrollBar() { - var isVisible = Observable.Merge( - this.GetObservable(MinimumProperty).Select(_ => Unit.Default), - this.GetObservable(MaximumProperty).Select(_ => Unit.Default), - this.GetObservable(ViewportSizeProperty).Select(_ => Unit.Default), - this.GetObservable(VisibilityProperty).Select(_ => Unit.Default)) - .Select(_ => CalculateIsVisible()); - this.Bind(IsVisibleProperty, isVisible, BindingPriority.Style); UpdatePseudoClasses(Orientation); } @@ -105,26 +96,20 @@ namespace Avalonia.Controls.Primitives public event EventHandler Scroll; /// - /// Calculates whether the scrollbar should be visible. + /// Calculates and updates whether the scrollbar should be visible. /// - /// The scrollbar's visibility. - private bool CalculateIsVisible() + private void UpdateIsVisible() { - switch (Visibility) + var isVisible = Visibility switch { - case ScrollBarVisibility.Visible: - return true; - - case ScrollBarVisibility.Disabled: - case ScrollBarVisibility.Hidden: - return false; - - case ScrollBarVisibility.Auto: - return double.IsNaN(ViewportSize) || Maximum > 0; - - default: - throw new InvalidOperationException("Invalid value for ScrollBar.Visibility."); - } + ScrollBarVisibility.Visible => true, + ScrollBarVisibility.Disabled => false, + ScrollBarVisibility.Hidden => false, + ScrollBarVisibility.Auto => (double.IsNaN(ViewportSize) || Maximum > 0), + _ => throw new InvalidOperationException("Invalid value for ScrollBar.Visibility.") + }; + + SetValue(IsVisibleProperty, isVisible, BindingPriority.Style); } protected override void OnKeyDown(KeyEventArgs e) @@ -153,6 +138,16 @@ namespace Avalonia.Controls.Primitives { UpdatePseudoClasses(newValue.GetValueOrDefault()); } + else + { + if (property == MinimumProperty || + property == MaximumProperty || + property == ViewportSizeProperty || + property == VisibilityProperty) + { + UpdateIsVisible(); + } + } } protected override void OnTemplateApplied(TemplateAppliedEventArgs e) diff --git a/tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs b/tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs similarity index 64% rename from tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs rename to tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs index c55912d94d..de40c247e6 100644 --- a/tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs +++ b/tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs @@ -7,12 +7,12 @@ using BenchmarkDotNet.Attributes; namespace Avalonia.Benchmarks.Layout { [MemoryDiagnoser] - public class CalendarBenchmark : IDisposable + public class ControlsBenchmark : IDisposable { private readonly IDisposable _app; private readonly TestRoot _root; - public CalendarBenchmark() + public ControlsBenchmark() { _app = UnitTestApplication.Start( TestServices.StyledWindow.With( @@ -38,6 +38,28 @@ namespace Avalonia.Benchmarks.Layout _root.LayoutManager.ExecuteLayoutPass(); } + [Benchmark] + [MethodImpl(MethodImplOptions.NoInlining)] + public void CreateButton() + { + var button = new Button(); + + _root.Child = button; + + _root.LayoutManager.ExecuteLayoutPass(); + } + + [Benchmark] + [MethodImpl(MethodImplOptions.NoInlining)] + public void CreateTextBox() + { + var textBox = new TextBox(); + + _root.Child = textBox; + + _root.LayoutManager.ExecuteLayoutPass(); + } + public void Dispose() { _app.Dispose();