//- small helpers (same discipline as mp3_player: zero heap allocs per frame) internal b32 ui_hovered(PlatformInput *input, Rect rect) { return input->mouse_in_window && rect_contains(rect, input->mouse_x, input->mouse_y); } internal b32 ui_clicked(PlatformInput *input, Rect rect) { return ButtonPressed(input->mouse[0]) && ui_hovered(input, rect); } internal f32 ui_baseline(App *app, FontKind kind, f32 size, f32 y0, f32 height) { f32 line = font_line_height(&app->fonts, kind, size); f32 ascent = font_ascent(&app->fonts, kind, size); return floorf(y0 + (height - line) * 0.5f + ascent + 0.5f); } internal String8 ui_format(Arena *arena, const char *format, ...) { va_list args; va_start(args, format); char buffer[256]; int length = vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); length = Clamp(length, 0, (int)sizeof(buffer) - 1); u8 *out = PushArray(arena, u8, (u64)Max(length, 1)); MemCpy(out, buffer, (u64)length); return ToString8Size(out, (u64)length); } internal void format_bytes(char *out, u32 size, f64 bytes) { const char *units[] = {"B", "KB", "MB", "GB", "TB"}; u32 unit = 0; while(bytes >= 1024.0 && unit + 1 < ArrayCount(units)) { bytes /= 1024.0; ++unit; } snprintf(out, size, "%.1f %s", bytes, units[unit]); } internal void app_message(App *app, const char *format, ...) { va_list args; va_start(args, format); vsnprintf(app->message, sizeof(app->message), format, args); va_end(args); app->message_ns = time_now_ns(); } internal b32 str8_contains_nocase(String8 haystack, const char *needle, u32 needle_size) { if(needle_size == 0) return true; for(u64 start = 0; start + needle_size <= haystack.size; ++start) { u32 index = 0; for(; index < needle_size; ++index) { u8 a = haystack.data[start + index], b = (u8)needle[index]; if(a >= 'A' && a <= 'Z') a += 32; if(b >= 'A' && b <= 'Z') b += 32; if(a != b) break; } if(index == needle_size) return true; } return false; } internal void scroll_update(ScrollList *scroll, f32 content, f32 view, f32 dt, b32 *animating) { (void)dt; (void)animating; f32 max_offset = Max(content - view, 0.0f); scroll->target = Clamp(scroll->target, 0.0f, max_offset); scroll->offset = scroll->target; } internal void app_apply_settings(App *app) { settings_clamp(&app->settings); if(app->diagram) { app->diagram->default_routing = app->settings.default_routing; app->diagram->focus_hops = app->settings.focus_hops; } } //- layout pool (identical RAD-style binary split tree to mp3_player) internal s32 layout_alloc_panel(Layout *layout) { for(s32 index = 0; index < UI_MAX_PANELS; ++index) { if(layout->panels[index].kind == PANEL_FREE) { MemSetZero(&layout->panels[index], sizeof(Panel)); layout->panels[index].kind = PANEL_LEAF; layout->panels[index].parent = -1; return index; } } return -1; } internal s32 layout_alloc_view(Layout *layout, ViewKind kind) { for(s32 index = 0; index < UI_MAX_VIEWS; ++index) { if(layout->views[index].kind == VIEW_NONE) { MemSetZero(&layout->views[index], sizeof(View)); layout->views[index].kind = (u32)kind; layout->views[index].cam_zoom = 1.0f; layout->views[index].cam_target_zoom = 1.0f; layout->views[index].cam_fit_pending = (kind == VIEW_CANVAS); return index; } } return -1; } internal s32 layout_leaf(Layout *layout, s32 parent, ViewKind *kinds, u32 count) { s32 panel = layout_alloc_panel(layout); Panel *p = &layout->panels[panel]; p->kind = PANEL_LEAF; p->parent = parent; for(u32 index = 0; index < count && p->tab_count < UI_MAX_TABS; ++index) { s32 view = layout_alloc_view(layout, kinds[index]); if(view >= 0) p->tabs[p->tab_count++] = (u8)view; } return panel; } internal s32 layout_split(Layout *layout, s32 parent, u32 axis, f32 ratio) { s32 panel = layout_alloc_panel(layout); Panel *p = &layout->panels[panel]; p->kind = PANEL_SPLIT; p->parent = parent; p->axis = axis; p->ratio = ratio; return panel; } internal void layout_add_status_bar(Layout *layout) { ViewKind kinds[] = {VIEW_STATUS}; s32 old_root = layout->root; s32 split = layout_split(layout, -1, 1, -1.0f); s32 bar = layout_leaf(layout, split, kinds, 1); layout->panels[split].children[0] = old_root; layout->panels[split].children[1] = bar; layout->panels[old_root].parent = split; layout->root = split; } internal void ui_layout_default(Layout *layout) { MemSetZero(layout, sizeof(*layout)); layout->version = UI_LAYOUT_VERSION; // [ Canvas (72%) ] | [ Outline | Palette | Layers ] / [ Inspector | Edges | Source | Views ] ViewKind canvas_kinds[] = {VIEW_CANVAS}; ViewKind top_kinds[] = {VIEW_OUTLINE, VIEW_PALETTE, VIEW_LAYERS}; ViewKind bot_kinds[] = {VIEW_INSPECTOR, VIEW_EDGES, VIEW_SOURCE, VIEW_VIEWS}; s32 root = layout_split(layout, -1, 0, 0.70f); s32 left = layout_leaf(layout, root, canvas_kinds, ArrayCount(canvas_kinds)); s32 right = layout_split(layout, root, 1, 0.46f); s32 top = layout_leaf(layout, right, top_kinds, ArrayCount(top_kinds)); s32 bottom = layout_leaf(layout, right, bot_kinds, ArrayCount(bot_kinds)); layout->panels[root].children[0] = left; layout->panels[root].children[1] = right; layout->panels[right].children[0] = top; layout->panels[right].children[1] = bottom; layout->root = root; layout->focused = left; layout_add_status_bar(layout); } internal b32 layout_valid(Layout *layout) { if(layout->version != UI_LAYOUT_VERSION || layout->root < 0 || layout->root >= UI_MAX_PANELS) return false; if(layout->panels[layout->root].kind == PANEL_FREE) return false; for(s32 index = 0; index < UI_MAX_PANELS; ++index) { Panel *p = &layout->panels[index]; if(p->kind == PANEL_SPLIT) { for(u32 c = 0; c < 2; ++c) { s32 child = p->children[c]; if(child < 0 || child >= UI_MAX_PANELS || layout->panels[child].kind == PANEL_FREE || layout->panels[child].parent != index) return false; } } else if(p->kind == PANEL_LEAF) { if(p->tab_count > UI_MAX_TABS) return false; for(u32 t = 0; t < p->tab_count; ++t) { if(p->tabs[t] >= UI_MAX_VIEWS || layout->views[p->tabs[t]].kind == VIEW_NONE || layout->views[p->tabs[t]].kind >= VIEW_KIND_COUNT) return false; } } else if(p->kind != PANEL_FREE) { return false; } } return layout->focused >= 0 && layout->focused < UI_MAX_PANELS && layout->panels[layout->focused].kind == PANEL_LEAF; } internal s32 layout_first_leaf(Layout *layout, s32 panel) { while(layout->panels[panel].kind == PANEL_SPLIT) panel = layout->panels[panel].children[0]; return panel; } internal s32 panel_active_view(Layout *layout, s32 panel) { Panel *p = &layout->panels[panel]; if(p->kind != PANEL_LEAF || p->tab_count == 0) return -1; return p->tabs[Min(p->active_tab, p->tab_count - 1)]; } internal void layout_collapse(Layout *layout, s32 leaf) { Panel *p = &layout->panels[leaf]; if(p->kind != PANEL_LEAF || p->tab_count > 0 || p->parent < 0) return; s32 parent = p->parent; Panel *pp = &layout->panels[parent]; s32 sibling = (pp->children[0] == leaf) ? pp->children[1] : pp->children[0]; s32 grand = pp->parent; Panel moved = layout->panels[sibling]; moved.parent = grand; *pp = moved; if(pp->kind == PANEL_SPLIT) { layout->panels[pp->children[0]].parent = parent; layout->panels[pp->children[1]].parent = parent; } layout->panels[sibling].kind = PANEL_FREE; layout->panels[leaf].kind = PANEL_FREE; if(layout->focused == leaf || layout->focused == sibling || layout->panels[layout->focused].kind != PANEL_LEAF) { layout->focused = layout_first_leaf(layout, parent); } } internal void layout_remove_tab(Layout *layout, s32 panel, u32 tab) { Panel *p = &layout->panels[panel]; for(u32 index = tab; index + 1 < p->tab_count; ++index) p->tabs[index] = p->tabs[index + 1]; --p->tab_count; if(p->active_tab >= p->tab_count && p->active_tab > 0) --p->active_tab; } internal void layout_close_view(Layout *layout, s32 panel, u32 tab) { Panel *p = &layout->panels[panel]; if(tab >= p->tab_count) return; layout->views[p->tabs[tab]].kind = VIEW_NONE; layout_remove_tab(layout, panel, tab); layout_collapse(layout, panel); } typedef enum DropZone { DROP_NONE, DROP_CENTER, DROP_LEFT, DROP_RIGHT, DROP_TOP, DROP_BOTTOM } DropZone; internal void layout_move_view(Layout *layout, s32 from_panel, s32 view, s32 to_panel, DropZone zone) { Panel *source = &layout->panels[from_panel]; u32 tab = 0; for(; tab < source->tab_count && source->tabs[tab] != view; ++tab) {} if(tab == source->tab_count) return; if(from_panel == to_panel && (zone == DROP_CENTER || source->tab_count == 1)) return; if(zone == DROP_CENTER) { Panel *target = &layout->panels[to_panel]; if(target->tab_count >= UI_MAX_TABS) return; layout_remove_tab(layout, from_panel, tab); target->tabs[target->tab_count] = (u8)view; target->active_tab = target->tab_count++; layout->focused = to_panel; layout_collapse(layout, from_panel); return; } s32 keep = layout_alloc_panel(layout); s32 fresh = (keep >= 0) ? layout_alloc_panel(layout) : -1; if(keep < 0 || fresh < 0) { if(keep >= 0) layout->panels[keep].kind = PANEL_FREE; return; } layout_remove_tab(layout, from_panel, tab); Panel *target = &layout->panels[to_panel]; layout->panels[keep] = *target; layout->panels[keep].parent = to_panel; Panel *fresh_panel = &layout->panels[fresh]; fresh_panel->kind = PANEL_LEAF; fresh_panel->parent = to_panel; fresh_panel->tabs[0] = (u8)view; fresh_panel->tab_count = 1; b32 fresh_first = (zone == DROP_LEFT || zone == DROP_TOP); target->kind = PANEL_SPLIT; target->axis = (zone == DROP_LEFT || zone == DROP_RIGHT) ? 0 : 1; target->ratio = 0.5f; target->children[0] = fresh_first ? fresh : keep; target->children[1] = fresh_first ? keep : fresh; target->tab_count = 0; s32 source_now = (from_panel == to_panel) ? keep : from_panel; layout->focused = fresh; layout_collapse(layout, source_now); for(s32 index = 0; index < UI_MAX_PANELS; ++index) { Panel *p = &layout->panels[index]; if(p->kind != PANEL_LEAF) continue; for(u32 t = 0; t < p->tab_count; ++t) { if(p->tabs[t] == view) { layout->focused = index; p->active_tab = t; } } } } internal void layout_compute(Layout *layout, s32 panel, Rect rect) { Panel *p = &layout->panels[panel]; p->rect = rect; if(p->kind != PANEL_SPLIT) return; f32 half = UI_GAP * 0.5f; if(p->axis == 0) { f32 split = rect.x0 + rect_w(rect) * p->ratio; layout_compute(layout, p->children[0], Rect{rect.x0, rect.y0, split - half, rect.y1}); layout_compute(layout, p->children[1], Rect{split + half, rect.y0, rect.x1, rect.y1}); } else { f32 split = (p->ratio < 0) ? Max(rect.y1 - half + p->ratio * UI_STATUS_H, rect.y0 + half) : rect.y0 + rect_h(rect) * p->ratio; layout_compute(layout, p->children[0], Rect{rect.x0, rect.y0, rect.x1, split - half}); layout_compute(layout, p->children[1], Rect{rect.x0, split + half, rect.x1, rect.y1}); } } internal s32 layout_find_kind(Layout *layout, ViewKind kind, s32 *out_panel, u32 group = 0) { for(s32 index = 0; index < UI_MAX_PANELS; ++index) { Panel *p = &layout->panels[index]; if(p->kind != PANEL_LEAF) continue; for(u32 t = 0; t < p->tab_count; ++t) { if(layout->views[p->tabs[t]].kind == (u32)kind && layout->views[p->tabs[t]].group == group) { if(out_panel) *out_panel = index; return p->tabs[t]; } } } return -1; } internal void layout_open(Layout *layout, ViewKind kind, u32 group = 0) { s32 panel = -1; s32 view = layout_find_kind(layout, kind, &panel, group); if(view >= 0 && kind != VIEW_CANVAS) { Panel *p = &layout->panels[panel]; for(u32 t = 0; t < p->tab_count; ++t) if(p->tabs[t] == view) p->active_tab = t; layout->focused = panel; return; } if(view >= 0 && kind == VIEW_CANVAS) { Panel *p = &layout->panels[panel]; for(u32 t = 0; t < p->tab_count; ++t) if(p->tabs[t] == view) p->active_tab = t; layout->focused = panel; return; } Panel *p = &layout->panels[layout->focused]; view = layout_alloc_view(layout, kind); if(view < 0) return; layout->views[view].group = group; if(p->tab_count >= UI_MAX_TABS) { layout->views[view].kind = VIEW_NONE; return; } p->tabs[p->tab_count] = (u8)view; p->active_tab = p->tab_count++; } internal void layout_split_with(Layout *layout, ViewKind kind, u32 axis, u32 group = 0) { s32 copy = layout_alloc_view(layout, kind); if(copy < 0) return; layout->views[copy].group = group; Panel *p = &layout->panels[layout->focused]; if(p->tab_count >= UI_MAX_TABS) { layout->views[copy].kind = VIEW_NONE; return; } p->tabs[p->tab_count++] = (u8)copy; layout_move_view(layout, layout->focused, copy, layout->focused, axis == 0 ? DROP_RIGHT : DROP_BOTTOM); } internal void layout_split_focused(Layout *layout, u32 axis) { s32 view = panel_active_view(layout, layout->focused); if(view < 0) return; layout_split_with(layout, (ViewKind)layout->views[view].kind, axis, layout->views[view].group); } internal b32 panel_is_bar(Layout *layout, s32 panel) { return rect_h(layout->panels[panel].rect) < UI_TAB_H + UI_COLUMN_H + UI_ROW_H; } internal void layout_focus_direction(Layout *layout, s32 dx, s32 dy) { Rect current = layout->panels[layout->focused].rect; s32 best = -1; f32 best_d = 1e9f; for(s32 index = 0; index < UI_MAX_PANELS; ++index) { Panel *p = &layout->panels[index]; if(p->kind != PANEL_LEAF || index == layout->focused || panel_is_bar(layout, index)) continue; Rect r = p->rect; f32 d = -1; if(dx > 0 && r.x0 >= current.x1 - 1 && r.y0 < current.y1 && r.y1 > current.y0) d = r.x0 - current.x1; if(dx < 0 && r.x1 <= current.x0 + 1 && r.y0 < current.y1 && r.y1 > current.y0) d = current.x0 - r.x1; if(dy > 0 && r.y0 >= current.y1 - 1 && r.x0 < current.x1 && r.x1 > current.x0) d = r.y0 - current.y1; if(dy < 0 && r.y1 <= current.y0 + 1 && r.x0 < current.x1 && r.x1 > current.x0) d = current.y0 - r.y1; if(d >= 0 && d < best_d) { best_d = d; best = index; } } if(best >= 0) layout->focused = best; } internal void layout_focus_next(Layout *layout) { for(s32 step = 1; step <= UI_MAX_PANELS; ++step) { s32 index = (layout->focused + step) % UI_MAX_PANELS; if(layout->panels[index].kind == PANEL_LEAF && !panel_is_bar(layout, index)) { layout->focused = index; return; } } } internal void layout_leaves(Layout *layout, s32 panel, s32 *out, u32 *count) { Panel *p = &layout->panels[panel]; if(p->kind == PANEL_SPLIT) { layout_leaves(layout, p->children[0], out, count); layout_leaves(layout, p->children[1], out, count); } else if(p->kind == PANEL_LEAF && *count < UI_MAX_PANELS) { out[(*count)++] = panel; } } internal void layout_cycle_views(Layout *layout, s32 direction) { s32 leaves[UI_MAX_PANELS]; u32 leaf_count = 0; layout_leaves(layout, layout->root, leaves, &leaf_count); s32 panels[UI_MAX_PANELS * UI_MAX_TABS]; u32 tabs[UI_MAX_PANELS * UI_MAX_TABS]; u32 count = 0; s32 at = -1; for(u32 leaf = 0; leaf < leaf_count; ++leaf) { s32 panel = leaves[leaf]; Panel *p = &layout->panels[panel]; if(panel_is_bar(layout, panel)) continue; for(u32 t = 0; t < p->tab_count; ++t) { if(panel == layout->focused && t == p->active_tab) at = (s32)count; panels[count] = panel; tabs[count] = t; ++count; } } if(count == 0) return; s32 next = (at < 0) ? 0 : (at + direction + (s32)count) % (s32)count; layout->focused = panels[next]; layout->panels[panels[next]].active_tab = tabs[next]; } internal b32 layout_view_visible(Layout *layout, ViewKind kind) { for(s32 index = 0; index < UI_MAX_PANELS; ++index) { s32 view = panel_active_view(layout, index); if(view >= 0 && layout->views[view].kind == (u32)kind) return true; } return false; } internal View *find_canvas_view(App *app) { s32 focused_v = panel_active_view(&app->layout, app->layout.focused); if(focused_v >= 0 && app->layout.views[focused_v].kind == VIEW_CANVAS) return &app->layout.views[focused_v]; for(s32 i = 0; i < UI_MAX_PANELS; ++i) { s32 v = panel_active_view(&app->layout, i); if(v >= 0 && app->layout.views[v].kind == VIEW_CANVAS) return &app->layout.views[v]; } return NULL; } //- view registry & profiler groups typedef enum ProfileMetric { PM_SECTION_FRAME, PM_FPS, PM_FRAME_CPU, PM_FRAME_SPLIT, PM_UI_SPLIT, PM_RENDER_SPLIT, PM_IDLE, PM_DRAW, PM_SECTION_MEMORY, PM_RSS, PM_RSS_KIND, PM_VIRTUAL, PM_STACK_MAIN, PM_SECTION_HEAP, PM_HEAP_LIVE, PM_HEAP_ALLOCS, PM_HEAP_FREES, PM_HEAP_BLOCKS, PM_HEAP_TOTAL, PM_HEAP_GLIBC, PM_SECTION_ARENAS, PM_ARENA, PM_SECTION_THREADS, PM_THREAD, PM_SECTION_PROCESS, PM_CPU, PM_FAULTS, PM_SWITCHES, PM_SECTION_GPU, PM_GPU_MEMORY, PM_GPU_SWAPCHAIN, PM_SECTION_FONTS, PM_FONT_GLYPHS, PM_FONT_RUNS, PM_FONT_ATLAS, PM_SECTION_DIAGRAM, PM_DIA_NODES, PM_DIA_EDGES, PM_DIA_MEMBERS, PM_DIA_LAYOUT, PM_DIA_UNDO, PM_COUNT, } ProfileMetric; global_variable const char *profile_labels[PM_COUNT] = { "frame", "frame rate", "frame cpu", "frame split", "ui split", "render split", "between frames", "draw", "memory", "resident", "resident by kind", "virtual", "main thread stack", "heap (malloc, whole process)", "live", "allocations", "frees", "blocks alive", "handed out", "glibc arenas", "arenas", "", "threads and stacks", "", "process", "cpu", "page faults", "context switches", "gpu", "allocated by us", "swapchain", "fonts", "glyph cache", "shaped text cache", "atlas", "diagram", "nodes", "edges", "sections / members", "last auto-layout", "undo history", }; global_variable const char *profile_group_names[] = {"frame", "memory", "heap", "arenas", "threads", "process", "gpu", "fonts", "diagram"}; internal b32 profile_is_section(u32 m) { return m == PM_SECTION_FRAME || m == PM_SECTION_MEMORY || m == PM_SECTION_HEAP || m == PM_SECTION_ARENAS || m == PM_SECTION_THREADS || m == PM_SECTION_PROCESS || m == PM_SECTION_GPU || m == PM_SECTION_FONTS || m == PM_SECTION_DIAGRAM; } typedef struct ViewType ViewType; struct ViewType { u32 kind; u32 group; const char *name; }; //- the manual (help.txt, embedded in app.cpp) // NOTE: line offsets built once, the help view and :help index into them #define HELP_MAX_LINES 2048 global_variable u32 help_line_offsets[HELP_MAX_LINES + 1]; global_variable u32 help_line_count; internal String8 help_text(void) { return EmbeddedString8(design_help_text); } internal void help_index(void) { if(help_line_count) return; String8 text = help_text(); help_line_offsets[0] = 0; u32 lines = 0; for(u64 i = 0; i < text.size && lines < HELP_MAX_LINES; ++i) { if(text.data[i] == '\n') help_line_offsets[++lines] = (u32)(i + 1); } // NOTE: a trailing newline doesn't start another line if(lines < HELP_MAX_LINES && help_line_offsets[lines] < text.size) help_line_offsets[++lines] = (u32)text.size + 1; help_line_count = lines; } internal String8 help_line(u32 line) { help_index(); if(line >= help_line_count) return str8_zero(); String8 text = help_text(); u32 start = help_line_offsets[line]; u32 end = help_line_offsets[line + 1] - 1; // drop the newline if(end > text.size) end = (u32)text.size; if(end > start && text.data[end - 1] == '\r') --end; return ToString8Size(text.data + start, end > start ? end - start : 0); } internal b32 help_line_is_heading(u32 line) { String8 text = help_line(line); if(text.size == 0) return false; b32 dashes = true, upper = false, lower = false; for(u64 i = 0; i < text.size; ++i) { u8 c = text.data[i]; if(c != '-' && c != '=') dashes = false; if(c >= 'A' && c <= 'Z') upper = true; if(c >= 'a' && c <= 'z') lower = true; } return dashes || (upper && !lower && text.data[0] != ' '); } // NOTE: numbered section headings look like "10. EDGES: KINDS, CREATING, ROUTING" internal String8 help_heading_title(u32 line) { String8 text = help_line(line); if(text.size == 0 || text.data[0] < '0' || text.data[0] > '9' || !help_line_is_heading(line)) return str8_zero(); u64 at = 0; while(at < text.size && text.data[at] >= '0' && text.data[at] <= '9') ++at; if(at >= text.size || text.data[at] != '.') return str8_zero(); ++at; while(at < text.size && text.data[at] == ' ') ++at; return ToString8Size(text.data + at, text.size - at); } // NOTE: best match first: a section whose title starts with the topic, then a section title // containing it, then any line of the body. common words map onto the section they mean. internal s32 help_find_topic(const char *topic) { local_persist const char *aliases[][2] = { {"keys", "keyboard"}, {"key", "keyboard"}, {"shortcuts", "keyboard"}, {"bindings", "keyboard"}, {"dsl", "the .dit.txt"}, {"format", "the .dit.txt"}, {"syntax", "the .dit.txt"}, {"text", "the .dit.txt"}, {"open", "opening"}, {"files", "opening"}, {"drop", "opening"}, {"drag", "opening"}, {"dnd", "opening"}, {"mac", "platform"}, {"macos", "platform"}, {"windows", "platform"}, {"linux", "platform"}, {"x11", "platform"}, {"wayland", "platform"}, {"vulkan", "troubleshooting"}, {"problems", "troubleshooting"}, {"scan", "scanning"}, {"scanner", "scanning"}, {"theme", "settings"}, {"themes", "settings"}, {"colors", "settings"}, {"fonts", "settings"}, {"save", "saving"}, {"export", "saving"}, {"svg", "saving"}, {"cli", "command line"}, {"options", "command line"}, {"panels", "panels"}, {"tabs", "panels"}, {"views", "the workspace"}, {"workspace", "the workspace"}, {"members", "nodes"}, {"sections", "nodes"}, {"node", "nodes"}, {"edge", "edges"}, {"focus", "layers"}, {"tags", "layers"}, {"mouse", "canvas: mouse"}, {"canvas", "canvas: keyboard"}, {"inspector", "tables"}, {"outline", "tables"}, {"start", "getting started"}, {"modes", "modes"}, {"command", "all commands"}, {"commands", "all commands"}, {"align", "layout"}, {"routing", "edges"}, }; for(u32 index = 0; index < ArrayCount(aliases); ++index) { if(!strcasecmp(topic, aliases[index][0])) { topic = aliases[index][1]; break; } } help_index(); u32 size = (u32)strlen(topic); if(!size) return -1; // NOTE: the body starts at the first numbered heading, the intro / contents never match u32 body = 0; while(body < help_line_count && help_heading_title(body).size == 0) ++body; for(u32 line = body; line < help_line_count; ++line) { String8 title = help_heading_title(line); if(title.size >= size && strncasecmp((char *)title.data, topic, size) == 0) return (s32)line; } for(u32 line = body; line < help_line_count; ++line) { String8 title = help_heading_title(line); if(title.size && str8_contains_nocase(title, topic, size)) return (s32)line; } for(u32 line = body; line < help_line_count; ++line) { if(str8_contains_nocase(help_line(line), topic, size)) return (s32)line; } return -1; } internal void app_open_help(App *app, const char *topic) { layout_open(&app->layout, VIEW_HELP); s32 panel = -1; s32 index = layout_find_kind(&app->layout, VIEW_HELP, &panel); if(index < 0) return; View *view = &app->layout.views[index]; if(topic && topic[0]) { s32 line = help_find_topic(topic); if(line < 0) { app_message(app, "help: nothing about \"%s\"", topic); return; } view->cursor = line; view->scroll.target = (f32)line * UI_ROW_H; app_message(app, "help: %s", topic); } else { app_message(app, "help: j/k scroll, :help jumps, Ctrl+w c closes the tab"); } } #define UI_MAX_VIEW_TYPES 48 typedef struct BaseView BaseView; struct BaseView { u32 kind; const char *name; const char *title; }; global_variable const BaseView ui_base_views[] = { {VIEW_CANVAS, "canvas", "Canvas"}, {VIEW_OUTLINE, "outline", "Outline"}, {VIEW_INSPECTOR, "inspector", "Inspector"}, {VIEW_EDGES, "edges", "Edges"}, {VIEW_LAYERS, "layers", "Layers"}, {VIEW_PALETTE, "palette", "Palette"}, {VIEW_SOURCE, "source", "Source"}, {VIEW_VIEWS, "views", "Views"}, {VIEW_STATUS, "status", "Status"}, {VIEW_HELP, "help", "Help"}, }; internal u32 view_types(ViewType *out) { u32 count = 0; for(u32 i = 0; i < ArrayCount(ui_base_views); ++i) out[count++] = ViewType{ui_base_views[i].kind, 0, ui_base_views[i].name}; u32 group = 0; for(u32 i = 0; i < settings_desc_count() && count < UI_MAX_VIEW_TYPES; ++i) { if(settings_desc(i)->kind == SETTING_KIND_SECTION) out[count++] = ViewType{VIEW_SETTINGS, group++, settings_desc(i)->name}; } for(u32 i = 0; i < ArrayCount(profile_group_names) && count < UI_MAX_VIEW_TYPES; ++i) out[count++] = ViewType{VIEW_PROFILER, i, profile_group_names[i]}; return count; } internal const char *view_type_name(u32 kind, u32 group) { ViewType types[UI_MAX_VIEW_TYPES]; u32 count = view_types(types); for(u32 i = 0; i < count; ++i) if(types[i].kind == kind && types[i].group == group) return types[i].name; return "?"; } internal String8 view_title(Arena *arena, View *view) { for(u32 i = 0; i < ArrayCount(ui_base_views); ++i) { if(ui_base_views[i].kind == view->kind) return str8((char *)ui_base_views[i].title); } String8 name = ui_format(arena, "%s", view_type_name(view->kind, view->group)); if(name.size && name.data[0] >= 'a' && name.data[0] <= 'z') name.data[0] -= 32; return name; } internal b32 view_resolve(const char *name, ViewType *out) { ViewType types[UI_MAX_VIEW_TYPES]; u32 count = view_types(types); u32 size = (u32)strlen(name), hits = 0; if(size == 0) return false; for(u32 i = 0; i < count; ++i) { if(!strcasecmp(types[i].name, name)) { *out = types[i]; return true; } if(!strncasecmp(types[i].name, name, size)) { *out = types[i]; ++hits; } } return hits == 1; } //- include the canvas renderer now that helpers & layout functions exist #include "app_canvas.cpp" //- watch-style table rows & columns typedef enum RowKind { ROW_NODE, ROW_SECTION, ROW_MEMBER, ROW_EDGE, ROW_LAYER, ROW_PALETTE_KIND, ROW_PALETTE_TPL, ROW_SOURCE_LINE, ROW_HELP_LINE, ROW_INSP_FIELD, ROW_SETTING, ROW_PROFILER, ROW_VIEW_TYPE, ROW_STATUS, ROW_HEADER, } RowKind; typedef struct Row Row; struct Row { u32 a; u32 b; u8 kind; u8 depth; u8 expandable; u8 expanded; }; #define UI_MAX_ROWS 4096 typedef enum InspField { INSP_TITLE, INSP_ROUTING, INSP_FOCUS, // node fields INSP_N_NAME, INSP_N_KIND, INSP_N_STEREO, INSP_N_BORDER, INSP_N_COLOR, INSP_N_TAG, INSP_N_NOTE, INSP_N_POS, INSP_N_COLLAPSED, INSP_N_PINNED, INSP_N_ADD_SECTION, // edge fields INSP_E_SRC, INSP_E_DST, INSP_E_KIND, INSP_E_ROUTING, INSP_E_SRC_PORT, INSP_E_DST_PORT, INSP_E_LABEL, INSP_E_SRC_MULT, INSP_E_DST_MULT, INSP_E_TAG, INSP_E_COLOR, } InspField; typedef enum StatusItem { STATUS_MODE, STATUS_MESSAGE, STATUS_SELECTION, STATUS_STATS, STATUS_PERF, STATUS_KEYS, STATUS_COUNT, } StatusItem; typedef struct Column Column; struct Column { const char *title; f32 width; b32 mono; }; internal u32 view_columns(ViewKind kind, Column *out) { switch(kind) { case VIEW_OUTLINE: out[0] = {"Structure", 0.56f, false}; out[1] = {"Kind", 0.22f, true}; out[2] = {"Tag", 0.22f, true}; return 3; case VIEW_INSPECTOR: out[0] = {"Field", 0.38f, false}; out[1] = {"Value", 0.44f, true}; out[2] = {"Info", 0.18f, true}; return 3; case VIEW_EDGES: out[0] = {"Source", 0.26f, false}; out[1] = {"Rel", 0.12f, true}; out[2] = {"Target", 0.26f, false}; out[3] = {"Label", 0.22f, false}; out[4] = {"Route", 0.14f, true}; return 5; case VIEW_LAYERS: out[0] = {"Layer / Tag", 0.44f, true}; out[1] = {"Nodes", 0.18f, true}; out[2] = {"Edges", 0.18f, true}; out[3] = {"State", 0.20f, true}; return 4; case VIEW_PALETTE: out[0] = {"Item", 0.46f, false}; out[1] = {"Action", 0.54f, true}; return 2; case VIEW_SOURCE: out[0] = {"#", 0.11f, true}; out[1] = {"DSL", 0.89f, true}; return 2; case VIEW_HELP: out[0] = {"#", 0.08f, true}; out[1] = {"Help (F1, :help )", 0.92f, true}; return 2; case VIEW_VIEWS: out[0] = {"View", 0.45f, false}; out[1] = {"Group", 0.55f, true}; return 2; case VIEW_SETTINGS: case VIEW_PROFILER: out[0] = {"Field", 0.34f, false}; out[1] = {"Value", 0.30f, true}; out[2] = {"Note", 0.36f, false}; return 3; case VIEW_STATUS: out[0] = {"Item", 0.28f, false}; out[1] = {"Value", 0.72f, true}; return 2; default: return 0; } } internal b32 view_expanded(View *view, u32 id) { return id < VIEW_EXPANDED_WORDS * 64 && (view->expanded[id >> 6] & (1ull << (id & 63))) != 0; } internal void view_set_expanded(View *view, u32 id, b32 on) { if(id >= VIEW_EXPANDED_WORDS * 64) return; if(on) view->expanded[id >> 6] |= (1ull << (id & 63)); else view->expanded[id >> 6] &= ~(1ull << (id & 63)); } internal void outline_push_node(App *app, View *view, s32 idx, u8 depth, Row *out, u32 *count, u32 max) { if(*count >= max) return; Diagram *dia = app->diagram; DiagramNode *n = &dia->nodes[idx]; b32 has_children = (n->section_count > 0); if(!has_children && n->kind == NODE_MODULE) { for(u32 j = 0; j < dia->node_count; ++j) if(dia->nodes[j].alive && dia->nodes[j].parent == idx) { has_children = true; break; } } b32 exp = has_children && (n->kind == NODE_MODULE ? !view_expanded(view, (u32)idx) : view_expanded(view, (u32)idx)); out[(*count)++] = Row{(u32)idx, 0, ROW_NODE, depth, (u8)has_children, (u8)exp}; if(!exp) return; // children inside container if(n->kind == NODE_MODULE) { for(u32 j = 0; j < dia->node_count && *count < max; ++j) { if(dia->nodes[j].alive && dia->nodes[j].parent == idx) { outline_push_node(app, view, (s32)j, (u8)(depth + 1), out, count, max); } } } // sections & members for(s32 s = n->first_section; s >= 0 && *count < max; s = dia->sections[s].next) { out[(*count)++] = Row{(u32)s, (u32)idx, ROW_SECTION, (u8)(depth + 1), 0, 0}; for(s32 m = dia->sections[s].first_member; m >= 0 && *count < max; m = dia->members[m].next) { out[(*count)++] = Row{(u32)m, (u32)idx, ROW_MEMBER, (u8)(depth + 2), 0, 0}; } } } internal u32 view_rows(App *app, View *view, Row *out, u32 max) { Diagram *dia = app->diagram; u32 count = 0; switch(view->kind) { case VIEW_OUTLINE: { for(u32 i = 0; i < dia->node_count && count < max; ++i) { if(dia->nodes[i].alive && dia->nodes[i].parent < 0) { outline_push_node(app, view, (s32)i, 0, out, &count, max); } } } break; case VIEW_INSPECTOR: { if(app->active_node >= 0 && app->active_node < (s32)dia->node_count && dia->nodes[app->active_node].alive) { DiagramNode *n = &dia->nodes[app->active_node]; out[count++] = Row{INSP_N_NAME, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_KIND, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_STEREO, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_BORDER, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_COLOR, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_TAG, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_NOTE, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_POS, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_COLLAPSED, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_N_PINNED, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; for(s32 s = n->first_section; s >= 0 && count < max; s = dia->sections[s].next) { out[count++] = Row{(u32)s, (u32)app->active_node, ROW_SECTION, 0, 1, (u8)!dia->sections[s].collapsed}; if(!dia->sections[s].collapsed) { for(s32 m = dia->sections[s].first_member; m >= 0 && count < max; m = dia->members[m].next) { out[count++] = Row{(u32)m, (u32)s, ROW_MEMBER, 1, 0, 0}; } } } if(count < max) out[count++] = Row{INSP_N_ADD_SECTION, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0}; } else if(app->active_edge >= 0 && app->active_edge < (s32)dia->edge_count && dia->edges[app->active_edge].alive) { out[count++] = Row{INSP_E_SRC, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_DST, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_KIND, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_ROUTING, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_SRC_PORT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_DST_PORT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_LABEL, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_SRC_MULT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_DST_MULT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_TAG, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_E_COLOR, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0}; } else { out[count++] = Row{INSP_TITLE, 0, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_ROUTING, 0, ROW_INSP_FIELD, 0, 0, 0}; out[count++] = Row{INSP_FOCUS, 0, ROW_INSP_FIELD, 0, 0, 0}; } } break; case VIEW_EDGES: { for(u32 i = 0; i < dia->edge_count && count < max; ++i) { if(dia->edges[i].alive) out[count++] = Row{i, 0, ROW_EDGE, 0, 0, 0}; } } break; case VIEW_LAYERS: { for(u32 i = 0; i < dia->tag_count && count < max; ++i) { out[count++] = Row{i, 0, ROW_LAYER, 0, 0, 0}; } } break; case VIEW_PALETTE: { out[count++] = Row{0, 0, ROW_HEADER, 0, 0, 0}; for(u32 i = 0; i < NODE_KIND_COUNT && count < max; ++i) out[count++] = Row{i, 0, ROW_PALETTE_KIND, 0, 0, 0}; out[count++] = Row{1, 0, ROW_HEADER, 0, 0, 0}; for(u32 i = 0; i < diagram_template_count() && count < max; ++i) out[count++] = Row{i, 0, ROW_PALETTE_TPL, 0, 0, 0}; } break; case VIEW_SOURCE: { // count lines in DSL (cached per frame on scratch during draw) ScratchArena sc = ScratchGet(); String8 dsl = diagram_to_dsl(sc.arena, dia); u32 lines = 1; for(u64 i = 0; i < dsl.size; ++i) if(dsl.data[i] == '\n') ++lines; for(u32 l = 0; l < lines && count < max; ++l) out[count++] = Row{l, 0, ROW_SOURCE_LINE, 0, 0, 0}; scratch_end(&sc); } break; case VIEW_HELP: { help_index(); for(u32 l = 0; l < help_line_count && count < max; ++l) out[count++] = Row{l, 0, ROW_HELP_LINE, 0, 0, 0}; } break; case VIEW_VIEWS: { ViewType types[UI_MAX_VIEW_TYPES]; u32 n = view_types(types); for(u32 i = 0; i < n && count < max; ++i) out[count++] = Row{types[i].kind, types[i].group, ROW_VIEW_TYPE, 0, 0, 0}; } break; case VIEW_SETTINGS: { u32 cur_group = UINT32_MAX; for(u32 i = 0; i < settings_desc_count() && count < max; ++i) { if(settings_desc(i)->kind == SETTING_KIND_SECTION) { cur_group = (cur_group == UINT32_MAX) ? 0 : cur_group + 1; continue; } if(cur_group == view->group) out[count++] = Row{i, 0, ROW_SETTING, 0, 0, 0}; } } break; case VIEW_PROFILER: { u32 cur_group = UINT32_MAX; Profiler *p = app->profiler; for(u32 m = 0; m < PM_COUNT && count < max; ++m) { if(profile_is_section(m)) { cur_group = (cur_group == UINT32_MAX) ? 0 : cur_group + 1; continue; } if(cur_group != view->group) continue; if(m == PM_ARENA && p) { for(u32 a = 0; a < p->arena_count && count < max; ++a) out[count++] = Row{m, a, ROW_PROFILER, 0, 0, 0}; } else if(m == PM_THREAD && p) { for(u32 t = 0; t < p->thread_count && count < max; ++t) out[count++] = Row{m, t, ROW_PROFILER, 0, 0, 0}; } else { out[count++] = Row{m, 0, ROW_PROFILER, 0, 0, 0}; } } } break; case VIEW_STATUS: { for(u32 i = 0; i < STATUS_COUNT && count < max; ++i) out[count++] = Row{i, 0, ROW_STATUS, 0, 0, 0}; } break; } return count; } typedef struct Cells Cells; struct Cells { String8 text[UI_MAX_COLUMNS]; b32 bright; b32 dim; b32 right; b32 has_swatch; u32 swatch; }; internal void view_cells(App *app, View *view, Row *row, Cells *out, Arena *arena, String8 *dsl_cache = NULL) { MemSetZero(out, sizeof(*out)); Diagram *dia = app->diagram; switch(row->kind) { case ROW_HEADER: { out->text[0] = str8(row->a == 0 ? "NODE ARCHETYPES" : "TEMPLATES"); out->dim = true; } break; case ROW_NODE: { DiagramNode *n = &dia->nodes[row->a]; out->text[0] = str8(n->name); out->text[1] = str8((char *)node_kind_name(n->kind)); out->text[2] = str8(n->tag); out->bright = ((s32)row->a == app->active_node || n->selected); out->dim = diagram_node_hidden(dia, (s32)row->a); } break; case ROW_SECTION: { NodeSection *s = &dia->sections[row->a]; out->text[0] = ui_format(arena, "[%s]", s->title); out->text[1] = ui_format(arena, "%u items", s->member_count); out->text[2] = str8(view->kind == VIEW_INSPECTOR ? "a:add dd:del" : ""); out->dim = true; } break; case ROW_MEMBER: { NodeMember *m = &dia->members[row->a]; out->text[0] = m->visibility ? ui_format(arena, "%c %s", m->visibility, m->name) : str8(m->name); out->text[1] = str8(m->type); out->text[2] = str8(m->badge); } break; case ROW_EDGE: { DiagramEdge *e = &dia->edges[row->a]; out->text[0] = str8(dia->nodes[e->src].name); out->text[1] = str8((char *)edge_kind_symbol(e->kind)); out->text[2] = str8(dia->nodes[e->dst].name); out->text[3] = str8(e->label); out->text[4] = str8((char *)edge_routing_name(e->routing == ROUTE_AUTO ? dia->default_routing : e->routing)); out->bright = ((s32)row->a == app->active_edge || e->selected); } break; case ROW_LAYER: { TagState *t = &dia->tags[row->a]; out->text[0] = str8(t->name); out->text[1] = ui_format(arena, "%u", t->node_count); out->text[2] = ui_format(arena, "%u", t->edge_count); out->text[3] = str8(t->hidden ? "hidden" : (t->dimmed ? "dimmed" : "visible")); out->dim = t->hidden || t->dimmed; out->bright = !t->hidden && !t->dimmed; } break; case ROW_PALETTE_KIND: { out->text[0] = str8((char *)node_kind_name(row->a)); out->text[1] = ui_format(arena, "enter: insert [%s]", node_kind_badge(row->a)); } break; case ROW_PALETTE_TPL: { const DiagramTemplate *t = diagram_template_info(row->a); out->text[0] = str8((char *)t->name); out->text[1] = str8((char *)t->description); } break; case ROW_HELP_LINE: { out->text[0] = ui_format(arena, "%4u", row->a + 1); out->text[1] = help_line(row->a); // NOTE: headings (all caps lines and their ---- underlines) stand out out->bright = help_line_is_heading(row->a); } break; case ROW_SOURCE_LINE: { out->text[0] = ui_format(arena, "%4u", row->a + 1); if(dsl_cache && dsl_cache->size > 0) { u32 cur = 0; u64 start = 0; for(u64 i = 0; i <= dsl_cache->size; ++i) { if(i == dsl_cache->size || dsl_cache->data[i] == '\n') { if(cur == row->a) { out->text[1] = ToString8Size(dsl_cache->data + start, i - start); break; } ++cur; start = i + 1; } } } } break; case ROW_INSP_FIELD: { switch(row->a) { case INSP_TITLE: out->text[0] = str8("diagram title"); out->text[1] = str8(dia->title); out->text[2] = str8("enter: edit"); break; case INSP_ROUTING: out->text[0] = str8("default routing"); out->text[1] = str8((char *)edge_routing_name(dia->default_routing)); out->text[2] = str8("h/l"); break; case INSP_FOCUS: out->text[0] = str8("focus mode"); out->text[1] = str8(dia->focus_mode ? "on" : "off"); out->text[2] = str8("F toggles"); break; case INSP_N_NAME: out->text[0] = str8("name"); out->text[1] = str8(dia->nodes[row->b].name); out->text[2] = str8("enter"); break; case INSP_N_KIND: out->text[0] = str8("kind"); out->text[1] = str8((char *)node_kind_name(dia->nodes[row->b].kind)); out->text[2] = str8("h/l"); break; case INSP_N_STEREO: out->text[0] = str8("stereotype"); out->text[1] = str8(dia->nodes[row->b].stereotype[0] ? dia->nodes[row->b].stereotype : "-"); out->text[2] = str8("enter"); break; case INSP_N_BORDER: out->text[0] = str8("border"); out->text[1] = str8((char *)border_style_name(dia->nodes[row->b].border_style)); out->text[2] = str8("h/l"); break; case INSP_N_COLOR: { u32 c = dia->nodes[row->b].color; out->text[0] = str8("accent color"); out->text[1] = c ? ui_format(arena, "#%08X", c) : str8("theme default"); out->text[2] = str8("enter / h l"); if(c) { out->has_swatch = true; out->swatch = c; } } break; case INSP_N_TAG: out->text[0] = str8("tag / layer"); out->text[1] = str8(dia->nodes[row->b].tag[0] ? dia->nodes[row->b].tag : "-"); out->text[2] = str8("enter"); break; case INSP_N_NOTE: out->text[0] = str8("note"); out->text[1] = str8(dia->nodes[row->b].note[0] ? dia->nodes[row->b].note : "-"); out->text[2] = str8("enter"); break; case INSP_N_POS: out->text[0] = str8("geometry"); out->text[1] = ui_format(arena, "%.0f, %.0f (%.0fx%.0f)", dia->nodes[row->b].x, dia->nodes[row->b].y, dia->nodes[row->b].w, dia->nodes[row->b].h); break; case INSP_N_COLLAPSED: out->text[0] = str8("collapsed"); out->text[1] = str8(dia->nodes[row->b].collapsed ? "yes" : "no"); out->text[2] = str8("h/l"); break; case INSP_N_PINNED: out->text[0] = str8("pinned"); out->text[1] = str8(dia->nodes[row->b].pinned ? "yes" : "no"); out->text[2] = str8("h/l"); break; case INSP_N_ADD_SECTION: out->text[0] = str8("+ add section"); out->text[1] = str8(""); out->text[2] = str8("enter / A"); out->bright = true; break; case INSP_E_SRC: out->text[0] = str8("source"); out->text[1] = str8(dia->nodes[dia->edges[row->b].src].name); break; case INSP_E_DST: out->text[0] = str8("target"); out->text[1] = str8(dia->nodes[dia->edges[row->b].dst].name); break; case INSP_E_KIND: out->text[0] = str8("relationship"); out->text[1] = ui_format(arena, "%s (%s)", edge_kind_name(dia->edges[row->b].kind), edge_kind_symbol(dia->edges[row->b].kind)); out->text[2] = str8("h/l"); break; case INSP_E_ROUTING: out->text[0] = str8("routing"); out->text[1] = str8((char *)edge_routing_name(dia->edges[row->b].routing)); out->text[2] = str8("h/l"); break; case INSP_E_SRC_PORT: out->text[0] = str8("source port"); out->text[1] = str8((char *)port_side_name(dia->edges[row->b].src_port)); out->text[2] = str8("h/l"); break; case INSP_E_DST_PORT: out->text[0] = str8("target port"); out->text[1] = str8((char *)port_side_name(dia->edges[row->b].dst_port)); out->text[2] = str8("h/l"); break; case INSP_E_LABEL: out->text[0] = str8("label"); out->text[1] = str8(dia->edges[row->b].label[0] ? dia->edges[row->b].label : "-"); out->text[2] = str8("enter"); break; case INSP_E_SRC_MULT: out->text[0] = str8("src mult"); out->text[1] = str8(dia->edges[row->b].src_mult[0] ? dia->edges[row->b].src_mult : "-"); out->text[2] = str8("enter"); break; case INSP_E_DST_MULT: out->text[0] = str8("dst mult"); out->text[1] = str8(dia->edges[row->b].dst_mult[0] ? dia->edges[row->b].dst_mult : "-"); out->text[2] = str8("enter"); break; case INSP_E_TAG: out->text[0] = str8("tag / layer"); out->text[1] = str8(dia->edges[row->b].tag[0] ? dia->edges[row->b].tag : "-"); out->text[2] = str8("enter"); break; case INSP_E_COLOR: { u32 c = dia->edges[row->b].color; out->text[0] = str8("color"); out->text[1] = c ? ui_format(arena, "#%08X", c) : str8("theme default"); out->text[2] = str8("enter"); if(c) { out->has_swatch = true; out->swatch = c; } } break; } } break; case ROW_SETTING: { SettingDesc *desc = settings_desc(row->a); out->text[0] = str8((char *)desc->name); out->text[2] = desc->note ? str8((char *)desc->note) : str8(""); if(desc->kind == SETTING_KIND_INFO) { if(desc->action == SETTING_INFO_FRAME) out->text[1] = ui_format(arena, "%.2f ms (%.0f fps, %u quads)", app->frame_ms, app->fps, app->draw.count); if(desc->action == SETTING_INFO_DIAGRAM) out->text[1] = ui_format(arena, "%u nodes, %u edges, %u members", dia->live_nodes, dia->live_edges, dia->member_count); if(desc->action == SETTING_INFO_DATA_DIR) out->text[1] = app->data_dir; if(desc->action == SETTING_INFO_RELOADS) out->text[1] = ui_format(arena, "%u", app->ui_generation); } else if(desc->kind == SETTING_KIND_ACTION) { out->text[1] = str8("run"); out->bright = true; } else { out->text[1] = settings_format(arena, &app->settings, desc); if(desc->kind == SETTING_KIND_COLOR) { out->has_swatch = true; out->swatch = *SettingField(&app->settings, desc, u32); } } } break; case ROW_PROFILER: { Profiler *p = app->profiler; out->text[0] = str8((char *)profile_labels[row->a]); if(!p) break; char a[32], b[32], c[32]; switch(row->a) { case PM_FPS: out->text[1] = ui_format(arena, "%.1f fps", app->fps); out->text[2] = ui_format(arena, "%u frames", p->frames_total); break; case PM_FRAME_CPU: out->text[1] = ui_format(arena, "%.2f ms", p->average); out->text[2] = ui_format(arena, "p50 %.2f p99 %.2f max %.2f", p->p50, p->p99, p->worst); break; case PM_FRAME_SPLIT: out->text[1] = ui_format(arena, "ui %.2f / vk %.2f ms", p->ui_ms, p->render_ms); out->text[2] = ui_format(arena, "p99 %.2f / %.2f", p->ui_p99, p->render_p99); break; case PM_UI_SPLIT: out->text[1] = ui_format(arena, "%.0f / %.0f / %.0f us", p->ui_phase_us[0], p->ui_phase_us[1], p->ui_phase_us[3]); out->text[2] = str8("keys / panels+canvas / overlay"); break; case PM_RENDER_SPLIT: out->text[1] = ui_format(arena, "%.2f / %.2f / %.2f ms", app->renderer.wait_ms, app->renderer.record_ms, app->renderer.submit_ms); out->text[2] = str8("fence / record / submit"); break; case PM_IDLE: out->text[1] = ui_format(arena, "%.1f ms", p->idle_ms); out->text[2] = str8("asleep in poll()"); break; case PM_DRAW: out->text[1] = ui_format(arena, "%u quads %u batches", app->draw.count, app->renderer.batches); out->text[2] = ui_format(arena, "peak %u / %u", app->draw.peak, (u32)DRAW_MAX_INSTANCES); break; case PM_RSS: format_bytes(a, sizeof(a), (f64)p->rss); format_bytes(b, sizeof(b), (f64)p->rss_peak); out->text[1] = str8(a); out->text[2] = ui_format(arena, "peak %s", b); break; case PM_RSS_KIND: format_bytes(a, sizeof(a), (f64)p->rss_anon); format_bytes(b, sizeof(b), (f64)p->rss_file); format_bytes(c, sizeof(c), (f64)p->rss_shmem); out->text[1] = ui_format(arena, "anon %s", a); out->text[2] = ui_format(arena, "file %s shm %s", b, c); break; case PM_VIRTUAL: format_bytes(a, sizeof(a), (f64)p->vm_size); format_bytes(b, sizeof(b), (f64)p->vm_lib); out->text[1] = str8(a); out->text[2] = ui_format(arena, "libs %s", b); break; case PM_STACK_MAIN: format_bytes(a, sizeof(a), (f64)p->vm_stack); out->text[1] = str8(a); break; case PM_HEAP_LIVE: format_bytes(a, sizeof(a), (f64)Max(AtomicLoadRelaxed(&p->heap_live), (s64)0)); format_bytes(b, sizeof(b), (f64)AtomicLoadRelaxed(&p->heap_peak)); out->text[1] = str8(a); out->text[2] = ui_format(arena, "peak %s", b); break; case PM_HEAP_ALLOCS: out->text[1] = ui_format(arena, "%llu", (unsigned long long)AtomicLoadRelaxed(&p->malloc_count)); out->text[2] = ui_format(arena, "%.0f /s", p->mallocs_per_s); break; case PM_HEAP_FREES: out->text[1] = ui_format(arena, "%llu", (unsigned long long)AtomicLoadRelaxed(&p->free_count)); out->text[2] = ui_format(arena, "%.0f /s", p->frees_per_s); break; case PM_HEAP_BLOCKS: out->text[1] = ui_format(arena, "%lld", (long long)((s64)AtomicLoadRelaxed(&p->malloc_count) - (s64)AtomicLoadRelaxed(&p->free_count))); break; case PM_HEAP_TOTAL: format_bytes(a, sizeof(a), (f64)AtomicLoadRelaxed(&p->heap_total)); out->text[1] = str8(a); break; case PM_HEAP_GLIBC: format_bytes(a, sizeof(a), (f64)p->heap_in_use); format_bytes(b, sizeof(b), (f64)p->heap_free); out->text[1] = ui_format(arena, "used %s", a); out->text[2] = ui_format(arena, "free %s", b); break; case PM_ARENA: { ProfileArena *ar = &p->arenas[row->b]; out->text[0] = str8(ar->name); format_bytes(a, sizeof(a), (f64)arena_position(ar->arena)); format_bytes(b, sizeof(b), (f64)ar->arena->capacity); format_bytes(c, sizeof(c), (f64)ar->arena->peak_position); out->text[1] = ui_format(arena, "%s / %s", a, b); out->text[2] = ui_format(arena, "peak %s %.0f/s", c, ar->allocs_per_s); } break; case PM_THREAD: { ProfileThread *th = &p->threads[row->b]; out->text[0] = str8(th->name); out->text[1] = ui_format(arena, "%.1f%% cpu", th->cpu * 100.0f); format_bytes(a, sizeof(a), (f64)th->stack_touched); out->text[2] = ui_format(arena, "stack %s %.0f csw/s", a, th->csw_per_s); } break; case PM_CPU: out->text[1] = ui_format(arena, "%.1f%%", p->cpu_process * 100.0f); break; case PM_FAULTS: out->text[1] = ui_format(arena, "%llu min / %llu maj", (unsigned long long)p->minflt, (unsigned long long)p->majflt); out->text[2] = ui_format(arena, "%.0f /s", p->minflt_per_s); break; case PM_SWITCHES: out->text[1] = ui_format(arena, "%llu vol / %llu inv", (unsigned long long)p->vcsw, (unsigned long long)p->ivcsw); out->text[2] = ui_format(arena, "%.0f /s", p->csw_per_s); break; case PM_GPU_MEMORY: format_bytes(a, sizeof(a), (f64)app->renderer.gpu_bytes); out->text[1] = str8(a); break; case PM_GPU_SWAPCHAIN: out->text[1] = ui_format(arena, "%ux%u (%u imgs)", app->renderer.extent.width, app->renderer.extent.height, app->renderer.image_count); out->text[2] = str8(app->renderer.present_mode == VK_PRESENT_MODE_MAILBOX_KHR ? "mailbox" : "fifo"); break; case PM_FONT_GLYPHS: out->text[1] = ui_format(arena, "%u / %u", app->fonts.glyph_count, (u32)FONT_GLYPH_SLOTS); out->text[2] = ui_format(arena, "%u faces", app->fonts.face_count); break; case PM_FONT_RUNS: out->text[1] = ui_format(arena, "%u / %u", app->fonts.run_count, (u32)FONT_RUN_SLOTS); break; case PM_FONT_ATLAS: out->text[1] = ui_format(arena, "%u%% full", (app->fonts.shelf_y * 100) / FONT_ATLAS_SIZE); out->text[2] = ui_format(arena, "%ux%u R8", (u32)FONT_ATLAS_SIZE, (u32)FONT_ATLAS_SIZE); break; case PM_DIA_NODES: out->text[1] = ui_format(arena, "%u live (%u visible)", dia->live_nodes, dia->visible_nodes); out->text[2] = ui_format(arena, "cap %u", dia->node_cap); break; case PM_DIA_EDGES: out->text[1] = ui_format(arena, "%u live (%u visible)", dia->live_edges, dia->visible_edges); out->text[2] = ui_format(arena, "cap %u", dia->edge_cap); break; case PM_DIA_MEMBERS: out->text[1] = ui_format(arena, "%u sec / %u mem", dia->section_count, dia->member_count); break; case PM_DIA_LAYOUT: out->text[1] = ui_format(arena, "%.2f ms", dia->layout_ms); break; case PM_DIA_UNDO: out->text[1] = ui_format(arena, "%u undo / %u redo", dia->undo_count, dia->redo_count); break; } } break; case ROW_VIEW_TYPE: { out->text[0] = str8((char *)view_type_name(row->a, row->b)); out->text[1] = str8(row->a == VIEW_SETTINGS ? "settings" : (row->a == VIEW_PROFILER ? "profiler" : "workspace")); } break; case ROW_STATUS: { const char *mode = "NORMAL"; if(app->overlay == UI_OVERLAY_SEARCH) mode = "SEARCH"; else if(app->overlay == UI_OVERLAY_COMMAND) mode = "CMD"; else if(app->overlay == UI_OVERLAY_EDIT) mode = "EDIT"; else if(app->canvas_drag == CANVAS_DRAG_CONNECT) mode = "CONNECT"; else if(dia->focus_mode) mode = "FOCUS"; b32 show_msg = app->message[0] && (time_now_ns() - app->message_ns < (u64)(ui_theme.message_seconds * 1e9)); switch(row->a) { case STATUS_MODE: out->text[0] = str8("mode"); out->text[1] = str8((char *)mode); out->bright = true; break; case STATUS_MESSAGE: out->text[0] = str8("message"); out->text[1] = show_msg ? str8(app->message) : str8("a:add e:edge-kind L:layout f:fit F:focus /:search :cmd"); out->dim = !show_msg; break; case STATUS_SELECTION: out->text[0] = str8("selection"); if(app->active_node >= 0 && dia->nodes[app->active_node].alive) { u32 sc = diagram_selected_node_count(dia); out->text[1] = sc > 1 ? ui_format(arena, "%s (+%u)", dia->nodes[app->active_node].name, sc - 1) : ui_format(arena, "%s [%s]", dia->nodes[app->active_node].name, node_kind_name(dia->nodes[app->active_node].kind)); } else if(app->active_edge >= 0 && dia->edges[app->active_edge].alive) { DiagramEdge *e = &dia->edges[app->active_edge]; out->text[1] = ui_format(arena, "%s %s %s", dia->nodes[e->src].name, edge_kind_symbol(e->kind), dia->nodes[e->dst].name); } out->right = true; break; case STATUS_STATS: out->text[0] = str8("diagram"); out->text[1] = ui_format(arena, "edge:%s %un %ue", edge_kind_symbol(app->canvas_connect_kind), dia->live_nodes, dia->live_edges); out->right = true; out->dim = true; break; case STATUS_PERF: out->text[0] = str8("perf"); out->text[1] = ui_format(arena, "%.2fms %uq", app->frame_ms, app->draw.count); out->right = true; out->dim = true; break; case STATUS_KEYS: out->text[0] = str8("keys"); if(app->count || app->pending) { if(app->count && app->pending) out->text[1] = ui_format(arena, "%u%c", app->count, (char)app->pending); else if(app->count) out->text[1] = ui_format(arena, "%u", app->count); else out->text[1] = ui_format(arena, "%c", (char)app->pending); } out->right = true; break; } } break; } } //- table actions (activate, adjust, inline edit) internal void start_edit(App *app, EditTargetKind kind, s32 index, const char *initial) { app->edit_kind = kind; app->edit_index = index; app->edit_inline = app->card_focused; app->overlay = UI_OVERLAY_EDIT; app->overlay_opened = true; snprintf(app->input, sizeof(app->input), "%s", initial ? initial : ""); app->input_size = (u32)strlen(app->input); } internal void edit_accept(App *app) { Diagram *dia = app->diagram; char text[161]; MemCpy(text, app->input, app->input_size); text[app->input_size] = 0; switch(app->edit_kind) { case EDIT_SETTING: if(!settings_parse(&app->settings, settings_desc((u32)app->edit_index), text)) app_message(app, "invalid value: %s", text); app_apply_settings(app); break; case EDIT_DIAGRAM_TITLE: snprintf(dia->title, sizeof(dia->title), "%s", text); dia->dirty = true; break; case EDIT_NODE_NAME: if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->nodes[app->edit_index].name, sizeof(dia->nodes[app->edit_index].name), "%s", text); diagram_measure_node(dia, app->edit_index); diagram_fit_containers(dia); ++dia->version; } break; case EDIT_NODE_STEREO: if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->nodes[app->edit_index].stereotype, sizeof(dia->nodes[app->edit_index].stereotype), "%s", text); diagram_measure_node(dia, app->edit_index); ++dia->version; } break; case EDIT_NODE_TAG: if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->nodes[app->edit_index].tag, sizeof(dia->nodes[app->edit_index].tag), "%s", text); diagram_rebuild_tags(dia); ++dia->version; } break; case EDIT_NODE_NOTE: if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->nodes[app->edit_index].note, sizeof(dia->nodes[app->edit_index].note), "%s", text); diagram_measure_node(dia, app->edit_index); ++dia->version; } break; case EDIT_NODE_COLOR: case EDIT_EDGE_COLOR: { u32 val = 0; if(text[0] && strcmp(text, "default") != 0 && strcmp(text, "0") != 0) { const char *at = (text[0] == '#') ? text + 1 : text; val = (u32)strtoul(at, NULL, 16); if(strlen(at) == 6) val = (val << 8) | 0xFF; } diagram_push_undo(dia); if(app->edit_kind == EDIT_NODE_COLOR && app->edit_index >= 0) dia->nodes[app->edit_index].color = val; if(app->edit_kind == EDIT_EDGE_COLOR && app->edit_index >= 0) dia->edges[app->edit_index].color = val; ++dia->version; } break; case EDIT_EDGE_LABEL: if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->edges[app->edit_index].label, sizeof(dia->edges[app->edit_index].label), "%s", text); ++dia->version; } break; case EDIT_EDGE_SRC_MULT: if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->edges[app->edit_index].src_mult, sizeof(dia->edges[app->edit_index].src_mult), "%s", text); ++dia->version; } break; case EDIT_EDGE_DST_MULT: if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->edges[app->edit_index].dst_mult, sizeof(dia->edges[app->edit_index].dst_mult), "%s", text); ++dia->version; } break; case EDIT_EDGE_TAG: if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->edges[app->edit_index].tag, sizeof(dia->edges[app->edit_index].tag), "%s", text); diagram_rebuild_tags(dia); ++dia->version; } break; case EDIT_SECTION_TITLE: if(app->edit_index >= 0 && dia->sections[app->edit_index].alive) { diagram_push_undo(dia); snprintf(dia->sections[app->edit_index].title, sizeof(dia->sections[app->edit_index].title), "%s", text); diagram_measure_node(dia, dia->sections[app->edit_index].node); ++dia->version; } break; case EDIT_MEMBER_LINE: if(app->edit_index >= 0 && dia->members[app->edit_index].alive) { diagram_push_undo(dia); NodeMember *m = &dia->members[app->edit_index]; const char *p = dsl_skip_ws(text); m->visibility = 0; if((*p == '+' || *p == '-' || *p == '#' || *p == '~' || *p == '@' || *p == '*') && (p[1] == ' ' || p[1] == '\t')) { m->visibility = *p; p = dsl_skip_ws(p + 1); } char work[161]; snprintf(work, sizeof(work), "%s", p); m->badge[0] = 0; m->type[0] = 0; char *lb = strrchr(work, '['), *rb = lb ? strchr(lb, ']') : NULL; if(lb && rb) { *lb = 0; *rb = 0; snprintf(m->badge, sizeof(m->badge), "%s", lb + 1); } char *colon = strchr(work, ':'); if(colon) { *colon = 0; snprintf(m->type, sizeof(m->type), "%s", dsl_skip_ws(colon + 1)); u32 tl = (u32)strlen(m->type); while(tl > 0 && m->type[tl - 1] == ' ') m->type[--tl] = 0; } snprintf(m->name, sizeof(m->name), "%s", work); u32 nl = (u32)strlen(m->name); while(nl > 0 && m->name[nl - 1] == ' ') m->name[--nl] = 0; diagram_measure_node(dia, dia->sections[m->section].node); diagram_fit_containers(dia); ++dia->version; } break; } } internal void view_adjust(App *app, View *view, Row *row, s32 dir) { Diagram *dia = app->diagram; if(row->kind == ROW_SETTING) { settings_adjust(&app->settings, settings_desc(row->a), dir); app_apply_settings(app); return; } if(row->kind == ROW_INSP_FIELD) { switch(row->a) { case INSP_ROUTING: dia->default_routing = (dia->default_routing - 1 + 3 + (u32)dir) % 3 + 1; app->settings.default_routing = dia->default_routing; break; case INSP_FOCUS: dia->focus_mode = !dia->focus_mode; break; case INSP_N_KIND: { diagram_push_undo(dia); DiagramNode *n = &dia->nodes[row->b]; n->kind = (n->kind + NODE_KIND_COUNT + (u32)dir) % NODE_KIND_COUNT; diagram_measure_node(dia, (s32)row->b); } break; case INSP_N_BORDER: { diagram_push_undo(dia); DiagramNode *n = &dia->nodes[row->b]; n->border_style = (n->border_style + BORDER_STYLE_COUNT + (u32)dir) % BORDER_STYLE_COUNT; } break; case INSP_N_COLLAPSED: diagram_push_undo(dia); dia->nodes[row->b].collapsed = !dia->nodes[row->b].collapsed; diagram_measure_node(dia, (s32)row->b); break; case INSP_N_PINNED: dia->nodes[row->b].pinned = !dia->nodes[row->b].pinned; break; case INSP_E_KIND: diagram_push_undo(dia); dia->edges[row->b].kind = (dia->edges[row->b].kind + EDGE_KIND_COUNT + (u32)dir) % EDGE_KIND_COUNT; break; case INSP_E_ROUTING: diagram_push_undo(dia); dia->edges[row->b].routing = (dia->edges[row->b].routing + ROUTE_COUNT + (u32)dir) % ROUTE_COUNT; break; case INSP_E_SRC_PORT: diagram_push_undo(dia); dia->edges[row->b].src_port = (dia->edges[row->b].src_port + PORT_COUNT + (u32)dir) % PORT_COUNT; break; case INSP_E_DST_PORT: diagram_push_undo(dia); dia->edges[row->b].dst_port = (dia->edges[row->b].dst_port + PORT_COUNT + (u32)dir) % PORT_COUNT; break; } ++dia->version; return; } if(row->kind == ROW_EDGE) { diagram_push_undo(dia); dia->edges[row->a].kind = (dia->edges[row->a].kind + EDGE_KIND_COUNT + (u32)dir) % EDGE_KIND_COUNT; ++dia->version; } } internal void view_activate(App *app, View *view, Row *row) { Diagram *dia = app->diagram; switch(row->kind) { case ROW_NODE: { diagram_clear_selection(dia); dia->nodes[row->a].selected = true; app->active_node = (s32)row->a; app->active_edge = -1; app->pan_to_active = true; } break; case ROW_EDGE: { diagram_clear_selection(dia); dia->edges[row->a].selected = true; app->active_edge = (s32)row->a; app->active_node = -1; } break; case ROW_LAYER: { dia->tags[row->a].hidden = !dia->tags[row->a].hidden; } break; case ROW_PALETTE_KIND: { View *cv = find_canvas_view(app); f32 wx = cv ? cv->cam_x : 200.0f; f32 wy = cv ? cv->cam_y : 200.0f; diagram_push_undo(dia); s32 idx = diagram_add_node(dia, (NodeKind)row->a, node_kind_name(row->a), wx - 110.0f, wy - 40.0f); if(idx >= 0) { if(row->a == NODE_STRUCT || row->a == NODE_CLASS) { s32 s = diagram_add_section(dia, idx, "fields"); diagram_add_member(dia, s, '+', "id", "u64", ""); } else if(row->a == NODE_INTERFACE) { s32 s = diagram_add_section(dia, idx, "methods"); diagram_add_member(dia, s, '+', "execute()", "void", "pure"); } else if(row->a == NODE_ENUM) { s32 s = diagram_add_section(dia, idx, "variants"); diagram_add_member(dia, s, 0, "None", "", ""); } diagram_clear_selection(dia); dia->nodes[idx].selected = true; app->active_node = idx; app->active_edge = -1; app_message(app, "added %s", node_kind_name(row->a)); } } break; case ROW_PALETTE_TPL: { const DiagramTemplate *t = diagram_template_info(row->a); diagram_push_undo(dia); diagram_load_template(dia, t->name); app->active_node = -1; app->active_edge = -1; for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true; app_message(app, "loaded template: %s (%u nodes, %u edges)", t->name, dia->live_nodes, dia->live_edges); } break; case ROW_VIEW_TYPE: layout_open(&app->layout, (ViewKind)row->a, row->b); break; case ROW_SECTION: if(view->kind == VIEW_INSPECTOR) { start_edit(app, EDIT_SECTION_TITLE, (s32)row->a, dia->sections[row->a].title); } break; case ROW_MEMBER: { NodeMember *m = &dia->members[row->a]; char buf[160]; snprintf(buf, sizeof(buf), "%s%s%s%s%s%s%s", m->visibility ? (char[3]){m->visibility, ' ', 0} : "", m->name, m->type[0] ? " : " : "", m->type, m->badge[0] ? " [" : "", m->badge, m->badge[0] ? "]" : ""); start_edit(app, EDIT_MEMBER_LINE, (s32)row->a, buf); } break; case ROW_INSP_FIELD: { switch(row->a) { case INSP_TITLE: start_edit(app, EDIT_DIAGRAM_TITLE, 0, dia->title); break; case INSP_N_NAME: start_edit(app, EDIT_NODE_NAME, (s32)row->b, dia->nodes[row->b].name); break; case INSP_N_STEREO: start_edit(app, EDIT_NODE_STEREO, (s32)row->b, dia->nodes[row->b].stereotype); break; case INSP_N_TAG: start_edit(app, EDIT_NODE_TAG, (s32)row->b, dia->nodes[row->b].tag); break; case INSP_N_NOTE: start_edit(app, EDIT_NODE_NOTE, (s32)row->b, dia->nodes[row->b].note); break; case INSP_N_COLOR: { char hex[16] = {}; if(dia->nodes[row->b].color) snprintf(hex, sizeof(hex), "#%08X", dia->nodes[row->b].color); start_edit(app, EDIT_NODE_COLOR, (s32)row->b, hex); } break; case INSP_N_ADD_SECTION: { diagram_push_undo(dia); s32 s = diagram_add_section(dia, (s32)row->b, "fields"); if(s >= 0) start_edit(app, EDIT_SECTION_TITLE, s, "fields"); } break; case INSP_E_LABEL: start_edit(app, EDIT_EDGE_LABEL, (s32)row->b, dia->edges[row->b].label); break; case INSP_E_SRC_MULT: start_edit(app, EDIT_EDGE_SRC_MULT, (s32)row->b, dia->edges[row->b].src_mult); break; case INSP_E_DST_MULT: start_edit(app, EDIT_EDGE_DST_MULT, (s32)row->b, dia->edges[row->b].dst_mult); break; case INSP_E_TAG: start_edit(app, EDIT_EDGE_TAG, (s32)row->b, dia->edges[row->b].tag); break; case INSP_E_COLOR: { char hex[16] = {}; if(dia->edges[row->b].color) snprintf(hex, sizeof(hex), "#%08X", dia->edges[row->b].color); start_edit(app, EDIT_EDGE_COLOR, (s32)row->b, hex); } break; default: view_adjust(app, view, row, 1); break; } } break; case ROW_SETTING: { SettingDesc *desc = settings_desc(row->a); if(desc->kind == SETTING_KIND_ACTION) { if(desc->action == SETTING_ACTION_RESET_LAYOUT) ui_layout_default(&app->layout); if(desc->action == SETTING_ACTION_RESET_SETTINGS) { settings_default(&app->settings); app_apply_settings(app); } if(desc->action == SETTING_ACTION_RESET_COLORS) settings_apply_scheme(&app->settings, app->settings.scheme < UI_SCHEME_CUSTOM ? app->settings.scheme : UI_SCHEME_MONO_DARK); } else if(desc->kind == SETTING_KIND_BOOL || desc->kind == SETTING_KIND_ENUM || desc->kind == SETTING_KIND_CHOICE || desc->kind == SETTING_KIND_FONT) { settings_adjust(&app->settings, desc, 1); app_apply_settings(app); } else if(desc->kind == SETTING_KIND_F32 || desc->kind == SETTING_KIND_U32 || desc->kind == SETTING_KIND_COLOR) { ScratchArena sc = ScratchGet(); String8 val = settings_format(sc.arena, &app->settings, desc); char buf[64]; snprintf(buf, sizeof(buf), Str8Fmt, Str8Arg(val)); scratch_end(&sc); start_edit(app, EDIT_SETTING, (s32)row->a, buf); } } break; } } //- floating inspector // NOTE: a small card next to the selection on the canvas, so editing a node doesn't send your eyes // (and the keyboard focus) to the other side of the window. it is the Inspector itself: same rows, // cells, activate and adjust, minus the fields that only make sense in the full panel. edits run // inline in the row. zero allocations: rows live on the scratch arena for the frame. #define CARD_WIDTH 296.0f #define CARD_MAX_ROWS 12 #define CARD_GAP 14.0f internal View *focused_view(App *app); internal b32 card_keeps_field(u32 field) { switch(field) { case INSP_N_BORDER: case INSP_N_POS: case INSP_N_COLLAPSED: case INSP_N_PINNED: case INSP_E_SRC: case INSP_E_DST: case INSP_E_SRC_PORT: case INSP_E_DST_PORT: return false; } return true; } internal b32 card_field_is_choice(u32 field) { return field == INSP_N_KIND || field == INSP_E_KIND || field == INSP_E_ROUTING; } internal u32 card_rows(App *app, Row *out, u32 max) { u32 count = view_rows(app, &app->card_view, out, max); u32 kept = 0; for(u32 index = 0; index < count; ++index) { if(out[index].kind == ROW_INSP_FIELD && !card_keeps_field(out[index].a)) continue; out[kept++] = out[index]; } return kept; } internal u32 card_target_of(App *app) { Diagram *dia = app->diagram; if(app->active_node >= 0 && app->active_node < (s32)dia->node_count && dia->nodes[app->active_node].alive) return (u32)app->active_node + 1; if(app->active_edge >= 0 && app->active_edge < (s32)dia->edge_count && dia->edges[app->active_edge].alive) return ((u32)app->active_edge + 1) | CARD_EDGE_BIT; return 0; } // NOTE: which row the running inline edit belongs to internal b32 card_row_is_edit(App *app, Row *row) { s32 index = app->edit_index; u32 field = 0; switch(app->edit_kind) { case EDIT_MEMBER_LINE: return row->kind == ROW_MEMBER && (s32)row->a == index; case EDIT_SECTION_TITLE: return row->kind == ROW_SECTION && (s32)row->a == index; case EDIT_NODE_NAME: field = INSP_N_NAME; break; case EDIT_NODE_STEREO: field = INSP_N_STEREO; break; case EDIT_NODE_TAG: field = INSP_N_TAG; break; case EDIT_NODE_NOTE: field = INSP_N_NOTE; break; case EDIT_NODE_COLOR: field = INSP_N_COLOR; break; case EDIT_EDGE_LABEL: field = INSP_E_LABEL; break; case EDIT_EDGE_SRC_MULT: field = INSP_E_SRC_MULT; break; case EDIT_EDGE_DST_MULT: field = INSP_E_DST_MULT; break; case EDIT_EDGE_TAG: field = INSP_E_TAG; break; case EDIT_EDGE_COLOR: field = INSP_E_COLOR; break; default: return false; } return row->kind == ROW_INSP_FIELD && row->a == field && (s32)row->b == index; } // NOTE: where the card goes on this canvas, or false when it doesn't show. draw_canvas calls it twice: // before its mouse handling, so clicks on the card never reach the diagram underneath, and again after // it, so the card follows this frame's selection, drag and snap. the app only redraws on input, a // stale decision would stick until the next mouse move. pure apart from the target reset. internal b32 card_place(App *app, PlatformInput *input, s32 view_index, View *view, Rect rect) { Diagram *dia = app->diagram; // one card, on the focused canvas, else the first one View *home = focused_view(app); if(!home || home->kind != VIEW_CANVAS) home = find_canvas_view(app); if(home != view) return false; u32 target = card_target_of(app); if(target != app->card_target) { app->card_target = target; app->card_dismissed = 0; app->card_focused = false; app->card_view.kind = VIEW_INSPECTOR; app->card_view.cursor = 0; app->card_view.scroll = ScrollList{}; } // NOTE: pressing a node already starts a node drag; get out of the way only once it really moves. // panning keeps the card, it follows the anchor b32 moved = fabsf(input->mouse_x - app->canvas_press_x) > 3.0f || fabsf(input->mouse_y - app->canvas_press_y) > 3.0f; b32 dragging = app->canvas_drag != CANVAS_DRAG_NONE && app->canvas_drag != CANVAS_DRAG_PAN && app->canvas_drag_view == view_index && moved; b32 show = ui_theme.floating_inspector && target && app->card_dismissed != target && !dragging && rect_w(rect) >= 360.0f && rect_h(rect) >= 200.0f; //- anchor: the node's screen box, or the edge's label point Rect anchor = {}; if(show && !(target & CARD_EDGE_BIT)) { s32 node = (s32)target - 1; DiagramNode *n = &dia->nodes[node]; Vec2f a = canvas_w2s(view, rect, n->x, n->y); Vec2f b = canvas_w2s(view, rect, n->x + n->w, n->y + n->h); anchor = Rect{a.x, a.y, b.x, b.y}; if(diagram_node_hidden(dia, node) || diagram_selected_node_count(dia) > 1) show = false; } else if(show) { s32 edge = (s32)(target & ~CARD_EDGE_BIT) - 1; EdgePath path; if(diagram_edge_hidden(dia, edge) || !diagram_edge_route(dia, edge, &path)) show = false; else { Vec2f p = canvas_w2s(view, rect, path.label_pos.x, path.label_pos.y); anchor = Rect{p.x - 4.0f, p.y - 4.0f, p.x + 4.0f, p.y + 4.0f}; } } if(show && (anchor.x1 < rect.x0 || anchor.x0 > rect.x1 || anchor.y1 < rect.y0 || anchor.y0 > rect.y1)) show = false; if(!show) { app->card_focused = false; return false; } ScratchArena scratch = ScratchGet(); Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS); u32 count = card_rows(app, rows, UI_MAX_ROWS); scratch_end(&scratch); f32 head = UI_ROW_H + 6.0f; f32 foot = UI_FONT_SMALL + 12.0f; f32 w = Min(CARD_WIDTH, rect_w(rect) - 16.0f); f32 h = Min(head + (f32)Min(count, (u32)CARD_MAX_ROWS) * UI_ROW_H + foot, rect_h(rect) - 44.0f); // right of the anchor, else left of it, else on top of it, always inside the canvas f32 x = anchor.x1 + CARD_GAP; if(x + w > rect.x1 - 8.0f) x = anchor.x0 - CARD_GAP - w; if(x < rect.x0 + 8.0f) x = Clamp(anchor.x1 + CARD_GAP, rect.x0 + 8.0f, rect.x1 - 8.0f - w); f32 y = Clamp(anchor.y0, rect.y0 + 36.0f, rect.y1 - 8.0f - h); app->card_rect = Rect{x, y, x + w, y + h}; return true; } internal void card_draw_input(App *app, Rect field) { DrawList *d = &app->draw; FontSystem *fonts = &app->fonts; String8 text = ToString8Size(app->input, app->input_size); f32 tw = font_measure(fonts, FONT_MONO, UI_FONT, text); f32 shift = Max(tw - (rect_w(field) - 14.0f), 0.0f); // keep the caret in view draw_rect(d, field, COLOR_BG, 0); draw_border(d, field, COLOR_ACCENT, 0, 1); draw_clip_push(d, rect_shrink(field, 1)); f32 x = field.x0 + 6.0f - shift; draw_text(d, fonts, FONT_MONO, UI_FONT, x, ui_baseline(app, FONT_MONO, UI_FONT, field.y0, rect_h(field)), text, COLOR_TEXT); draw_rect(d, Rect{x + tw + 1.0f, field.y0 + 4.0f, x + tw + 2.5f, field.y1 - 4.0f}, COLOR_ACCENT, 0); draw_clip_pop(d); app->edit_drawn = true; } internal void card_draw(App *app, PlatformInput *input, s32 panel, Rect canvas, b32 owns_mouse) { DrawList *d = &app->draw; FontSystem *fonts = &app->fonts; Diagram *dia = app->diagram; View *cv = &app->card_view; Rect box = app->card_rect; b32 editing = (app->overlay == UI_OVERLAY_EDIT && app->edit_inline); // NOTE: clicks only count when the card was under the mouse before the canvas handled it, else a // click that just selected a node could land on that node's freshly placed card too b32 live = (app->overlay == UI_OVERLAY_NONE) && owns_mouse; ScratchArena scratch = ScratchGet(); Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS); u32 count = card_rows(app, rows, UI_MAX_ROWS); cv->row_count = count; cv->cursor = count ? Clamp(cv->cursor, 0, (s32)count - 1) : 0; f32 row_h = UI_ROW_H; f32 pad = 10.0f; Rect head = {box.x0, box.y0, box.x1, box.y0 + row_h + 6.0f}; Rect foot = {box.x0, box.y1 - (UI_FONT_SMALL + 12.0f), box.x1, box.y1}; Rect body = {box.x0, head.y1, box.x1, foot.y0}; f32 lx = body.x0 + pad; // label column f32 vx = body.x0 + rect_w(body) * 0.38f; // value column f32 rx = body.x1 - pad; // right edge of the content b32 over = owns_mouse && ui_hovered(input, box); draw_shadow(d, box, 0x00000099u, 0, 18); draw_rect(d, box, COLOR_PANEL, 0); //- header: what this is, and a close button { draw_rect(d, head, COLOR_HEADER, 0); String8 title = {}; if(app->card_target & CARD_EDGE_BIT) { DiagramEdge *e = &dia->edges[(app->card_target & ~CARD_EDGE_BIT) - 1]; title = ui_format(scratch.arena, "%s %s %s", dia->nodes[e->src].name, edge_kind_symbol(e->kind), dia->nodes[e->dst].name); } else { DiagramNode *n = &dia->nodes[app->card_target - 1]; title = (n->parent >= 0) ? ui_format(scratch.arena, "%s in %s", node_kind_badge(n->kind), dia->nodes[n->parent].name) : str8((char *)node_kind_badge(n->kind)); } Rect close = {head.x1 - rect_h(head), head.y0, head.x1, head.y1}; b32 hot = live && ui_hovered(input, close); if(hot) { draw_rect(d, close, COLOR_HOVER, 0); app->canvas_cursor = PLATFORM_CURSOR_POINTER; if(ButtonPressed(input->mouse[0])) { app->card_dismissed = app->card_target; app->card_focused = false; } } f32 base = ui_baseline(app, FONT_MONO, UI_FONT_SMALL, head.y0, rect_h(head)); draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, lx, base, close.x0 - lx - 4.0f, title, COLOR_TEXT_DIM); f32 cx = (close.x0 + close.x1) * 0.5f, cy = (close.y0 + close.y1) * 0.5f; draw_line(d, cx - 4.0f, cy - 4.0f, cx + 4.0f, cy + 4.0f, 1.2f, hot ? COLOR_TEXT : COLOR_TEXT_DIM); draw_line(d, cx - 4.0f, cy + 4.0f, cx + 4.0f, cy - 4.0f, 1.2f, hot ? COLOR_TEXT : COLOR_TEXT_DIM); draw_rect(d, Rect{head.x0, head.y1 - 1, head.x1, head.y1}, COLOR_PANEL_EDGE, 0); } //- rows f32 view_h = Max(rect_h(body), 1.0f); cv->visible_rows = (u32)Max(floorf(view_h / row_h), 1.0f); if(over && input->scroll_y != 0.0f) cv->scroll.target += input->scroll_y * row_h * (f32)ui_theme.scroll_lines; // follow the row being edited (a member just added with 'a' sits at the end of its section) if(editing) { for(u32 index = 0; index < count; ++index) { if(!card_row_is_edit(app, &rows[index])) continue; if((s32)index != cv->cursor) { cv->cursor = (s32)index; f32 y = (f32)index * row_h; if(y < cv->scroll.target) cv->scroll.target = y; if(y + row_h > cv->scroll.target + view_h) cv->scroll.target = y + row_h - view_h; cv->scroll.offset = cv->scroll.target; // jump, the field has to be on screen this frame } break; } } scroll_update(&cv->scroll, (f32)count * row_h, view_h, app->dt, &app->animating); u32 first = (u32)Max(floorf(cv->scroll.offset / row_h), 0.0f); u32 last = Min(count, first + cv->visible_rows + 2); Rect edit_field = {}; Cells cells; draw_clip_push(d, body); for(u32 index = first; index < last; ++index) { Row *row = &rows[index]; f32 y = body.y0 + (f32)index * row_h - cv->scroll.offset; Rect r = {body.x0, y, body.x1, y + row_h}; b32 hov = live && over && ui_hovered(input, rect_intersect(r, body)); b32 sel = app->card_focused && (s32)index == cv->cursor; b32 edit = editing && card_row_is_edit(app, row); f32 base = ui_baseline(app, FONT_REGULAR, UI_FONT, y, row_h); f32 mono = ui_baseline(app, FONT_MONO, UI_FONT, y, row_h); Rect hit = {}; // the row's small button: + on a section, x on a member s32 dir = 0; // choice fields: which arrow the mouse is on view_cells(app, cv, row, &cells, scratch.arena); if(sel) draw_rect(d, r, COLOR_SELECTED, 0); else if(hov) draw_rect(d, r, COLOR_HOVER, 0); switch(row->kind) { case ROW_INSP_FIELD: { b32 action = (row->a == INSP_N_ADD_SECTION); draw_text_fit(d, fonts, FONT_REGULAR, UI_FONT, lx, base, (action ? rx : vx - 8.0f) - lx, cells.text[0], action ? COLOR_TEXT_FAINT : COLOR_TEXT_DIM); if(action) break; if(edit) { edit_field = Rect{vx - 6.0f, y + 2.0f, rx + 4.0f, y + row_h - 2.0f}; } else if(card_field_is_choice(row->a)) { // < value >: the arrows step back and forth, the value steps forward u32 arrow = (hov || sel) ? COLOR_TEXT : COLOR_TEXT_FAINT; draw_text(d, fonts, FONT_MONO, UI_FONT, vx, mono, str8("<"), arrow); draw_text(d, fonts, FONT_MONO, UI_FONT, rx - 8.0f, mono, str8(">"), arrow); draw_text_fit(d, fonts, FONT_MONO, UI_FONT, vx + 16.0f, mono, rx - vx - 28.0f, cells.text[1], COLOR_TEXT); if(hov) dir = (input->mouse_x >= vx - 4.0f && input->mouse_x < vx + 14.0f) ? -1 : 1; } else { f32 x = vx; if(cells.has_swatch) { f32 side = Min(row_h - 10.0f, 12.0f); Rect sw = {x, y + (row_h - side) * 0.5f, x + side, y + (row_h + side) * 0.5f}; draw_rect(d, sw, cells.swatch, 0); draw_border(d, sw, COLOR_PANEL_EDGE, 0, 1); x += side + 7.0f; } b32 empty = cells.text[1].size == 0; draw_text_fit(d, fonts, FONT_MONO, UI_FONT, x, mono, rx - x, empty ? str8("-") : cells.text[1], empty ? COLOR_TEXT_FAINT : COLOR_TEXT); } } break; case ROW_SECTION: { NodeSection *s = &dia->sections[row->a]; draw_text(d, fonts, FONT_MONO, UI_FONT, lx, mono, str8(s->collapsed ? "+" : "-"), COLOR_TEXT_DIM); if(edit) { edit_field = Rect{lx + 14.0f, y + 2.0f, rx + 4.0f, y + row_h - 2.0f}; break; } draw_text_fit(d, fonts, FONT_SEMIBOLD, UI_FONT_SMALL, lx + 16.0f, ui_baseline(app, FONT_SEMIBOLD, UI_FONT_SMALL, y, row_h), vx - lx - 20.0f, str8(s->title), COLOR_TEXT_DIM); if(hov || sel) { hit = Rect{rx - row_h + 6.0f, y, rx + 6.0f, y + row_h}; b32 on = hov && ui_hovered(input, hit); if(on) draw_rect(d, hit, COLOR_HOVER, 0); f32 cx = (hit.x0 + hit.x1) * 0.5f, cy = y + row_h * 0.5f; draw_line(d, cx - 4.0f, cy, cx + 4.0f, cy, 1.2f, on ? COLOR_ACCENT : COLOR_TEXT_DIM); draw_line(d, cx, cy - 4.0f, cx, cy + 4.0f, 1.2f, on ? COLOR_ACCENT : COLOR_TEXT_DIM); draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, vx, mono, hit.x0 - vx - 4.0f, str8("a: add"), COLOR_TEXT_FAINT); } else { draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, vx, mono, rx - vx, cells.text[1], COLOR_TEXT_FAINT); } } break; case ROW_MEMBER: { f32 nx = lx + 16.0f; if(edit) { edit_field = Rect{nx - 6.0f, y + 2.0f, rx + 4.0f, y + row_h - 2.0f}; break; } f32 right = rx; if(hov || sel) { hit = Rect{rx - row_h + 6.0f, y, rx + 6.0f, y + row_h}; b32 on = hov && ui_hovered(input, hit); if(on) draw_rect(d, hit, COLOR_HOVER, 0); f32 cx = (hit.x0 + hit.x1) * 0.5f, cy = y + row_h * 0.5f; draw_line(d, cx - 3.5f, cy - 3.5f, cx + 3.5f, cy + 3.5f, 1.2f, on ? COLOR_TEXT : COLOR_TEXT_FAINT); draw_line(d, cx - 3.5f, cy + 3.5f, cx + 3.5f, cy - 3.5f, 1.2f, on ? COLOR_TEXT : COLOR_TEXT_FAINT); right = hit.x0 - 4.0f; } else if(cells.text[2].size) { f32 bw = Min(font_measure(fonts, FONT_MONO, UI_FONT_SMALL, cells.text[2]) + 8.0f, (rx - vx) * 0.5f); Rect pill = {rx - bw, y + 5.0f, rx, y + row_h - 5.0f}; draw_rect(d, pill, COLOR_TAB, 0); draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, pill.x0 + 4.0f, ui_baseline(app, FONT_MONO, UI_FONT_SMALL, y, row_h), bw - 8.0f, cells.text[2], COLOR_TEXT_DIM); right = pill.x0 - 6.0f; } // NOTE: members flow like on the node (name, then its type), they don't use the columns f32 name_w = font_measure(fonts, FONT_MONO, UI_FONT, cells.text[0]); f32 type_w = font_measure(fonts, FONT_MONO, UI_FONT, cells.text[1]); f32 room = right - nx; f32 fit_n = (name_w + 10.0f + type_w <= room) ? name_w : Max(room * 0.62f, room - 10.0f - type_w); f32 tx = nx + draw_text_fit(d, fonts, FONT_MONO, UI_FONT, nx, mono, fit_n, cells.text[0], COLOR_TEXT) + 10.0f; if(tx < right) draw_text_fit(d, fonts, FONT_MONO, UI_FONT, tx, mono, right - tx, cells.text[1], COLOR_TEXT_DIM); } break; } if(hov) app->canvas_cursor = PLATFORM_CURSOR_POINTER; //- mouse: one click does the obvious thing, no double clicks needed if(hov && ButtonPressed(input->mouse[0])) { app->layout.focused = panel; app->card_focused = true; cv->cursor = (s32)index; b32 on_hit = hit.x1 > hit.x0 && ui_hovered(input, hit); if(row->kind == ROW_SECTION) { NodeSection *s = &dia->sections[row->a]; if(on_hit) { diagram_push_undo(dia); s32 m = diagram_add_member(dia, (s32)row->a, '+', "new_field", "u32", ""); if(m >= 0) start_edit(app, EDIT_MEMBER_LINE, m, "+ new_field : u32"); } else if(input->mouse_x < lx + 14.0f) { s->collapsed = !s->collapsed; diagram_measure_node(dia, s->node); ++dia->version; } else { view_activate(app, cv, row); } } else if(row->kind == ROW_MEMBER && on_hit) { diagram_push_undo(dia); diagram_delete_member(dia, (s32)row->a); app_message(app, "deleted member (u to undo)"); } else if(dir) { view_adjust(app, cv, row, dir); } else { view_activate(app, cv, row); } } } //- the inline edit field, drawn last so it sits on top of its row if(edit_field.x1 > edit_field.x0) { card_draw_input(app, edit_field); // a click anywhere else keeps the text, like leaving a field in any property editor if(ButtonPressed(input->mouse[0]) && !ui_hovered(input, edit_field)) { app->overlay = UI_OVERLAY_NONE; edit_accept(app); } } draw_clip_pop(d); f32 content = (f32)count * row_h; if(content > view_h + 0.5f) { f32 thumb_h = Max(view_h * view_h / content, 16.0f); f32 thumb_y = body.y0 + (view_h - thumb_h) * (cv->scroll.offset / (content - view_h)); draw_rect(d, Rect{body.x1 - 4, thumb_y, body.x1 - 1, thumb_y + thumb_h}, COLOR_TRACK, 0); } //- footer: the keys that work right now { const char *hint = "enter: edit here I: hide"; if(editing) hint = "enter: save esc: cancel"; else if(app->card_focused) hint = "j/k h/l: change enter: edit a: add esc"; draw_rect(d, Rect{foot.x0, foot.y0, foot.x1, foot.y0 + 1}, COLOR_PANEL_EDGE, 0); draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, lx, ui_baseline(app, FONT_MONO, UI_FONT_SMALL, foot.y0, rect_h(foot)), rx - lx, str8((char *)hint), COLOR_TEXT_FAINT); } draw_border(d, box, app->card_focused ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE, 0, 1); scratch_end(&scratch); } //- watch-style table rendering (reorderable / resizable / hideable columns, identical to mp3_player) typedef struct TableColumns TableColumns; struct TableColumns { u32 logical[UI_MAX_COLUMNS]; f32 x[UI_MAX_COLUMNS]; f32 w[UI_MAX_COLUMNS]; u32 count; }; internal void table_columns_compute(View *view, Column *columns, u32 total, Rect rect, TableColumns *out) { if(view->column_count != total) { view->column_count = total; view->column_hidden = 0; for(u32 c = 0; c < total; ++c) { view->column_order[c] = (u8)c; view->column_weight[c] = columns[c].width; } } f32 sum = 0; out->count = 0; for(u32 pos = 0; pos < total; ++pos) { u32 c = view->column_order[pos]; if(c >= total || (view->column_hidden & (1u << c))) continue; out->logical[out->count++] = c; sum += Max(view->column_weight[c], 0.04f); } if(out->count == 0 && total > 0) { out->logical[0] = 0; out->count = 1; sum = Max(view->column_weight[0], 0.04f); } f32 x = rect.x0, width = rect_w(rect); for(u32 i = 0; i < out->count; ++i) { f32 w = width * Max(view->column_weight[out->logical[i]], 0.04f) / sum; out->x[i] = x; out->w[i] = w; x += w; } } internal void draw_table(App *app, PlatformInput *input, s32 panel, View *view, Rect rect, b32 focused) { DrawList *d = &app->draw; FontSystem *fonts = &app->fonts; ScratchArena scratch = ScratchGet(); b32 modal = (app->overlay != UI_OVERLAY_NONE); s32 vi = (s32)(view - app->layout.views); Column columns[UI_MAX_COLUMNS]; u32 col_total = view_columns((ViewKind)view->kind, columns); TableColumns layout; table_columns_compute(view, columns, col_total, rect, &layout); f32 header_h = UI_COLUMN_H; Rect header = {rect.x0, rect.y0, rect.x1, rect.y0 + header_h}; Rect body = {rect.x0, header.y1, rect.x1, rect.y1}; //- column headers (drag to reorder, drag edge to resize, middle click to hide) if(header_h > 0 && layout.count > 0) { draw_rect(d, header, COLOR_HEADER, 0); f32 hb = ui_baseline(app, FONT_MONO, UI_FONT_SMALL, header.y0, header_h); for(u32 i = 0; i < layout.count; ++i) { u32 c = layout.logical[i]; Rect cell = {layout.x[i], header.y0, layout.x[i] + layout.w[i], header.y1}; if(i > 0) { draw_rect(d, Rect{cell.x0, header.y0 + 3, cell.x0 + 1, header.y1 - 3}, COLOR_PANEL_EDGE, 0); Rect edge = {cell.x0 - 4, header.y0, cell.x0 + 4, header.y1}; if(!modal && ui_hovered(input, edge)) { app->table_cursor = PLATFORM_CURSOR_RESIZE_X; if(ButtonPressed(input->mouse[0])) { app->column_resize_view = vi; app->column_resize_index = i - 1; } } } draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, cell.x0 + 8, hb, layout.w[i] - 12, str8((char *)columns[c].title), COLOR_TEXT_DIM); if(!modal && ui_hovered(input, cell) && app->column_resize_view < 0) { if(ButtonPressed(input->mouse[2]) && layout.count > 1) view->column_hidden |= (1u << c); } } draw_rect(d, Rect{header.x0, header.y1 - 1, header.x1, header.y1}, COLOR_PANEL_EDGE, 0); } if(app->column_resize_view == vi) { if(input->mouse[0].ended_down && app->column_resize_index + 1 < layout.count) { u32 a = layout.logical[app->column_resize_index]; u32 b = layout.logical[app->column_resize_index + 1]; f32 pair_w = layout.w[app->column_resize_index] + layout.w[app->column_resize_index + 1]; f32 sum_w = view->column_weight[a] + view->column_weight[b]; f32 t = Clamp((input->mouse_x - layout.x[app->column_resize_index]) / Max(pair_w, 1.0f), 0.12f, 0.88f); view->column_weight[a] = sum_w * t; view->column_weight[b] = sum_w * (1.0f - t); app->table_cursor = PLATFORM_CURSOR_RESIZE_X; } else { app->column_resize_view = -1; } } Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS); u32 count = view_rows(app, view, rows, UI_MAX_ROWS); view->row_count = count; if(count > 0) view->cursor = Clamp(view->cursor, 0, (s32)count - 1); else view->cursor = 0; f32 row_h = UI_ROW_H; f32 view_h = Max(rect_h(body), 1.0f); view->visible_rows = (u32)Max(floorf(view_h / row_h), 1.0f); if(!modal && ui_hovered(input, body) && input->scroll_y != 0.0f) { view->scroll.target += input->scroll_y * row_h * (f32)ui_theme.scroll_lines; } scroll_update(&view->scroll, (f32)count * row_h, view_h, app->dt, &app->animating); u32 first = (u32)Max(floorf(view->scroll.offset / row_h), 0.0f); u32 last = Min(count, first + view->visible_rows + 2); view->first_visible = first; String8 dsl_cache = {}; if(view->kind == VIEW_SOURCE) dsl_cache = diagram_to_dsl(scratch.arena, app->diagram); draw_clip_push(d, body); Cells cells; for(u32 index = first; index < last; ++index) { Row *row = &rows[index]; f32 y = body.y0 + (f32)index * row_h - view->scroll.offset; Rect r = {body.x0, y, body.x1, y + row_h}; b32 sel = ((s32)index == view->cursor); b32 hov = !modal && ui_hovered(input, rect_intersect(r, body)); view_cells(app, view, row, &cells, scratch.arena, &dsl_cache); if(sel) draw_rect(d, r, focused ? COLOR_SELECTED : color_alpha(COLOR_SELECTED, 0.55f), 0); else if(hov) draw_rect(d, r, COLOR_HOVER, 0); else if(ui_theme.zebra && (index & 1)) draw_rect(d, r, COLOR_ZEBRA, 0); u32 color = cells.bright ? COLOR_ACCENT : (cells.dim ? COLOR_TEXT_FAINT : COLOR_TEXT); for(u32 pos = 0; pos < layout.count; ++pos) { u32 c = layout.logical[pos]; f32 cx = layout.x[pos]; f32 w = layout.w[pos]; f32 indent = 0.0f; if(c == 0) { indent = (f32)row->depth * ui_theme.tree_indent; if(row->expandable) { draw_text(d, fonts, FONT_MONO, UI_FONT, cx + 8 + indent, ui_baseline(app, FONT_MONO, UI_FONT, y, row_h), str8(row->expanded ? "-" : "+"), COLOR_TEXT_DIM); indent += 13.0f; } } FontKind font = columns[c].mono ? FONT_MONO : ((cells.bright && c == 0) ? FONT_SEMIBOLD : FONT_REGULAR); if(c == 1 && cells.has_swatch) { f32 side = Min(row_h - 8, 14.0f); Rect sw = {cx + 8, y + (row_h - side) * 0.5f, cx + 8 + side, y + (row_h + side) * 0.5f}; draw_rect(d, sw, cells.swatch, 0); draw_border(d, sw, COLOR_PANEL_EDGE, 0, 1); indent = side + 8; } u32 tint = (c > 0 && !cells.bright) ? (cells.dim ? COLOR_TEXT_FAINT : COLOR_TEXT_DIM) : color; draw_text_fit(d, fonts, font, UI_FONT, cx + 8 + indent, ui_baseline(app, font, UI_FONT, y, row_h), w - 14 - indent, cells.text[c], tint); } if(hov && ButtonPressed(input->mouse[0])) { app->layout.focused = panel; view->cursor = (s32)index; u64 now = time_now_ns(); b32 twice = (now - app->last_click_ns < (u64)ui_theme.double_click_ms * 1000000ull) && fabsf(input->mouse_x - app->last_click_x) < 4 && fabsf(input->mouse_y - app->last_click_y) < 4; b32 on_box = row->expandable && input->mouse_x < body.x0 + 8 + row->depth * ui_theme.tree_indent + 14.0f; if(on_box && row->kind == ROW_NODE) { view_set_expanded(view, row->a, !view_expanded(view, row->a)); now = 0; } else if(on_box && row->kind == ROW_SECTION) { app->diagram->sections[row->a].collapsed = !app->diagram->sections[row->a].collapsed; diagram_measure_node(app->diagram, app->diagram->sections[row->a].node); now = 0; } else if(twice) { view_activate(app, view, row); now = 0; } else if(row->kind == ROW_NODE) { diagram_clear_selection(app->diagram); app->diagram->nodes[row->a].selected = true; app->active_node = (s32)row->a; app->active_edge = -1; } else if(row->kind == ROW_EDGE) { diagram_clear_selection(app->diagram); app->diagram->edges[row->a].selected = true; app->active_edge = (s32)row->a; app->active_node = -1; } app->last_click_ns = now; app->last_click_x = input->mouse_x; app->last_click_y = input->mouse_y; } } draw_clip_pop(d); if(count == 0) { const char *empty = "empty"; if(view->kind == VIEW_LAYERS) empty = "no tags yet (set a node's tag in Inspector)"; if(view->kind == VIEW_EDGES) empty = "no edges (drag from a node port or right-drag a node)"; draw_text_fit(d, fonts, FONT_REGULAR, UI_FONT, body.x0 + 10, body.y0 + 18, rect_w(body) - 20, str8((char *)empty), COLOR_TEXT_FAINT); } f32 content = (f32)count * row_h; if(content > view_h && ui_theme.show_scrollbars) { f32 thumb_h = Max(view_h * view_h / content, 20.0f); f32 thumb_y = body.y0 + (view_h - thumb_h) * (view->scroll.offset / (content - view_h)); draw_rect(d, Rect{body.x1 - 4, thumb_y, body.x1 - 1, thumb_y + thumb_h}, COLOR_TRACK, 0); } scratch_end(&scratch); } //- panels & bar (exact RAD docking UX) internal DropZone drop_zone(Rect rect, f32 x, f32 y) { f32 u = (x - rect.x0) / Max(rect_w(rect), 1.0f); f32 v = (y - rect.y0) / Max(rect_h(rect), 1.0f); if(u < 0.25f) return DROP_LEFT; if(u > 0.75f) return DROP_RIGHT; if(v < 0.25f) return DROP_TOP; if(v > 0.75f) return DROP_BOTTOM; return DROP_CENTER; } internal Rect drop_preview(Rect rect, DropZone zone) { switch(zone) { case DROP_LEFT: return Rect{rect.x0, rect.y0, rect.x0 + rect_w(rect) * 0.5f, rect.y1}; case DROP_RIGHT: return Rect{rect.x0 + rect_w(rect) * 0.5f, rect.y0, rect.x1, rect.y1}; case DROP_TOP: return Rect{rect.x0, rect.y0, rect.x1, rect.y0 + rect_h(rect) * 0.5f}; case DROP_BOTTOM: return Rect{rect.x0, rect.y0 + rect_h(rect) * 0.5f, rect.x1, rect.y1}; default: return rect; } } internal b32 draw_bar(App *app, PlatformInput *input, s32 panel, s32 view_index, Rect rect, b32 modal) { DrawList *d = &app->draw; FontSystem *fonts = &app->fonts; View *view = &app->layout.views[view_index]; f32 size = UI_FONT_SMALL; ScratchArena scratch = ScratchGet(); draw_rect(d, rect, COLOR_BG, 0); if(!modal && ui_hovered(input, rect) && app->drag_view < 0) { if(ButtonPressed(input->mouse[0])) { app->drag_view = view_index; app->drag_panel = panel; app->drag_x = input->mouse_x; app->drag_y = input->mouse_y; app->drag_active = false; } if(ButtonPressed(input->mouse[2])) { Panel *p = &app->layout.panels[panel]; scratch_end(&scratch); layout_close_view(&app->layout, panel, p->active_tab); return true; } } Row *rows = PushArray(scratch.arena, Row, 64); u32 count = view_rows(app, view, rows, 64); u32 shown = (view->kind == VIEW_STATUS || view->kind == VIEW_INSPECTOR || view->kind == VIEW_PROFILER || view->kind == VIEW_SETTINGS) ? 1 : 0; Cells *all = PushArray(scratch.arena, Cells, count + 1); f32 base = ui_baseline(app, FONT_MONO, size, rect.y0, Min(rect_h(rect), UI_STATUS_H)); f32 right = rect.x1 - 10; draw_clip_push(d, rect); for(u32 pass = 0; pass < 2; ++pass) { f32 left = rect.x0; for(u32 index = 0; index < count; ++index) { Cells *cells = &all[index]; if(pass == 0) view_cells(app, view, &rows[index], cells, scratch.arena); String8 text = cells->text[shown]; if(text.size == 0 || (b32)cells->right != (pass == 0)) continue; u32 color = cells->dim ? COLOR_TEXT_DIM : COLOR_TEXT; if(cells->right) { right -= font_measure(fonts, FONT_MONO, size, text); draw_text(d, fonts, FONT_MONO, size, right, base, text, color); right -= 18; } else if(cells->bright) { f32 w = font_measure(fonts, FONT_MONO, size, text) + 16; draw_rect(d, Rect{left, rect.y0, left + w, rect.y1}, COLOR_TAB_ACTIVE, 0); draw_text(d, fonts, FONT_MONO, size, left + 8, base, text, COLOR_ACCENT); left += w + 10; } else if(right - left > 24) { left += draw_text_fit(d, fonts, FONT_REGULAR, size, left, base, right - left - 8, text, COLOR_TEXT_DIM) + 18; } } } draw_clip_pop(d); scratch_end(&scratch); return false; } internal void draw_panels(App *app, PlatformInput *input) { DrawList *d = &app->draw; FontSystem *fonts = &app->fonts; Layout *layout = &app->layout; u32 cursor = PLATFORM_CURSOR_DEFAULT; b32 modal = (app->overlay != UI_OVERLAY_NONE); f32 tab_font = UI_FONT_SMALL + 0.5f; //- split resize handles for(s32 index = 0; index < UI_MAX_PANELS && !modal; ++index) { Panel *p = &layout->panels[index]; if(p->kind != PANEL_SPLIT) continue; Rect a = layout->panels[p->children[0]].rect; Rect handle = (p->axis == 0) ? Rect{a.x1 - 1, p->rect.y0, a.x1 + UI_GAP + 1, p->rect.y1} : Rect{p->rect.x0, a.y1 - 1, p->rect.x1, a.y1 + UI_GAP + 1}; b32 over = ui_hovered(input, handle) && app->drag_view < 0; if(over && ButtonPressed(input->mouse[0])) app->resize_panel = index; if(over || app->resize_panel == index) cursor = (p->axis == 0) ? PLATFORM_CURSOR_RESIZE_X : PLATFORM_CURSOR_RESIZE_Y; } if(app->resize_panel >= 0) { Panel *p = &layout->panels[app->resize_panel]; if(p->kind == PANEL_SPLIT) { f32 t = (p->axis == 0) ? (input->mouse_x - p->rect.x0) / rect_w(p->rect) : (input->mouse_y - p->rect.y0) / rect_h(p->rect); p->ratio = Clamp(t, 0.08f, 0.92f); f32 lines = (p->rect.y1 - input->mouse_y) / UI_STATUS_H; if(p->axis == 1 && lines < 3.5f) p->ratio = -Max(roundf(lines), 1.0f); } if(!input->mouse[0].ended_down) app->resize_panel = -1; } //- leaves for(s32 index = 0; index < UI_MAX_PANELS; ++index) { Panel *p = &layout->panels[index]; if(p->kind != PANEL_LEAF) continue; b32 focused = (layout->focused == index) && !modal; Rect rect = p->rect; if(panel_is_bar(layout, index)) { s32 view = panel_active_view(layout, index); if(view >= 0 && draw_bar(app, input, index, view, rect, modal)) return; continue; } draw_rect(d, rect, COLOR_PANEL, 0); //- tab bar Rect bar = {rect.x0, rect.y0, rect.x1, rect.y0 + UI_TAB_H}; draw_rect(d, bar, COLOR_BG, 0); ScratchArena tab_scratch = ScratchGet(); String8 *titles = PushArray(tab_scratch.arena, String8, UI_MAX_TABS); f32 *widths = PushArray(tab_scratch.arena, f32, UI_MAX_TABS); f32 total = 0; for(u32 t = 0; t < p->tab_count; ++t) { titles[t] = view_title(tab_scratch.arena, &layout->views[p->tabs[t]]); widths[t] = font_measure(fonts, FONT_REGULAR, tab_font, titles[t]) + 24; total += widths[t] + 1; } f32 fit = (total > rect_w(bar) && total > 0) ? rect_w(bar) / total : 1.0f; draw_clip_push(d, bar); f32 tx = bar.x0; for(u32 t = 0; t < p->tab_count; ++t) { String8 title = titles[t]; f32 w = Max(widths[t] * fit, 28.0f); Rect tab = {tx, bar.y0, tx + w, bar.y1}; b32 active = (t == p->active_tab); b32 over = ui_hovered(input, rect_intersect(tab, bar)) && !modal; draw_rect(d, tab, active ? COLOR_TAB_ACTIVE : (over ? COLOR_TAB : COLOR_BG), 0); if(active && focused) draw_rect(d, Rect{tab.x0, tab.y0, tab.x1, tab.y0 + 2}, COLOR_ACCENT, 0); draw_text_fit(d, fonts, FONT_REGULAR, tab_font, tab.x0 + 12 * Min(fit, 1.0f), ui_baseline(app, FONT_REGULAR, tab_font, tab.y0, UI_TAB_H), w - 16, title, active ? (focused ? COLOR_ACCENT : COLOR_TEXT) : COLOR_TEXT_DIM); if(over && ButtonPressed(input->mouse[0])) { p->active_tab = t; layout->focused = index; app->drag_view = p->tabs[t]; app->drag_panel = index; app->drag_x = input->mouse_x; app->drag_y = input->mouse_y; app->drag_active = false; } if(over && ButtonPressed(input->mouse[2])) { draw_clip_pop(d); scratch_end(&tab_scratch); layout_close_view(layout, index, t); return; } tx += w + 1; } draw_clip_pop(d); scratch_end(&tab_scratch); Rect empty = {tx, bar.y0, bar.x1, bar.y1}; if(!modal && !PlatformWindowIsLayer(app->window) && ui_clicked(input, empty) && app->drag_view < 0) { u64 now = time_now_ns(); if(now - app->last_click_ns < (u64)ui_theme.double_click_ms * 1000000ull) { PlatformWindowToggleMaximized(app->window); app->last_click_ns = 0; } else { app->last_click_ns = now; PlatformWindowBeginMove(app->window); } } //- content: Canvas OR Watch Table Rect content = {rect.x0, bar.y1, rect.x1, rect.y1}; s32 view = panel_active_view(layout, index); if(view >= 0) { draw_clip_push(d, content); if(layout->views[view].kind == VIEW_CANVAS) { draw_canvas(app, input, index, &layout->views[view], content, focused); } else { draw_table(app, input, index, &layout->views[view], content, focused); } draw_clip_pop(d); } else { draw_text(d, fonts, FONT_MONO, UI_FONT_SMALL, content.x0 + 10, content.y0 + 20, str8(":view canvas | outline | inspector | edges | layers | palette | source"), COLOR_TEXT_FAINT); } draw_border(d, rect, focused ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE, 0, 1); } //- tab drag & drop if(app->drag_view >= 0) { f32 moved = fabsf(input->mouse_x - app->drag_x) + fabsf(input->mouse_y - app->drag_y); if(moved > 6) app->drag_active = true; if(app->drag_active) { cursor = PLATFORM_CURSOR_GRABBING; for(s32 index = 0; index < UI_MAX_PANELS; ++index) { Panel *p = &layout->panels[index]; if(p->kind != PANEL_LEAF || !ui_hovered(input, p->rect)) continue; DropZone zone = drop_zone(p->rect, input->mouse_x, input->mouse_y); Rect preview = drop_preview(p->rect, zone); draw_rect(d, preview, COLOR_DROP, 0); draw_border(d, preview, COLOR_FOCUS_EDGE, 0, 1); if(!input->mouse[0].ended_down) { layout_move_view(layout, app->drag_panel, app->drag_view, index, zone); break; } } if(app->drag_view >= 0 && layout->views[app->drag_view].kind != VIEW_NONE) { ScratchArena gs = ScratchGet(); String8 title = view_title(gs.arena, &layout->views[app->drag_view]); f32 w = font_measure(fonts, FONT_REGULAR, tab_font, title) + 24; Rect ghost = {input->mouse_x + 10, input->mouse_y + 10, input->mouse_x + 10 + w, input->mouse_y + 10 + UI_TAB_H}; draw_rect(d, ghost, COLOR_TAB_ACTIVE, 0); draw_border(d, ghost, COLOR_FOCUS_EDGE, 0, 1); draw_text(d, fonts, FONT_REGULAR, tab_font, ghost.x0 + 12, ui_baseline(app, FONT_REGULAR, tab_font, ghost.y0, UI_TAB_H), title, COLOR_TEXT); scratch_end(&gs); } } if(!input->mouse[0].ended_down) app->drag_view = -1; } if(cursor == PLATFORM_CURSOR_DEFAULT) cursor = (app->canvas_cursor != PLATFORM_CURSOR_DEFAULT) ? app->canvas_cursor : app->table_cursor; if(app->cursor_shape != cursor) { app->cursor_shape = cursor; PlatformWindowSetCursor(app->window, (PlatformCursor)cursor); } } //- search & command bar (with live completion) internal void search_update(App *app) { Diagram *dia = app->diagram; app->result_count = 0; app->result_cursor = 0; if(app->input_size == 0) return; for(u32 i = 0; i < dia->node_count && app->result_count < UI_MAX_RESULTS; ++i) { DiagramNode *n = &dia->nodes[i]; if(!n->alive) continue; if(str8_contains_nocase(str8(n->name), app->input, app->input_size) || str8_contains_nocase(str8(n->tag), app->input, app->input_size) || str8_contains_nocase(str8(n->stereotype), app->input, app->input_size)) { app->results[app->result_count++] = SearchResult{0, (s32)i, -1}; } } for(u32 i = 0; i < dia->member_count && app->result_count < UI_MAX_RESULTS; ++i) { NodeMember *m = &dia->members[i]; if(!m->alive) continue; if(str8_contains_nocase(str8(m->name), app->input, app->input_size) || str8_contains_nocase(str8(m->type), app->input, app->input_size)) { app->results[app->result_count++] = SearchResult{2, (s32)i, dia->sections[m->section].node}; } } for(u32 i = 0; i < dia->edge_count && app->result_count < UI_MAX_RESULTS; ++i) { DiagramEdge *e = &dia->edges[i]; if(!e->alive) continue; if(e->label[0] && str8_contains_nocase(str8(e->label), app->input, app->input_size)) { app->results[app->result_count++] = SearchResult{1, (s32)i, -1}; } } } internal void search_accept(App *app, SearchResult r) { Diagram *dia = app->diagram; diagram_clear_selection(dia); if(r.kind == 0 || r.kind == 2) { s32 ni = (r.kind == 0) ? r.index : r.parent_node; if(ni >= 0 && dia->nodes[ni].alive) { dia->nodes[ni].selected = true; app->active_node = ni; app->active_edge = -1; app->pan_to_active = true; } } else if(r.kind == 1 && r.index >= 0 && dia->edges[r.index].alive) { dia->edges[r.index].selected = true; app->active_edge = r.index; app->active_node = -1; } } typedef struct CommandDesc CommandDesc; struct CommandDesc { const char *name; const char *args; const char *help; b32 takes_arg; }; global_variable const CommandDesc ui_commands[] = { {"view", "", "open or focus a view", 1}, {"vs", "", "split right with a view", 1}, {"sp", "", "split below with a view", 1}, {"set", "=", "change any setting", 1}, {"add", " ", "add a node (struct/class/iface/module/...)", 1}, {"connect", " ", "connect two nodes by key", 1}, {"layout", "","auto-layout the diagram", 1}, {"route", "","set default edge routing", 1}, {"align", "", "align or distribute selected nodes", 1}, {"tpl", "", "load showcase template", 1}, {"scan", "", "scan C/C++ headers into a live diagram", 1}, {"save", "", "save diagram (binary or readable DSL)", 1}, {"open", "", "load diagram file", 1}, {"export", "", "export crisp themed vector SVG", 1}, {"focus", "", "toggle focus mode (dim outside neighborhood)", 0}, {"fit", "", "fit diagram in canvas", 0}, {"clear", "", "clear the diagram", 0}, {"columns", "", "restore hidden columns of focused table", 0}, {"reset", "", "reset panel layout to default", 0}, {"zoom", "<0.6-3>", "set ui zoom", 1}, {"help", "[topic]", "open the manual (F1), jump to a topic", 0}, {"q", "", "quit", 0}, }; typedef struct Completion Completion; struct Completion { String8 text; String8 detail; u32 from; char suffix; }; #define UI_MAX_COMPLETIONS 64 typedef struct CompletionList CompletionList; struct CompletionList { Completion items[UI_MAX_COMPLETIONS]; u32 count; }; internal u32 completion_match(const char *candidate, const char *token, u32 token_size) { String8 c = str8((char *)candidate); if(token_size == 0) return 2; if(c.size >= token_size && strncasecmp(candidate, token, token_size) == 0) return 2; return str8_contains_nocase(c, token, token_size) ? 1 : 0; } internal u32 command_completions(App *app, Arena *arena, CompletionList *out) { out->count = 0; const char *in = app->input; u32 len = app->input_size; u32 sp = 0; while(sp < len && in[sp] != ' ') ++sp; if(sp == len) { for(u32 tier = 2; tier >= 1; --tier) { for(u32 i = 0; i < ArrayCount(ui_commands) && out->count < UI_MAX_COMPLETIONS; ++i) { if(completion_match(ui_commands[i].name, in, len) == tier) { out->items[out->count++] = Completion{str8((char *)ui_commands[i].name), ui_format(arena, "%s %s", ui_commands[i].args, ui_commands[i].help), 0, (char)(ui_commands[i].takes_arg ? ' ' : 0)}; } } } return out->count; } u32 arg_from = sp + 1; while(arg_from < len && in[arg_from] == ' ') ++arg_from; const char *arg = in + arg_from; u32 alen = len - arg_from; if((sp == 4 && !strncasecmp(in, "view", 4)) || (sp == 2 && (!strncasecmp(in, "vs", 2) || !strncasecmp(in, "sp", 2)))) { ViewType types[UI_MAX_VIEW_TYPES]; u32 n = view_types(types); for(u32 i = 0; i < n && out->count < UI_MAX_COMPLETIONS; ++i) { if(completion_match(types[i].name, arg, alen)) { out->items[out->count++] = Completion{str8((char *)types[i].name), str8(types[i].kind == VIEW_SETTINGS ? "settings" : (types[i].kind == VIEW_PROFILER ? "profiler" : "view")), arg_from, 0}; } } } else if(sp == 3 && !strncasecmp(in, "set", 3)) { const char *eq = NULL; for(u32 i = arg_from; i < len; ++i) if(in[i] == '=') { eq = in + i; break; } if(!eq) { for(u32 i = 0; i < settings_desc_count() && out->count < UI_MAX_COMPLETIONS; ++i) { SettingDesc *desc = settings_desc(i); if(desc->kind == SETTING_KIND_SECTION || desc->kind == SETTING_KIND_ACTION || desc->kind == SETTING_KIND_INFO) continue; if(completion_match(desc->name, arg, alen)) { out->items[out->count++] = Completion{str8((char *)desc->name), settings_format(arena, &app->settings, desc), arg_from, '='}; } } } } else if(sp == 3 && !strncasecmp(in, "tpl", 3)) { for(u32 i = 0; i < diagram_template_count() && out->count < UI_MAX_COMPLETIONS; ++i) { const DiagramTemplate *t = diagram_template_info(i); if(completion_match(t->name, arg, alen)) out->items[out->count++] = Completion{str8((char *)t->name), str8((char *)t->description), arg_from, 0}; } } else if(sp == 6 && !strncasecmp(in, "layout", 6)) { const char *modes[] = {"layered", "lr", "force", "grid"}; const char *desc[] = {"hierarchical top-down (Sugiyama)", "hierarchical left-to-right", "spring force-directed", "compact grid"}; for(u32 i = 0; i < 4; ++i) if(completion_match(modes[i], arg, alen)) out->items[out->count++] = Completion{str8((char *)modes[i]), str8((char *)desc[i]), arg_from, 0}; } else if(sp == 5 && !strncasecmp(in, "route", 5)) { const char *modes[] = {"orthogonal", "straight", "bezier"}; for(u32 i = 0; i < 3; ++i) if(completion_match(modes[i], arg, alen)) out->items[out->count++] = Completion{str8((char *)modes[i]), str8("edge routing"), arg_from, 0}; } else if(sp == 3 && !strncasecmp(in, "add", 3)) { for(u32 i = 0; i < NODE_KIND_COUNT; ++i) if(completion_match(diagram_node_kind_names[i], arg, alen)) out->items[out->count++] = Completion{str8((char *)diagram_node_kind_names[i]), str8("node kind"), arg_from, ' '}; } return out->count; } internal void command_accept_completion(App *app, Completion *item) { u32 pos = Min(item->from, (u32)sizeof(app->input) - 1); u32 take = Min((u32)item->text.size, (u32)sizeof(app->input) - 1 - pos); MemCpy(app->input + pos, item->text.data, take); pos += take; if(item->suffix && pos + 1 < sizeof(app->input)) app->input[pos++] = item->suffix; app->input_size = pos; app->result_cursor = 0; } //- opening files (the :open command and drag and drop) internal b32 app_path_is_directory(const char *path) { #if PLATFORM_WINDOWS WCHAR wide[MAX_PATH * 2]; win32_utf8_to_wide(path, -1, wide, ArrayCount(wide)); DWORD attributes = GetFileAttributesW(wide); return attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY); #else struct stat st; return stat(path, &st) == 0 && S_ISDIR(st.st_mode); #endif } internal void app_refit_canvases(App *app) { app->active_node = app->active_edge = -1; for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true; } // NOTE: a .dit / .dit.txt replaces the diagram (undoable), a directory runs the C/C++ scanner internal b32 app_open_file(App *app, const char *path) { Diagram *dia = app->diagram; diagram_push_undo(dia); if(app_path_is_directory(path)) { u32 found = diagram_scan_cpp(dia, path); app_refit_canvases(app); app_message(app, "scanned %s -> %u nodes, %u edges", path, found, dia->live_edges); return true; } if(diagram_load_file(dia, path)) { app_refit_canvases(app); app_message(app, "loaded %s (%u nodes, %u edges)", path, dia->live_nodes, dia->live_edges); return true; } app_message(app, "could not load %s", path); return false; } internal void app_handle_drops(App *app, PlatformInput *input) { if(input->drop_count == 0) return; Log("drop: %u path(s), first: %s", input->drop_count, input->drop_paths[0]); // NOTE: one diagram per window for now, the first file wins app_open_file(app, input->drop_paths[0]); if(input->drop_count > 1) { app_message(app, "opened %s (%u files dropped, one diagram at a time)", input->drop_paths[0], input->drop_count); } } internal void command_run(App *app) { Diagram *dia = app->diagram; char text[161]; MemCpy(text, app->input, app->input_size); text[app->input_size] = 0; char *arg = strchr(text, ' '); if(arg) { *arg++ = 0; while(*arg == ' ') ++arg; } if(!strcmp(text, "q") || !strcmp(text, "q!") || !strcmp(text, "qa") || !strcmp(text, "wq")) { app->quit = true; } else if((!strcmp(text, "view") || !strcmp(text, "vs") || !strcmp(text, "sp")) && arg && arg[0]) { ViewType type; if(view_resolve(arg, &type)) { if(!strcmp(text, "vs")) layout_split_with(&app->layout, (ViewKind)type.kind, 0, type.group); else if(!strcmp(text, "sp")) layout_split_with(&app->layout, (ViewKind)type.kind, 1, type.group); else layout_open(&app->layout, (ViewKind)type.kind, type.group); } else { app_message(app, "unknown view: %s", arg); } } else if(!strcmp(text, "set") && arg) { if(settings_set(&app->settings, arg)) { app_apply_settings(app); app_message(app, "set %s", arg); } else app_message(app, "unknown setting or value: %s", arg); } else if(!strcmp(text, "zoom") && arg) { app->settings.zoom = (f32)atof(arg); app_apply_settings(app); } else if(!strcmp(text, "layout")) { diagram_push_undo(dia); if(!arg || !strcasecmp(arg, "layered") || !strcasecmp(arg, "td")) diagram_layout_layered(dia, false); else if(!strcasecmp(arg, "lr")) diagram_layout_layered(dia, true); else if(!strcasecmp(arg, "force")) diagram_layout_force(dia, 60); else if(!strcasecmp(arg, "grid")) diagram_layout_grid(dia); for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true; app_message(app, "layout complete in %.2f ms", dia->layout_ms); } else if(!strcmp(text, "route") && arg) { if(!strncasecmp(arg, "ortho", 5)) dia->default_routing = ROUTE_ORTHOGONAL; else if(!strncasecmp(arg, "str", 3)) dia->default_routing = ROUTE_STRAIGHT; else if(!strncasecmp(arg, "bez", 3)) dia->default_routing = ROUTE_BEZIER; app->settings.default_routing = dia->default_routing; app_message(app, "routing: %s", edge_routing_name(dia->default_routing)); } else if(!strcmp(text, "align") && arg) { diagram_push_undo(dia); if(!strcasecmp(arg, "left")) diagram_align_selected(dia, ALIGN_LEFT); else if(!strcasecmp(arg, "right")) diagram_align_selected(dia, ALIGN_RIGHT); else if(!strcasecmp(arg, "top")) diagram_align_selected(dia, ALIGN_TOP); else if(!strcasecmp(arg, "bot") || !strcasecmp(arg, "bottom")) diagram_align_selected(dia, ALIGN_BOTTOM); else if(!strcasecmp(arg, "cx")) diagram_align_selected(dia, ALIGN_CENTER_X); else if(!strcasecmp(arg, "cy")) diagram_align_selected(dia, ALIGN_CENTER_Y); else if(!strcasecmp(arg, "dh")) diagram_align_selected(dia, DISTRIBUTE_H); else if(!strcasecmp(arg, "dv")) diagram_align_selected(dia, DISTRIBUTE_V); } else if(!strcmp(text, "add") && arg) { char kind_s[32] = {}; const char *rest = dsl_read_quoted(arg, kind_s, sizeof(kind_s)); rest = dsl_skip_ws(rest); s32 k = dsl_match_node_kind(kind_s); if(k < 0) { k = NODE_STRUCT; rest = arg; } View *cv = find_canvas_view(app); diagram_push_undo(dia); s32 ni = diagram_add_node(dia, (NodeKind)k, rest[0] ? rest : node_kind_name((u32)k), cv ? cv->cam_x - 100.0f : 100.0f, cv ? cv->cam_y - 40.0f : 100.0f); if(ni >= 0) { diagram_clear_selection(dia); dia->nodes[ni].selected = true; app->active_node = ni; app->active_edge = -1; app_message(app, "added %s \"%s\"", node_kind_name((u32)k), dia->nodes[ni].name); } } else if(!strcmp(text, "connect") && arg) { char k1[48] = {}, k2[48] = {}, k3[24] = {}; const char *p = dsl_read_quoted(arg, k1, sizeof(k1)); p = dsl_read_quoted(p, k2, sizeof(k2)); dsl_read_quoted(p, k3, sizeof(k3)); s32 a = diagram_find_node_by_key(dia, k1), b = diagram_find_node_by_key(dia, k2); if(a >= 0 && b >= 0) { s32 ek = k3[0] ? dsl_match_edge_sym(k3) : (s32)app->canvas_connect_kind; if(ek < 0) ek = EDGE_ASSOCIATION; diagram_push_undo(dia); diagram_add_edge(dia, a, b, (EdgeKind)ek, ""); app_message(app, "connected %s %s %s", k1, edge_kind_symbol((u32)ek), k2); } else { app_message(app, "node key not found (check Source view for keys)"); } } else if(!strcmp(text, "tpl") && arg) { diagram_push_undo(dia); if(diagram_load_template(dia, arg)) { app->active_node = app->active_edge = -1; for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true; app_message(app, "loaded template: %s (%u nodes, %u edges)", arg, dia->live_nodes, dia->live_edges); } else { app_message(app, "unknown template: %s", arg); } } else if(!strcmp(text, "scan") && arg) { diagram_push_undo(dia); u32 found = diagram_scan_cpp(dia, arg); app->active_node = app->active_edge = -1; for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true; app_message(app, "scanned %s -> %u nodes, %u edges", arg, found, dia->live_edges); } else if(!strcmp(text, "save")) { const char *path = (arg && arg[0]) ? arg : (dia->file_path[0] ? dia->file_path : "diagram.dit.txt"); if(diagram_save_file(dia, path)) app_message(app, "saved %s", path); else app_message(app, "failed to save %s", path); } else if(!strcmp(text, "help")) { app_open_help(app, arg); } else if(!strcmp(text, "open") && arg) { app_open_file(app, arg); } else if(!strcmp(text, "export") && arg) { if(diagram_export_svg(dia, arg, app->settings.colors)) app_message(app, "exported SVG -> %s", arg); else app_message(app, "failed to export %s", arg); } else if(!strcmp(text, "focus")) { dia->focus_mode = !dia->focus_mode; app_message(app, "focus mode: %s", dia->focus_mode ? "on" : "off"); } else if(!strcmp(text, "fit")) { View *cv = find_canvas_view(app); if(cv) canvas_fit_view(app, cv, false); } else if(!strcmp(text, "clear")) { diagram_push_undo(dia); diagram_clear(dia); app->active_node = app->active_edge = -1; app_message(app, "cleared diagram"); } else if(!strcmp(text, "columns")) { s32 v = panel_active_view(&app->layout, app->layout.focused); if(v >= 0) app->layout.views[v].column_count = 0; } else if(!strcmp(text, "reset")) { ui_layout_default(&app->layout); } else if(text[0]) { app_message(app, "not a command: %s", text); } } internal void draw_overlay(App *app, PlatformInput *input, Rect window) { if(app->overlay == UI_OVERLAY_NONE) return; if(app->overlay == UI_OVERLAY_EDIT && app->edit_inline && app->edit_drawn) return; DrawList *d = &app->draw; FontSystem *fonts = &app->fonts; Diagram *dia = app->diagram; ScratchArena scratch = ScratchGet(); CompletionList *completions = NULL; u32 shown = (app->overlay == UI_OVERLAY_SEARCH) ? Min(app->result_count, 12u) : 0; if(app->overlay == UI_OVERLAY_COMMAND) { completions = PushStruct(scratch.arena, CompletionList); u32 count = command_completions(app, scratch.arena, completions); app->result_cursor = (count == 0) ? 0 : Clamp(app->result_cursor, 0, (s32)count - 1); shown = Min(count, 12u); } f32 w = Min(600.0f, rect_w(window) - 40); f32 h = 40 + (f32)shown * UI_ROW_H + (shown ? 6 : 0); Rect box = {window.x0 + (rect_w(window) - w) * 0.5f, window.y0 + rect_h(window) * 0.16f, 0, 0}; box.x1 = box.x0 + w; box.y1 = box.y0 + h; draw_rect(d, window, 0x00000066u, 0); draw_shadow(d, box, 0x000000AAu, 0, 18); draw_rect(d, box, COLOR_TAB_ACTIVE, 0); draw_border(d, box, COLOR_FOCUS_EDGE, 0, 1); String8 label = str8("/"); if(app->overlay == UI_OVERLAY_COMMAND) label = str8(":"); else if(app->overlay == UI_OVERLAY_EDIT) label = str8("edit = "); f32 size = UI_FONT + 1; f32 base = ui_baseline(app, FONT_MONO, size, box.y0, 40); f32 lw = draw_text(d, fonts, FONT_MONO, size, box.x0 + 14, base, label, COLOR_TEXT_DIM); f32 tw = draw_text(d, fonts, FONT_MONO, size, box.x0 + 14 + lw, base, ToString8Size(app->input, app->input_size), COLOR_TEXT); draw_rect(d, Rect{box.x0 + 15 + lw + tw, box.y0 + 11, box.x0 + 23 + lw + tw, box.y0 + 29}, 0xFFFFFF99u, 0); f32 y = box.y0 + 40; if(completions) { u32 first = (app->result_cursor >= (s32)shown) ? (u32)app->result_cursor - shown + 1 : 0; for(u32 row = 0; row < shown; ++row) { u32 index = first + row; Completion *item = &completions->items[index]; Rect rect = {box.x0 + 4, y, box.x1 - 4, y + UI_ROW_H}; if((s32)index == app->result_cursor) draw_rect(d, rect, COLOR_SELECTED, 0); if(ui_clicked(input, rect)) command_accept_completion(app, item); f32 b = ui_baseline(app, FONT_MONO, UI_FONT, y, UI_ROW_H); f32 name = draw_text(d, fonts, FONT_MONO, UI_FONT, rect.x0 + 10, b, item->text, COLOR_TEXT); f32 dx = Max(name + 24, rect_w(rect) * 0.28f); draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, rect.x0 + dx, b, rect_w(rect) - dx - 10, item->detail, COLOR_TEXT_DIM); y += UI_ROW_H; } shown = 0; } for(u32 index = 0; index < shown; ++index) { SearchResult *r = &app->results[index]; Rect rect = {box.x0 + 4, y, box.x1 - 4, y + UI_ROW_H}; if((s32)index == app->result_cursor) draw_rect(d, rect, COLOR_SELECTED, 0); if(ui_clicked(input, rect)) { search_accept(app, *r); app->overlay = UI_OVERLAY_NONE; } String8 kind = str8("node"), name = str8(""); if(r->kind == 0) { kind = str8((char *)node_kind_name(dia->nodes[r->index].kind)); name = str8(dia->nodes[r->index].name); } else if(r->kind == 1) { kind = str8("edge"); name = ui_format(scratch.arena, "%s %s %s : %s", dia->nodes[dia->edges[r->index].src].name, edge_kind_symbol(dia->edges[r->index].kind), dia->nodes[dia->edges[r->index].dst].name, dia->edges[r->index].label); } else { kind = str8("member"); name = ui_format(scratch.arena, "%s.%s : %s", dia->nodes[r->parent_node].name, dia->members[r->index].name, dia->members[r->index].type); } f32 b = ui_baseline(app, FONT_REGULAR, UI_FONT, y, UI_ROW_H); draw_text(d, fonts, FONT_MONO, UI_FONT_SMALL, rect.x0 + 10, b, kind, COLOR_TEXT_FAINT); draw_text_fit(d, fonts, FONT_REGULAR, UI_FONT, rect.x0 + 86, b, rect_w(rect) - 96, name, COLOR_TEXT); y += UI_ROW_H; } scratch_end(&scratch); } //- keyboard (vim motions everywhere + canvas hotkeys) internal View *focused_view(App *app) { s32 view = panel_active_view(&app->layout, app->layout.focused); return view >= 0 ? &app->layout.views[view] : NULL; } internal void ui_keyboard_overlay(App *app, PlatformInput *input) { for(u32 index = 0; index < input->key_count; ++index) { PlatformKeyEvent *event = &input->keys[index]; u32 sym = event->keysym; b32 ctrl = (event->mods & PLATFORM_MOD_CTRL) != 0; if(sym == XKB_KEY_Escape || (ctrl && (sym == XKB_KEY_bracketleft || sym == XKB_KEY_c))) { app->overlay = UI_OVERLAY_NONE; return; } if(sym == XKB_KEY_Return || sym == XKB_KEY_KP_Enter) { UIOverlay ov = app->overlay; app->overlay = UI_OVERLAY_NONE; if(ov == UI_OVERLAY_SEARCH && app->result_count > 0) search_accept(app, app->results[app->result_cursor]); else if(ov == UI_OVERLAY_COMMAND) command_run(app); else if(ov == UI_OVERLAY_EDIT) edit_accept(app); return; } if(sym == XKB_KEY_BackSpace) { if(app->input_size == 0 && app->overlay != UI_OVERLAY_EDIT) { app->overlay = UI_OVERLAY_NONE; return; } while(app->input_size > 0) { u8 c = (u8)app->input[--app->input_size]; if((c & 0xC0) != 0x80) break; } } if(ctrl && sym == XKB_KEY_u) app->input_size = 0; if(ctrl && sym == XKB_KEY_w) { while(app->input_size > 0 && app->input[app->input_size - 1] == ' ') --app->input_size; while(app->input_size > 0 && app->input[app->input_size - 1] != ' ') --app->input_size; } if(app->overlay == UI_OVERLAY_COMMAND) { ScratchArena sc = ScratchGet(); CompletionList *list = PushStruct(sc.arena, CompletionList); u32 cnt = command_completions(app, sc.arena, list); if(sym == XKB_KEY_Tab && cnt) command_accept_completion(app, &list->items[Clamp(app->result_cursor, 0, (s32)cnt - 1)]); s32 step = 0; if(sym == XKB_KEY_Down || (ctrl && (sym == XKB_KEY_n || sym == XKB_KEY_j))) step = 1; if(sym == XKB_KEY_Up || sym == XKB_KEY_ISO_Left_Tab || (ctrl && (sym == XKB_KEY_p || sym == XKB_KEY_k))) step = -1; if(step && cnt) app->result_cursor = (app->result_cursor + step + (s32)cnt) % (s32)cnt; scratch_end(&sc); continue; } s32 move = 0; if(sym == XKB_KEY_Down || sym == XKB_KEY_Tab || (ctrl && (sym == XKB_KEY_n || sym == XKB_KEY_j))) move = 1; if(sym == XKB_KEY_Up || sym == XKB_KEY_ISO_Left_Tab || (ctrl && (sym == XKB_KEY_p || sym == XKB_KEY_k))) move = -1; if(move && app->result_count) app->result_cursor = (app->result_cursor + move + (s32)app->result_count) % (s32)app->result_count; } if(!app->overlay_opened && input->text_size > 0) { u32 take = Min((u32)sizeof(app->input) - 1 - app->input_size, input->text_size); MemCpy(app->input + app->input_size, input->text, take); app->input_size += take; if(app->overlay == UI_OVERLAY_COMMAND) app->result_cursor = 0; } if(app->overlay == UI_OVERLAY_SEARCH) search_update(app); } internal void delete_selected(App *app) { Diagram *dia = app->diagram; u32 del_n = 0, del_e = 0; diagram_push_undo(dia); for(u32 i = 0; i < dia->edge_count; ++i) { if(dia->edges[i].alive && (dia->edges[i].selected || (s32)i == app->active_edge)) { diagram_delete_edge(dia, (s32)i); ++del_e; } } for(u32 i = 0; i < dia->node_count; ++i) { if(dia->nodes[i].alive && (dia->nodes[i].selected || (s32)i == app->active_node)) { diagram_delete_node(dia, (s32)i); ++del_n; } } app->active_node = -1; app->active_edge = -1; diagram_rebuild_tags(dia); if(del_n || del_e) app_message(app, "deleted %u node(s), %u edge(s) (u to undo)", del_n, del_e); } // NOTE: the floating inspector takes the keys while it has focus and is on the focused canvas internal View *keyboard_view(App *app) { View *view = focused_view(app); if(app->card_focused) { if(app->card_visible && view && view->kind == VIEW_CANVAS) return &app->card_view; app->card_focused = false; } return view; } internal u32 keyboard_rows(App *app, View *view, Row *rows) { if(!view || view->kind == VIEW_CANVAS) return 0; return (view == &app->card_view) ? card_rows(app, rows, UI_MAX_ROWS) : view_rows(app, view, rows, UI_MAX_ROWS); } internal void ui_keyboard(App *app, PlatformInput *input) { if(app->overlay != UI_OVERLAY_NONE) { ui_keyboard_overlay(app, input); app->overlay_opened = false; return; } if(input->key_count == 0) return; Diagram *dia = app->diagram; View *view = keyboard_view(app); ScratchArena scratch = ScratchGet(); Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS); u32 count = keyboard_rows(app, view, rows); for(u32 index = 0; index < input->key_count && app->overlay == UI_OVERLAY_NONE; ++index) { PlatformKeyEvent *event = &input->keys[index]; u32 sym = event->keysym; b32 ctrl = (event->mods & PLATFORM_MOD_CTRL) != 0; b32 shift = (event->mods & PLATFORM_MOD_SHIFT) != 0; u32 pending = app->pending; u32 n = app->count ? app->count : 1; s32 cursor = view ? view->cursor : 0; s32 page = view ? Max((s32)view->visible_rows, 1) : 10; Row *row = (view && count) ? &rows[Clamp(cursor, 0, (s32)count - 1)] : NULL; if(sym == XKB_KEY_Shift_L || sym == XKB_KEY_Shift_R || sym == XKB_KEY_Control_L || sym == XKB_KEY_Control_R || sym == XKB_KEY_Alt_L || sym == XKB_KEY_Alt_R || sym == XKB_KEY_Super_L || sym == XKB_KEY_Super_R) continue; app->pending = 0; //- two-key commands if(pending) { Layout *layout = &app->layout; if(pending == 'g') { if(sym == XKB_KEY_g && view && view->kind != VIEW_CANVAS) view->cursor = 0; if(sym == XKB_KEY_t || sym == XKB_KEY_T) { Panel *p = &layout->panels[layout->focused]; if(p->tab_count) p->active_tab = (p->active_tab + (sym == XKB_KEY_t ? 1 : p->tab_count - 1)) % p->tab_count; } } else if(pending == 'd' && sym == XKB_KEY_d) { if(view && view->kind == VIEW_INSPECTOR && row && row->kind == ROW_MEMBER) { diagram_push_undo(dia); diagram_delete_member(dia, (s32)row->a); app_message(app, "deleted member"); } else if(view && view->kind == VIEW_INSPECTOR && row && row->kind == ROW_SECTION) { diagram_push_undo(dia); diagram_delete_section(dia, (s32)row->a); app_message(app, "deleted section"); } else { delete_selected(app); } } else if(pending == 'y' && sym == XKB_KEY_y) { if(app->active_node >= 0 && dia->nodes[app->active_node].alive) { diagram_push_undo(dia); s32 cp = diagram_duplicate_node(dia, app->active_node, 36.0f, 36.0f); if(cp >= 0) { diagram_clear_selection(dia); dia->nodes[cp].selected = true; app->active_node = cp; app_message(app, "duplicated %s", dia->nodes[cp].name); } } } else if(pending == 'w') { switch(sym) { case XKB_KEY_h: case XKB_KEY_Left: layout_focus_direction(layout, -1, 0); break; case XKB_KEY_l: case XKB_KEY_Right: layout_focus_direction(layout, 1, 0); break; case XKB_KEY_k: case XKB_KEY_Up: layout_focus_direction(layout, 0, -1); break; case XKB_KEY_j: case XKB_KEY_Down: layout_focus_direction(layout, 0, 1); break; case XKB_KEY_w: case XKB_KEY_W: layout_focus_next(layout); break; case XKB_KEY_v: layout_split_focused(layout, 0); break; case XKB_KEY_s: layout_split_focused(layout, 1); break; case XKB_KEY_c: case XKB_KEY_q: { Panel *p = &layout->panels[layout->focused]; if(p->tab_count) layout_close_view(layout, layout->focused, p->active_tab); } break; case XKB_KEY_equal: { for(s32 i = 0; i < UI_MAX_PANELS; ++i) if(layout->panels[i].kind == PANEL_SPLIT && layout->panels[i].ratio > 0) layout->panels[i].ratio = 0.5f; } break; } } app->count = 0; view = keyboard_view(app); count = keyboard_rows(app, view, rows); continue; } //- the manual, from anywhere if(sym == XKB_KEY_F1) { app_open_help(app, NULL); app->count = 0; view = keyboard_view(app); count = keyboard_rows(app, view, rows); continue; } //- numeric prefix if(!ctrl && ((sym >= XKB_KEY_1 && sym <= XKB_KEY_9) || (sym == XKB_KEY_0 && app->count))) { app->count = Min(app->count * 10 + (sym - XKB_KEY_0), 9999u); continue; } //- global ctrl shortcuts (zoom, panels, undo/redo) if(ctrl) { switch(sym) { case XKB_KEY_equal: case XKB_KEY_plus: app->settings.zoom += 0.1f; app_apply_settings(app); continue; case XKB_KEY_minus: app->settings.zoom -= 0.1f; app_apply_settings(app); continue; case XKB_KEY_0: app->settings.zoom = UI_ZOOM_DEFAULT; app_apply_settings(app); continue; case XKB_KEY_w: app->pending = 'w'; continue; case XKB_KEY_h: case XKB_KEY_Left: layout_focus_direction(&app->layout, -1, 0); break; case XKB_KEY_l: case XKB_KEY_Right: layout_focus_direction(&app->layout, 1, 0); break; case XKB_KEY_k: case XKB_KEY_Up: layout_focus_direction(&app->layout, 0, -1); break; case XKB_KEY_j: case XKB_KEY_Down: layout_focus_direction(&app->layout, 0, 1); break; case XKB_KEY_Tab: layout_cycle_views(&app->layout, 1); break; case XKB_KEY_ISO_Left_Tab: layout_cycle_views(&app->layout, -1); break; case XKB_KEY_z: if(diagram_undo(dia)) app_message(app, "undo"); break; case XKB_KEY_r: if(diagram_redo(dia)) app_message(app, "redo"); break; case XKB_KEY_d: if(view && view->kind != VIEW_CANVAS) view->cursor = Clamp(cursor + (s32)n * page / 2, 0, Max((s32)count - 1, 0)); break; case XKB_KEY_u: if(view && view->kind != VIEW_CANVAS) view->cursor = Clamp(cursor - (s32)n * page / 2, 0, Max((s32)count - 1, 0)); break; } app->count = 0; continue; } //- floating inspector: esc / tab give the keys back to the canvas if(view == &app->card_view) { b32 handled = true; if(sym == XKB_KEY_Escape || sym == XKB_KEY_Tab || sym == XKB_KEY_ISO_Left_Tab) { app->card_focused = false; } else if(sym == XKB_KEY_I) { app->settings.floating_inspector = false; app->card_focused = false; app_message(app, "floating inspector: off (I brings it back)"); } else if((sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) && row && row->kind == ROW_MEMBER) { diagram_push_undo(dia); diagram_delete_member(dia, (s32)row->a); app_message(app, "deleted member (u to undo)"); } else if((sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) && row && row->kind == ROW_SECTION) { diagram_push_undo(dia); diagram_delete_section(dia, (s32)row->a); app_message(app, "deleted section (u to undo)"); } else if(sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) { // NOTE: never delete the whole node from inside its own inspector } else if(row && row->kind == ROW_SECTION && (sym == XKB_KEY_h || sym == XKB_KEY_Left || sym == XKB_KEY_l || sym == XKB_KEY_Right)) { NodeSection *sec = &dia->sections[row->a]; sec->collapsed = (sym == XKB_KEY_h || sym == XKB_KEY_Left); diagram_measure_node(dia, sec->node); ++dia->version; } else { handled = false; } if(handled) { app->count = 0; view = keyboard_view(app); count = keyboard_rows(app, view, rows); continue; } } //- global non-ctrl shortcuts if(sym == XKB_KEY_colon) { app->overlay = UI_OVERLAY_COMMAND; app->overlay_opened = true; app->input_size = 0; app->result_cursor = 0; continue; } if(sym == XKB_KEY_slash || sym == XKB_KEY_question) { app->overlay = UI_OVERLAY_SEARCH; app->overlay_opened = true; app->input_size = 0; app->result_count = 0; app->result_cursor = 0; continue; } if(sym == XKB_KEY_u) { if(diagram_undo(dia)) app_message(app, "undo"); app->count = 0; continue; } if(sym == XKB_KEY_F) { dia->focus_mode = !dia->focus_mode; app_message(app, "focus mode: %s", dia->focus_mode ? "on" : "off"); app->count = 0; continue; } if(sym == XKB_KEY_Escape) { diagram_clear_selection(dia); app->active_node = app->active_edge = -1; app->message[0] = 0; app->count = 0; continue; } if(sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) { delete_selected(app); app->count = 0; continue; } if(sym == XKB_KEY_q) { app->quit = true; return; } //- Canvas-focused keyboard actions if(view && view->kind == VIEW_CANVAS) { f32 pan = 80.0f / Max(view->cam_zoom, 0.1f) * (f32)n; f32 nud = (f32)Max(ui_theme.grid_size, 8u) * (f32)n; switch(sym) { case XKB_KEY_h: case XKB_KEY_Left: view->cam_target_x -= pan; break; case XKB_KEY_l: case XKB_KEY_Right: view->cam_target_x += pan; break; case XKB_KEY_k: case XKB_KEY_Up: view->cam_target_y -= pan; break; case XKB_KEY_j: case XKB_KEY_Down: view->cam_target_y += pan; break; case XKB_KEY_H: if(app->active_node >= 0) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, -nud, 0); diagram_fit_containers(dia); } break; case XKB_KEY_L: { if(shift && app->active_node >= 0 && diagram_selected_node_count(dia) > 0 && app->count) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, nud, 0); diagram_fit_containers(dia); } else { diagram_push_undo(dia); diagram_layout_layered(dia, false); canvas_fit_view(app, view, false); app_message(app, "layered layout (%.2f ms)", dia->layout_ms); } } break; case XKB_KEY_K: if(app->active_node >= 0) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, 0, -nud); diagram_fit_containers(dia); } break; case XKB_KEY_J: if(app->active_node >= 0) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, 0, nud); diagram_fit_containers(dia); } break; case XKB_KEY_plus: case XKB_KEY_equal: case XKB_KEY_KP_Add: view->cam_target_zoom = Clamp(view->cam_target_zoom * 1.2f, 0.08f, 4.0f); break; case XKB_KEY_minus: case XKB_KEY_KP_Subtract: view->cam_target_zoom = Clamp(view->cam_target_zoom / 1.2f, 0.08f, 4.0f); break; case XKB_KEY_0: view->cam_target_zoom = 1.0f; break; case XKB_KEY_f: canvas_fit_view(app, view, false); break; case XKB_KEY_Z: canvas_fit_view(app, view, true); break; case XKB_KEY_g: app->pending = 'g'; continue; case XKB_KEY_G: app->settings.grid_style = (app->settings.grid_style + 1) % GRID_STYLE_COUNT; app_message(app, "grid: %s", ui_grid_names[app->settings.grid_style]); break; case XKB_KEY_S: app->settings.snap_to_grid = !app->settings.snap_to_grid; app_message(app, "snap: %s", app->settings.snap_to_grid ? "on" : "off"); break; case XKB_KEY_m: app->settings.show_minimap = !app->settings.show_minimap; break; case XKB_KEY_r: dia->default_routing = (dia->default_routing - 1 + 1) % 3 + 1; app->settings.default_routing = dia->default_routing; app_message(app, "routing: %s", edge_routing_name(dia->default_routing)); break; case XKB_KEY_e: app->canvas_connect_kind = (app->canvas_connect_kind + 1) % EDGE_KIND_COUNT; app_message(app, "edge tool: %s (%s) — drag a port or right-drag a node", edge_kind_name(app->canvas_connect_kind), edge_kind_symbol(app->canvas_connect_kind)); break; case XKB_KEY_a: { diagram_push_undo(dia); s32 ni = diagram_add_node(dia, NODE_STRUCT, "NewStruct", app->canvas_last_wx - 100.0f, app->canvas_last_wy - 36.0f); if(ni >= 0) { s32 s = diagram_add_section(dia, ni, "fields"); diagram_add_member(dia, s, '+', "field", "u32", ""); diagram_clear_selection(dia); dia->nodes[ni].selected = true; app->active_node = ni; app->active_edge = -1; start_edit(app, EDIT_NODE_NAME, ni, dia->nodes[ni].name); } } break; case XKB_KEY_c: if(app->active_node >= 0 && dia->nodes[app->active_node].alive) { diagram_push_undo(dia); dia->nodes[app->active_node].collapsed = !dia->nodes[app->active_node].collapsed; diagram_measure_node(dia, app->active_node); } break; case XKB_KEY_I: app->settings.floating_inspector = !app->settings.floating_inspector; app_message(app, "floating inspector: %s", app->settings.floating_inspector ? "on" : "off"); break; case XKB_KEY_Return: case XKB_KEY_KP_Enter: case XKB_KEY_i: // enter steps into the floating inspector when it is up, i always renames if(sym != XKB_KEY_i && app->card_visible) { app->card_focused = true; break; } if(app->active_node >= 0 && dia->nodes[app->active_node].alive) start_edit(app, EDIT_NODE_NAME, app->active_node, dia->nodes[app->active_node].name); else if(app->active_edge >= 0 && dia->edges[app->active_edge].alive) start_edit(app, EDIT_EDGE_LABEL, app->active_edge, dia->edges[app->active_edge].label); break; case XKB_KEY_d: app->pending = 'd'; continue; case XKB_KEY_y: app->pending = 'y'; continue; case XKB_KEY_Tab: { if(dia->live_nodes > 0) { s32 start = (app->active_node >= 0) ? app->active_node : -1; for(u32 step = 1; step <= dia->node_count; ++step) { u32 idx = (u32)(start + (s32)step) % dia->node_count; if(dia->nodes[idx].alive && !diagram_node_hidden(dia, (s32)idx)) { diagram_clear_selection(dia); dia->nodes[idx].selected = true; app->active_node = (s32)idx; app->active_edge = -1; canvas_pan_to_node(app, view, (s32)idx); break; } } } } break; } app->count = 0; continue; } //- Table-focused vim motions s32 motion = INT32_MIN; switch(sym) { case XKB_KEY_j: case XKB_KEY_Down: motion = cursor + (s32)n; break; case XKB_KEY_k: case XKB_KEY_Up: motion = cursor - (s32)n; break; case XKB_KEY_G: case XKB_KEY_End: motion = app->count ? (s32)app->count - 1 : (s32)count - 1; break; case XKB_KEY_Home: motion = 0; break; case XKB_KEY_Page_Down: motion = cursor + page; break; case XKB_KEY_Page_Up: motion = cursor - page; break; case XKB_KEY_g: case XKB_KEY_d: case XKB_KEY_y: app->pending = (u32)('a' + (sym - XKB_KEY_a)); continue; case XKB_KEY_l: case XKB_KEY_Right: if(row && row->expandable && row->kind == ROW_NODE) view_set_expanded(view, row->a, true); else if(view && row) view_adjust(app, view, row, 1); break; case XKB_KEY_h: case XKB_KEY_Left: if(row && row->expandable && row->kind == ROW_NODE) view_set_expanded(view, row->a, false); else if(view && row) view_adjust(app, view, row, -1); break; case XKB_KEY_Return: case XKB_KEY_KP_Enter: case XKB_KEY_o: case XKB_KEY_space: if(view && row) view_activate(app, view, row); break; case XKB_KEY_a: if(view && view->kind == VIEW_INSPECTOR && app->active_node >= 0 && dia->nodes[app->active_node].alive) { diagram_push_undo(dia); s32 sec = -1; if(row && row->kind == ROW_SECTION) sec = (s32)row->a; else if(row && row->kind == ROW_MEMBER) sec = (s32)row->b; else sec = dia->nodes[app->active_node].last_section; if(sec < 0) sec = diagram_add_section(dia, app->active_node, "fields"); s32 m = diagram_add_member(dia, sec, '+', "new_field", "u32", ""); if(m >= 0) start_edit(app, EDIT_MEMBER_LINE, m, "+ new_field : u32"); } break; case XKB_KEY_A: if(view && view->kind == VIEW_INSPECTOR && app->active_node >= 0 && dia->nodes[app->active_node].alive) { diagram_push_undo(dia); s32 sec = diagram_add_section(dia, app->active_node, "methods"); if(sec >= 0) start_edit(app, EDIT_SECTION_TITLE, sec, "methods"); } break; case XKB_KEY_Tab: layout_focus_next(&app->layout); break; } if(motion != INT32_MIN && view && count) { view->cursor = Clamp(motion, 0, (s32)count - 1); f32 y = (f32)view->cursor * UI_ROW_H; f32 vh = (f32)view->visible_rows * UI_ROW_H; if(y < view->scroll.target) view->scroll.target = y; if(y + UI_ROW_H > view->scroll.target + vh) view->scroll.target = y + UI_ROW_H - vh; if(rows[view->cursor].kind == ROW_NODE) { diagram_clear_selection(dia); dia->nodes[rows[view->cursor].a].selected = true; app->active_node = (s32)rows[view->cursor].a; app->active_edge = -1; } else if(rows[view->cursor].kind == ROW_EDGE) { diagram_clear_selection(dia); dia->edges[rows[view->cursor].a].selected = true; app->active_edge = (s32)rows[view->cursor].a; app->active_node = -1; } } app->count = 0; view = keyboard_view(app); count = keyboard_rows(app, view, rows); } scratch_end(&scratch); } //- frame internal String8 app_font_file(App *app, u32 choice) { if(choice >= UI_FONT_CHOICES) return str8_zero(); if(app->font_files[choice].size == 0 && font_path_readable(font_choice(choice)->path)) { app->font_files[choice] = font_load_path(app->arena, font_choice(choice)->path); } return app->font_files[choice]; } internal void ui_frame(App *app, PlatformInput *input_raw) { app->animating = false; app->ticking = false; app->table_cursor = PLATFORM_CURSOR_DEFAULT; app->canvas_cursor = PLATFORM_CURSOR_DEFAULT; app->canvas_hover_node = -1; app->canvas_hover_edge = -1; ui_settings = &app->settings; ui_theme = app->settings; font_set_data(&app->fonts, FONT_REGULAR, app_font_file(app, app->settings.font_ui)); font_set_data(&app->fonts, FONT_SEMIBOLD, app_font_file(app, app->settings.font_bold)); font_set_data(&app->fonts, FONT_MONO, app_font_file(app, app->settings.font_mono)); f32 zoom = (app->settings.zoom > 0) ? app->settings.zoom : 1.0f; if(app->ui_scale > 0.25f && app->ui_scale < 4.0f) zoom *= app->ui_scale; font_set_scale(&app->fonts, (f32)app->scale * zoom); PlatformInput input = *input_raw; input.mouse_x /= zoom; input.mouse_y /= zoom; if(!layout_valid(&app->layout)) ui_layout_default(&app->layout); app_handle_drops(app, &input); u64 t_keys = time_now_ns(); ui_keyboard(app, &input); if(app->quit) return; u64 t_keys_end = time_now_ns(); f32 w = (f32)app->width / zoom; f32 h = (f32)app->height / zoom; Rect window = {0, 0, w, h}; draw_begin(&app->draw, app->renderer.extent.width, app->renderer.extent.height, (f32)app->scale * zoom); app->draw.rounding = 1.0f; app->draw.text_gamma = app->settings.text_gamma; if(PlatformWindowIsLayer(app->window)) draw_rect(&app->draw, window, COLOR_BG, 0); Rect panels = {UI_GAP, UI_GAP, w - UI_GAP, h - UI_GAP}; layout_compute(&app->layout, app->layout.root, panels); app->card_visible = false; app->edit_drawn = false; u64 t_panels = time_now_ns(); draw_panels(app, &input); u64 t_overlay = time_now_ns(); draw_overlay(app, &input, window); u64 t_end = time_now_ns(); if(app->profiler) { u64 spans[PROFILE_UI_PHASES] = {t_keys_end - t_keys, t_overlay - t_panels, 0, t_end - t_overlay}; for(u32 phase = 0; phase < PROFILE_UI_PHASES; ++phase) { f32 *us = &app->profiler->ui_phase_us[phase]; *us += ((f32)spans[phase] / 1000.0f - *us) * 0.05f; } } app->pan_to_active = false; if(app->profiler && app->profiler->sampling) app->ticking = true; }