Browse Source

Fix scrollable controls in XAML.

pull/72/head^2
Steven Kirk 11 years ago
parent
commit
09d6e22f5e
  1. 7
      Perspex.Controls/Primitives/Track.cs
  2. 2
      Perspex.Controls/ScrollViewer.cs
  3. 24
      Perspex.Layout/Layoutable.cs
  4. 1
      Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj
  5. 134
      Tests/Perspex.Controls.UnitTests/Primitives/TrackTests.cs

7
Perspex.Controls/Primitives/Track.cs

@ -124,7 +124,12 @@ namespace Perspex.Controls.Primitives
var thumbFraction = this.ViewportSize / range;
var valueFraction = (this.Value - this.Minimum) / range;
if (double.IsNaN(thumbFraction) || double.IsInfinity(thumbFraction))
if (double.IsNaN(valueFraction) || double.IsInfinity(valueFraction))
{
valueFraction = 0;
thumbFraction = 1;
}
else if (double.IsNaN(thumbFraction) || double.IsInfinity(thumbFraction))
{
thumbFraction = 0;
}

2
Perspex.Controls/ScrollViewer.cs

@ -41,7 +41,7 @@ namespace Perspex.Controls
PerspexProperty.Register<ScrollViewer, double>("VerticalScrollBarViewportSize");
public static readonly PerspexProperty<bool> CanScrollHorizontallyProperty =
PerspexProperty.RegisterAttached<ScrollViewer, Control, bool>("CanScrollHorizontally", false);
PerspexProperty.RegisterAttached<ScrollViewer, Control, bool>("CanScrollHorizontally", true);
public static readonly PerspexProperty<ScrollBarVisibility> HorizontalScrollBarVisibilityProperty =
PerspexProperty.RegisterAttached<ScrollBar, Control, ScrollBarVisibility>("HorizontalScrollBarVisibility", ScrollBarVisibility.Auto);

24
Perspex.Layout/Layoutable.cs

@ -195,9 +195,7 @@ namespace Perspex.Layout
var desiredSize = this.MeasureCore(availableSize).Constrain(availableSize);
if (desiredSize.Width < 0 || desiredSize.Height < 0 ||
double.IsInfinity(desiredSize.Width) || double.IsInfinity(desiredSize.Height) ||
double.IsNaN(desiredSize.Width) || double.IsNaN(desiredSize.Height))
if (IsInvalidSize(desiredSize))
{
throw new InvalidOperationException("Invalid size returned for Measure.");
}
@ -211,9 +209,7 @@ namespace Perspex.Layout
public void Arrange(Rect rect, bool force = false)
{
if (rect.Width < 0 || rect.Height < 0 ||
double.IsInfinity(rect.Width) || double.IsInfinity(rect.Height) ||
double.IsNaN(rect.Width) || double.IsNaN(rect.Height))
if (IsInvalidRect(rect))
{
throw new InvalidOperationException("Invalid Arrange rectangle.");
}
@ -441,6 +437,22 @@ namespace Perspex.Layout
return double.IsNaN(control.Width) || double.IsNaN(control.Height);
}
private static bool IsInvalidRect(Rect rect)
{
return rect.Width < 0 || rect.Height < 0 ||
double.IsInfinity(rect.X) || double.IsInfinity(rect.Y) ||
double.IsInfinity(rect.Width) || double.IsInfinity(rect.Height) ||
double.IsNaN(rect.X) || double.IsNaN(rect.Y) ||
double.IsNaN(rect.Width) || double.IsNaN(rect.Height);
}
private static bool IsInvalidSize(Size size)
{
return size.Width < 0 || size.Height < 0 ||
double.IsInfinity(size.Width) || double.IsInfinity(size.Height) ||
double.IsNaN(size.Width) || double.IsNaN(size.Height);
}
private Tuple<ILayoutRoot, int> GetLayoutRoot()
{
var control = (IVisual)this;

1
Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

@ -94,6 +94,7 @@
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Mixins\SelectableMixinTests.cs" />
<Compile Include="Parsers\GridLengthsParserTests.cs" />
<Compile Include="Primitives\TrackTests.cs" />
<Compile Include="Primitives\PopupTests.cs" />
<Compile Include="DropDownTests.cs" />
<Compile Include="Presenters\DeckPresenterTests.cs" />

134
Tests/Perspex.Controls.UnitTests/Primitives/TrackTests.cs

@ -0,0 +1,134 @@
// -----------------------------------------------------------------------
// <copyright file="TrackTests.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.UnitTests.Primitives
{
using System;
using System.Collections.Specialized;
using System.Linq;
using Layout;
using Moq;
using Perspex.Controls.Presenters;
using Perspex.Controls.Primitives;
using Perspex.LogicalTree;
using Perspex.Platform;
using Perspex.Styling;
using Perspex.VisualTree;
using Splat;
using Templates;
using Xunit;
public class TrackTests
{
[Fact]
public void Measure_Should_Return_Thumb_DesiredWidth_In_Vertical_Orientation()
{
var thumb = new Thumb
{
Width = 12,
};
var target = new Track
{
Thumb = thumb,
Orientation = Orientation.Vertical,
};
target.Measure(new Size(100, 100));
Assert.Equal(new Size(12, 0), target.DesiredSize);
}
[Fact]
public void Measure_Should_Return_Thumb_DesiredHeight_In_Horizontal_Orientation()
{
var thumb = new Thumb
{
Height = 12,
};
var target = new Track
{
Thumb = thumb,
Orientation = Orientation.Horizontal,
};
target.Measure(new Size(100, 100));
Assert.Equal(new Size(0, 12), target.DesiredSize);
}
[Fact]
public void Should_Arrange_Thumb_In_Horizontal_Orientation()
{
var thumb = new Thumb
{
Height = 12,
};
var target = new Track
{
Thumb = thumb,
Orientation = Orientation.Horizontal,
Minimum = 100,
Maximum = 200,
Value = 150,
ViewportSize = 50,
};
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
Assert.Equal(new Rect(25, 0, 50, 12), thumb.Bounds);
}
[Fact]
public void Should_Arrange_Thumb_In_Vertical_Orientation()
{
var thumb = new Thumb
{
Width = 12,
};
var target = new Track
{
Thumb = thumb,
Orientation = Orientation.Vertical,
Minimum = 100,
Maximum = 300,
Value = 150,
ViewportSize = 50,
};
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
Assert.Equal(new Rect(0, 18, 12, 25), thumb.Bounds);
}
[Fact]
public void Thumb_Should_Fill_Track_When_Minimum_Equals_Maximum()
{
var thumb = new Thumb
{
Height = 12,
};
var target = new Track
{
Thumb = thumb,
Orientation = Orientation.Horizontal,
Minimum = 100,
Maximum = 100,
};
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
Assert.Equal(new Rect(0, 0, 100, 12), thumb.Bounds);
}
}
}
Loading…
Cancel
Save