10 changed files with 444 additions and 101 deletions
@ -0,0 +1,56 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="ScrollBar.cs" company="Steven Kirk">
|
|||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls.Primitives |
|||
{ |
|||
public class ScrollBar : TemplatedControl |
|||
{ |
|||
public static readonly PerspexProperty<double> MinimumProperty = |
|||
PerspexProperty.Register<ScrollBar, double>("Minimum"); |
|||
|
|||
public static readonly PerspexProperty<double> MaximumProperty = |
|||
PerspexProperty.Register<ScrollBar, double>("Maximum", defaultValue: 100.0); |
|||
|
|||
public static readonly PerspexProperty<double> ValueProperty = |
|||
PerspexProperty.Register<ScrollBar, double>("Value"); |
|||
|
|||
public static readonly PerspexProperty<double> ViewportSizeProperty = |
|||
PerspexProperty.Register<ScrollBar, double>("ViewportSize", defaultValue: double.NaN); |
|||
|
|||
public static readonly PerspexProperty<Orientation> OrientationProperty = |
|||
PerspexProperty.Register<ScrollBar, Orientation>("Orientation"); |
|||
|
|||
public double Minimum |
|||
{ |
|||
get { return this.GetValue(MinimumProperty); } |
|||
set { this.SetValue(MinimumProperty, value); } |
|||
} |
|||
|
|||
public double Maximum |
|||
{ |
|||
get { return this.GetValue(MaximumProperty); } |
|||
set { this.SetValue(MaximumProperty, value); } |
|||
} |
|||
|
|||
public double Value |
|||
{ |
|||
get { return this.GetValue(ValueProperty); } |
|||
set { this.SetValue(ValueProperty, value); } |
|||
} |
|||
|
|||
public double ViewportSize |
|||
{ |
|||
get { return this.GetValue(ViewportSizeProperty); } |
|||
set { this.SetValue(ViewportSizeProperty, value); } |
|||
} |
|||
|
|||
public Orientation Orientation |
|||
{ |
|||
get { return this.GetValue(OrientationProperty); } |
|||
set { this.SetValue(OrientationProperty, value); } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,151 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="Thumb.cs" company="Steven Kirk">
|
|||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls.Primitives |
|||
{ |
|||
using System; |
|||
using Perspex.Input; |
|||
using Perspex.Interactivity; |
|||
|
|||
public class Track : Control |
|||
{ |
|||
public static readonly PerspexProperty<double> MinimumProperty = |
|||
ScrollBar.MinimumProperty.AddOwner<Track>(); |
|||
|
|||
public static readonly PerspexProperty<double> MaximumProperty = |
|||
ScrollBar.MaximumProperty.AddOwner<Track>(); |
|||
|
|||
public static readonly PerspexProperty<double> ValueProperty = |
|||
ScrollBar.ValueProperty.AddOwner<Track>(); |
|||
|
|||
public static readonly PerspexProperty<double> ViewportSizeProperty = |
|||
ScrollBar.ViewportSizeProperty.AddOwner<Track>(); |
|||
|
|||
public static readonly PerspexProperty<Orientation> OrientationProperty = |
|||
ScrollBar.OrientationProperty.AddOwner<Track>(); |
|||
|
|||
public static readonly PerspexProperty<Thumb> ThumbProperty = |
|||
PerspexProperty.Register<Track, Thumb>("Thumb"); |
|||
|
|||
public Track() |
|||
{ |
|||
this.GetObservableWithHistory(ThumbProperty).Subscribe(val => |
|||
{ |
|||
if (val.Item1 != null) |
|||
{ |
|||
val.Item1.DragDelta -= ThumbDragged; |
|||
} |
|||
|
|||
this.ClearVisualChildren(); |
|||
|
|||
if (val.Item2 != null) |
|||
{ |
|||
val.Item2.DragDelta += ThumbDragged; |
|||
this.AddVisualChild(val.Item2); |
|||
} |
|||
}); |
|||
|
|||
AffectsArrange(MinimumProperty); |
|||
AffectsArrange(MaximumProperty); |
|||
AffectsArrange(ValueProperty); |
|||
} |
|||
|
|||
public double Minimum |
|||
{ |
|||
get { return this.GetValue(MinimumProperty); } |
|||
set { this.SetValue(MinimumProperty, value); } |
|||
} |
|||
|
|||
public double Maximum |
|||
{ |
|||
get { return this.GetValue(MaximumProperty); } |
|||
set { this.SetValue(MaximumProperty, value); } |
|||
} |
|||
|
|||
public double Value |
|||
{ |
|||
get { return this.GetValue(ValueProperty); } |
|||
set { this.SetValue(ValueProperty, value); } |
|||
} |
|||
|
|||
public double ViewportSize |
|||
{ |
|||
get { return this.GetValue(ViewportSizeProperty); } |
|||
set { this.SetValue(ViewportSizeProperty, value); } |
|||
} |
|||
|
|||
public Orientation Orientation |
|||
{ |
|||
get { return this.GetValue(OrientationProperty); } |
|||
set { this.SetValue(OrientationProperty, value); } |
|||
} |
|||
|
|||
public Thumb Thumb |
|||
{ |
|||
get { return this.GetValue(ThumbProperty); } |
|||
set { this.SetValue(ThumbProperty, value); } |
|||
} |
|||
|
|||
protected override Size MeasureOverride(Size availableSize) |
|||
{ |
|||
var thumb = this.Thumb; |
|||
|
|||
if (thumb != null) |
|||
{ |
|||
thumb.Measure(availableSize); |
|||
|
|||
if (this.Orientation == Orientation.Horizontal) |
|||
{ |
|||
return new Size(0, thumb.DesiredSize.Value.Height); |
|||
} |
|||
else |
|||
{ |
|||
return new Size(thumb.DesiredSize.Value.Width, 0); |
|||
} |
|||
} |
|||
|
|||
return base.MeasureOverride(availableSize); |
|||
} |
|||
|
|||
protected override Size ArrangeOverride(Size finalSize) |
|||
{ |
|||
var thumb = this.Thumb; |
|||
|
|||
if (thumb != null) |
|||
{ |
|||
var range = this.Maximum - this.Minimum; |
|||
var thumbsize = this.ViewportSize / range; |
|||
|
|||
if (this.Orientation == Orientation.Horizontal) |
|||
{ |
|||
var width = finalSize.Width * thumbsize; |
|||
var x = finalSize.Width * (this.Value / range); |
|||
thumb.Arrange(new Rect(x, 0, width, finalSize.Height)); |
|||
} |
|||
else |
|||
{ |
|||
} |
|||
} |
|||
|
|||
return finalSize; |
|||
} |
|||
|
|||
private void ThumbDragged(object sender, VectorEventArgs e) |
|||
{ |
|||
var range = this.Maximum - this.Minimum; |
|||
|
|||
if (this.Orientation == Orientation.Horizontal) |
|||
{ |
|||
var value = this.Value + e.Vector.X * (range / this.ActualSize.Width); |
|||
|
|||
value = Math.Max(value, this.Minimum); |
|||
value = Math.Min(value, this.Maximum - this.ViewportSize); |
|||
|
|||
this.Value = value; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="ScrollBarStyle.cs" company="Steven Kirk">
|
|||
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Themes.Default |
|||
{ |
|||
using System.Linq; |
|||
using Perspex.Controls; |
|||
using Perspex.Controls.Presenters; |
|||
using Perspex.Controls.Primitives; |
|||
using Perspex.Media; |
|||
using Perspex.Styling; |
|||
|
|||
public class ScrollBarStyle : Styles |
|||
{ |
|||
public ScrollBarStyle() |
|||
{ |
|||
this.AddRange(new[] |
|||
{ |
|||
new Style(x => x.OfType<ScrollBar>()) |
|||
{ |
|||
Setters = new[] |
|||
{ |
|||
new Setter(ScrollBar.TemplateProperty, ControlTemplate.Create<ScrollBar>(this.Template)), |
|||
new Setter(ScrollBar.HeightProperty, 20.0), |
|||
}, |
|||
}, |
|||
}); |
|||
} |
|||
|
|||
private Control Template(ScrollBar control) |
|||
{ |
|||
return new Border |
|||
{ |
|||
Background = Brushes.Silver, |
|||
Content = new Track |
|||
{ |
|||
[~Track.MinimumProperty] = control[~ScrollBar.MinimumProperty], |
|||
[~Track.MaximumProperty] = control[~ScrollBar.MaximumProperty], |
|||
[~Track.ValueProperty] = control[~ScrollBar.ValueProperty], |
|||
[~Track.ViewportSizeProperty] = control[~ScrollBar.ViewportSizeProperty], |
|||
[~Track.OrientationProperty] = control[~ScrollBar.OrientationProperty], |
|||
Thumb = new Thumb |
|||
{ |
|||
Template = ControlTemplate.Create<Thumb>(this.ThumbTemplate), |
|||
}, |
|||
}, |
|||
}; |
|||
} |
|||
|
|||
private Control ThumbTemplate(Thumb control) |
|||
{ |
|||
return new Border |
|||
{ |
|||
Background = Brushes.Gray, |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue