// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls.Primitives { using System; using System.Reactive; using System.Reactive.Linq; /// /// A scrollbar control. /// public class ScrollBar : RangeBase { /// /// Defines the property. /// public static readonly PerspexProperty ViewportSizeProperty = PerspexProperty.Register(nameof(ViewportSize), defaultValue: double.NaN); /// /// Defines the property. /// public static readonly PerspexProperty VisibilityProperty = PerspexProperty.Register(nameof(Visibility)); /// /// Defines the property. /// public static readonly PerspexProperty OrientationProperty = PerspexProperty.Register(nameof(Orientation)); /// /// 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(_ => this.CalculateIsVisible()); this.Bind(ScrollBar.IsVisibleProperty, isVisible, BindingPriority.Style); } /// /// Gets or sets the amount of the scrollable content that is currently visible. /// public double ViewportSize { get { return this.GetValue(ViewportSizeProperty); } set { this.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 this.GetValue(VisibilityProperty); } set { this.SetValue(VisibilityProperty, value); } } /// /// Gets or sets the orientation of the scrollbar. /// public Orientation Orientation { get { return this.GetValue(OrientationProperty); } set { this.SetValue(OrientationProperty, value); } } /// protected override Size MeasureOverride(Size availableSize) { return base.MeasureOverride(availableSize); } /// /// Calculates whether the scrollbar should be visible. /// /// The scrollbar's visibility. private bool CalculateIsVisible() { switch (this.Visibility) { case ScrollBarVisibility.Visible: return true; case ScrollBarVisibility.Hidden: return false; case ScrollBarVisibility.Auto: var viewportSize = this.ViewportSize; return double.IsNaN(viewportSize) || viewportSize < this.Maximum - this.Minimum; default: throw new InvalidOperationException("Invalid value for ScrollBar.Visibility."); } } } }