simplified code

This commit is contained in:
Alessandro Mauri 2025-09-12 22:18:15 +02:00
parent 71a959b9a1
commit 48a333e501

View File

@ -36,28 +36,13 @@ struct Layout {
Rect content_offset; // combined effect of margin, border and padding
}
// Total width of a layout element, complete with margin, border and padding
macro Size Layout.total_width(&el)
{
Rect min = (Rect){.w = el.w.min, .h = el.h.min}.expand(el.content_offset);
Rect max = (Rect){.w = el.w.max, .h = el.h.max}.expand(el.content_offset);
return {.min = min.w, .max = max.w};
}
// Total height of a layout element, complete with margin, border and padding
macro Size Layout.total_height(&el)
{
Rect min = (Rect){.w = el.w.min, .h = el.h.min}.expand(el.content_offset);
Rect max = (Rect){.w = el.w.max, .h = el.h.max}.expand(el.content_offset);
return {.min = min.h, .max = max.h};
}
// Returns the width and height of a @FIT() element based on it's wanted size (min/max)
// and the content size, this function is used to both update the parent's children size and
// give the dimensions of a fit element
// TODO: test and cleanup this function
macro Point Layout.get_dimensions(&el)
{
Point dim;
// if the direction is ROW then the text is placed horizontally with the children
if (el.dir == ROW) {
Size content_width = el.children.w + el.text.width;
@ -74,11 +59,10 @@ macro Point Layout.get_dimensions(&el)
Size height = el.h.combine(content_height);
short final_height = height.greater();
Point dim = {
dim = {
.x = final_width + el.content_offset.x + el.content_offset.w,
.y = final_height + el.content_offset.y + el.content_offset.h,
};
return dim;
} else {
// if the direction is COLUMN the text and children are one on top of the other
Size content_width = el.children.w.comb_max(el.text.width);
@ -95,12 +79,16 @@ macro Point Layout.get_dimensions(&el)
Size height = el.h.combine(content_height);
short final_height = height.greater();
Point dim = {
dim = {
.x = final_width + el.content_offset.x + el.content_offset.w,
.y = final_height + el.content_offset.y + el.content_offset.h,
};
return dim;
}
// GROSS HACK FOR EXACT DIMENSIONS
if (el.w.@is_exact()) dim.x = el.w.min + el.content_offset.x + el.content_offset.w;
if (el.h.@is_exact()) dim.y = el.h.min + el.content_offset.y + el.content_offset.h;
return dim;
}
// The content space of the element
@ -176,33 +164,8 @@ fn void resolve_dimensions(Elem* e, Elem* p)
Layout* pl = &p.layout;
Point elem_dimensions = el.get_dimensions();
short text_min_height;
// ASSIGN WIDTH
switch {
case el.w.@is_exact():
// if width is exact then assign it to the bounds
e.bounds.w = el.total_width().min;
case el.w.@is_grow():
// done in another pass
break;
case el.w.@is_fit(): // fit the element's children
e.bounds.w = elem_dimensions.x;
default: unreachable("width is not exact, grow or fit");
}
// ASSIGN HEIGHT
switch {
case el.h.@is_exact():
// if width is exact then assign it to the bounds and add border and paddign
e.bounds.h = el.total_height().min;
case el.h.@is_grow():
break;
// done in another pass
case el.h.@is_fit(): // fit the element's children
e.bounds.h = elem_dimensions.y;
default: unreachable("width is not exact, grow or fit");
}
e.bounds.w = elem_dimensions.x;
e.bounds.h = elem_dimensions.y;
switch (pl.dir) {
case ROW: