refactor: adopt WinUI.Dock approach for layout updates and tab content hosting
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using CommunityToolkit.WinUI.Controls;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using CommunityToolkit.WinUI.Controls;
|
||||
|
||||
namespace Ghost.Editor.View.Controls.Docking;
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace Ghost.Editor.View.Controls.Docking;
|
||||
/// A container that can host multiple dock modules with splitters.
|
||||
/// </summary>
|
||||
[TemplatePart(Name = PART_GRID, Type = typeof(Grid))]
|
||||
public partial class DockPanel : DockContainer
|
||||
public class DockPanel : DockContainer
|
||||
{
|
||||
private const string PART_GRID = "PART_Grid";
|
||||
private const double SPLITTER_THICKNESS = 1;
|
||||
private const double SPLITTER_THICKNESS = 4;
|
||||
|
||||
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
|
||||
nameof(Orientation), typeof(Orientation), typeof(DockPanel), new PropertyMetadata(Orientation.Horizontal, OnOrientationChanged));
|
||||
@@ -74,123 +74,44 @@ public partial class DockPanel : DockContainer
|
||||
{
|
||||
if (_grid == null) return;
|
||||
|
||||
// Remove splitters and children that are no longer in the collection
|
||||
for (int i = _grid.Children.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var child = _grid.Children[i];
|
||||
if (child is GridSplitter)
|
||||
{
|
||||
_grid.Children.RemoveAt(i);
|
||||
}
|
||||
else if (child is DockModule module && !Children.Contains(module))
|
||||
{
|
||||
_grid.Children.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
_grid.Children.Clear();
|
||||
_grid.RowDefinitions.Clear();
|
||||
_grid.ColumnDefinitions.Clear();
|
||||
|
||||
if (Children.Count == 0)
|
||||
{
|
||||
_grid.RowDefinitions.Clear();
|
||||
_grid.ColumnDefinitions.Clear();
|
||||
return;
|
||||
}
|
||||
if (Children.Count == 0) return;
|
||||
|
||||
if (Orientation == Orientation.Horizontal)
|
||||
{
|
||||
_grid.RowDefinitions.Clear();
|
||||
|
||||
int requiredColumns = (Children.Count * 2) - 1;
|
||||
while (_grid.ColumnDefinitions.Count > requiredColumns)
|
||||
for (int i = 0; i < Children.Count; i++)
|
||||
{
|
||||
_grid.ColumnDefinitions.RemoveAt(_grid.ColumnDefinitions.Count - 1);
|
||||
}
|
||||
|
||||
for (var i = 0; i < Children.Count; i++)
|
||||
{
|
||||
int columnIndex = i * 2;
|
||||
if (columnIndex >= _grid.ColumnDefinitions.Count)
|
||||
{
|
||||
_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
||||
}
|
||||
else
|
||||
{
|
||||
_grid.ColumnDefinitions[columnIndex].Width = new GridLength(1, GridUnitType.Star);
|
||||
}
|
||||
|
||||
_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
||||
var child = Children[i];
|
||||
if (!_grid.Children.Contains(child))
|
||||
{
|
||||
_grid.Children.Add(child);
|
||||
}
|
||||
|
||||
Grid.SetColumn(child, columnIndex);
|
||||
Grid.SetRow(child, 0);
|
||||
Grid.SetColumn(child, i * 2);
|
||||
_grid.Children.Add(child);
|
||||
|
||||
if (i < Children.Count - 1)
|
||||
{
|
||||
int splitterIndex = i * 2 + 1;
|
||||
if (splitterIndex >= _grid.ColumnDefinitions.Count)
|
||||
{
|
||||
_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||||
}
|
||||
else
|
||||
{
|
||||
_grid.ColumnDefinitions[splitterIndex].Width = GridLength.Auto;
|
||||
}
|
||||
|
||||
_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
||||
var splitter = new GridSplitter { ResizeDirection = GridSplitter.GridResizeDirection.Columns, Width = SPLITTER_THICKNESS };
|
||||
Grid.SetColumn(splitter, splitterIndex);
|
||||
Grid.SetRow(splitter, 0);
|
||||
Grid.SetColumn(splitter, i * 2 + 1);
|
||||
_grid.Children.Add(splitter);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_grid.ColumnDefinitions.Clear();
|
||||
|
||||
int requiredRows = (Children.Count * 2) - 1;
|
||||
while (_grid.RowDefinitions.Count > requiredRows)
|
||||
for (int i = 0; i < Children.Count; i++)
|
||||
{
|
||||
_grid.RowDefinitions.RemoveAt(_grid.RowDefinitions.Count - 1);
|
||||
}
|
||||
|
||||
for (var i = 0; i < Children.Count; i++)
|
||||
{
|
||||
int rowIndex = i * 2;
|
||||
if (rowIndex >= _grid.RowDefinitions.Count)
|
||||
{
|
||||
_grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
|
||||
}
|
||||
else
|
||||
{
|
||||
_grid.RowDefinitions[rowIndex].Height = new GridLength(1, GridUnitType.Star);
|
||||
}
|
||||
|
||||
_grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
|
||||
var child = Children[i];
|
||||
if (!_grid.Children.Contains(child))
|
||||
{
|
||||
_grid.Children.Add(child);
|
||||
}
|
||||
|
||||
Grid.SetRow(child, rowIndex);
|
||||
Grid.SetColumn(child, 0);
|
||||
Grid.SetRow(child, i * 2);
|
||||
_grid.Children.Add(child);
|
||||
|
||||
if (i < Children.Count - 1)
|
||||
{
|
||||
int splitterIndex = i * 2 + 1;
|
||||
if (splitterIndex >= _grid.RowDefinitions.Count)
|
||||
{
|
||||
_grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
||||
}
|
||||
else
|
||||
{
|
||||
_grid.RowDefinitions[splitterIndex].Height = GridLength.Auto;
|
||||
}
|
||||
|
||||
_grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
||||
var splitter = new GridSplitter { ResizeDirection = GridSplitter.GridResizeDirection.Rows, Height = SPLITTER_THICKNESS };
|
||||
Grid.SetRow(splitter, splitterIndex);
|
||||
Grid.SetColumn(splitter, 0);
|
||||
Grid.SetRow(splitter, i * 2 + 1);
|
||||
_grid.Children.Add(splitter);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user