// Copyright (c) The Perspex Project. All rights reserved. // 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 Perspex.Data; namespace Perspex.Controls.Primitives { /// /// A scrollbar control. /// public class ScrollBar : RangeBase { /// /// Defines the property. /// public static readonly StyledProperty ViewportSizeProperty = PerspexProperty.Register(nameof(ViewportSize), defaultValue: double.NaN); /// /// Defines the property. /// public static readonly StyledProperty VisibilityProperty = PerspexProperty.Register(nameof(Visibility)); /// /// Defines the property. /// public static readonly StyledProperty OrientationProperty = PerspexProperty.Register(nameof(Orientation)); /// /// Initializes static members of the class. /// static ScrollBar() { PseudoClass(OrientationProperty, o => o == Perspex.Controls.Orientation.Vertical, ":vertical"); PseudoClass(OrientationProperty, o => o == Perspex.Controls.Orientation.Horizontal, ":horizontal"); } /// /// Initializes a new instance of the class. /// 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()); Bind(IsVisibleProperty, isVisible, BindingPriority.Style); } /// /// Gets or sets the amount of the scrollable content that is currently visible. /// public double ViewportSize { get { return GetValue(ViewportSizeProperty); } set { SetValue(ViewportSizeProperty, value); } } /// /// Gets or sets a value that indicates whether the scrollbar should hide itself when it /// is not needed. /// public ScrollBarVisibility Visibility { get { return GetValue(VisibilityProperty); } set { SetValue(VisibilityProperty, value); } } /// /// Gets or sets the orientation of the scrollbar. /// public Orientation Orientation { get { return GetValue(OrientationProperty); } set { SetValue(OrientationProperty, value); } } /// /// Calculates whether the scrollbar should be visible. /// /// The scrollbar's visibility. private bool CalculateIsVisible() { switch (Visibility) { case ScrollBarVisibility.Visible: return true; case ScrollBarVisibility.Hidden: return false; case ScrollBarVisibility.Auto: var viewportSize = ViewportSize; return double.IsNaN(viewportSize) || viewportSize < Maximum - Minimum; default: throw new InvalidOperationException("Invalid value for ScrollBar.Visibility."); } } } }