Skip to content

feature: SizedBox, Center components & rendering fix - #47

Open
paramendula wants to merge 2 commits into
primequantuM4:mainfrom
paramendula:main
Open

feature: SizedBox, Center components & rendering fix#47
paramendula wants to merge 2 commits into
primequantuM4:mainfrom
paramendula:main

Conversation

@paramendula

Copy link
Copy Markdown

New:
Center component: allows to center child widgets, improving layout flexibility (Documented)
SizedBox component: a box of fixed size that has one child (useful for fixed layouts, Documented)
CanvasBuffer.fill: implemented a fill method for CanvasBuffer to support background coloring (Documented)
SizedBox demo: incorporates SizedBox and Center components in a simple showcase app

Rendering bug fix:
Resolved an issue in StatefulComponentInstance.render where child component bounds were accessed before being set (my demo was throwing exceptions because of that)

implemented
fix: StatefulComponentInstance.render was accessing child.bounds before
they were set
@primequantuM4

Copy link
Copy Markdown
Owner

@paramendula thanks for contributing! I'll get back to you tomorrow!

}) : _childInstance = component.child.createInstance();

@override
Size measure(Size maxSize) {

@primequantuM4 primequantuM4 Nov 27, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The measure function is meant to report the component’s full size to the layout engine so it can position everything correctly. Right now the returned width and height don’t account for padding.vertical and padding.horizontal. If these values aren’t included, the layout engine may place the component incorrectly or produce visual glitches.

The same applies to any style properties that add visual size, such as border width or border height. These also contribute to the component’s actual layout size and should be added to the measured width and height.

Including padding and style in the measurement will prevent unexpected UI issues and keep the layout consistent.

for (final child in childrenInstance) {
Logger.trace("StatefulComponent", "Item $child is being rendered");
child.render(buffer, child.bounds);
child.render(buffer, bounds);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

child.bounds is the rectangle assigned by LayoutEngine._layoutRecursiveCompute. That’s how every ParentComponentInstance (rows, columns, buttons, etc.) learns the exact slot it should draw into. Passing the parent’s bounds instead would tell every child to render over the entire parent area, so siblings would stack on top of one another and ignore their measured positions, padding, and absolute offsets. Hit testing and clipping would likewise break because children no longer know their true coordinates.

I noticed that _SizedBoxInstance extends ComponentInstance instead of ParentComponentInstance changing this will allow the layout tree calculation to reach the child nodes.

/// Fills a rectangular [area] with a specified [backgroundColor].
///
/// All cells within [area] will have their background color set to [backgroundColor].
void fill(Rect area, AnsiColorType backgroundColor) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition! absolutely needed

}

@override
int fitHeight() => size.height + margin.vertical;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@primequantuM4 primequantuM4 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together! The addition is solid and it's clear care went into the implementation. There are just a couple of pieces to adjust so the component fits smoothly into the layout system.

  1. The measure function should report the full layout size, which includes padding (vertical and horizontal) and any style properties that affect the final dimensions, like borders. Adding these in will help avoid unexpected placement issues in the layout engine.

  2. SizedBoxInstance is currently extending ComponentInstance, but its role matches ParentComponentInstance more closely. Updating the base class will make it behave consistently with the other layout containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants