From 73ebfc94750785fcc89474714e218ffb5a19ae5d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 29 Nov 2014 21:12:02 +0100 Subject: [PATCH] Hide scrollbars on ScrollViewer when not needed. Also added an image size slider to test. --- Perspex.Controls/Primitives/ScrollBar.cs | 3 +++ Perspex.Controls/Primitives/Track.cs | 4 ++-- Perspex.Controls/ScrollViewer.cs | 12 ++++++------ Perspex.Layout/LayoutManager.cs | 2 +- TestApplication/Program.cs | 15 ++++++++++++--- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Perspex.Controls/Primitives/ScrollBar.cs b/Perspex.Controls/Primitives/ScrollBar.cs index 518d696e3b..f48576eed2 100644 --- a/Perspex.Controls/Primitives/ScrollBar.cs +++ b/Perspex.Controls/Primitives/ScrollBar.cs @@ -7,6 +7,7 @@ namespace Perspex.Controls.Primitives { using System; + using System.Reactive.Linq; public class ScrollBar : TemplatedControl { @@ -27,6 +28,8 @@ namespace Perspex.Controls.Primitives public ScrollBar() { + this.Classes.Add(":vertical"); + this.GetObservable(OrientationProperty).Subscribe(o => { if (o == Orientation.Horizontal) diff --git a/Perspex.Controls/Primitives/Track.cs b/Perspex.Controls/Primitives/Track.cs index f3c223c2f8..6a44128484 100644 --- a/Perspex.Controls/Primitives/Track.cs +++ b/Perspex.Controls/Primitives/Track.cs @@ -119,9 +119,9 @@ namespace Perspex.Controls.Primitives { var range = this.Maximum - this.Minimum; var thumbFraction = this.ViewportSize / range; - var valueFraction = this.Value / range; + var valueFraction = (this.Value - this.Minimum) / range; - if (double.IsNaN(thumbFraction)) + if (double.IsNaN(thumbFraction) || double.IsInfinity(thumbFraction)) { thumbFraction = 0; } diff --git a/Perspex.Controls/ScrollViewer.cs b/Perspex.Controls/ScrollViewer.cs index 50db1338fd..7bbbc20126 100644 --- a/Perspex.Controls/ScrollViewer.cs +++ b/Perspex.Controls/ScrollViewer.cs @@ -60,9 +60,9 @@ namespace Perspex.Controls this.GetObservable(ViewportProperty).StartWith(this.Viewport)) .Select(x => new { Extent = x[0], Viewport = x[1] }); - //this.horizontalScrollBar.Bind( - // IsVisibleProperty, - // extentAndViewport.Select(x => x.Extent.Width > x.Viewport.Width)); + this.horizontalScrollBar.Bind( + IsVisibleProperty, + extentAndViewport.Select(x => x.Extent.Width > x.Viewport.Width)); this.horizontalScrollBar.Bind( ScrollBar.MaximumProperty, @@ -72,9 +72,9 @@ namespace Perspex.Controls ScrollBar.ViewportSizeProperty, extentAndViewport.Select(x => x.Viewport.Width)); - //this.verticalScrollBar.Bind( - // IsVisibleProperty, - // extentAndViewport.Select(x => x.Extent.Height > x.Viewport.Height)); + this.verticalScrollBar.Bind( + IsVisibleProperty, + extentAndViewport.Select(x => x.Extent.Height > x.Viewport.Height)); this.verticalScrollBar.Bind( ScrollBar.MaximumProperty, diff --git a/Perspex.Layout/LayoutManager.cs b/Perspex.Layout/LayoutManager.cs index 8c25c127d2..1443759146 100644 --- a/Perspex.Layout/LayoutManager.cs +++ b/Perspex.Layout/LayoutManager.cs @@ -37,9 +37,9 @@ namespace Perspex.Layout public void ExecuteLayoutPass() { + this.LayoutQueued = false; this.root.Measure(this.root.ClientSize); this.root.Arrange(new Rect(this.root.ClientSize)); - this.LayoutQueued = false; } public void InvalidateMeasure(ILayoutable item) diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index f508d7b064..6a78e637fe 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -203,6 +203,8 @@ namespace TestApplication private static TabItem ImagesTab() { + ScrollBar size; + return new TabItem { Header = "Images", @@ -214,6 +216,13 @@ namespace TestApplication Gap = 8, Children = new Controls { + (size = new ScrollBar + { + Minimum = 100, + Maximum = 400, + Value = 400, + Orientation = Orientation.Horizontal, + }), new ScrollViewer { Width = 200, @@ -221,10 +230,10 @@ namespace TestApplication Content = new Image { Source = new Bitmap("github_icon.png"), - Width = 400, - Height = 400, + [!Image.WidthProperty] = size[!ScrollBar.ValueProperty], + [!Image.HeightProperty] = size[!ScrollBar.ValueProperty], }, - } + }, } }, };