JFLib

ui/ids.rs#

Source: lib/ui/ids.rs

ids -- widget ids from names: hashing and a stack of scopes

  wid(name) -> u32             const: the id of a name outside every scope, for constants
                               (const SAVE: u32 = ui::wid("save") == ui.id("save") there)
  wid_path(&[names]) -> u32    const: the last name inside the scopes of the ones before
                               (wid_path(&["layers", "add"]) == ui.id("add") after
                               ui.id_push_str("layers"))
  ui.id(name) -> u32           a name in the current scope (takes &self: ui.button(ui.id("save"), ..))
  ui.id_n(n) -> u32            a number in the current scope (the rows of a loop)
  ui.id_push(n) / ui.id_push_str(name) / ui.id_pop()   open / close a scope: a number (an
                               index, a document's id) or a name
  ui.id_scope(n, |ui| ...)     id_push, the closure, id_pop
The names of hashed ids are kept for the frame in scripts, with jfgui_DEBUG=1 and while the
inspector is open (it shows them: inspect.rs).

A hashed id is FNV-1a over the bytes (a number: its four bytes, little endian) and a tag
byte (1 name, 2 number), seeded with the scope, then mixed (murmur3's finaliser) into
ID_HASH_BASE .. 0x0FFE_FFFF.  Constants written by hand stay below ID_HASH_BASE and never
meet hashed ids; bits 28..31 stay the library's (part_id); the 64 Ki ids above the range
leave room for widgets that also take id + 1 + i.  Two names of one pass collide with a
chance of about 1 : 134 million per pair; scripts and jfgui_DEBUG=1 warn about two names
with one id and about an id two widgets share (naming the hashed names involved).  The
stack starts empty in every pass; a scope still open at the end of App::frame is closed
(a UIWARN the first time).
/// the first hashed id; ids written by hand stay below it
ID_HASH_BASE: u32 = 0x0800_0000
  • const fn wid(name: &str) -> u32 the id of a name outside every scope (the same as ui.id(name) there)

  • const fn wid_path(names: &[&str]) -> u32 the id of the last name inside the scopes of the names before it (id_push_str each); no names: wid("")

impl Ui

  • fn id(&self, name: &str) -> u32 the id of a name in the current scope
  • fn id_n(&self, n: u32) -> u32 the id of a number in the current scope (the rows of a loop)
  • fn id_push(&mut self, n: u32) open a scope named by a number (an index, a document's id); always pair with id_pop
  • fn id_push_str(&mut self, name: &str) open a scope named by a name; always pair with id_pop
  • fn id_pop(&mut self) close the scope of the last id_push / id_push_str
  • fn id_scope<R>(&mut self, n: u32, f: impl FnOnce(&mut Ui) -> R) -> R id_push(n), f(ui), id_pop()

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