feat: move identification to measure process.

This commit is contained in:
rabbitism
2024-02-21 23:34:39 +08:00
parent 6f4e11c7f5
commit 395d185714
3 changed files with 31 additions and 10 deletions

View File

@@ -38,10 +38,16 @@ public class ToolBar: HeaderedItemsControl
}
public static readonly AttachedProperty<OverflowMode> OverflowModeProperty =
AvaloniaProperty.RegisterAttached<ToolBar, Control?, OverflowMode>("OverflowMode");
AvaloniaProperty.RegisterAttached<ToolBar, Control, OverflowMode>("OverflowMode");
public static void SetOverflowMode(Control? obj, OverflowMode value) => obj.SetValue(OverflowModeProperty, value);
public static OverflowMode GetOverflowMode(Control? obj) => obj.GetValue(OverflowModeProperty);
public static void SetOverflowMode(Control obj, OverflowMode value) => obj.SetValue(OverflowModeProperty, value);
public static OverflowMode GetOverflowMode(Control obj) => obj.GetValue(OverflowModeProperty);
internal static readonly AttachedProperty<bool> IsOverflowItemProperty =
AvaloniaProperty.RegisterAttached<ToolBar, Control, bool>("IsOverflowItem");
internal static void SetIsOverflowItem(Control obj, bool value) => obj.SetValue(IsOverflowItemProperty, value);
internal static bool GetIsOverflowItem(Control obj) => obj.GetValue(IsOverflowItemProperty);
static ToolBar()
{

View File

@@ -2,7 +2,6 @@
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.VisualTree;
namespace Ursa.Controls;
@@ -45,16 +44,18 @@ public class ToolBarPanel: StackPanel
Size measureSize = availableSize;
bool horizontal = Orientation == Orientation.Horizontal;
bool hasVisibleChildren = false;
measureSize = horizontal
? measureSize.WithWidth(double.PositiveInfinity)
: measureSize.WithHeight(double.PositiveInfinity);
measureSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
int index = 0;
if (logicalChildren is null) return size;
for (int count = logicalChildren.Count; index < count; ++index)
{
Control control = logicalChildren[index];
var mode = ToolBar.GetOverflowMode(control);
if(mode == OverflowMode.Always) continue;
if (mode == OverflowMode.Always)
{
ToolBar.SetIsOverflowItem(control, true);
continue;
}
bool isVisible = control.IsVisible;
if (isVisible)
{
@@ -83,6 +84,21 @@ public class ToolBarPanel: StackPanel
protected override Size ArrangeOverride(Size finalSize)
{
var logicalChildren = _parent?.GetLogicalChildren().OfType<Control>().ToList();
Children.Clear();
OverflowPanel?.Children.Clear();
foreach (var child in logicalChildren)
{
if (ToolBar.GetIsOverflowItem(child))
{
OverflowPanel?.Children.Add(child);
}
else
{
this.Children.Add(child);
}
}
return base.ArrangeOverride(finalSize);
if (_parent is null)
{
return finalSize;