JFLib

30. Live inspector#

A running app can be looked into without changing it: the inspector shows which widget is where, with which id, label, value and state, how the layout boxes nest, the Tab order, what the last frames repainted, and the frame's cost. It is part of ui (lib/ui/inspect.rs) and off by default; closed it costs nothing (the runtime tests one flag; nothing is recorded or drawn, the frames are byte for byte those of an app without it). In a development build it also edits the app's look and operates its widgets ("Editing in the inspector" below) and shows the file and line where the app called each widget, with "Im Editor öffnen" ("Sources" below).

  • Turning it on: --inspect opens it at the start; jfgui_INSPECT=1 only arms it (its hot keys work), jfgui_INSPECT=open opens it too. In code: ui.inspect_enable(true) (arm), ui.inspect_show(true) (open; arms it), ui.inspect_open(). The hot keys Ctrl+Shift+I and F12 open and close it while it is armed (they are then the inspector's, not the app's); ui.inspect_keys(&[VK_F11 | MOD_CTRL << 16]) sets others, &[] none.
  • The overlay in every window of the app: the widget under the pointer outlined (orange) with a tag -- kind, id (#100, hashed ids in hex with their name from ui.id("...") when the frame used it), label, size in DIP, state flags; the selected widget (blue). Options in the tool window: Umrisse (every layout box), Tab-Reihenfolge (the Tab stops numbered as Tab visits them), Neu gezeichnet (the rectangles each frame repainted, fading out: a widget that repaints too much shows at once). The overlay is drawn into the framebuffer after the app's commands, outside their lists: the app's dirty regions stay its own, and --verify-dirty checks the overlay too.
  • The tool window "Inspektor" (a tool window: it never takes the keyboard by itself; its Esc, close button and drops never reach the app): Widgets -- the tree of the current frame (the semantic records of every app window inside the layout boxes they were emitted in; boxes without widgets are left out; collapsible; selecting a row highlights the widget in the app) and the selected widget's properties (kind, id and name, label, rectangle in DIP and px, value / min / max, state, layer, window) with a script selector (kind:"Label" when that names it alone, else #id, else its centre X,Y), copied with Kopieren or Ctrl+C in the inspector; Bildaufbau -- passes of the last frame, the time of App::frame and of the render, the repainted area, draw commands (and the bytes of the command lists), the number of widgets, the glyph cache's use; Farben -- every theme token with its colour. Auswählen starts the pick: the next click into the app selects the widget there and does not reach the app; Esc ends the pick.
  • The inspector's own window never appears in the tree, the overlay or the pick. It takes one of the eight window slots (WIN_MAX); F6 / Ctrl+F6 reach it like any tool window.
  • A widget appears in the tree when it records itself (ui.sem, as every library widget does); custom widgets built from hit_rect and drawing calls should call ui.sem(K_*, id, label, value) after their behave -- the dump, scripts, screen readers and the inspector see the same records.
# apps/gallery/tests/inspect.uis (excerpt)
inspect snap                         # the main window's frame
inspect arm
key f12                              # the hot key opens it
inspect expect open
move "Standard"
inspect expect hover button:"Standard"
inspect pick
click "Standard"                     # selects; the app does not see the click
inspect expect selected button:"Standard"
inspect expect selector "button:\"Standard\""
click button:"Kopieren"
inspect expect copied "button:\"Standard\""
key f12                              # closed again
inspect expect same                  # renders byte for byte as at the snap
inspect expect idle                  # and did no work since it closed

The script commands: inspect on|off|toggle|arm|disarm, inspect pick, inspect select S|none, inspect option outlines|order|dirty on|off, inspect tab widgets|frame|colors|look, inspect copy, inspect snap, inspect expect open|closed|pick on|off|flash on|off|idle|same| excluded|selected S|none|hover S|none|selector "s"|copied "s" (excluded: no record of the inspector's window in the tree). The inspector's window has the id 2147422208 (expect window 2147422208 open, shot F window 2147422208).

Hot reload: cargo jflib watch, build_rs.py watch#

An app of your own: cargo jflib watch [-p PKG] [-- ARGS] in its folder does what this section describes for the machine's own target, with its session in target/jflib-watch/NAME; it watches every file of the project's own packages and builds as cargo run does. In this repository:

python build_rs.py watch NAME [--target win|linux] [-- ARGS] builds the app, starts it and watches its sources, its assets and build files and the sources of every library and style crate it links. When files were saved it builds again (a development build, incremental; never --release) and starts the app again in its session: the app is where it was, with the change in it. Nothing in the app changes for this -- the widgets stay compiled Rust, no state structure is prescribed:

  • The app's state comes back by replaying the session's input. A development build of ui (--cfg jfgui_dev, lib/ui/hotreload.rs) started with jfgui_HOTRELOAD=DIR records every platform event that reaches the app (pointer, wheel, keys, text, input-method commits, a window's close button: the fields --log-events prints, with the time), the main window's size and dpi, the app's keyboard state (UIC_ACTIVE) and what the app's clipboard reads returned. The next start replays the log before its first wait: the clock goes from record to record (timers fire at their deadlines, animations jump), every batch of events goes through the normal event handling, so the new code builds the state from the same input. The replay leaves the system's clipboard, file dialogs, sounds, URLs and drags to other programs alone.
  • Divergence: every press, wheel turn, release, key and text event must reach the same widget id as when it was recorded (the widget under the pointer, the pressed one, the focused one). When the change moved widgets so that an event reaches another one, the start reports diverged: a press reached #40 instead of #20 and ends with exit code 3; watch puts the log aside and starts the app afresh. Input a replay cannot repeat -- a drop or drag of another program, the system's file dialog, a drag out of the app -- marks the log: the next start is fresh as well.
  • Kept besides the state: the window's position and size (the session's window.ini through jfgui_STATE; tool windows open again only when the replay opens them), the style, the text scale and the inspector -- open, tab, options and its selection (by its script selector) -- in carry.ini; a fresh start keeps them too.
  • A failed build: the old version keeps running; the error appears in the console and at the top of the inspector ("Bau fehlgeschlagen"), which opens for it.
  • The session lives in build/<target>/hot/NAME/ (events.log, carry.ini, window.ini, replay.txt with the start's report, build.txt, the watcher's request ctl); the program runs as a copy build/<target>/NAME.hot[.exe], so the next build can write the exe. Asked to end, the app ends as with --auto-close (placement, log and carried state written); one that does not end within 5 s is killed. watch begins a new session; an app closed by the user starts afresh after the next change; Ctrl+C ends watch and the app.
  • Off it costs nothing: without jfgui_HOTRELOAD the runtime's hooks test one flag, nothing is recorded or read (the inspector's expect same / idle hold); in --release builds (and for the browser and Android) the flag is a constant false and the hooks fold away.
  • What a replay does not know: files the app read or wrote, the network, the timing of background tasks, agent actions, the real clock -- such parts of the state may differ after a restart.
  • Tests: python tools/tests/test_hotreload.py [--quick] [--target win|linux] (the watcher with a stand-in program -- the suite runs this part; a temporary app built, recorded, replayed, diverged and failed headless on Windows and Linux; the window loop on a Windows desktop of its own, never on the user's screen).

Editing in the inspector (development builds)#

In a development build (build_rs.py's default; it passes --cfg jfgui_dev, a --release build keeps the inspector read-only) the inspector also changes the running app's look -- only through what exists already (ui/inspect_edit.rs). Nothing in the app changes for it: no API to call, no state structure. The app's own values (a slider's, a check box's, a text box's) are never written; the inspector gives input instead.

  • Farben is editable: select a token in the list, type #RRGGBB / #AARRGGBB into its box (Enter) or open the colour picker beside it. The colours of the mode in use change (light, dark or high contrast); T_ACCENT's row sets the accent the style derives its ramp from.
  • Darstellung (a fourth tab): the style (the styles the app links, §34), light / dark / high contrast, the accent, the language (the columns of the app's catalogue, else German and English; the library's texts follow at once, the inspector's too), the main window's scale (its dpi; the window keeps its size in pixels, like a browser's zoom) and the text scale; corner radius, spacing, control height; the font family (a font file or an installed family) and the sizes of the text styles. Theme values go through the app theme (ui.set_theme over the app's own theme, or none), the rest through theme_set / set_contrast, set_style, i18n::set_language_code, set_text_scale.
  • Every changed value is marked (its name in the accent colour, "geändert" in the list), has its own reset button and is listed under Änderungen (old → new). Alles zurücksetzen, undo and redo (the bar, Ctrl+Z / Ctrl+Y in the tool window; the moves of one drag of a slider or the colour picker are one step: History with Snapshot steps). Changes hold for the running session -- also when the inspector is closed -- until they are taken over:
  • Als Theme speichern writes the app theme in use, changes included, as a theme file (default config://themes/inspector.theme; a font file is written relative to the theme file); Liste kopieren copies "Das hast du geändert": the changed theme keys as lines of a theme file, the rest (style, mode, language, scales) as lines of code, all of it usable as a theme file. Layout and texts are changed in the code.
  • Bedienen (Widgets tab, below the properties) operates the selected widget through synthetic input, as a script does: a click, a drag by dx / dy from its centre, a slider's value (the way a screen reader sets it: the widget takes it as input), text typed into a text box (a click, Ctrl+A, the text), a command (cmd ID). The matching script line is shown.
  • The tool window itself is built in the style's own look: the app theme and an edited text scale do not reach it, so it stays usable whatever is edited (style, mode and language do).
# apps/gallery/tests/inspect_edit.uis (excerpt)
inspect on
inspect set token bg #E0F0E0           # the colours in use (light)
expect token bg #E0F0E0
inspect set radius 8                   # DIP, or 150%
inspect expect changes 2
inspect expect value radius "8 DIP"
inspect undo                           # the radius again as before
inspect save build/insp/edit.theme     # the app theme with the changes
inspect list                           # "Das hast du geändert" to the clipboard
inspect reset                          # every change back
inspect select #140
inspect do value 75                    # a slider's value; also click, drag DX DY, type "s", cmd ID

Script commands (KEY = token NAME | accent | radius | spacing | height | size STYLE | font | mode | style | lang | scale | textscale): inspect set KEY VALUE (token bg #RRGGBB, radius 8|150%|none, spacing 110, height 36, size body 15, font FILE|FAMILY| none, mode light|dark|contrast, style kde, lang en, scale 150, textscale 130), inspect reset [KEY], inspect undo|redo, inspect save PATH, inspect list, inspect do click|drag DX DY|value N|type "s"|cmd ID [ARG], inspect expect changes N|list "s"|value KEY "s"|undo on|off|redo on|off|message "s"|none. Tests: apps/gallery/tests/inspect_edit.uis, inspect_edit_rtl.uis (mirrored, 150 %), apps/themedemo/tests/inspect_edit.uis (the linked styles, edits over an app theme).

Sources: where the app called a widget (development builds)#

In a development build the Widgets tab shows, below the selector, Quelle: the file and line of the app's call that built the selected widget (or layout box), e.g. apps/gallery/sec_edit.rs:66, with a copy button and Im Editor öffnen (ui/inspect_src.rs). Nothing in the app changes for it:

  • How it is recorded: the library's widget calls (ui.button, ui.label_button, ui.textbox, ui.slider, ui.combobox, ui.list_item, ui.box_begin, ui.dialog_buttons, ui.sem ... -- every public call that builds a widget or a container) carry #[cfg_attr(jfgui_dev, track_caller)]; their first statement opens a scope that, while the inspector is open, sets the call being built from core::panic::Location::caller(). A call from inside the library keeps the app's call that encloses it, so a composite widget -- a number box with its text box, a combo box, the rows of a menu bar or a list, the buttons of dialog_buttons -- reports the app's line, not the library's inner one. A widget built by the library with no app call around it (the runtime's own chrome, the inspector's window) shows the library's call; a widget an app builds from drawing calls and ui.sem shows its ui.sem line.
  • What it costs: closed (the default) every scope tests one flag and nothing is stored or drawn (inspect expect same / idle hold); a --release build has no #[track_caller] and empty scopes (ui/inspect_src_off.rs): the same code as without the feature. The path is the one rustc got: build_rs.py makes it relative to the repository (--remap-path-prefix).
  • The editor (the first that is set or found): jfgui_EDITOR, a command line with {file}, {line} and {col} (code -g {file}:{line}, "C:\Program Files\...\clion64.exe" --line {line} {file}; without {file} the file is appended; "..." / '...' keep blanks); else VISUAL, then EDITOR (a known editor gets the line: code / codium -g FILE:LINE, JetBrains IDEs --line N FILE, vi / vim / nano / emacs / gedit +N FILE, kate -l N FILE, subl FILE:LINE, notepad++ -nN FILE); else what is found: CLion (clion64.exe in PATH, the Toolbox's %LOCALAPPDATA%\Programs\CLion, %ProgramFiles%\JetBrains\CLion *; Linux: clion / clion.sh in PATH, the Toolbox's script, the snap), then VS Code (code.cmd in PATH and Code.exe beside its bin, the install folders; Linux: code in PATH). The file is passed with its absolute path: below jfgui_SRC, else below the folder that holds the program's build folder, else below the working folder. The editor starts on its own (plat::process::launch: no pipes, it stays open when the app ends or hot reload restarts it).
  • Permissions: starting the editor is the app starting a program, so the app's manifest decides as for Command::spawn (a process rule that names the editor, an ask process line that asks -- open again after the answer; an app without PERMISSIONS may). A refusal names the program below the source ("Die App darf … nicht starten (PERMISSIONS: process)"), and the source can still be copied. This is a deliberate choice: the inspector runs in the app's process with the app's rights, and an exception for development builds would be one in every development run of every app. To open sources from an app with a manifest, set jfgui_EDITOR to a program name and add that name (process code, or ask process code), or add the path the refusal names.
  • Scripts never start the user's editor: a headless run takes only an editor the script set with inspect editor "COMMAND".
# apps/gallery/tests/inspect_source.uis (excerpt)
inspect on
inspect select #702                           # a number box: the app's number_box call
inspect expect source "apps/gallery/sec_edit.rs:"
click button:"Quelle kopieren"                # "apps/gallery/sec_edit.rs:71" to the clipboard
inspect editor "stub-editor --line {line} {file}"
inspect open dry                              # built and checked, nothing started
inspect expect opened "stub-editor --line "
inspect editor "whoami"                       # a real start of a harmless program
inspect open

Script commands: inspect editor "COMMAND"|none (the editor of this run: sets jfgui_EDITOR), inspect open [dry], inspect expect source "PREFIX"|none (the selected widget's source starts with PREFIX), inspect expect sources "PREFIX" (every record of the app's windows: a library widget call left unmarked shows a line of lib/ui and fails it), inspect expect opened "s"|none (the last opening's command line contains s; taken by the check), inspect expect note "s"|none (the note below the source). Tests: apps/gallery/tests/inspect_source.uis (widget kinds, composite widgets, a menu, a dialog, a tool window, copy, the editor's command line, a missing and a real program, closed it stores nothing), apps/audiotest/tests/inspect_perm.uis (a manifest without process: refused), apps/plattest/tests/launch.uis (plat::process::launch: the words arrive as given).

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