From 601a396aa8162635c6c59127683398deee1f1440 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Thu, 19 Dec 2024 00:29:30 +0100 Subject: [PATCH] scrollbar fixup --- src/ugui_div.c3 | 60 ++++++++++++++++++++++++---------------------- src/ugui_layout.c3 | 22 +++++++++++------ 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/ugui_div.c3 b/src/ugui_div.c3 index 6d636d6..ea10c0a 100644 --- a/src/ugui_div.c3 +++ b/src/ugui_div.c3 @@ -3,23 +3,22 @@ module ugui; import std::io; import std::math; -enum DivLayout { - LAYOUT_ROW, - LAYOUT_COLUMN, - LAYOUT_FLOATING, - LAYOUT_ABSOLUTE, -} +const short SCROLLBAR_DIM = 5; // div element struct ElemDiv { - DivLayout layout; - struct scroll { - bool can_x; - bool can_y; - bool on_x; - bool on_y; - float value_x; - float value_y; + Layout layout; + struct scroll_x { + bool enabled; + bool on; + bool focus; + float value; + } + struct scroll_y { + bool enabled; + bool on; + bool focus; + float value; } Rect children_bounds; // current frame children bounds Rect pcb; // previous frame children bounds @@ -43,8 +42,8 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size, bool scroll_x = false, boo } else if (c_elem.type != ETYPE_DIV) { return UgError.WRONG_ELEMENT_TYPE?; } - c_elem.div.scroll.can_x = scroll_x; - c_elem.div.scroll.can_y = scroll_y; + c_elem.div.scroll_x.enabled = scroll_x; + c_elem.div.scroll_y.enabled = scroll_y; // 2. layout the element c_elem.bounds = ctx.position_element(parent, size); @@ -103,34 +102,37 @@ fn void! Ctx.div_end(&ctx) // set the scrollbar flag, is used in layout - // vertical overflow - c_elem.div.scroll.on_y = cbc.y > bc.y && c_elem.div.scroll.can_y; // horizontal overflow - c_elem.div.scroll.on_x = cbc.x > bc.x && c_elem.div.scroll.can_x; + c_elem.div.scroll_x.on = cbc.x > bc.x && c_elem.div.scroll_x.enabled; + // vertical overflow + c_elem.div.scroll_y.on = cbc.y > bc.y && c_elem.div.scroll_y.enabled; + + short wdim = c_elem.div.scroll_y.focus ? SCROLLBAR_DIM*3 : SCROLLBAR_DIM; + short hdim = c_elem.div.scroll_x.focus ? SCROLLBAR_DIM*3 : SCROLLBAR_DIM; - if (c_elem.div.scroll.on_y) { + if (c_elem.div.scroll_y.on) { Rect vslider = { - .x = c_elem.bounds.x + c_elem.bounds.w - 10, + .x = c_elem.bounds.x + c_elem.bounds.w - wdim, .y = c_elem.bounds.y, - .w = 10, + .w = wdim, .h = c_elem.bounds.h, }; - DivLayout prev_l = c_elem.div.layout; + Layout prev_l = c_elem.div.layout; c_elem.div.layout = LAYOUT_ABSOLUTE; - ctx.slider_ver("div_scrollbar_vertical", vslider, &c_elem.div.scroll.value_y, max((float)bc.y / cbc.y, (float)0.15))!; + ctx.slider_ver("div_scrollbar_vertical", vslider, &c_elem.div.scroll_y.value, max((float)bc.y / cbc.y, (float)0.15))!; c_elem.div.layout = prev_l; } - if (c_elem.div.scroll.on_x) { + if (c_elem.div.scroll_x.on) { Rect hslider = { .x = c_elem.bounds.x, - .y = c_elem.bounds.y + c_elem.bounds.h - 10, + .y = c_elem.bounds.y + c_elem.bounds.h - hdim, .w = c_elem.bounds.w, - .h = 10, + .h = hdim, }; - DivLayout prev_l = c_elem.div.layout; + Layout prev_l = c_elem.div.layout; c_elem.div.layout = LAYOUT_ABSOLUTE; - ctx.slider_hor("div_scrollbar_horizontal", hslider, &c_elem.div.scroll.value_x, max((float)bc.x / cbc.x, (float)0.15))!; + ctx.slider_hor("div_scrollbar_horizontal", hslider, &c_elem.div.scroll_x.value, max((float)bc.x / cbc.x, (float)0.15))!; c_elem.div.layout = prev_l; } diff --git a/src/ugui_layout.c3 b/src/ugui_layout.c3 index a65ee78..095b41a 100644 --- a/src/ugui_layout.c3 +++ b/src/ugui_layout.c3 @@ -1,5 +1,11 @@ module ugui; +enum Layout { + LAYOUT_ROW, + LAYOUT_COLUMN, + LAYOUT_FLOATING, + LAYOUT_ABSOLUTE, +} fn void! Ctx.layout_set_row(&ctx) { @@ -139,19 +145,21 @@ fn Rect Ctx.position_element(&ctx, Elem *parent, Rect rect, bool style = false) }; // 4. Calculate the "scrolled" view - Point off; + Rect off; Rect* cb = &div.children_bounds; - if (div.scroll.can_x && div.scroll.on_x) { - off.x = (short)((float)(div.pcb.w - parent.bounds.w) * div.scroll.value_x); + if (div.scroll_x.enabled && div.scroll_x.on) { + off.x = (short)((float)(div.pcb.w - parent.bounds.w) * div.scroll_x.value); + off.w = SCROLLBAR_DIM; } - if (div.scroll.can_y && div.scroll.on_y) { - off.y = (short)((float)(div.pcb.h - parent.bounds.h) * div.scroll.value_y); + if (div.scroll_y.enabled && div.scroll_y.on) { + off.y = (short)((float)(div.pcb.h - parent.bounds.h) * div.scroll_y.value); + off.h = SCROLLBAR_DIM; } Rect view = { .x = parent.bounds.x + off.x, .y = parent.bounds.y + off.y, - .w = parent.bounds.w, - .h = parent.bounds.h, + .w = parent.bounds.w - off.w, + .h = parent.bounds.h - off.h, }; // 5. check if the placement overflows the children ounds, if so update them