|
|
using System.ComponentModel; |
|
|
using System.Windows; |
|
|
using System.Windows.Controls; |
|
|
using ScreenGrab.Extensions; |
|
|
|
|
|
namespace ScreenGrab; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class AutoGrid : Grid |
|
|
{ |
|
|
#region Protected Methods |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override Size MeasureOverride(Size constraint) |
|
|
{ |
|
|
PerformLayout(); |
|
|
return base.MeasureOverride(constraint); |
|
|
} |
|
|
|
|
|
#endregion Protected Methods |
|
|
|
|
|
#region Public Fields |
|
|
|
|
|
public static readonly DependencyProperty ChildHorizontalAlignmentProperty = DependencyProperty.Register( |
|
|
"ChildHorizontalAlignment", |
|
|
typeof(HorizontalAlignment?), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, |
|
|
OnChildHorizontalAlignmentChanged)); |
|
|
|
|
|
public static readonly DependencyProperty ChildMarginProperty = DependencyProperty.Register( |
|
|
"ChildMargin", |
|
|
typeof(Thickness?), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, OnChildMarginChanged)); |
|
|
|
|
|
public static readonly DependencyProperty ChildVerticalAlignmentProperty = DependencyProperty.Register( |
|
|
"ChildVerticalAlignment", |
|
|
typeof(VerticalAlignment?), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, |
|
|
OnChildVerticalAlignmentChanged)); |
|
|
|
|
|
public static readonly DependencyProperty ColumnCountProperty = DependencyProperty.RegisterAttached( |
|
|
"ColumnCount", |
|
|
typeof(int), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, ColumnCountChanged)); |
|
|
|
|
|
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.RegisterAttached( |
|
|
"Columns", |
|
|
typeof(string), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, ColumnsChanged)); |
|
|
|
|
|
public static readonly DependencyProperty ColumnWidthProperty = DependencyProperty.RegisterAttached( |
|
|
"ColumnWidth", |
|
|
typeof(GridLength), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure, |
|
|
FixedColumnWidthChanged)); |
|
|
|
|
|
public static readonly DependencyProperty IsAutoIndexingProperty = DependencyProperty.Register( |
|
|
"IsAutoIndexing", |
|
|
typeof(bool), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsMeasure)); |
|
|
|
|
|
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register( |
|
|
"Orientation", |
|
|
typeof(Orientation), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsMeasure)); |
|
|
|
|
|
public static readonly DependencyProperty RowCountProperty = DependencyProperty.RegisterAttached( |
|
|
"RowCount", |
|
|
typeof(int), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, RowCountChanged)); |
|
|
|
|
|
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.RegisterAttached( |
|
|
"RowHeight", |
|
|
typeof(GridLength), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure, |
|
|
FixedRowHeightChanged)); |
|
|
|
|
|
public static readonly DependencyProperty RowsProperty = DependencyProperty.RegisterAttached( |
|
|
"Rows", |
|
|
typeof(string), |
|
|
typeof(AutoGrid), |
|
|
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, RowsChanged)); |
|
|
|
|
|
#endregion Public Fields |
|
|
|
|
|
#region Public Properties |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Presets the horizontal alignment of all child controls")] |
|
|
public HorizontalAlignment? ChildHorizontalAlignment |
|
|
{ |
|
|
get => (HorizontalAlignment?)GetValue(ChildHorizontalAlignmentProperty); |
|
|
set => SetValue(ChildHorizontalAlignmentProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Presets the margin of all child controls")] |
|
|
public Thickness? ChildMargin |
|
|
{ |
|
|
get => (Thickness?)GetValue(ChildMarginProperty); |
|
|
set => SetValue(ChildMarginProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Presets the vertical alignment of all child controls")] |
|
|
public VerticalAlignment? ChildVerticalAlignment |
|
|
{ |
|
|
get => (VerticalAlignment?)GetValue(ChildVerticalAlignmentProperty); |
|
|
set => SetValue(ChildVerticalAlignmentProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Defines a set number of columns")] |
|
|
public int ColumnCount |
|
|
{ |
|
|
get => (int)GetValue(ColumnCountProperty); |
|
|
set => SetValue(ColumnCountProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Defines all columns using comma separated grid length notation")] |
|
|
public string Columns |
|
|
{ |
|
|
get => (string)GetValue(ColumnsProperty); |
|
|
set => SetValue(ColumnsProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Presets the width of all columns set using the ColumnCount property")] |
|
|
public GridLength ColumnWidth |
|
|
{ |
|
|
get => (GridLength)GetValue(ColumnWidthProperty); |
|
|
set => SetValue(ColumnWidthProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Set to false to disable the auto layout functionality")] |
|
|
public bool IsAutoIndexing |
|
|
{ |
|
|
get => (bool)GetValue(IsAutoIndexingProperty); |
|
|
set => SetValue(IsAutoIndexingProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description( |
|
|
"Defines the directionality of the autolayout. Use vertical for a column first layout, horizontal for a row first layout.")] |
|
|
public Orientation Orientation |
|
|
{ |
|
|
get => (Orientation)GetValue(OrientationProperty); |
|
|
set => SetValue(OrientationProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Defines a set number of rows")] |
|
|
public int RowCount |
|
|
{ |
|
|
get => (int)GetValue(RowCountProperty); |
|
|
set => SetValue(RowCountProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Presets the height of all rows set using the RowCount property")] |
|
|
public GridLength RowHeight |
|
|
{ |
|
|
get => (GridLength)GetValue(RowHeightProperty); |
|
|
set => SetValue(RowHeightProperty, value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Category("Layout")] |
|
|
[Description("Defines all rows using comma separated grid length notation")] |
|
|
public string Rows |
|
|
{ |
|
|
get => (string)GetValue(RowsProperty); |
|
|
set => SetValue(RowsProperty, value); |
|
|
} |
|
|
|
|
|
#endregion Public Properties |
|
|
|
|
|
#region Public Methods |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ColumnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if ((int)e.NewValue < 0) |
|
|
return; |
|
|
|
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
|
|
|
|
|
|
var width = GridLength.Auto; |
|
|
if (grid.ColumnDefinitions.Count > 0) |
|
|
width = grid.ColumnDefinitions[0].Width; |
|
|
|
|
|
|
|
|
grid.ColumnDefinitions.Clear(); |
|
|
for (var i = 0; i < (int)e.NewValue; i++) |
|
|
grid.ColumnDefinitions.Add( |
|
|
new ColumnDefinition { Width = width }); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (string.IsNullOrEmpty((string)e.NewValue)) |
|
|
return; |
|
|
|
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
grid.ColumnDefinitions.Clear(); |
|
|
|
|
|
var defs = Parse((string)e.NewValue); |
|
|
foreach (var def in defs) |
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = def }); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void FixedColumnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
|
|
|
|
|
|
if (grid.ColumnDefinitions.Count == 0) |
|
|
grid.ColumnDefinitions.Add(new ColumnDefinition()); |
|
|
|
|
|
|
|
|
for (var i = 0; i < grid.ColumnDefinitions.Count; i++) |
|
|
grid.ColumnDefinitions[i].Width = (GridLength)e.NewValue; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void FixedRowHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
|
|
|
|
|
|
if (grid.RowDefinitions.Count == 0) |
|
|
grid.RowDefinitions.Add(new RowDefinition()); |
|
|
|
|
|
|
|
|
for (var i = 0; i < grid.RowDefinitions.Count; i++) |
|
|
grid.RowDefinitions[i].Height = (GridLength)e.NewValue; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static GridLength[] Parse(string text) |
|
|
{ |
|
|
var tokens = text.Split(','); |
|
|
var definitions = new GridLength[tokens.Length]; |
|
|
for (var i = 0; i < tokens.Length; i++) |
|
|
{ |
|
|
var str = tokens[i]; |
|
|
double value; |
|
|
|
|
|
|
|
|
if (str.Contains('*')) |
|
|
{ |
|
|
if (!double.TryParse(str.Replace("*", ""), out value)) |
|
|
value = 1.0; |
|
|
|
|
|
definitions[i] = new GridLength(value, GridUnitType.Star); |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
if (double.TryParse(str, out value)) |
|
|
{ |
|
|
definitions[i] = new GridLength(value); |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
definitions[i] = GridLength.Auto; |
|
|
} |
|
|
|
|
|
return definitions; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void RowCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if ((int)e.NewValue < 0) |
|
|
return; |
|
|
|
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
|
|
|
|
|
|
var height = GridLength.Auto; |
|
|
if (grid.RowDefinitions.Count > 0) |
|
|
height = grid.RowDefinitions[0].Height; |
|
|
|
|
|
|
|
|
grid.RowDefinitions.Clear(); |
|
|
for (var i = 0; i < (int)e.NewValue; i++) |
|
|
grid.RowDefinitions.Add( |
|
|
new RowDefinition { Height = height }); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void RowsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (string.IsNullOrEmpty((string)e.NewValue)) |
|
|
return; |
|
|
|
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
grid.RowDefinitions.Clear(); |
|
|
|
|
|
var defs = Parse((string)e.NewValue); |
|
|
foreach (var def in defs) |
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = def }); |
|
|
} |
|
|
|
|
|
#endregion Public Methods |
|
|
|
|
|
#region Private Methods |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnChildHorizontalAlignmentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
foreach (UIElement child in grid.Children) |
|
|
if (grid.ChildHorizontalAlignment.HasValue) |
|
|
child.SetValue(HorizontalAlignmentProperty, grid.ChildHorizontalAlignment); |
|
|
else |
|
|
child.SetValue(HorizontalAlignmentProperty, DependencyProperty.UnsetValue); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnChildMarginChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
foreach (UIElement child in grid.Children) |
|
|
if (grid.ChildMargin.HasValue) |
|
|
child.SetValue(MarginProperty, grid.ChildMargin); |
|
|
else |
|
|
child.SetValue(MarginProperty, DependencyProperty.UnsetValue); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnChildVerticalAlignmentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
{ |
|
|
if (d is not AutoGrid grid) return; |
|
|
|
|
|
foreach (UIElement child in grid.Children) |
|
|
if (grid.ChildVerticalAlignment.HasValue) |
|
|
child.SetValue(VerticalAlignmentProperty, grid.ChildVerticalAlignment); |
|
|
else |
|
|
child.SetValue(VerticalAlignmentProperty, DependencyProperty.UnsetValue); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ApplyChildLayout(UIElement child) |
|
|
{ |
|
|
if (ChildMargin != null) child.SetIfDefault(MarginProperty, ChildMargin.Value); |
|
|
if (ChildHorizontalAlignment != null) |
|
|
child.SetIfDefault(HorizontalAlignmentProperty, ChildHorizontalAlignment.Value); |
|
|
if (ChildVerticalAlignment != null) child.SetIfDefault(VerticalAlignmentProperty, ChildVerticalAlignment.Value); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int Clamp(int value, int max) |
|
|
{ |
|
|
return value > max ? max : value; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void PerformLayout() |
|
|
{ |
|
|
var fillRowFirst = Orientation == Orientation.Horizontal; |
|
|
var rowCount = RowDefinitions.Count; |
|
|
var colCount = ColumnDefinitions.Count; |
|
|
|
|
|
if (rowCount == 0 || colCount == 0) |
|
|
return; |
|
|
|
|
|
var position = 0; |
|
|
var skip = new bool[rowCount, colCount]; |
|
|
foreach (UIElement child in Children) |
|
|
{ |
|
|
var childIsCollapsed = child.Visibility == Visibility.Collapsed; |
|
|
if (IsAutoIndexing && !childIsCollapsed) |
|
|
{ |
|
|
if (fillRowFirst) |
|
|
{ |
|
|
var row = Clamp(position / colCount, rowCount - 1); |
|
|
var col = Clamp(position % colCount, colCount - 1); |
|
|
if (skip[row, col]) |
|
|
{ |
|
|
position++; |
|
|
row = position / colCount; |
|
|
col = position % colCount; |
|
|
} |
|
|
|
|
|
SetRow(child, row); |
|
|
SetColumn(child, col); |
|
|
position += GetColumnSpan(child); |
|
|
|
|
|
var offset = GetRowSpan(child) - 1; |
|
|
while (offset > 0) skip[row + offset--, col] = true; |
|
|
} |
|
|
else |
|
|
{ |
|
|
var row = Clamp(position % rowCount, rowCount - 1); |
|
|
var col = Clamp(position / rowCount, colCount - 1); |
|
|
if (skip[row, col]) |
|
|
{ |
|
|
position++; |
|
|
row = position % rowCount; |
|
|
col = position / rowCount; |
|
|
} |
|
|
|
|
|
SetRow(child, row); |
|
|
SetColumn(child, col); |
|
|
position += GetRowSpan(child); |
|
|
|
|
|
var offset = GetColumnSpan(child) - 1; |
|
|
while (offset > 0) skip[row, col + offset--] = true; |
|
|
} |
|
|
} |
|
|
|
|
|
ApplyChildLayout(child); |
|
|
} |
|
|
} |
|
|
|
|
|
#endregion Private Methods |
|
|
} |