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

@@ -10,7 +10,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Pages.ToolBarDemo"> x:Class="Ursa.Demo.Pages.ToolBarDemo">
<StackPanel> <StackPanel>
<u:ToolBar HorizontalAlignment="Left"> <u:ToolBar HorizontalAlignment="Left" Header="Hello World">
<Button Content="Button 1" /> <Button Content="Button 1" />
<Button Content="Button 2" /> <Button Content="Button 2" />
<Button Content="Button 3" u:ToolBar.OverflowMode="Always" /> <Button Content="Button 3" u:ToolBar.OverflowMode="Always" />
@@ -20,6 +20,5 @@
<template:ToolBarItemTemplateSelector/> <template:ToolBarItemTemplateSelector/>
</u:ToolBar.ItemTemplate> </u:ToolBar.ItemTemplate>
</u:ToolBar> </u:ToolBar>
<CheckBox Content="Check"></CheckBox>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -38,10 +38,16 @@ public class ToolBar: HeaderedItemsControl
} }
public static readonly AttachedProperty<OverflowMode> OverflowModeProperty = 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 void SetOverflowMode(Control obj, OverflowMode value) => obj.SetValue(OverflowModeProperty, value);
public static OverflowMode GetOverflowMode(Control? obj) => obj.GetValue(OverflowModeProperty); 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() static ToolBar()
{ {

View File

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