|
| 1 | +using System.Collections.Generic; |
| 2 | +using Microsoft.Maui.Layouts; |
| 3 | + |
| 4 | +// This is a temporary namespace until we rename everything and move the legacy layouts |
| 5 | +namespace Microsoft.Maui.Controls.Layout2 |
| 6 | +{ |
| 7 | + public class GridLayout : Layout, IGridLayout |
| 8 | + { |
| 9 | + List<IGridRowDefinition> _rowDefinitions = new List<IGridRowDefinition>(); |
| 10 | + List<IGridColumnDefinition> _columnDefinitions = new List<IGridColumnDefinition>(); |
| 11 | + |
| 12 | + public IReadOnlyList<IGridRowDefinition> RowDefinitions => _rowDefinitions; |
| 13 | + public IReadOnlyList<IGridColumnDefinition> ColumnDefinitions => _columnDefinitions; |
| 14 | + |
| 15 | + public double RowSpacing { get; set; } |
| 16 | + public double ColumnSpacing { get; set; } |
| 17 | + |
| 18 | + Dictionary<IView, GridInfo> _viewInfo = new Dictionary<IView, GridInfo>(); |
| 19 | + |
| 20 | + // TODO ezhart This needs to override Remove and clean up any row/column/span info for the removed child |
| 21 | + |
| 22 | + public int GetColumn(IView view) |
| 23 | + { |
| 24 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 25 | + { |
| 26 | + return gridInfo.Col; |
| 27 | + } |
| 28 | + |
| 29 | + return 0; |
| 30 | + } |
| 31 | + |
| 32 | + public int GetColumnSpan(IView view) |
| 33 | + { |
| 34 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 35 | + { |
| 36 | + return gridInfo.ColSpan; |
| 37 | + } |
| 38 | + |
| 39 | + return 1; |
| 40 | + } |
| 41 | + |
| 42 | + public int GetRow(IView view) |
| 43 | + { |
| 44 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 45 | + { |
| 46 | + return gridInfo.Row; |
| 47 | + } |
| 48 | + |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + public int GetRowSpan(IView view) |
| 53 | + { |
| 54 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 55 | + { |
| 56 | + return gridInfo.RowSpan; |
| 57 | + } |
| 58 | + |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + protected override ILayoutManager CreateLayoutManager() => new GridLayoutManager(this); |
| 63 | + |
| 64 | + public void AddRowDefinition(IGridRowDefinition gridRowDefinition) |
| 65 | + { |
| 66 | + _rowDefinitions.Add(gridRowDefinition); |
| 67 | + } |
| 68 | + |
| 69 | + public void AddColumnDefinition(IGridColumnDefinition gridColumnDefinition) |
| 70 | + { |
| 71 | + _columnDefinitions.Add(gridColumnDefinition); |
| 72 | + } |
| 73 | + |
| 74 | + public void SetRow(IView view, int row) |
| 75 | + { |
| 76 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 77 | + { |
| 78 | + gridInfo.Row = row; |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + _viewInfo[view] = new GridInfo { Row = row }; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void SetRowSpan(IView view, int span) |
| 87 | + { |
| 88 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 89 | + { |
| 90 | + gridInfo.RowSpan = span; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + _viewInfo[view] = new GridInfo { RowSpan = span }; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void SetColumn(IView view, int col) |
| 99 | + { |
| 100 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 101 | + { |
| 102 | + gridInfo.Col = col; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + _viewInfo[view] = new GridInfo { Col = col }; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public void SetColumnSpan(IView view, int span) |
| 111 | + { |
| 112 | + if (_viewInfo.TryGetValue(view, out GridInfo gridInfo)) |
| 113 | + { |
| 114 | + gridInfo.ColSpan = span; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + _viewInfo[view] = new GridInfo { ColSpan = span }; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + class GridInfo |
| 123 | + { |
| 124 | + public int Row { get; set; } |
| 125 | + public int Col { get; set; } |
| 126 | + public int RowSpan { get; set; } = 1; |
| 127 | + public int ColSpan { get; set; } = 1; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments