From 2e813c867d85344e6f03d5b8a224bacd22a443bf Mon Sep 17 00:00:00 2001 From: susloparov Date: Fri, 11 Nov 2016 23:16:06 +0700 Subject: [PATCH] Removed Orientation Property from GridSplitter It's possible to unambiguously detect orientation based on Column/RowDefenitions or other elements. --- src/Avalonia.Controls/GridSplitter.cs | 93 ++++++++++--------- .../GridSplitterTests.cs | 4 - 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/Avalonia.Controls/GridSplitter.cs b/src/Avalonia.Controls/GridSplitter.cs index 4b8ef06400..982cb7cbb6 100644 --- a/src/Avalonia.Controls/GridSplitter.cs +++ b/src/Avalonia.Controls/GridSplitter.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; + using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.VisualTree; @@ -18,45 +19,15 @@ namespace Avalonia.Controls /// public class GridSplitter : Thumb { - /// - /// Defines the property. - /// - public static readonly StyledProperty OrientationProperty = - AvaloniaProperty.Register(nameof(Orientation)); + private List _definitions; protected Grid _grid; - private DefinitionBase _prevDefinition; - private DefinitionBase _nextDefinition; - private List _definitions; - - /// - /// Gets or sets the orientation of the GridsSlitter. - /// - /// - /// if null, it's inferred from column/row definition (should be auto). - /// - public Orientation Orientation { - get - { - return GetValue(OrientationProperty); - } - set - { - SetValue(OrientationProperty, value); - } - } + private Orientation _orientation; - /// - /// Initializes static members of the class. - /// - static GridSplitter() - { - PseudoClass(OrientationProperty, o => o == Avalonia.Controls.Orientation.Vertical, ":vertical"); - PseudoClass(OrientationProperty, o => o == Avalonia.Controls.Orientation.Horizontal, ":horizontal"); - } + private DefinitionBase _prevDefinition; private void GetDeltaConstraints(out double min, out double max) { @@ -74,7 +45,7 @@ namespace Avalonia.Controls protected override void OnDragDelta(VectorEventArgs e) { - var delta = Orientation == Orientation.Vertical ? e.Vector.X : e.Vector.Y; + var delta = _orientation == Orientation.Vertical ? e.Vector.X : e.Vector.Y; double max; double min; GetDeltaConstraints(out min, out max); @@ -137,24 +108,58 @@ namespace Avalonia.Controls { base.OnAttachedToVisualTree(e); _grid = this.GetVisualParent(); - - if (Orientation == Orientation.Vertical) + + _orientation = DetectOrientation(); + + int pos; + if (_orientation == Orientation.Vertical) { Cursor = new Cursor(StandardCursorType.SizeWestEast); - var col = GetValue(Grid.ColumnProperty); + pos = GetValue(Grid.ColumnProperty); _definitions = _grid.ColumnDefinitions.Cast().ToList(); - _prevDefinition = _definitions[col - 1]; - _nextDefinition = _definitions[col + 1]; + PseudoClasses.Add(":vertical"); } else { Cursor = new Cursor(StandardCursorType.SizeNorthSouth); - var row = GetValue(Grid.RowProperty); + pos = GetValue(Grid.RowProperty); _definitions = _grid.RowDefinitions.Cast().ToList(); - _prevDefinition = _definitions[row - 1]; - _nextDefinition = _definitions[row + 1]; + PseudoClasses.Add(":horizontal"); } + _prevDefinition = _definitions[pos - 1]; + _nextDefinition = _definitions[pos + 1]; } - } -} + private Orientation DetectOrientation() + { + if(!_grid.ColumnDefinitions.Any()) + return Orientation.Horizontal; + if (!_grid.RowDefinitions.Any()) + return Orientation.Vertical; + + var col = GetValue(Grid.ColumnProperty); + var row = GetValue(Grid.RowProperty); + var width = _grid.ColumnDefinitions[col].Width; + var height = _grid.RowDefinitions[row].Height; + if (!width.IsAuto && !height.IsAuto) + { + throw new InvalidOperationException("Whether RowDefenition or ColumnDefenition matched with the GridSplitter should have Auto size"); + } + if (width.IsAuto && !height.IsAuto) + { + return Orientation.Vertical; + } + if (!width.IsAuto && height.IsAuto) + { + return Orientation.Horizontal; + } + if (_grid.Children.OfType() // Decision based on other controls in the same column + .Where(c => Grid.GetColumn(c) == col) + .Any(c => c.GetType() != typeof(GridSplitter))) + { + return Orientation.Horizontal; + } + return Orientation.Vertical; + } + } +} \ No newline at end of file diff --git a/tests/Avalonia.Controls.UnitTests/GridSplitterTests.cs b/tests/Avalonia.Controls.UnitTests/GridSplitterTests.cs index 90b9913141..ea18dfc95f 100644 --- a/tests/Avalonia.Controls.UnitTests/GridSplitterTests.cs +++ b/tests/Avalonia.Controls.UnitTests/GridSplitterTests.cs @@ -19,7 +19,6 @@ namespace Avalonia.Controls.UnitTests var control1 = new Border { [Grid.ColumnProperty] = 0 }; var splitter = new GridSplitter { - Orientation = Orientation.Vertical, [Grid.ColumnProperty] = 1, }; var control2 = new Border { [Grid.ColumnProperty] = 2 }; @@ -41,7 +40,6 @@ namespace Avalonia.Controls.UnitTests }; var root = new TestRoot { Child = grid }; - Assert.Equal(splitter.Orientation, Orientation.Vertical); root.Measure(new Size(200, 100)); root.Arrange(new Rect(0, 0, 200, 100)); @@ -71,7 +69,6 @@ namespace Avalonia.Controls.UnitTests var control1 = new Border { [Grid.RowProperty] = 0 }; var splitter = new GridSplitter { - Orientation = Orientation.Horizontal, [Grid.RowProperty] = 1, }; var control2 = new Border { [Grid.RowProperty] = 2 }; @@ -93,7 +90,6 @@ namespace Avalonia.Controls.UnitTests }; var root = new TestRoot { Child = grid }; - Assert.Equal(splitter.Orientation, Orientation.Horizontal); root.Measure(new Size(100, 200)); root.Arrange(new Rect(0, 0, 100, 200));