JFLib

13. Pitfalls#

  • Ids must be unique in a frame. Two widgets with the same id share hover / press state: clicks seem to get lost, or a click on one acts on the other (a text box below a segmented control whose segments take id + 1 ..). Scripts (and jfgui_DEBUG=1) print a UIWARN for an id two widgets of one pass share, with both positions; expect_nowarn fails on it. Ids are per window: windows may use the same ids (a text box of one id in two app windows keeps its own caret, selection and undo in each). So do the state slots (ui.slot(id)): a box of one id measures in every window by itself and a view of one id keeps its own scroll position, so two windows built by the same code (a browser's windows with their own title bars, a dialog box of a fixed id) settle each in its own size; a window's slots go with its state (window_destroy), and a panel that floats out into a tool window starts there with fresh ones (its scroll positions too). The same holds for the state the widget families keep by id: the code and rich-text editors' views (scroll position, find bar, completion list), the colour pickers' fields, the font pickers' lists, the spell checks of TB_SPELL boxes and their suggestion menus, the charts' drawings and line-chart cursors, the dock and window areas (dock_begin, desk_begin), the file dialog (filedlg), the canvases of canvas_damage and the menu key's target -- a browser's windows built by the same code with the same ids each keep their own (apps/viewtest/tests/perwindow.uis, apps/spelltest/tests/windows.uis). Calls that name an id address the window being built when it has such a widget; else -- outside the passes (App::command, a task), or in a window without one -- code_status, code_events, code_diag, code_popup_item and spell_errors read the first window, the main window first, that has such a widget, code_find, code_reveal and richtext_link go to the first editor of the id that is built, canvas_damage and canvas_gpu_damage to the canvas of that id in every window. What is the app's stays app-wide: the code editors' theme and font size, the spell checker, the folder a file dialog used last and filedlg_paths(), the dock's floating windows' ids (the area's id + 1 ..: areas in two windows that float panels need ids of their own).
  • ui.canvas(...) records the version before you react to its input. When handling the returned UiInput changes the content, bump the version and call ui.repass(); otherwise the dirty-region renderer shows the change one frame late (--verify-dirty reports it).
  • ui.canvas, ui.title() and a few others return borrows of ui: clone / copy the value before the next ui call.
  • A surface shown with image / draw_image / a Draw object is compared by its version: bump it on every pixel change.
  • flyout_begin / flyout_end, dialog_begin / dialog_end, window_begin / window_end, box_begin / box_end, list_begin / list_end always come in pairs, also when the begin call returned "closed".
  • Do structural work in App::command, not in frame: frame may run several times per input event (settle passes).
  • Overlays over a canvas: wrap them in ui.clip_push(rect) / ui.clip_pop() so they never spill onto neighbouring panels; boxes placed with next_rect are fine next to normal children.
  • Panics end the program with exit code 3 and a crash line on stderr; decoders return ERR_* for bad input instead of panicking.
  • shot / dump in a script do not create folders: build_rs.py test creates them for the scripts it runs; create them yourself for runs by hand.
  • Windows paths: / works everywhere; the file dialog returns / separators.
  • Python helper scripts that patch sources: Path.write_text writes CRLF on Windows (use write_bytes), shell heredocs turn \\n into newlines.

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