From 8691aea157ed70030442b9f24fe542daa5c1481a Mon Sep 17 00:00:00 2001 From: Dariusz Komosinski Date: Sat, 20 Jun 2020 21:23:33 +0200 Subject: [PATCH] Cleanup code and add doc comments. --- src/Avalonia.Controls/Primitives/ScrollBar.cs | 42 ++++++---- src/Avalonia.Controls/ScrollViewer.cs | 80 ++++++++++--------- 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/ScrollBar.cs b/src/Avalonia.Controls/Primitives/ScrollBar.cs index 42f8eb1137..fc82fcc7a7 100644 --- a/src/Avalonia.Controls/Primitives/ScrollBar.cs +++ b/src/Avalonia.Controls/Primitives/ScrollBar.cs @@ -41,14 +41,20 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty OrientationProperty = AvaloniaProperty.Register(nameof(Orientation), Orientation.Vertical); - public static readonly StyledProperty AllowAutoHideProperty = - AvaloniaProperty.Register(nameof(AllowAutoHide), true); - + /// + /// Defines the property. + /// public static readonly DirectProperty IsExpandedProperty = AvaloniaProperty.RegisterDirect( nameof(IsExpanded), o => o.IsExpanded); + /// + /// Defines the property. + /// + public static readonly StyledProperty AllowAutoHideProperty = + AvaloniaProperty.Register(nameof(AllowAutoHide), true); + private Button _lineUpButton; private Button _lineDownButton; private Button _pageUpButton; @@ -73,18 +79,6 @@ namespace Avalonia.Controls.Primitives UpdatePseudoClasses(Orientation); } - public bool IsExpanded - { - get => _isExpanded; - private set => SetAndRaise(IsExpandedProperty, ref _isExpanded, value); - } - - public bool AllowAutoHide - { - get { return GetValue(AllowAutoHideProperty); } - set { SetValue(AllowAutoHideProperty, value); } - } - /// /// Gets or sets the amount of the scrollable content that is currently visible. /// @@ -113,6 +107,24 @@ namespace Avalonia.Controls.Primitives set { SetValue(OrientationProperty, value); } } + /// + /// Gets a value that indicates whether the scrollbar is expanded. + /// + public bool IsExpanded + { + get => _isExpanded; + private set => SetAndRaise(IsExpandedProperty, ref _isExpanded, value); + } + + /// + /// Gets a value that indicates whether the scrollbar can hide itself when user is not interacting with it. + /// + public bool AllowAutoHide + { + get => GetValue(AllowAutoHideProperty); + set => SetValue(AllowAutoHideProperty, value); + } + public event EventHandler Scroll; /// diff --git a/src/Avalonia.Controls/ScrollViewer.cs b/src/Avalonia.Controls/ScrollViewer.cs index 3be31bd67f..109768cead 100644 --- a/src/Avalonia.Controls/ScrollViewer.cs +++ b/src/Avalonia.Controls/ScrollViewer.cs @@ -160,9 +160,6 @@ namespace Avalonia.Controls nameof(VerticalScrollBarViewportSize), o => o.VerticalScrollBarViewportSize); - public static readonly DirectProperty IsExpandedProperty = - ScrollBar.IsExpandedProperty.AddOwner(o => o.IsExpanded); - /// /// Defines the property. /// @@ -171,6 +168,15 @@ namespace Avalonia.Controls nameof(VerticalScrollBarVisibility), ScrollBarVisibility.Auto); + /// + /// Defines the property. + /// + public static readonly DirectProperty IsExpandedProperty = + ScrollBar.IsExpandedProperty.AddOwner(o => o.IsExpanded); + + /// + /// Defines the property. + /// public static readonly StyledProperty AllowAutoHideProperty = ScrollBar.AllowAutoHideProperty.AddOwner(); @@ -223,18 +229,6 @@ namespace Avalonia.Controls remove => RemoveHandler(ScrollChangedEvent, value); } - public bool IsExpanded - { - get => _isExpanded; - private set => SetAndRaise(ScrollBar.IsExpandedProperty, ref _isExpanded, value); - } - - public bool AllowAutoHide - { - get { return GetValue(AllowAutoHideProperty); } - set { SetValue(AllowAutoHideProperty, value); } - } - /// /// Gets the extent of the scrollable content. /// @@ -405,6 +399,24 @@ namespace Avalonia.Controls get { return _viewport.Height; } } + /// + /// Gets a value that indicates whether any scrollbar is expanded. + /// + public bool IsExpanded + { + get => _isExpanded; + private set => SetAndRaise(ScrollBar.IsExpandedProperty, ref _isExpanded, value); + } + + /// + /// Gets a value that indicates whether scrollbars can hide itself when user is not interacting with it. + /// + public bool AllowAutoHide + { + get => GetValue(AllowAutoHideProperty); + set => SetValue(AllowAutoHideProperty, value); + } + /// /// Scrolls the content up one line. /// @@ -642,45 +654,41 @@ namespace Avalonia.Controls _scrollBarExpandSubscription?.Dispose(); - var horizontalScrollBar = e.NameScope.Find("PART_HorizontalScrollBar"); - var verticalScrollBar = e.NameScope.Find("PART_VerticalScrollBar"); - - IObservable horizontalExpanded = null, verticalExpanded = null; + _scrollBarExpandSubscription = SubscribeToScrollBars(e); + } - if (horizontalScrollBar != null) + private IDisposable SubscribeToScrollBars(TemplateAppliedEventArgs e) + { + static IObservable GetExpandedObservable(ScrollBar scrollBar) { - horizontalExpanded = horizontalScrollBar - .GetObservable(ScrollBar.IsExpandedProperty); + return scrollBar?.GetObservable(ScrollBar.IsExpandedProperty); } - if (verticalScrollBar != null) - { - verticalExpanded = verticalScrollBar - .GetObservable(ScrollBar.IsExpandedProperty); - } + var horizontalScrollBar = e.NameScope.Find("PART_HorizontalScrollBar"); + var verticalScrollBar = e.NameScope.Find("PART_VerticalScrollBar"); + + var horizontalExpanded = GetExpandedObservable(horizontalScrollBar); + var verticalExpanded = GetExpandedObservable(verticalScrollBar); - IObservable finalExpanded = null; + IObservable actualExpanded = null; if (horizontalExpanded != null && verticalExpanded != null) { - finalExpanded = horizontalExpanded.CombineLatest(verticalExpanded, (h, v) => h || v); + actualExpanded = horizontalExpanded.CombineLatest(verticalExpanded, (h, v) => h || v); } else { if (horizontalExpanded != null) { - finalExpanded = horizontalExpanded; - } + actualExpanded = horizontalExpanded; + } else if (verticalExpanded != null) { - finalExpanded = verticalExpanded; + actualExpanded = verticalExpanded; } } - if (finalExpanded != null) - { - _scrollBarExpandSubscription = finalExpanded.Subscribe(OnScrollBarExpandedChanged); - } + return actualExpanded?.Subscribe(OnScrollBarExpandedChanged); } private void OnScrollBarExpandedChanged(bool isExpanded)