--- name: desktop-ui description: Build desktop UIs with immediate-mode GUI (IMGUI) — Dear ImGui patterns, retained vs immediate mode tradeoffs, widget composition, layout, rendering integration. Use when building native desktop applications. license: MIT --- ## What I do Guide desktop UI development using immediate-mode GUI (IMGUI) principles. ## Core Philosophy ### UI as Communication Barrier UI minimizes bits sent to useful bits received. Users already know standard widgets: - Button: Trigger event - Checkbox: Toggle boolean - Slider: Change numeric value - Text Field: String input - Scroll Bar: Navigate large content ### Two-Layer Architecture ``` ┌─────────────────────────────────┐ │ BUILDER CODE (App-Specific) │ │ - Arranges widgets │ │ - High change frequency │ └─────────────────────────────────┘ ↓ ┌─────────────────────────────────┐ │ CORE CODE (UI Framework) │ │ - Common codepaths │ │ - Layout, rendering, input │ └─────────────────────────────────┘ ``` --- ## Immediate Mode (Every Frame) ### API Pattern ```c // Widget rebuilt every frame - no manual state if (UI_Button("Save")) { /* clicked */ } // Conditional - no explicit show/hide UI_Checkbox(&show_panel, "Show"); if (show_panel) { UI_Button("Option A"); UI_Button("Option B"); } ``` ### Layout ```c // Parent stack approach UI_PushParent(parent); UI_Button("Foo"); UI_Button("Bar"); UI_PopParent(); ``` ### Offline Autolayout 1. **Build**: Construct widget hierarchy 2. **Autolayout**: Compute sizes 3. **Render**: Draw with computed positions ### Semantic Sizing ```c enum UI_SizeKind { UI_SizeKind_Pixels, // Direct value UI_SizeKind_TextContent, // Fit text UI_SizeKind_PercentOfParent, UI_SizeKind_ChildrenSum, }; ``` ### Keying (Widget Identity) ```c UI_Button("Button##%d", i); // Hash but don't display "##i" UI_Button("###Label"); // Hash after "###", display before ``` --- ## Feature Flags Over Types Don't use widget "types". Use feature flags: ```c enum { UI_WidgetFlag_Clickable = (1 << 0), UI_WidgetFlag_DrawText = (1 << 1), UI_WidgetFlag_DrawBorder = (1 << 2), UI_WidgetFlag_DrawBackground = (1 << 3), UI_WidgetFlag_HotAnimation = (1 << 4), UI_WidgetFlag_ActiveAnimation= (1 << 5), }; UI_Comm UI_Button(String8 string) { return UI_CommFromWidget(UI_WidgetMake( UI_WidgetFlag_Clickable | UI_WidgetFlag_DrawText | UI_WidgetFlag_DrawBorder | UI_WidgetFlag_DrawBackground | UI_WidgetFlag_HotAnimation | UI_WidgetFlag_ActiveAnimation, string )); } ``` **Why**: 20 features = 2^20 combinations. Flags = N codepaths, not 2^N. --- ## Input Handling ### UI_Comm Struct ```c struct UI_Comm { UI_Widget *widget; Vec2F32 mouse; B8 clicked; B8 double_clicked; B8 pressed; B8 released; B8 hovering; }; ``` ### Hotkey Pattern ```c UI_Comm btn = UI_Button("Save"); if (btn.clicked || Input_HotkeyPressed(HOTKEY_SAVE)) { SaveDocument(); } ``` ### Keyboard Navigation - Tab/Shift+Tab: Cycle focus - Arrow keys: Directional navigation - Gamepad: Same system (D-pad nav, A = click, B = back) --- ## Text Input ### Terminology - **Point**: Position between characters (0 = before first char) - **Cursor**: Point that changes with input - **Selection**: Contiguous range of points ### Shortcuts - Left/Right: Move 1 char - Ctrl+Left/Right: Word navigation - Ctrl+A: Select all - Backspace: Delete before cursor - Ctrl+Backspace: Delete word left - Home/End: Line start/end --- ## State Management ### Widget Caching ```c struct UI_Widget { UI_Widget *parent, *first, *last, *next, *prev; UI_Key key; F32 computed_size[Axis2_COUNT]; Rng2F32 rect; F32 hot_t, active_t; // Animation state }; ``` ### Mutation Rules - All state changes during widget tree construction (immediate mode) - Not during rendering - Homogeneous mutations: user click = hotkey = script --- ## Rendering ### GPU Batching - Batch similar draw calls - Use signed distance fields for text - Target 60+ FPS --- ## Quick Checklist - [ ] Two-layer: Core + Builder code - [ ] Immediate mode (rebuild every frame) - [ ] Feature flags, not widget types - [ ] Parent stack for hierarchy - [ ] Semantic sizing - [ ] Hash-based keying - [ ] Limit mutation to build phase - [ ] Hot/active animation states --- Use this skill when building desktop UIs with IMGUI principles.