Browse Source

Merge pull request #3676 from MarchingCube/scrollbar-delinq

More control benchmarks and ScrollBar constructor optimization
pull/3699/head
Dariusz Komosiński 6 years ago
committed by GitHub
parent
commit
3cd149f735
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 47
      src/Avalonia.Controls/Primitives/ScrollBar.cs
  2. 26
      tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs

47
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
/// </summary>
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<ScrollEventArgs> Scroll;
/// <summary>
/// Calculates whether the scrollbar should be visible.
/// Calculates and updates whether the scrollbar should be visible.
/// </summary>
/// <returns>The scrollbar's visibility.</returns>
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<Orientation>());
}
else
{
if (property == MinimumProperty ||
property == MaximumProperty ||
property == ViewportSizeProperty ||
property == VisibilityProperty)
{
UpdateIsVisible();
}
}
}
protected override void OnTemplateApplied(TemplateAppliedEventArgs e)

26
tests/Avalonia.Benchmarks/Layout/CalendarBenchmark.cs → 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();
Loading…
Cancel
Save