Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve collapsed panels logic #331

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions src/Dock.Avalonia/Controls/ProportionalStackPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected override Size MeasureOverride(Size constraint)

AssignProportions(Children);

var previousisCollapsed = false;
var needsNextSplitter = false;

// Measure each of the Children
for (var i = 0; i < Children.Count; i++)
Expand All @@ -242,8 +242,6 @@ protected override Size MeasureOverride(Size constraint)
var isCollapsed = !isSplitter && GetIsCollapsed(control);
if (isCollapsed)
{
// TODO: Also handle next is empty.
previousisCollapsed = true;
var size = new Size();
control.Measure(size);
continue;
Expand All @@ -270,29 +268,22 @@ protected override Size MeasureOverride(Size constraint)
break;
}
}

needsNextSplitter = true;
}
else
{
var nextisCollapsed = false;
if (i + 1 < Children.Count)
{
var nextControl = Children[i + 1];
nextisCollapsed = !ProportionalStackPanelSplitter.IsSplitter(nextControl, out _ ) && GetIsCollapsed(nextControl);
}

if (previousisCollapsed || nextisCollapsed)
if (!needsNextSplitter)
{
var size = new Size();
control.Measure(size);
previousisCollapsed = true;
continue;
}

control.Measure(remainingSize);
needsNextSplitter = false;
}

previousisCollapsed = false;

var desiredSize = control.DesiredSize;

// Decrease the remaining space for the rest of the children
Expand Down Expand Up @@ -351,7 +342,7 @@ protected override Size ArrangeOverride(Size arrangeSize)

AssignProportions(Children);

var previousisCollapsed = false;
var needsNextSplitter = false;

for (var i = 0; i < Children.Count; i++)
{
Expand All @@ -362,31 +353,23 @@ protected override Size ArrangeOverride(Size arrangeSize)
var isCollapsed = !isSplitter && GetIsCollapsed(control);
if (isCollapsed)
{
// TODO: Also handle next is empty.
previousisCollapsed = true;
var rect = new Rect();
control.Arrange(rect);
index++;
continue;
}

var nextisCollapsed = false;
if (i + 1 < Children.Count)
{
var nextControl = Children[i + 1];
nextisCollapsed = !ProportionalStackPanelSplitter.IsSplitter(nextControl, out _) && GetIsCollapsed(nextControl);
}

if (isSplitter && (previousisCollapsed || nextisCollapsed))
if (!isSplitter)
needsNextSplitter = true;
else if (isSplitter && !needsNextSplitter)
{
var rect = new Rect();
control.Arrange(rect);
index++;
needsNextSplitter = false;
continue;
}

previousisCollapsed = false;

// Determine the remaining space left to arrange the element
var remainingRect = new Rect(
left,
Expand Down
68 changes: 30 additions & 38 deletions src/Dock.Avalonia/Controls/ProportionalStackPanelSplitter.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
Expand Down Expand Up @@ -168,26 +169,19 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

private Control? FindNextChild(ProportionalStackPanel panel)
{
var children = panel.Children;
int nextIndex;

if (Parent is ContentPresenter parentContentPresenter)
{
nextIndex = children.IndexOf(parentContentPresenter) + 1;
}
else
{
nextIndex = children.IndexOf(this) + 1;
}

return children[nextIndex];
return GetSiblingInDirection(panel, 1);
}

private void SetTargetProportion(double dragDelta)
{
var target = GetTargetElement();
var panel = GetPanel();
if (target is null || panel is null)
if (panel == null)
{
return;
}

var target = GetTargetElement(panel);
if (target is null)
{
return;
}
Expand Down Expand Up @@ -274,35 +268,33 @@ private void UpdateHeightOrWidth()
return null;
}

private Control? GetTargetElement()
private Control? GetSiblingInDirection(ProportionalStackPanel panel, int direction)
{
if (Parent is ContentPresenter presenter)
{
if (presenter.GetVisualParent() is not Panel panel)
{
return null;
}
Debug.Assert(direction == -1 || direction == 1);

var children = panel.Children;
int siblingIndex;

var parent = Parent as Control;
var index = parent is null ? -1 : panel.Children.IndexOf(parent);
if (index > 0 && panel.Children.Count > 0)
{
return panel.Children[index - 1];
}
if (Parent is ContentPresenter parentContentPresenter)
{
siblingIndex = children.IndexOf(parentContentPresenter) + direction;
}
else
{
var panel = GetPanel();
if (panel is not null)
{
var index = panel.Children.IndexOf(this);
if (index > 0 && panel.Children.Count > 0)
{
return panel.Children[index - 1];
}
}
siblingIndex = children.IndexOf(this) + direction;
}

return null;
while (siblingIndex >= 0 && siblingIndex < children.Count &&
(ProportionalStackPanel.GetIsCollapsed(children[siblingIndex]) || IsSplitter(children[siblingIndex], out _)))
{
siblingIndex += direction;
}

return siblingIndex >= 0 && siblingIndex < children.Count ? children[siblingIndex] : null;
}

private Control? GetTargetElement(ProportionalStackPanel panel)
{
return GetSiblingInDirection(panel, -1);
}
}
Loading