// Modified from https://github.com/AvaloniaUI/AvaloniaAutoGrid /*The MIT License (MIT) Copyright (c) 2013 Charles Brown (carbonrobot) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Linq; using Avalonia; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Layout; namespace StabilityMatrix.Avalonia.Controls; /// /// Defines a flexible grid area that consists of columns and rows. /// Depending on the orientation, either the rows or the columns are auto-generated, /// and the children's position is set according to their index. /// [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] public class AutoGrid : Grid { /// /// Gets or sets the child horizontal alignment. /// /// The child horizontal alignment. [Category("Layout"), Description("Presets the horizontal alignment of all child controls")] public HorizontalAlignment? ChildHorizontalAlignment { get => (HorizontalAlignment?)GetValue(ChildHorizontalAlignmentProperty); set => SetValue(ChildHorizontalAlignmentProperty, value); } /// /// Gets or sets the child margin. /// /// The child margin. [Category("Layout"), Description("Presets the margin of all child controls")] public Thickness? ChildMargin { get => (Thickness?)GetValue(ChildMarginProperty); set => SetValue(ChildMarginProperty, value); } /// /// Gets or sets the child vertical alignment. /// /// The child vertical alignment. [Category("Layout"), Description("Presets the vertical alignment of all child controls")] public VerticalAlignment? ChildVerticalAlignment { get => (VerticalAlignment?)GetValue(ChildVerticalAlignmentProperty); set => SetValue(ChildVerticalAlignmentProperty, value); } /// /// Gets or sets the column count /// [Category("Layout"), Description("Defines a set number of columns")] public int ColumnCount { get => (int)GetValue(ColumnCountProperty)!; set => SetValue(ColumnCountProperty, value); } /// /// Gets or sets the fixed column width /// [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); } /// /// Gets or sets a value indicating whether the children are automatically indexed. /// /// The default is true. /// Note that if children are already indexed, setting this property to false will not remove their indices. /// /// [Category("Layout"), Description("Set to false to disable the auto layout functionality")] public bool IsAutoIndexing { get => (bool)GetValue(IsAutoIndexingProperty)!; set => SetValue(IsAutoIndexingProperty, value); } /// /// Gets or sets the orientation. /// The default is Vertical. /// /// The orientation. [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); } /// /// Gets or sets the number of rows /// [Category("Layout"), Description("Defines a set number of rows")] public int RowCount { get => (int)GetValue(RowCountProperty)!; set => SetValue(RowCountProperty, value); } /// /// Gets or sets the fixed row height /// [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); } /// /// Handles the column count changed event /// public static void ColumnCountChanged(AvaloniaPropertyChangedEventArgs e) { if ((int)e.NewValue! < 0) return; var grid = (AutoGrid)e.Sender; // look for an existing column definition for the height var width = grid.ColumnWidth; if (!grid.IsSet(ColumnWidthProperty) && grid.ColumnDefinitions.Count > 0) width = grid.ColumnDefinitions[0].Width; // clear and rebuild grid.ColumnDefinitions.Clear(); for (var i = 0; i < (int)e.NewValue; i++) grid.ColumnDefinitions.Add( new ColumnDefinition() { Width = width }); } /// /// Handle the fixed column width changed event /// public static void FixedColumnWidthChanged(AvaloniaPropertyChangedEventArgs e) { var grid = (AutoGrid)e.Sender; // add a default column if missing if (grid.ColumnDefinitions.Count == 0) grid.ColumnDefinitions.Add(new ColumnDefinition()); // set all existing columns to this width foreach (var t in grid.ColumnDefinitions) t.Width = (GridLength)e.NewValue!; } /// /// Handle the fixed row height changed event /// public static void FixedRowHeightChanged(AvaloniaPropertyChangedEventArgs e) { var grid = (AutoGrid)e.Sender; // add a default row if missing if (grid.RowDefinitions.Count == 0) grid.RowDefinitions.Add(new RowDefinition()); // set all existing rows to this height foreach (var t in grid.RowDefinitions) t.Height = (GridLength)e.NewValue!; } /// /// Handles the row count changed event /// public static void RowCountChanged(AvaloniaPropertyChangedEventArgs e) { if ((int)e.NewValue! < 0) return; var grid = (AutoGrid)e.Sender; // look for an existing row to get the height var height = grid.RowHeight; if (!grid.IsSet(RowHeightProperty) && grid.RowDefinitions.Count > 0) height = grid.RowDefinitions[0].Height; // clear and rebuild grid.RowDefinitions.Clear(); for (var i = 0; i < (int)e.NewValue; i++) grid.RowDefinitions.Add( new RowDefinition() { Height = height }); } /// /// Called when [child horizontal alignment changed]. /// private static void OnChildHorizontalAlignmentChanged(AvaloniaPropertyChangedEventArgs e) { var grid = (AutoGrid)e.Sender; foreach (var child in grid.Children) { child.SetValue(HorizontalAlignmentProperty, grid.ChildHorizontalAlignment ?? AvaloniaProperty.UnsetValue); } } /// /// Called when [child layout changed]. /// private static void OnChildMarginChanged(AvaloniaPropertyChangedEventArgs e) { var grid = (AutoGrid)e.Sender; foreach (var child in grid.Children) { child.SetValue(MarginProperty, grid.ChildMargin ?? AvaloniaProperty.UnsetValue); } } /// /// Called when [child vertical alignment changed]. /// private static void OnChildVerticalAlignmentChanged(AvaloniaPropertyChangedEventArgs e) { var grid = (AutoGrid)e.Sender; foreach (var child in grid.Children) { child.SetValue(VerticalAlignmentProperty, grid.ChildVerticalAlignment ?? AvaloniaProperty.UnsetValue); } } /// /// Apply child margins and layout effects such as alignment /// private void ApplyChildLayout(Control child) { if (ChildMargin != null) { child.SetValue(MarginProperty, ChildMargin.Value, BindingPriority.Template); } if (ChildHorizontalAlignment != null) { child.SetValue(HorizontalAlignmentProperty, ChildHorizontalAlignment.Value, BindingPriority.Template); } if (ChildVerticalAlignment != null) { child.SetValue(VerticalAlignmentProperty, ChildVerticalAlignment.Value, BindingPriority.Template); } } /// /// Clamp a value to its maximum. /// private int Clamp(int value, int max) { return (value > max) ? max : value; } /// /// Perform the grid layout of row and column indexes /// 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 (var child in Children.OfType()) { var childIsCollapsed = !child.IsVisible; 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); } } public static readonly AvaloniaProperty ChildHorizontalAlignmentProperty = AvaloniaProperty.Register("ChildHorizontalAlignment"); public static readonly AvaloniaProperty ChildMarginProperty = AvaloniaProperty.Register("ChildMargin"); public static readonly AvaloniaProperty ChildVerticalAlignmentProperty = AvaloniaProperty.Register("ChildVerticalAlignment"); public static readonly AvaloniaProperty ColumnCountProperty = AvaloniaProperty.RegisterAttached("ColumnCount", typeof(AutoGrid), 1); public static readonly AvaloniaProperty ColumnWidthProperty = AvaloniaProperty.RegisterAttached("ColumnWidth", typeof(AutoGrid), GridLength.Auto); public static readonly AvaloniaProperty IsAutoIndexingProperty = AvaloniaProperty.Register("IsAutoIndexing", true); public static readonly AvaloniaProperty OrientationProperty = AvaloniaProperty.Register("Orientation", Orientation.Vertical); public static readonly AvaloniaProperty RowCountProperty = AvaloniaProperty.RegisterAttached("RowCount", typeof(AutoGrid), 1); public static readonly AvaloniaProperty RowHeightProperty = AvaloniaProperty.RegisterAttached("RowHeight", typeof(AutoGrid), GridLength.Auto); static AutoGrid() { AffectsMeasure(ChildHorizontalAlignmentProperty, ChildMarginProperty, ChildVerticalAlignmentProperty, ColumnCountProperty, ColumnWidthProperty, IsAutoIndexingProperty, OrientationProperty, RowHeightProperty); ChildHorizontalAlignmentProperty.Changed.Subscribe(OnChildHorizontalAlignmentChanged); ChildMarginProperty.Changed.Subscribe(OnChildMarginChanged); ChildVerticalAlignmentProperty.Changed.Subscribe(OnChildVerticalAlignmentChanged); ColumnCountProperty.Changed.Subscribe(ColumnCountChanged); RowCountProperty.Changed.Subscribe(RowCountChanged); ColumnWidthProperty.Changed.Subscribe(FixedColumnWidthChanged); RowHeightProperty.Changed.Subscribe(FixedRowHeightChanged); } #region Overrides /// /// Measures the children of a in anticipation of arranging them during the pass. /// /// Indicates an upper limit size that should not be exceeded. /// /// that represents the required size to arrange child content. /// protected override Size MeasureOverride(Size constraint) { PerformLayout(); return base.MeasureOverride(constraint); } #endregion Overrides }