Context: input, focus, timers, commands, semantics#
Source: lib/ui/ctx.rs
ctx -- UI context: input state, passes, hit testing, hot / active / focus, state slots and
animations, deferred commands and timers, semantic records, clip, layers, modal,
disabled regions, warnings [src/ui/ui_ctx.s]
App-facing:
now() -> ms milliseconds (virtual clock in headless runs)
key(vk) -> bool vk pressed this frame and not yet consumed (consumes it)
key_peek(vk) / key_down(vk) the same without consuming / while the key is held
mods() -> MOD_* mouse() -> (x, y) px (-1 outside) pen() -> (pressure, PTR_*)
focus(id) / focused() -> id give / ask the keyboard focus
cmd_run(id, arg) queue a command for App::command (runs after the frame)
timer(id, ms) one-shot timer -> App::command(UIC_TIMER, id)
anim(id, target, ms) -> value animated value 0..256 towards target
warn(msg) "UIWARN f<frame>: ..." on stderr; scripts and jfgui_DEBUG
also warn once about an id two widgets of one pass share
(compound widgets take id + 1 .. or (id << 3 | k) parts)
Widget building blocks:
behave(id, hbflags) -> EV_* hit box on last_rect + pointer / keyboard behaviour
hit(id, hbflags) hit box only
hit_rect(id, x, y, w, h, hb) behave on an explicit rectangle (px)
slot(id) -> &mut Slot persistent per-id state record (per window)
sem(kind, id, label, value) semantic record (state from the last behave)
clip_push(x, y, w, h) / clip_pop() clip drawing and hit boxes (px)
layer_set(l) -> previous modal(layer) disabled_push() / disabled_pop()
Runtime (runtime.rs, win.rs): frame_begin / frame_end (a pass over all windows),
uiw_pass_begin / uiw_pass_end (one window), input_event, input_clear_edges, hot_update.
struct KeyEvent(E2) a key as it went down or up, for a canvas that passes the keyboard on (a web page's keydown / keyup events): ui.key_event_takevk: u32-- VK_*mods: u32-- MOD_* at the timedown: bool-- down (true) or uprepeat: bool-- a down of the system's auto-repeat while the key is heldscancode: u32-- the platform's code of the key's place on the keyboard, whatever the layout: Windows' scan code (0xE0.. for extended keys), Linux's evdev code, (G7a) the USB HID usage on macOS, iOS and in the browser; 0 where the platform does not tell (Android)location: u32-- KL_STANDARD, KL_LEFT / KL_RIGHT (the two Shift, Ctrl, Alt, Windows keys), KL_NUMPADch: u32-- the character the keyboard layout gives the key with Shift, Caps Lock and AltGr as they are but without Ctrl, Alt and the Windows / Super key (0: none) -- what a browser names the key (KeyboardEvent.key): "z" for Ctrl+Z on a German keyboard, "+" for its Ctrl++, "@" for AltGr+Q, "Z" for Ctrl+Shift+Z. Keys that type no character (Enter, Tab, Esc, Backspace, the arrows, F1 ...) have none. Windows (10 1607 and later: ToUnicodeEx), Linux (the keymap), macOS (charactersIgnoringModifiers); 0 in the browser and on Android; a headless run's keys have the US layout's (scripts:keydown ... char "c")dead: bool-- the key is a dead key (´ ^ ~ ...): ch is its spacing character, the text it makes comes with the next keycode: u32-- (G7a) where the key sits as a USB HID usage, the same on every platform and layout (plat::KC_*: KC_W is the key right of Q on a US keyboard, Z on a French one); 0 where the platform does not tell (Android). plat::key_label(code) says what the layout prints on it
struct Semsemantic record (dump / expect) [SR_*]id: u32kind: u16state: u16-- SS_*rect: [i32; 4]-- x, y, w, h physical pxlabel: Option<String>value: i32max: i32min: i32-- the lower end of a slider's range (accessibility)layer: u8depth: u8win: u8-- window index (0 = the main window)ax: Option<alloc::boxed::Box<SemAx>>-- what the record says beyond kind, label and value (its own 64-bit id, a parent, a description, cells, states ...): set through the record's setters (id64, parent, desc ...; section 19 of the guide); None for the records of the built-in widgets
struct SemAxWhat a semantic record says beyond its kind, label and value (Sem::ax): its setters fill it (ui.sem_rect(..).id64(7).parent(3).desc("..")); a web page's accessibility tree needs it.id: u64-- the record's own id (below 2^62): a screen reader's requests for it come back with it (ui.a11y_request_take); 0: noneparent: u64-- the record it hangs under in the accessibility tree: one with this id64 (or, without one, this widget id) published in the same frame and window; 0: by its rectanglename: Option<String>-- its name when the label is something else (a text box's or drop-down's label is its text)desc: Option<String>-- a description beside the name (a hint, a page's aria-describedby)url: Option<String>-- a link's addressvalue_text: Option<String>-- the text a screen reader says for a value (a slider's "Monday", a page's aria-valuetext)states: u32-- AS_*set: [u32; 2]-- its position in its set (1-based) and the set's size (0: unknown)level: u32-- the level of a heading, tree item, list item or row (1 the top; 0: none / the value)cell: [u32; 4]-- a cell's row, column (0-based), row span, column span (spans 0: no place)grid: [u32; 2]-- a table's rows and columns (0: unknown)live: u32-- LIVE_* (0: as its kind says)range: Option<[f64; 4]>-- value, minimum, maximum, step with fractions (a meter at 0.4 of 1); None: Sem::value / min / maxsel: Option<[u32; 2]>-- a focused text box's selection: anchor, caret in characters (a page's field; the built-in boxes report theirs themselves)
struct Slotpersistent per-id state (slot) [SL_*]seen: u32afrom: u16ato: u16at0: u32adur: u32measw: i32meash: i32fixed: i32wsum: i32scrollx: i32scrolly: i32aux: i32
fn ease(curve: u32, t: i32) -> i32ease(curve, t) -> the eased progress: t and the result in 0..256 (EASE_OUT_BACK overshoots above 256 on the way)
impl Ui
fn now(&self) -> u32ui_now: milliseconds since start (virtual clock in headless runs)fn mods(&self) -> u32ui_mods: MOD_* bits of the current inputfn mouse(&self) -> (i32, i32)ui_mouse: pointer position in px (-1 when outside)fn pen(&self) -> (u32, u32)ui_pen: (pressure 0 = none / 1..UI_PRESSURE_ONE, PTR_*) of the last pointer eventfn pointer_lock(&mut self, on: bool) -> plat::PointerLock(G7a) lock the pointer to this window (on) or let it go (off) -> the state right after the call (plat::win_pointer_lock): Locked, Pending while the platform waits (the window has no focus yet, the browser waits for the next press in the page, Wayland for the compositor), Unsupported (Android, iOS, a display without the means), Refused. While locked the pointer is hidden and stays put, ui.pointer_motion has its raw motion, a change of the state gives a pass. Esc, the window losing the focus, minimizing and closing it always let it go (Esc then reaches the app as no key); it is never taken back by itself. A headless run (scripts) plays the lock: Locked at once, Esc lets it go,motion DX DYmoves itfn pointer_lock_state(&self) -> plat::PointerLock(G7a) the state of this window's pointer lock (Off when another window holds it)fn pointer_motion(&self) -> (f64, f64)(G7a) the raw motion of the locked pointer since the last pass of this window: (dx, dy), right and down positive, in the device's counts where the platform gives them (Windows, Wayland, X11, the browser with unadjustedMovement), else in its accelerated pixels (macOS, the browser without it); (0, 0) while the pointer is not locked. A game turns its camera by it times a sensitivityfn key_code_down(&self, code: u32) -> bool(G7a) the key at USB HID usagecode(plat::KC_*: where it sits, whatever the layout) is held; KeyEvent::code of its down and upfn focus(&mut self, id: u32)ui_focus(id): give the keyboard focus to a widget (focus ring shown). The id is looked up in whichever window emits it in the next pass (that window is activated).fn focus_quiet(&mut self, id: u32)the keyboard focus to a widget of this window without the focus ring: the keyboard goes back to it after a pointer gesture (a ring only follows keyboard navigation)fn focused(&self) -> u32ui_focused: id of the focused widget (0 = none)fn key(&mut self, vk: u32) -> boolui_key(vk): pressed this frame and not yet consumed (consumes it)fn capture_shortcut(&mut self, vk: u32, mods: u32) -> boolCapture an exact application shortcut before text widgets handle it. Unlike key(), this bypasses the focused textbox's key reservation. Use only for an editing surface whose history/commands the application owns, and not while a modal draft dialog owns input. Other shortcuts and the normal key() behavior are unchanged.fn key_peek(&mut self, vk: u32) -> boolui_key_peek(vk): pressed this frame and unconsumed (not consumed)fn key_down(&self, vk: u32) -> boolui_key_down(vk): held (mods: VK_SHIFT / VK_CONTROL / VK_MENU)fn key_first(&self) -> u32ui_key_first: the first unconsumed key this frame (VK | mods << 16) or 0fn key_take(&mut self) -> u32key_take -> the first unconsumed key this frame (VK | mods << 16), consumed; 0 when none is left. A canvas with HB_KEYS takes every key with it while it has the focus (\ev & EV_FOCUS\\, call it until 0); the keys a focused text box keeps stay with the box, and while a popup of the window is open (a menu, a flyout) the keys are its.fn key_event_take(&mut self) -> Option<KeyEvent>(E2) the next key as it went down or up (KeyEvent: the key, modifiers, down / up, auto- repeat, scancode, left / right / number block), consumed; None when none is left. For a canvas with HB_KEYS that passes the keyboard on as it is (a web page's keydown and keyup): call it while the canvas has the focus (ev & EV_FOCUS) until None, instead of key_take (a down taken here is taken for key_take too, and one key_take took is not here). The typed text comes through text_take as before. None while a popup of the window is openfn text_take(&mut self, cap: usize) -> Vec<u32>ui_text_take(cap): the typed codepoints (at most cap), consumed; when cap leaves some, they come in the next pass (nothing typed is lost)fn cursor_at(&self, x: i32, y: i32) -> u32(E3) the cursor shape at a point (px) of the current window in the previous pass: the topmost hit box's (CUR_*: ui.cursor_next's, else the shape of its flags; CUR_CUSTOM for the custom cursor), CUR_ARROW where there is nonefn hit_flags_at(&mut self, x: i32, y: i32) -> u32ui_hit_flags_at(x, y): HB_* flags of the topmost hit box at a point of the main window (previous pass) -- non-client hit testing of the platformfn hit(&mut self, id: u32, hbflags: u32)ui_hit(id, hbflags): register last_rect (clipped) as a hit box of the current layerfn behave(&mut self, id: u32, hbflags: u32) -> u32ui_behave(id, hbflags) -> EV_*: the standard pointer / keyboard behaviour of a widget occupying last_rectfn hit_rect(&mut self, id: u32, x: i32, y: i32, w: i32, h: i32, hbflags: u32) -> u32ui_hit_rect(id, x, y, w, h, hbflags) -> EV_*: behave on an explicit rectangle (px)fn focus_ring(&self, ev: u32) -> boolui_focus_ring(ev): the focus visual should be drawn for the widget that just called behave (its events)fn slot(&mut self, id: u32) -> &mut Slotui_slot(id): persistent per-id state of the window being built (created on first use): every window keeps its own slots, so the same id in two windows (a title bar's, a dialog's box) measures and scrolls in each by itself; outside the passes the main window's. A window's slots go with its state (window_destroy)fn anim(&mut self, id: u32, target: u32, ms: u32) -> u32ui_anim(id, target, ms) -> current value (0..65535, usually 0..256) moving towards target (ease-out)fn anim_ex(&mut self, id: u32, target: u32, ms: u32, curve: u32) -> u32anim_ex(id, target, ms, curve) -> current value moving towards target along an EASE_* curve (EASE_OUT_BACK may overshoot; the value stays >= 0)fn anim_color(&mut self, id: u32, argb: u32, ms: u32, curve: u32) -> u32anim_color(id, argb, ms, curve) -> the colour on its way to argb (a transition: hover fills, state colours); the first call starts at argbfn cmd_run(&mut self, id: u32, arg: u32)ui_cmd_run(id, arg): queue a command for App::command after this frame (in order)fn timer(&mut self, id: u32, ms: u32)ui_timer(id, ms): App::command(UIC_TIMER, id) after ms (replaces a timer with the same id)fn last_reset(&mut self)g_last_rect = 0, g_last_state = 0: the apps' hidden probe records (JFPainter's *_sem helpers zero both before ui_sem; later records of the pass see the zeroes too) // pn-corefn last_set(&mut self, rect: [i32; 4], state: u32)g_last_rect = rect, g_last_state = state (apps that build a widget from drawing calls set the record's rectangle and state before ui_sem) // pn-corefn last_state(&self) -> u32g_last_state: the SS_* state of the last behave // pn-corefn mbtn(&self) -> u32g_mbtn / g_pressed / g_released: mouse buttons held / pressed / released this frame (bit 1 << BTN_*) // pn-corefn pressed(&self) -> u32fn released(&self) -> u32fn sem(&mut self, kind: u16, id: u32, label: Option<&str>, value: i32) -> &mut Semui_sem(kind, id, label, value) -> the record (rect = last_rect, state from the last behave); the label is copiedfn sem_rect(&mut self, kind: u16, id: u32, rect: [i32; 4], label: Option<&str>, value: i32) -> &mut Semsem_rect(kind, id, rect, label, value) -> the record: a semantic record of content the app draws itself (a link or heading of a web page inside a canvas) at rect (px of the window, as last_rect), without a hit box of its own and without a state; last_rect and the state of the last widget stay. Scripts find it by its label (click "Label"presses at its middle, on the hit box that lies there: the canvas), screen readers read it (section 19 of the guide). Publish the records of what is visible, after the canvas.fn clip_push(&mut self, x: i32, y: i32, w: i32, h: i32)ui_clip_push(x, y, w, h): px; intersects drawing and hit clippingfn clip_pop(&mut self)ui_clip_popfn layer_set(&mut self, layer: u32) -> u32ui_layer_set(layer) -> previous: switch the command layer and hit layer togetherfn begin_pass(&mut self, w: i32, h: i32)ui_begin_pass(w, h): start recording a frame for a w x h px window (one window)fn end_pass(&mut self) -> boolui_end_pass -> another pass is needed (one window)fn repass(&mut self)ui_repass: request another pass before this frame is shownfn modal(&mut self, layer: u32)ui_modal(layer): a modal element (dialog) is open onlayerthis passfn modal_active(&self) -> u32ui_modal_active -> layer of the modal element of the last pass (0 = none)fn disabled_push(&mut self)ui_disabled_push / ui_disabled_pop: widgets in between are disabledfn disabled_pop(&mut self)fn warn(&mut self, msg: &str)ui_warn: "UIWARN f<frame>: <msg>" on stderr