#pragma once #include #include #include #include #include #include #include namespace htab::ui { struct UiScale { double scale = 1.0; bool compact = false; bool phys_tiny = false; bool touch = false; }; inline UiScale compute_ui_scale(const gemui_app_t* app, int w, int h) { double sys_scale = std::max(0.5, gemui_scale(app)); bool phys_tiny = (w <= 320 || h <= 360); double eff_scale = phys_tiny ? 1.0 : sys_scale; bool compact = phys_tiny || (static_cast(w) / eff_scale <= 320.0) || (static_cast(h) / eff_scale <= 360.0); bool touch = !phys_tiny && gemui_touch_mode(app); return UiScale{eff_scale, compact, phys_tiny, touch}; } inline int spx(int logical_px, const UiScale& ui) { if (logical_px <= 0) return 0; int v = static_cast(std::floor(static_cast(logical_px) * ui.scale + 0.5)); return v < 1 ? 1 : v; } inline int pt_delta_for_ui(const gemui_theme_t* t, int logical_px, const UiScale& ui) { double sc = (t && t->scale > 0.0) ? t->scale : 1.0; int base_px = (t && t->base_font_size > 0) ? t->base_font_size : 15; int pt_delta = 0; if (ui.phys_tiny) { int target = std::max(9, logical_px); pt_delta = static_cast(std::ceil(static_cast(target - base_px) / sc)); } else { pt_delta = logical_px - 15; } while (base_px + static_cast(std::llround(static_cast(pt_delta) * sc)) < 9) { ++pt_delta; } return pt_delta; } inline int measure_text_px(cairo_t* cr, const gemui_theme_t* t, const std::string& text, int logical_px, const UiScale& ui, bool bold = false) { if (text.empty()) return 0; cairo_save(cr); cairo_rectangle(cr, 0, 0, 0, 0); cairo_clip(cr); int tw = gemui_draw_text(cr, t, gemui_rect_t{0, 0, 10000, 128}, text.c_str(), pt_delta_for_ui(t, logical_px, ui), bold, GEMUI_ALIGN_LEFT, t->fg_main); cairo_restore(cr); return tw; } struct TextEditor { std::string text; size_t cursor = 0; void set_text(std::string s) { text = std::move(s); cursor = text.size(); } void clear() { text.clear(); cursor = 0; } size_t prev_cp_pos(size_t pos) const { if (pos == 0) return 0; size_t p = pos - 1; while (p > 0 && (static_cast(text[p]) & 0xC0) == 0x80) { --p; } return p; } size_t next_cp_pos(size_t pos) const { if (pos >= text.size()) return text.size(); size_t p = pos + 1; while (p < text.size() && (static_cast(text[p]) & 0xC0) == 0x80) { ++p; } return p; } void move_left() { cursor = prev_cp_pos(cursor); } void move_right() { cursor = next_cp_pos(cursor); } void move_home() { cursor = 0; } void move_end() { cursor = text.size(); } void insert_utf8(const char* utf8) { if (!utf8 || !utf8[0]) return; std::string clean; for (const char* p = utf8; *p; ++p) { unsigned char uc = static_cast(*p); if (uc >= 0x20 && uc != 0x7F) clean.push_back(*p); } if (clean.empty()) return; text.insert(cursor, clean); cursor += clean.size(); } void backspace() { if (cursor == 0) return; size_t p = prev_cp_pos(cursor); text.erase(p, cursor - p); cursor = p; } void del_char() { if (cursor >= text.size()) return; size_t n = next_cp_pos(cursor); text.erase(cursor, n - cursor); } bool handle_key(KeySym ks, unsigned int state, const char* utf8) { bool ctrl = (state & ControlMask) != 0; if (!ctrl) { if (ks == XK_Left) { move_left(); return true; } if (ks == XK_Right) { move_right(); return true; } if (ks == XK_Home) { move_home(); return true; } if (ks == XK_End) { move_end(); return true; } if (ks == XK_BackSpace) { backspace(); return true; } if (ks == XK_Delete) { del_char(); return true; } if (utf8 && utf8[0]) { insert_utf8(utf8); return true; } } return false; } }; inline void draw_single_line_editor(cairo_t* cr, const gemui_theme_t* t, gemui_rect_t r, const TextEditor& ed, const char* placeholder, bool focused, int logical_px, const UiScale& ui) { double rad = 3.0 * ui.scale; double lw = std::max(1.0, (focused ? 1.5 : 1.0) * ui.scale); gemui_fill_round(cr, r, rad, focused ? t->bg_input_focus : t->bg_input); gemui_stroke_round(cr, r, rad, lw, focused ? t->highlight : t->border); int pad = spx(4, ui); int vpad = std::max(1, spx(1, ui)); gemui_rect_t inner{r.x + pad, r.y + vpad, std::max(1, r.w - pad * 2), std::max(1, r.h - vpad * 2)}; if (inner.w <= 4 || inner.h <= 4) return; int delta = pt_delta_for_ui(t, logical_px, ui); if (ed.text.empty() && !focused) { if (placeholder && placeholder[0]) { gemui_draw_text(cr, t, inner, placeholder, delta, false, GEMUI_ALIGN_LEFT, t->fg_muted); } return; } std::string prefix = ed.text.substr(0, std::min(ed.cursor, ed.text.size())); int caret_x = measure_text_px(cr, t, prefix, logical_px, ui, false); int edge_guard = spx(4, ui); int scroll_x = 0; if (caret_x > inner.w - edge_guard) { scroll_x = caret_x - (inner.w - edge_guard); } cairo_save(cr); cairo_rectangle(cr, inner.x, inner.y, inner.w, inner.h); cairo_clip(cr); if (!ed.text.empty()) { gemui_rect_t draw_r{inner.x - scroll_x, inner.y, std::max(inner.w + scroll_x + spx(32, ui), 8000), inner.h}; gemui_draw_text(cr, t, draw_r, ed.text.c_str(), delta, false, GEMUI_ALIGN_LEFT, t->fg_bright); } else if (placeholder && placeholder[0]) { gemui_draw_text(cr, t, inner, placeholder, delta, false, GEMUI_ALIGN_LEFT, t->fg_muted); } if (focused) { int cx = inner.x + caret_x - scroll_x; if (cx >= inner.x && cx <= inner.x + inner.w - 1) { int cpad = std::max(1, spx(2, ui)); gemui_set_source(cr, t->highlight); cairo_set_line_width(cr, std::max(1.5, 1.5 * ui.scale)); cairo_move_to(cr, cx + 0.5, inner.y + cpad); cairo_line_to(cr, cx + 0.5, inner.y + inner.h - cpad); cairo_stroke(cr); } } cairo_restore(cr); } } // namespace htab::ui