diff --git a/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs b/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs index a508e98..513d289 100644 --- a/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs +++ b/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs @@ -1,32 +1,55 @@ using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Alignments; using TUI.Engine.Nodes.Attributes.Orientations; +using TUI.Engine.Nodes.Attributes.Resizing; using TUI.Engine.Nodes.Components; namespace TUI.Engine.Nodes.Containers; public static class ContainerExtension { - // public static Position GetNextNodePosition(this INode node, - // Orientation orientation, - // Size allowableSize, - // Position currentChildrenPosition) - // { - // var nodeSize = node.GetSize(allowableSize); - // - // return orientation switch - // { - // Orientation.Horizontal => currentChildrenPosition with - // { - // Left = currentChildrenPosition.Left + nodeSize.Width - // }, - // Orientation.Vertical => currentChildrenPosition with - // { - // Top = currentChildrenPosition.Top + nodeSize.Height - // }, - // _ => throw new ArgumentOutOfRangeException() - // }; - // } + public static Size GetSize(this IContainer container, Resizing resizing) + { + int width = resizing switch + { + // Resizing.Fixed => _fixedWeight, + // Resizing.Hug => GetAllowableSize().Width, + // Resizing.Adaptive => GetAllowableSize().Width, + _ => 0 + }; + + int height = resizing switch + { + // Resizing.Fixed => _fixedHeight, + // Resizing.Hug => GetAllowableSize().Height, + // Resizing.Adaptive => GetAllowableSize().Height, + _ => 0 + }; + + return new Size(width, height); + } + + public static Position GetNextNodePosition(this IContainer container, Orientation orientation, Size allowableSize, + Position containerPosition) + { + var nodeSize = container.GetSize(allowableSize); + + return GetNextNodePosition(orientation, nodeSize, containerPosition); + } + + private static Position GetNextNodePosition(Orientation orientation, Size nodeSize, Position nodePosition) => + orientation switch + { + Orientation.Horizontal => nodePosition with + { + Left = nodePosition.Left + nodeSize.Width + }, + Orientation.Vertical => nodePosition with + { + Top = nodePosition.Top + nodeSize.Height + }, + _ => throw new ArgumentOutOfRangeException() + }; public static Size GetSize(this IContainer container, Size allowableSize) { diff --git a/src/TUI/Engine/Rendering/ContainerCraftsman.cs b/src/TUI/Engine/Rendering/ContainerCraftsman.cs index 05f363d..2a70802 100644 --- a/src/TUI/Engine/Rendering/ContainerCraftsman.cs +++ b/src/TUI/Engine/Rendering/ContainerCraftsman.cs @@ -19,14 +19,13 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable public Size Draw(IContainer container, Position sketchPosition, Size allowableSize) { var sketchSize = container.GetSize(allowableSize); - var controlNumber = 0; + var nodes = container.GetNodes(); - while (controlNumber < container.GetNodes().Count) + while (controlNumber < nodes.Count) { - var node = container.GetNodes()[controlNumber]; - - sketchPosition = RenderNode(node, container.Orientation, sketchSize, sketchPosition); + var node = nodes[controlNumber]; + sketchPosition = DrawNode(node, container.Orientation, sketchPosition, sketchSize); controlNumber++; } @@ -34,55 +33,37 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable return sketchSize; } - private Position RenderNode(INode node, Orientation orientation, Size defaultSize, Position position) + private Position DrawNode(INode node, Orientation orientation, Position sketchPosition, Size sketchSize) { switch (node) { - case IContainer container when orientation == Orientation.Horizontal: - Draw(container, position, defaultSize); - return position with - { - Left = position.Left + defaultSize.Width - }; - case IContainer container when orientation == Orientation.Vertical: - Draw(container, position, defaultSize); - return position with - { - Top = position.Top + defaultSize.Height - }; - case IComponent component when orientation == Orientation.Horizontal: - var componentWidth = _componentCraftsman.Draw(component, position, defaultSize).Width; - return position with - { - Left = position.Left + (defaultSize.Width <= componentWidth ? componentWidth : defaultSize.Width) - }; - case IComponent component when orientation == Orientation.Vertical: - var componentHeight = _componentCraftsman.Draw(component, position, defaultSize).Height; - return position with - { - Top = position.Top + (defaultSize.Height <= componentHeight ? componentHeight : defaultSize.Height) - }; + case IContainer childContainer: + Draw(childContainer, sketchPosition, sketchSize); + return GetNextNodePosition(orientation, sketchSize, sketchPosition); + case IComponent childComponent: + var componentSize = _componentCraftsman.Draw(childComponent, sketchPosition, sketchSize); + return GetNextNodePosition(orientation, sketchSize, sketchPosition, componentSize); default: throw new InvalidCastException(); } } -} - -// private int GetWidth() => -// ResizingHorizontal switch -// { -// Resizing.Fixed => _fixedWidth, -// Resizing.Hug => GetAllowableSize().Width, -// Resizing.Adaptive => GetAllowableSize().Width, -// _ => 0 -// }; -// -// private int GetHeight() => -// ResizingVertical switch -// { -// Resizing.Fixed => _fixedHeight, -// Resizing.Hug => GetAllowableSize().Height, -// Resizing.Adaptive => GetAllowableSize().Height, -// _ => 0 -// }; + private static Position GetNextNodePosition( + Orientation orientation, + Size defaultSize, + Position position, + Size? componentSize = null) + { + switch (orientation) + { + case Orientation.Horizontal: + var componentWidth = componentSize?.Width.Max(defaultSize.Width) ?? defaultSize.Width; + return position with { Left = position.Left + componentWidth }; + case Orientation.Vertical: + var componentHeight = componentSize?.Height.Max(defaultSize.Height) ?? defaultSize.Height; + return position with { Top = position.Top + componentHeight }; + default: + throw new InvalidCastException(); + } + } +} \ No newline at end of file diff --git a/src/TUI/Engine/Rendering/IntegerExtension.cs b/src/TUI/Engine/Rendering/IntegerExtension.cs new file mode 100644 index 0000000..af53b8b --- /dev/null +++ b/src/TUI/Engine/Rendering/IntegerExtension.cs @@ -0,0 +1,6 @@ +namespace TUI.Engine.Rendering; + +public static class IntegerExtension +{ + public static int Max(this int value, int maxValue) => maxValue <= value ? value : maxValue; +} \ No newline at end of file