From 78fc1c1e87eb2ddc441c0e5ccb3e1bc7d98a9726 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Mon, 14 Jul 2025 13:05:30 +0200 Subject: [PATCH] cleaner get_parent() --- lib/ugui.c3l/src/ugui_core.c3 | 7 ++++-- lib/ugui.c3l/src/ugui_layout.c3 | 41 ++++----------------------------- 2 files changed, 10 insertions(+), 38 deletions(-) diff --git a/lib/ugui.c3l/src/ugui_core.c3 b/lib/ugui.c3l/src/ugui_core.c3 index c96725a..144d236 100644 --- a/lib/ugui.c3l/src/ugui_core.c3 +++ b/lib/ugui.c3l/src/ugui_core.c3 @@ -65,7 +65,7 @@ alias ElemCache = cache::Cache{Id, Elem, MAX_ELEMENTS}; alias CmdQueue = fifo::Fifo{Cmd}; -faultdef INVALID_SIZE, EVENT_UNSUPPORTED, UNEXPECTED_ELEMENT, WRONG_ELEMENT_TYPE, WRONG_ID; +faultdef INVALID_SIZE, EVENT_UNSUPPORTED, WRONG_ELEMENT_TYPE, WRONG_ID; const Rect DIV_FILL = { .x = 0, .y = 0, .w = 0, .h = 0 }; @@ -117,7 +117,10 @@ struct Ctx { fn Elem*? Ctx.get_parent(&ctx) { Id parent_id = ctx.tree.get(ctx.active_div)!; - return ctx.cache.search(parent_id); + Elem*? parent = ctx.cache.search(parent_id); + if (catch parent) return parent; + if (parent.type != ETYPE_DIV) return WRONG_ELEMENT_TYPE?; + return parent; } macro @bits(#a) => $typeof(#a).sizeof*8; diff --git a/lib/ugui.c3l/src/ugui_layout.c3 b/lib/ugui.c3l/src/ugui_layout.c3 index e59cb66..805b2c9 100644 --- a/lib/ugui.c3l/src/ugui_layout.c3 +++ b/lib/ugui.c3l/src/ugui_layout.c3 @@ -9,51 +9,25 @@ enum Layout { fn void? Ctx.layout_set_row(&ctx) { - Id parent_id = ctx.tree.get(ctx.active_div)!; - Elem *parent = ctx.cache.search(parent_id)!; - - if (parent.type != ETYPE_DIV) { - // what? - return UNEXPECTED_ELEMENT?; - } - + Elem *parent = ctx.get_parent()!; parent.div.layout = LAYOUT_ROW; } fn void? Ctx.layout_set_column(&ctx) { - Id parent_id = ctx.tree.get(ctx.active_div)!; - Elem *parent = ctx.cache.search(parent_id)!; - - if (parent.type != ETYPE_DIV) { - // what? - return UNEXPECTED_ELEMENT?; - } - + Elem *parent = ctx.get_parent()!; parent.div.layout = LAYOUT_COLUMN; } fn void? Ctx.layout_set_floating(&ctx) { - Id parent_id = ctx.tree.get(ctx.active_div)!; - Elem *parent = ctx.cache.search(parent_id)!; - - if (parent.type != ETYPE_DIV) { - // what? - return UNEXPECTED_ELEMENT?; - } - + Elem *parent = ctx.get_parent()!; parent.div.layout = LAYOUT_FLOATING; } fn void? Ctx.layout_next_row(&ctx) { - Id parent_id = ctx.tree.get(ctx.active_div)!; - Elem *parent = ctx.cache.search(parent_id)!; - - if (parent.type != ETYPE_DIV) { - return UNEXPECTED_ELEMENT?; - } + Elem *parent = ctx.get_parent()!; parent.div.origin_r = { .x = parent.bounds.x, @@ -64,12 +38,7 @@ fn void? Ctx.layout_next_row(&ctx) fn void? Ctx.layout_next_column(&ctx) { - Id parent_id = ctx.tree.get(ctx.active_div)!; - Elem *parent = ctx.cache.search(parent_id)!; - - if (parent.type != ETYPE_DIV) { - return UNEXPECTED_ELEMENT?; - } + Elem *parent = ctx.get_parent()!; parent.div.origin_c = { .x = parent.div.children_bounds.bottom_right().x,