JFLib

3. The model: immediate mode#

  • App::frame runs for every pass and describes the whole UI by calling widget methods on ui. 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 are i32 (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 frame free of side effects other than reacting to EV_* flags.

  • Heavy or structural work (file I/O, opening dialogs, big edits) goes through ui.cmd_run(cmd, arg); it reaches App::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 a UIWARN). 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, or ui.invalidate() in every App::frame, would ask for frames without end: after 8 such frames in a row without an event they wait for the next event, with a UIWARN. Return true when you handled it. The library also delivers:

    IdWhen
    UIC_CLOSEthe user closes the main window; return true to keep it (ask "save changes?" first)
    UIC_TIMERarg = timer id of ui.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; true keeps it
    UIC_DROParg = window id: files (or text, see drop_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_ACTIVEarg 1: one of the app's windows got the keyboard, 0: none has it (pause animations, poll less)
    UIC_SUSPENDarg 1: 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 see plat::inhibit (section 21)
    UIC_SESSION_ENDlogout, shutdown or SIGTERM: save now; UIC_EXIT and 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 it
    UIC_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_PICKarg 1: the user picked a colour on the screen (ui.picked_color()), 0: cancelled or failed (ui.screen_error())
    UIC_SCREENSHOTarg 1: the picture of ui.screenshot(..) is there (ui.screenshot_take()), 0: failed
    UIC_RECENTarg 1: the file ui.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); false ends the program with exit code 2.

  • Ids are plain u32 constants, 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 infobar id + 1 (its close button), list and grid rows id + 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| ...) (or id_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") and ui::wid_path(&["layers", "add"]) are the same values as consts (window ids, #id selectors in scripts). Hashed ids start at ID_HASH_BASE (0x0800_0000): keep hand-written ids below it and both kinds mix. Scripts and jfgui_DEBUG=1 warn 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 */ }
    }

Source: docs/GUIDE.md in the JFLib repository.