3. The model: immediate mode#
App::frameruns for every pass and describes the whole UI by calling widget methods onui. Nothing is retained between frames except your own data and small per-id state inside the library (animations, scroll positions, measurements).Every widget returns
EV_*flags:ui.button(id, "OK") & EV_ACT != 0= clicked / changed / selected. Others:EV_COMMIT(value committed: drag released, Enter, focus lost),EV_HOVER,EV_HELD,EV_FOCUS,EV_DOUBLE,EV_RCLICK,EV_OPEN,EV_DRAG.Value widgets edit your variables through
&mut:ui.checkbox(id, "Label", &mut self.flag). Booleans of the API arei32(0 / 1, checkboxes also 2 = indeterminate), as in the assembly.After an action the library builds the frame once more, so everything shows the new state. Keep
framefree of side effects other than reacting toEV_*flags.Heavy or structural work (file I/O, opening dialogs, big edits) goes through
ui.cmd_run(cmd, arg); it reachesApp::command(ui, id, arg)between frames, in the order queued (the queue grows as needed; only beyond 65536 waiting commands does it drop new ones, with aUIWARN). After the commands the frame is built again (up to three rounds of frame and commands in one go; when the third round's commands changed something, the next frame follows at once). Commands queued by every frame, orui.invalidate()in everyApp::frame, would ask for frames without end: after 8 such frames in a row without an event they wait for the next event, with aUIWARN. Returntruewhen you handled it. The library also delivers:Id When UIC_CLOSEthe user closes the main window; return trueto keep it (ask "save changes?" first)UIC_TIMERarg= timer id ofui.timer(id, ms)(one-shot; library timers arrive too: ignore unknown ids)UIC_ESCAPEEsc that no widget consumed UIC_DPI,UIC_THEMEthe dpi / the theme changed UIC_WINCLOSEarg= window id: the user closes a secondary window;truekeeps itUIC_DROParg= window id: files (or text, seedrop_accept) were dropped outside every drop target (ui.drop_list(),ui.drop_data(),ui.drop_pos())UIC_DRAG_ENDarg=DE_*effect (0 = cancelled / refused): a drag of the app ended (section 7, drag and drop)UIC_WAKEplat::wake()from another thread (a background job finished)UIC_EXITthe window loop ends (only after a real window ran: save settings here) UIC_ACTIVEarg1: one of the app's windows got the keyboard, 0: none has it (pause animations, poll less)UIC_SUSPENDarg1: the system goes to sleep (save, close connections), 2: the app went to the background -- the browser hid the tab (visibilitychange), Android stopped the activity (onStop); the app may be ended without another word, so save here too --, 0: it woke up / is visible again; to keep it awake seeplat::inhibit(section 21)UIC_SESSION_ENDlogout, shutdown or SIGTERM: save now; UIC_EXITand the end follow. Windows: only a confirmed end (WM_ENDSESSION; a logout another app cancels sends nothing), taken while the message is handled -- Windows may end the process right after itUIC_SETTINGSarg=plat::SET_THEME | SET_LOCALE | SET_CONTRAST | SET_FONTS | SET_MONITORS: system settings changed; a system theme and the format (i18n::locale()) are already updated;SET_MONITORS: a monitor came, went, moved or changed its scale or work area (ui.monitors(), section 21)UIC_HOTKEY,UIC_HOTKEY_LOSTarg= id: a global shortcut was pressed anywhere / the desktop dropped it (ui.hotkey_error(id); section 21)UIC_PICKarg1: the user picked a colour on the screen (ui.picked_color()), 0: cancelled or failed (ui.screen_error())UIC_SCREENSHOTarg1: the picture ofui.screenshot(..)is there (ui.screenshot_take()), 0: failedUIC_RECENTarg1: the fileui.recent_open(..)asked the browser about is readable now (ui.recent_reopened()names it), 0: refused or gone (section 21)App::init(ui)runs before the first frame (register codecs, read arguments, load settings);falseends the program with exit code 2.Ids are plain
u32constants, unique among the widgets of a frame (duplicate ids break hover / press tracking). Repeated items:BASE + i. Bits 28..31 are reserved for the library (part_id, split-button chevrons). A box id may be 0 (automatic). Some widgets also take the ids after their own: a segmented control, tabs, wizard steps and breadcrumbs one per item (id + 1 + i), an infobarid + 1(its close button), list and grid rowsid + 1 + i-- leave that room before the next widget's id.Ids from names spare the bookkeeping:
ui.id("save")hashes a name,ui.id_n(i)a number, both inside the current scope;ui.id_scope(i, |ui| ...)(orid_push(i)/id_push_str(name)...id_pop()) gives every row of a loop its own scope, so the same names can repeat.ui::wid("save")andui::wid_path(&["layers", "add"])are the same values asconsts (window ids,#idselectors in scripts). Hashed ids start atID_HASH_BASE(0x0800_0000): keep hand-written ids below it and both kinds mix. Scripts andjfgui_DEBUG=1warn when two names hash alike, when one id is used twice (naming it) and when a scope is still open at the end of the frame.for (i, item) in self.items.iter().enumerate() { let del = ui.id_scope(i as u32, |ui| { ui.text(TS_BODY, &item.name); ui.button(ui.id("del"), "Löschen") & EV_ACT != 0 }); if del { /* remove item i after the loop */ } }