Browse Source

Not throw exception in last or first position.

pull/801/head
Denis 10 years ago
parent
commit
d03662a373
  1. 57
      src/Avalonia.Controls/GridSplitter.cs

57
src/Avalonia.Controls/GridSplitter.cs

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.VisualTree; using Avalonia.VisualTree;
@ -12,16 +11,16 @@ using Avalonia.VisualTree;
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
/// <summary> /// <summary>
/// Represents the control that redistributes space between columns or rows of a Grid control. /// Represents the control that redistributes space between columns or rows of a Grid control.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Unlike WPF GridSplitter, Avalonia GridSplitter has only one Behavior, GridResizeBehavior.PreviousAndNext. /// Unlike WPF GridSplitter, Avalonia GridSplitter has only one Behavior, GridResizeBehavior.PreviousAndNext.
/// </remarks> /// </remarks>
public class GridSplitter : Thumb public class GridSplitter : Thumb
{ {
private List<DefinitionBase> _definitions; private List<DefinitionBase> _definitions;
protected Grid _grid; private Grid _grid;
private DefinitionBase _nextDefinition; private DefinitionBase _nextDefinition;
@ -31,13 +30,13 @@ namespace Avalonia.Controls
private void GetDeltaConstraints(out double min, out double max) private void GetDeltaConstraints(out double min, out double max)
{ {
double prevDefinitionLen = GetActualLength(_prevDefinition); var prevDefinitionLen = GetActualLength(_prevDefinition);
double prevDefinitionMin = GetMinLength(_prevDefinition); var prevDefinitionMin = GetMinLength(_prevDefinition);
double prevDefinitionMax = GetMaxLength(_prevDefinition); var prevDefinitionMax = GetMaxLength(_prevDefinition);
double nextDefinitionLen = GetActualLength(_nextDefinition); var nextDefinitionLen = GetActualLength(_nextDefinition);
double nextDefinitionMin = GetMinLength(_nextDefinition); var nextDefinitionMin = GetMinLength(_nextDefinition);
double nextDefinitionMax = GetMaxLength(_nextDefinition); var nextDefinitionMax = GetMaxLength(_nextDefinition);
// Determine the minimum and maximum the columns can be resized // Determine the minimum and maximum the columns can be resized
min = -Math.Min(prevDefinitionLen - prevDefinitionMin, nextDefinitionMax - nextDefinitionLen); min = -Math.Min(prevDefinitionLen - prevDefinitionMin, nextDefinitionMax - nextDefinitionLen);
max = Math.Min(prevDefinitionMax - prevDefinitionLen, nextDefinitionLen - nextDefinitionMin); max = Math.Min(prevDefinitionMax - prevDefinitionLen, nextDefinitionLen - nextDefinitionMin);
@ -69,26 +68,32 @@ namespace Avalonia.Controls
private double GetActualLength(DefinitionBase definition) private double GetActualLength(DefinitionBase definition)
{ {
if (definition == null)
return 0;
var columnDefinition = definition as ColumnDefinition; var columnDefinition = definition as ColumnDefinition;
return columnDefinition?.ActualWidth ?? ((RowDefinition)definition).ActualHeight; return columnDefinition?.ActualWidth ?? ((RowDefinition) definition).ActualHeight;
} }
private double GetMinLength(DefinitionBase definition) private double GetMinLength(DefinitionBase definition)
{ {
if (definition == null)
return 0;
var columnDefinition = definition as ColumnDefinition; var columnDefinition = definition as ColumnDefinition;
return columnDefinition?.MinWidth ?? ((RowDefinition)definition).MinHeight; return columnDefinition?.MinWidth ?? ((RowDefinition) definition).MinHeight;
} }
private double GetMaxLength(DefinitionBase definition) private double GetMaxLength(DefinitionBase definition)
{ {
if (definition == null)
return 0;
var columnDefinition = definition as ColumnDefinition; var columnDefinition = definition as ColumnDefinition;
return columnDefinition?.MaxWidth ?? ((RowDefinition)definition).MaxHeight; return columnDefinition?.MaxWidth ?? ((RowDefinition) definition).MaxHeight;
} }
private bool IsStar(DefinitionBase definition) private bool IsStar(DefinitionBase definition)
{ {
var columnDefinition = definition as ColumnDefinition; var columnDefinition = definition as ColumnDefinition;
return columnDefinition?.Width.IsStar ?? ((RowDefinition)definition).Height.IsStar; return columnDefinition?.Width.IsStar ?? ((RowDefinition) definition).Height.IsStar;
} }
private void SetLengthInStars(DefinitionBase definition, double value) private void SetLengthInStars(DefinitionBase definition, double value)
@ -100,7 +105,7 @@ namespace Avalonia.Controls
} }
else else
{ {
((RowDefinition)definition).Height = new GridLength(value, GridUnitType.Star); ((RowDefinition) definition).Height = new GridLength(value, GridUnitType.Star);
} }
} }
@ -111,28 +116,32 @@ namespace Avalonia.Controls
_orientation = DetectOrientation(); _orientation = DetectOrientation();
int pos; int defenitionIndex; //row or col
if (_orientation == Orientation.Vertical) if (_orientation == Orientation.Vertical)
{ {
Cursor = new Cursor(StandardCursorType.SizeWestEast); Cursor = new Cursor(StandardCursorType.SizeWestEast);
pos = GetValue(Grid.ColumnProperty);
_definitions = _grid.ColumnDefinitions.Cast<DefinitionBase>().ToList(); _definitions = _grid.ColumnDefinitions.Cast<DefinitionBase>().ToList();
defenitionIndex = GetValue(Grid.ColumnProperty);
PseudoClasses.Add(":vertical"); PseudoClasses.Add(":vertical");
} }
else else
{ {
Cursor = new Cursor(StandardCursorType.SizeNorthSouth); Cursor = new Cursor(StandardCursorType.SizeNorthSouth);
pos = GetValue(Grid.RowProperty); defenitionIndex = GetValue(Grid.RowProperty);
_definitions = _grid.RowDefinitions.Cast<DefinitionBase>().ToList(); _definitions = _grid.RowDefinitions.Cast<DefinitionBase>().ToList();
PseudoClasses.Add(":horizontal"); PseudoClasses.Add(":horizontal");
} }
_prevDefinition = _definitions[pos - 1];
_nextDefinition = _definitions[pos + 1]; if (defenitionIndex > 0)
_prevDefinition = _definitions[defenitionIndex - 1];
if (defenitionIndex < _definitions.Count - 1)
_nextDefinition = _definitions[defenitionIndex + 1];
} }
private Orientation DetectOrientation() private Orientation DetectOrientation()
{ {
if(!_grid.ColumnDefinitions.Any()) if (!_grid.ColumnDefinitions.Any())
return Orientation.Horizontal; return Orientation.Horizontal;
if (!_grid.RowDefinitions.Any()) if (!_grid.RowDefinitions.Any())
return Orientation.Vertical; return Orientation.Vertical;
@ -141,10 +150,6 @@ namespace Avalonia.Controls
var row = GetValue(Grid.RowProperty); var row = GetValue(Grid.RowProperty);
var width = _grid.ColumnDefinitions[col].Width; var width = _grid.ColumnDefinitions[col].Width;
var height = _grid.RowDefinitions[row].Height; 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) if (width.IsAuto && !height.IsAuto)
{ {
return Orientation.Vertical; return Orientation.Vertical;
@ -155,7 +160,7 @@ namespace Avalonia.Controls
} }
if (_grid.Children.OfType<Control>() // Decision based on other controls in the same column if (_grid.Children.OfType<Control>() // Decision based on other controls in the same column
.Where(c => Grid.GetColumn(c) == col) .Where(c => Grid.GetColumn(c) == col)
.Any(c => c.GetType() != typeof(GridSplitter))) .Any(c => c.GetType() != typeof (GridSplitter)))
{ {
return Orientation.Horizontal; return Orientation.Horizontal;
} }

Loading…
Cancel
Save