From f8ab74eafe053b8d7c706dd5e7af6761dd8e6182 Mon Sep 17 00:00:00 2001 From: jacob Date: Sun, 28 Dec 2025 17:57:35 -0600 Subject: [PATCH] keep alignment of truncated text --- src/pp/pp_vis/pp_vis_core.c | 6 +++--- src/ui/ui_core.c | 32 ++++++++++++++++++++------------ src/ui/ui_core.h | 1 + 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/pp/pp_vis/pp_vis_core.c b/src/pp/pp_vis/pp_vis_core.c index d92ae289..0447b973 100644 --- a/src/pp/pp_vis/pp_vis_core.c +++ b/src/pp/pp_vis/pp_vis_core.c @@ -611,7 +611,7 @@ void V_TickForever(WaveLaneCtx *lane) V_CmdNode *first_cmd_node = 0; V_CmdNode *last_cmd_node = 0; - b32 has_mouse_focus = (UI_MatchKey(ui_frame->hovered_box, vis_box) && UI_IsKeyNil(ui_frame->active_box)) || UI_MatchKey(ui_frame->active_box, vis_box); + b32 has_mouse_focus = UI_IsKeyNil(ui_frame->hot_box) || UI_MatchKey(ui_frame->hot_box, vis_box); b32 has_keyboard_focus = 1; if (!window_frame.has_focus) { @@ -1086,8 +1086,8 @@ void V_TickForever(WaveLaneCtx *lane) UI_SetNext(BackgroundColor, bg_color); UI_SetNext(BorderColor, border_color); UI_SetNext(Border, 1); - UI_SetNext(Width, UI_SHRINK(theme.text_padding_x, 0)); - UI_SetNext(Height, UI_SHRINK(theme.text_padding_y, 0)); + UI_SetNext(Width, UI_SHRINK(3, 0)); + UI_SetNext(Height, UI_SHRINK(1, 0)); UI_SetNext(ChildAlignment, UI_Alignment_Center); UI_SetNext(Flags, UI_BoxFlag_DrawText | UI_BoxFlag_Interactable); UI_SetNext(Text, tab_name); diff --git a/src/ui/ui_core.c b/src/ui/ui_core.c index 84ad4439..c7da7a4e 100644 --- a/src/ui/ui_core.c +++ b/src/ui/ui_core.c @@ -746,12 +746,18 @@ UI_Frame *UI_BeginFrame(UI_FrameFlag frame_flags, Vec4 swapchain_color) } // Update box hot & active states + UI_Box *hot_box = active_box; + if (hovered_box && !active_box) + { + hot_box = hovered_box; + } + for (u64 pre_index = 0; pre_index < UI.boxes_count; ++pre_index) { UI_Box *box = last_frame->boxes_pre[pre_index]; UI_Report *report = &box->report; report->is_hovered = box == hovered_box; - report->is_hot = box == active_box || (box == hovered_box && (box == active_box || active_box == 0)); + report->is_hot = box == hot_box; f32 target_hovered = report->is_hovered; f32 target_hot = report->is_hot; f32 target_active = box == active_box; @@ -766,6 +772,7 @@ UI_Frame *UI_BeginFrame(UI_FrameFlag frame_flags, Vec4 swapchain_color) } frame->hovered_box = hovered_box ? hovered_box->key : UI_NilKey; + frame->hot_box = hot_box ? hot_box->key : UI_NilKey; frame->active_box = active_box ? active_box->key : UI_NilKey; } @@ -1383,21 +1390,25 @@ void UI_EndFrame(UI_Frame *frame) // Truncate run u64 final_rects_count = 0; GC_RunRect *final_rects = 0; + f32 final_baseline_length = 0; if (should_truncate) { // Get elipses run GC_Run elipses_run = GC_RunFromString(scratch.arena, Lit("..."), box->desc.font, box->desc.font_size); - f32 elipses_start_pos = max_baseline - elipses_run.baseline_length; + f32 truncation_offset = max_baseline - elipses_run.baseline_length; // Append non-overflowed rects + f32 elipses_offset = 0; final_rects = PushStructsNoZero(scratch.arena, GC_RunRect, raw_run.rects_count); for (u64 rect_idx = 0; rect_idx < raw_run.rects_count; ++rect_idx) { GC_RunRect rr = raw_run.rects[rect_idx]; - if (rr.baseline_pos + rr.advance <= elipses_start_pos) + if (rr.baseline_pos + rr.advance <= truncation_offset) { final_rects[final_rects_count] = rr; final_rects_count += 1; + elipses_offset = MaxF32(elipses_offset, rr.baseline_pos + rr.advance); + final_baseline_length = MaxF32(final_baseline_length, rr.baseline_pos + rr.advance); } } @@ -1405,11 +1416,12 @@ void UI_EndFrame(UI_Frame *frame) for (u64 rect_idx = 0; rect_idx < elipses_run.rects_count; ++rect_idx) { GC_RunRect rr = elipses_run.rects[rect_idx]; - rr.baseline_pos += elipses_start_pos; - if (rr.baseline_pos > 0) + rr.baseline_pos += elipses_offset; + if (rr.baseline_pos + rr.advance <= max_baseline) { final_rects[final_rects_count] = rr; final_rects_count += 1; + final_baseline_length = MaxF32(final_baseline_length, rr.baseline_pos + rr.advance); } } } @@ -1417,20 +1429,16 @@ void UI_EndFrame(UI_Frame *frame) { final_rects = raw_run.rects; final_rects_count = raw_run.rects_count; + final_baseline_length = raw_run.baseline_length; } UI_AxisAlignment x_alignment = box->desc.child_alignment[Axis_X]; UI_AxisAlignment y_alignment = box->desc.child_alignment[Axis_Y]; - if (should_truncate) - { - x_alignment = UI_AxisAlignment_Start; - } // Compute baseline f32 ascent = raw_run.font_ascent; f32 font_descent = raw_run.font_descent; f32 cap = raw_run.font_cap; - f32 baseline_width = raw_run.baseline_length; f32 baseline_height = ascent + font_descent; Vec2 box_dims = DimsFromRng2(box->rect); Vec2 baseline = Zi; @@ -1443,12 +1451,12 @@ void UI_EndFrame(UI_Frame *frame) case UI_AxisAlignment_End: { baseline.x = box->rect.p1.x; - baseline.x -= baseline_width; + baseline.x -= final_baseline_length; } break; case UI_AxisAlignment_Center: { baseline.x = box->rect.p0.x; - baseline.x += (box_dims.x - baseline_width) / 2; + baseline.x += (box_dims.x - final_baseline_length) / 2; } break; } switch (y_alignment) diff --git a/src/ui/ui_core.h b/src/ui/ui_core.h index 34f27d69..8a951ebc 100644 --- a/src/ui/ui_core.h +++ b/src/ui/ui_core.h @@ -349,6 +349,7 @@ Struct(UI_Frame) // Control Vec2 cursor_pos; UI_Key hovered_box; + UI_Key hot_box; UI_Key active_box; // Cmds