// 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.Linq; using System.Reactive.Linq; using Perspex.Controls.Presenters; using Perspex.Controls.Primitives; namespace Perspex.Controls { /// /// A control scrolls its content if the content is bigger than the space available. /// public class ScrollViewer : ContentControl { /// /// Defines the property. /// public static readonly AttachedProperty CanScrollHorizontallyProperty = PerspexProperty.RegisterAttached(nameof(CanScrollHorizontally), true); /// /// Defines the property. /// public static readonly DirectProperty ExtentProperty = PerspexProperty.RegisterDirect(nameof(Extent), o => o.Extent, (o, v) => o.Extent = v); /// /// Defines the property. /// public static readonly DirectProperty OffsetProperty = PerspexProperty.RegisterDirect( nameof(Offset), o => o.Offset, (o, v) => o.Offset = v); /// /// Defines the property. /// public static readonly DirectProperty ViewportProperty = PerspexProperty.RegisterDirect(nameof(Viewport), o => o.Viewport, (o, v) => o.Viewport = v); /// /// Defines the HorizontalScrollBarMaximum property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly DirectProperty HorizontalScrollBarMaximumProperty = PerspexProperty.RegisterDirect( nameof(HorizontalScrollBarMaximum), o => o.HorizontalScrollBarMaximum); /// /// Defines the HorizontalScrollBarValue property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly DirectProperty HorizontalScrollBarValueProperty = PerspexProperty.RegisterDirect( nameof(HorizontalScrollBarValue), o => o.HorizontalScrollBarValue, (o, v) => o.HorizontalScrollBarValue = v); /// /// Defines the HorizontalScrollBarViewportSize property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly DirectProperty HorizontalScrollBarViewportSizeProperty = PerspexProperty.RegisterDirect( nameof(HorizontalScrollBarViewportSize), o => o.HorizontalScrollBarViewportSize); /// /// Defines the property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly AttachedProperty HorizontalScrollBarVisibilityProperty = PerspexProperty.RegisterAttached( nameof(HorizontalScrollBarVisibility), ScrollBarVisibility.Auto); /// /// Defines the VerticalScrollBarMaximum property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly DirectProperty VerticalScrollBarMaximumProperty = PerspexProperty.RegisterDirect( nameof(VerticalScrollBarMaximum), o => o.VerticalScrollBarMaximum); /// /// Defines the VerticalScrollBarValue property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly DirectProperty VerticalScrollBarValueProperty = PerspexProperty.RegisterDirect( nameof(VerticalScrollBarValue), o => o.VerticalScrollBarValue, (o, v) => o.VerticalScrollBarValue = v); /// /// Defines the VerticalScrollBarViewportSize property. /// /// /// There is no public C# accessor for this property as it is intended to be bound to by a /// in the control's template. /// public static readonly DirectProperty VerticalScrollBarViewportSizeProperty = PerspexProperty.RegisterDirect( nameof(VerticalScrollBarViewportSize), o => o.VerticalScrollBarViewportSize); /// /// Defines the property. /// public static readonly AttachedProperty VerticalScrollBarVisibilityProperty = PerspexProperty.RegisterAttached( nameof(VerticalScrollBarVisibility), ScrollBarVisibility.Auto); private Size _extent; private Vector _offset; private Size _viewport; /// /// Initializes static members of the class. /// static ScrollViewer() { AffectsValidation(ExtentProperty, OffsetProperty); AffectsValidation(ViewportProperty, OffsetProperty); } /// /// Initializes a new instance of the class. /// public ScrollViewer() { var extentAndViewport = Observable.CombineLatest( this.GetObservable(ExtentProperty), this.GetObservable(ViewportProperty)) .Select(x => new { Extent = x[0], Viewport = x[1] }); } /// /// Gets the extent of the scrollable content. /// public Size Extent { get { return _extent; } private set { if (SetAndRaise(ExtentProperty, ref _extent, value)) { CalculatedPropertiesChanged(); } } } /// /// Gets or sets the current scroll offset. /// public Vector Offset { get { return _offset; } set { value = ValidateOffset(this, value); if (SetAndRaise(OffsetProperty, ref _offset, value)) { CalculatedPropertiesChanged(); } } } /// /// Gets the size of the viewport on the scrollable content. /// public Size Viewport { get { return _viewport; } private set { SetAndRaise(ViewportProperty, ref _viewport, value); } } /// /// Gets a value indicating whether the content can be scrolled horizontally. /// public bool CanScrollHorizontally { get { return GetValue(CanScrollHorizontallyProperty); } set { SetValue(CanScrollHorizontallyProperty, value); } } /// /// Gets or sets the horizontal scrollbar visibility. /// public ScrollBarVisibility HorizontalScrollBarVisibility { get { return GetValue(HorizontalScrollBarVisibilityProperty); } set { SetValue(HorizontalScrollBarVisibilityProperty, value); } } /// /// Gets or sets the vertical scrollbar visibility. /// public ScrollBarVisibility VerticalScrollBarVisibility { get { return GetValue(VerticalScrollBarVisibilityProperty); } set { SetValue(VerticalScrollBarVisibilityProperty, value); } } /// /// Gets the maximum horizontal scrollbar value. /// protected double HorizontalScrollBarMaximum { get { return Max(_extent.Width - _viewport.Width, 0); } } /// /// Gets or sets the horizontal scrollbar value. /// protected double HorizontalScrollBarValue { get { return _offset.X; } set { if (_offset.X != value) { var old = Offset.X; Offset = Offset.WithX(value); RaisePropertyChanged(HorizontalScrollBarValueProperty, old, value); } } } /// /// Gets the size of the horizontal scrollbar viewport. /// protected double HorizontalScrollBarViewportSize { get { return Max((_viewport.Width / _extent.Width) * (_extent.Width - _viewport.Width), 0); } } /// /// Gets the maximum vertical scrollbar value. /// protected double VerticalScrollBarMaximum { get { return Max(_extent.Height - _viewport.Height, 0); } } /// /// Gets or sets the vertical scrollbar value. /// protected double VerticalScrollBarValue { get { return _offset.Y; } set { if (_offset.Y != value) { var old = Offset.Y; Offset = Offset.WithY(value); RaisePropertyChanged(VerticalScrollBarValueProperty, old, value); } } } /// /// Gets the size of the vertical scrollbar viewport. /// protected double VerticalScrollBarViewportSize { get { return Max((_viewport.Height / _extent.Height) * (_extent.Height - _viewport.Height), 0); } } internal static Vector CoerceOffset(Size extent, Size viewport, Vector offset) { var maxX = Math.Max(extent.Width - viewport.Width, 0); var maxY = Math.Max(extent.Height - viewport.Height, 0); return new Vector(Clamp(offset.X, 0, maxX), Clamp(offset.Y, 0, maxY)); } private static double Clamp(double value, double min, double max) { return (value < min) ? min : (value > max) ? max : value; } private static double Max(double x, double y) { var result = Math.Max(x, y); return double.IsNaN(result) ? 0 : Math.Round(result); } private static Vector ValidateOffset(PerspexObject o, Vector value) { ScrollViewer scrollViewer = o as ScrollViewer; if (scrollViewer != null) { var extent = scrollViewer.Extent; var viewport = scrollViewer.Viewport; return CoerceOffset(extent, viewport, value); } else { return value; } } private void CalculatedPropertiesChanged() { // Pass old values of 0 here because we don't have the old values at this point, // and it shouldn't matter as only the template uses these properies. RaisePropertyChanged(HorizontalScrollBarMaximumProperty, 0, HorizontalScrollBarMaximum); RaisePropertyChanged(HorizontalScrollBarValueProperty, 0, HorizontalScrollBarValue); RaisePropertyChanged(HorizontalScrollBarViewportSizeProperty, 0, HorizontalScrollBarViewportSize); RaisePropertyChanged(VerticalScrollBarMaximumProperty, 0, VerticalScrollBarMaximum); RaisePropertyChanged(VerticalScrollBarValueProperty, 0, VerticalScrollBarValue); RaisePropertyChanged(VerticalScrollBarViewportSizeProperty, 0, VerticalScrollBarViewportSize); } } }