From 981ec53d074c12a0d6dd2afb64ae6f04e789971f Mon Sep 17 00:00:00 2001 From: susloparov Date: Thu, 10 Dec 2015 20:28:38 +0600 Subject: [PATCH] Seperated GridSplitter into HorizontalGridSplitter and VerticalGridSplitter --- src/Perspex.Controls/GridSplitter.cs | 194 +++++++++++------- .../Views/LogicalTreeView.cs | 2 +- .../Views/VisualTreeView.cs | 2 +- src/Perspex.Themes.Default/DefaultTheme.paml | 4 +- ...itter.paml => HorizontalGridSplitter.paml} | 4 +- .../Perspex.Themes.Default.csproj | 5 +- .../VerticalGridSplitter.paml | 8 + 7 files changed, 138 insertions(+), 81 deletions(-) rename src/Perspex.Themes.Default/{GridSplitter.paml => HorizontalGridSplitter.paml} (53%) create mode 100644 src/Perspex.Themes.Default/VerticalGridSplitter.paml diff --git a/src/Perspex.Controls/GridSplitter.cs b/src/Perspex.Controls/GridSplitter.cs index cb77cb6aad..b4e76ad41e 100644 --- a/src/Perspex.Controls/GridSplitter.cs +++ b/src/Perspex.Controls/GridSplitter.cs @@ -2,8 +2,8 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Collections.Generic; -using System.Linq; + +using Perspex.Collections; using Perspex.Controls.Primitives; using Perspex.Input; using Perspex.VisualTree; @@ -13,17 +13,16 @@ namespace Perspex.Controls /// /// Unlike WPF GridSplitter, Perspex GridSplitter has only one Behavior. It's GridResizeBehavior.PreviousAndNext /// - public class GridSplitter : Thumb + public abstract class GridSplitterBase : Thumb + where T : DefinitionBase { - private Grid _grid; - - private DefinitionBase _prevDefinition; + protected PerspexList _definitions; - private DefinitionBase _nextDefinition; + protected Grid _grid; - private bool _isResizingColumns; + protected T _nextDefinition; - private List _definitions; + protected T _prevDefinition; /// /// Decide depending on set size @@ -42,73 +41,40 @@ namespace Perspex.Controls throw new InvalidOperationException("GridSpliter Should have width or height set. It affects whether columns or rows it resizes"); } - private double GetActualLength(DefinitionBase definition) - { - var columnDefinition = definition as ColumnDefinition; - return columnDefinition?.ActualWidth ?? ((RowDefinition)definition).ActualHeight; - } - - private double GetMinLength(DefinitionBase definition) - { - var columnDefinition = definition as ColumnDefinition; - return columnDefinition?.MinWidth ?? ((RowDefinition)definition).MinHeight; - } - - private double GetMaxLength(DefinitionBase definition) - { - var columnDefinition = definition as ColumnDefinition; - return columnDefinition?.MaxWidth ?? ((RowDefinition)definition).MaxHeight; - } - - private bool IsStar(DefinitionBase definition) - { - var columnDefinition = definition as ColumnDefinition; - return columnDefinition?.Width.IsStar ?? ((RowDefinition)definition).Height.IsStar; - } + protected abstract double GetActualLength(T definition); - private void SetLengthInStars(DefinitionBase definition, double value) - { - var columnDefinition = definition as ColumnDefinition; - if (columnDefinition != null) - { - columnDefinition.Width = new GridLength(value, GridUnitType.Star); - } - else - { - ((RowDefinition)definition).Height = new GridLength(value, GridUnitType.Star); - } - } + protected abstract double GetMinLength(T definition); + protected abstract double GetMaxLength(T definition); + protected abstract bool IsStar(T definition); + protected abstract void SetLengthInStars(T definition, double value); private void GetDeltaConstraints(out double min, out double max) { - double prevDefinitionLen = GetActualLength(_prevDefinition); - double prevDefinitionMin = GetMinLength(_prevDefinition); - double prevDefinitionMax = GetMaxLength(_prevDefinition); - - double nextDefinitionLen = GetActualLength(_nextDefinition); - double nextDefinitionMin = GetMinLength(_nextDefinition); - double nextDefinitionMax = GetMaxLength(_nextDefinition); + var prevDefinitionLen = GetActualLength(_prevDefinition); + var prevDefinitionMin = GetMinLength(_prevDefinition); + var prevDefinitionMax = GetMaxLength(_prevDefinition); + var nextDefinitionLen = GetActualLength(_nextDefinition); + var nextDefinitionMin = GetMinLength(_nextDefinition); + var nextDefinitionMax = GetMaxLength(_nextDefinition); // Determine the minimum and maximum the columns can be resized min = -Math.Min(prevDefinitionLen - prevDefinitionMin, nextDefinitionMax - nextDefinitionLen); max = Math.Min(prevDefinitionMax - prevDefinitionLen, nextDefinitionLen - nextDefinitionMin); } - protected override void OnDragDelta(VectorEventArgs e) { - var delta = _isResizingColumns ? e.Vector.X : e.Vector.Y; + var delta = GetDelta(e); double max; double min; GetDeltaConstraints(out min, out max); delta = Math.Min(Math.Max(delta, min), max); - foreach (var definition in _definitions) { if (definition == _prevDefinition) @@ -119,35 +85,115 @@ namespace Perspex.Controls { SetLengthInStars(_nextDefinition, GetActualLength(_nextDefinition) - delta); } - else if(IsStar(definition)) + else if (IsStar(definition)) { SetLengthInStars(definition, GetActualLength(definition)); // same size but in stars } } } + protected abstract double GetDelta(VectorEventArgs vectorEventArgs); + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { - //new Range base.OnAttachedToVisualTree(e); _grid = this.GetVisualParent(); - _isResizingColumns = IsResizingColumns(); - if (_isResizingColumns) - { - Cursor = new Cursor(StandardCursorType.SizeWestEast); - var col = GetValue(Grid.ColumnProperty); - _definitions = _grid.ColumnDefinitions.Cast().ToList(); - _prevDefinition = _definitions[col - 1]; - _nextDefinition = _definitions[col + 1]; - } - else - { - Cursor = new Cursor(StandardCursorType.SizeNorthSouth); - var row = GetValue(Grid.RowProperty); - _definitions = _grid.RowDefinitions.Cast().ToList(); - _prevDefinition = _definitions[row - 1]; - _nextDefinition = _definitions[row + 1]; - } } } -} + + public class HorizontalGridSplitter : GridSplitterBase + { + public HorizontalGridSplitter() + { + Cursor = new Cursor(StandardCursorType.SizeNorthSouth); + } + + protected override double GetActualLength(RowDefinition definition) + { + return definition.ActualHeight; + } + + protected override double GetMaxLength(RowDefinition definition) + { + return definition.MaxHeight; + } + + protected override double GetMinLength(RowDefinition definition) + { + return definition.MinHeight; + } + + protected override bool IsStar(RowDefinition definition) + { + return definition.Height.IsStar; + } + + protected override double GetDelta(VectorEventArgs vectorEventArgs) + { + return vectorEventArgs.Vector.Y; + } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + var row = GetValue(Grid.RowProperty); + _definitions = _grid.RowDefinitions; + _prevDefinition = _definitions[row - 1]; + _nextDefinition = _definitions[row + 1]; + } + + protected override void SetLengthInStars(RowDefinition definition, double value) + { + definition.Height = new GridLength(value, GridUnitType.Star); + } + } + + public class VerticalGridSplitter : GridSplitterBase + { + + + + public VerticalGridSplitter() + { + Cursor = new Cursor(StandardCursorType.SizeWestEast); + } + + protected override double GetActualLength(ColumnDefinition definition) + { + return definition.ActualWidth; + } + + protected override double GetMaxLength(ColumnDefinition definition) + { + return definition.MaxWidth; + } + + protected override double GetMinLength(ColumnDefinition definition) + { + return definition.MinWidth; + } + + protected override bool IsStar(ColumnDefinition definition) + { + return definition.Width.IsStar; + } + protected override double GetDelta(VectorEventArgs vectorEventArgs) + { + return vectorEventArgs.Vector.X; + } + + protected override void SetLengthInStars(ColumnDefinition definition, double value) + { + definition.Width = new GridLength(value, GridUnitType.Star); + } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + var col = GetValue(Grid.ColumnProperty); + _definitions = _grid.ColumnDefinitions; + _prevDefinition = _definitions[col - 1]; + _nextDefinition = _definitions[col + 1]; + } + } +} \ No newline at end of file diff --git a/src/Perspex.Diagnostics/Views/LogicalTreeView.cs b/src/Perspex.Diagnostics/Views/LogicalTreeView.cs index 276506bf09..434b7a574f 100644 --- a/src/Perspex.Diagnostics/Views/LogicalTreeView.cs +++ b/src/Perspex.Diagnostics/Views/LogicalTreeView.cs @@ -52,7 +52,7 @@ namespace Perspex.Diagnostics.Views }, [!ItemsControl.ItemsProperty] = this.WhenAnyValue(x => x.ViewModel.Nodes), }), - new GridSplitter + new VerticalGridSplitter { [Grid.ColumnProperty] = 1, Width = 4, diff --git a/src/Perspex.Diagnostics/Views/VisualTreeView.cs b/src/Perspex.Diagnostics/Views/VisualTreeView.cs index df7277e396..0ef992ead3 100644 --- a/src/Perspex.Diagnostics/Views/VisualTreeView.cs +++ b/src/Perspex.Diagnostics/Views/VisualTreeView.cs @@ -53,7 +53,7 @@ namespace Perspex.Diagnostics.Views }, [!ItemsControl.ItemsProperty] = this.WhenAnyValue(x => x.ViewModel.Nodes), }), - new GridSplitter + new VerticalGridSplitter { [Grid.ColumnProperty] = 1, Width = 4, diff --git a/src/Perspex.Themes.Default/DefaultTheme.paml b/src/Perspex.Themes.Default/DefaultTheme.paml index 5902e568b3..4dd9df8d32 100644 --- a/src/Perspex.Themes.Default/DefaultTheme.paml +++ b/src/Perspex.Themes.Default/DefaultTheme.paml @@ -1,12 +1,12 @@  - - + + diff --git a/src/Perspex.Themes.Default/GridSplitter.paml b/src/Perspex.Themes.Default/HorizontalGridSplitter.paml similarity index 53% rename from src/Perspex.Themes.Default/GridSplitter.paml rename to src/Perspex.Themes.Default/HorizontalGridSplitter.paml index ee0757a3f5..7cdd47fc30 100644 --- a/src/Perspex.Themes.Default/GridSplitter.paml +++ b/src/Perspex.Themes.Default/HorizontalGridSplitter.paml @@ -1,5 +1,5 @@ -