JFLib

Text boxes and the clipboard#

ui/edit.rs#

Source: lib/ui/edit.rs

edit -- text input: single-line and multi-line text boxes, number boxes, the clipboard for
widgets                                                             [src/ui/ui_edit.s]

  textbox(id, placeholder, &mut buf, cap_flags) -> EV
       WinUI TextBox (32 DIP high).  buf: the app's UTF-8 text; cap_flags = the size of
       the asm's buffer in bytes incl. the NUL (bits 0..22: the text holds at most cap - 1
       bytes) | TB_PASSWORD | TB_READONLY | TB_DIGITS | TB_SIGNED.  placeholder (or None)
       TB_PASSWORD: while it has the keyboard the input method's purpose is IP_PASSWORD (set
       by the box itself, the purpose of before put back after; no plat::ime_purpose needed);
       always one line (TB_MULTI with it is ignored).
       shows while the text is empty (cut with "…" where the box is narrower).
       ST_DISABLED via next_flags.
       EV_ACT: the text changed this frame; EV_COMMIT: Enter, Esc or focus lost; EV_FOCUS
       while it has the keyboard focus.
       Semantic record K_TEXTBOX: label = the text (one dot per character for passwords),
       value = caret, max = selection anchor (byte indices of the label, of the dots for
       passwords; 0 while not focused), min = the
       kind of box: (TB_MULTI | TB_PASSWORD | TB_READONLY) >> 24.
       TB_MULTI (U1): several lines (natural height 5 lines): Enter inserts a line break,
       Ctrl+Enter commits, Up / Down keep the preferred x, PgUp / PgDn move a view, Home /
       End work on the line (Ctrl: the text), the wheel and the caret scroll it vertically,
       a thin bar shows the position; TB_WRAP wraps at the width (else it scrolls
       horizontally; soft hyphens: a hyphen where a line breaks after one), TB_HYPHENATE
       with it hyphenates words that do not fit (hyph.rs; the hyphen is drawn, not part of
       the text).  Pasted text keeps its lines.
       TB_SPELL: spell checking -- misspelled words get a red wavy line, a right click on
       one offers suggestions, "add to dictionary" and "ignore" (edit_spell.rs: spell_enable,
       spell_language, spell_sources, spell_set, speller, spell_state, spell_errors).
  number_box(id, &mut v, &range) -> EV
       an integer in a text box: digits only ('-' too when the range minimum is negative);
       Up / Down add -/+ step; typed text is taken on Enter or focus loss; the value is
       clamped to the range.  EV_ACT when v changed, EV_COMMIT on Enter / focus loss.
       Semantic value = v, max = range maximum.  PgUp / PgDn: 10 steps.
  number_box_f(id, &mut v, &range_f) -> EV   (U1)
       an f64 in a text box: shown with the range's decimals and decimal_sep; ',' and '.'
       are both accepted; Up / Down = one step (step 0: 1 % of the range), PgUp / PgDn =
       10 steps; clamped, snapped and rounded like the slider.  Semantic value =
       v * 10^decimals.
  (U2) both number boxes follow v while they have the focus as long as the user has not
       edited their text; Enter / focus loss only store the text when it was edited.
  nb_live(id, &mut v) / nb_live_f(id, &mut v) -> bool   the number typed so far into box
       id (being edited) is stored when it differs from v (clamped / fixed to the range),
       without reformatting the text -- property rows preview while the user types
  open_folder(path) / open_url(url) -> 0 / -1   (U2) headless runs only print them
  clip_set(fmt, data) -> 0 / -1, clip_get(fmt) -> data   the clipboard (headless runs:
       an in-process clipboard, so scripts are deterministic)
  text_key_filter(k) (crate)  the keys the focused text box consumes are hidden from key()

Keys: Left/Right (+Ctrl words, +Shift select), Home/End, Backspace/Delete (+Ctrl words),
Ctrl+A/C/X/V, Ctrl+Z undo, Ctrl+Y / Ctrl+Shift+Z redo, Shift+Delete cut, Ctrl+Insert copy,
Shift+Insert paste, Enter commits, Esc drops the focus.  Plain character keys are consumed
(their text arrives as typed text).  Tab, Up/Down/PgUp/PgDn, F-keys, Alt chords and other
Ctrl chords stay for the app.
Mouse: click places the caret (Shift+click extends), drag selects, double click selects a
word.  The caret blinks every 530 ms (timer UIT_CARET; always visible in headless runs).

Only one text box has the keyboard focus, so the edit state (caret, anchor, undo history)
is one block that belongs to the focused box (ed_*); the horizontal / vertical scroll
offsets are kept per box in its state slot.  The asm edits the app's NUL-terminated buffer
in place; here the box's text is moved into S::buf for the call (byte for byte the asm's
buffer up to its NUL) and handed back as a String.

Deviation (Rust ownership): when another number box takes the edit buffer, the asm stores
the previous owner's edited text into that owner's value right away (through the `&v` it
kept); here that store waits for the owner's next number_box call (S::pend_text), which
runs before the owner reads its value.  The events it reports and the settled frame are the
same; an owner that is never drawn again keeps its old value.
  • struct TextboxPresentation Opt-in presentation for a multiline editor embedded in document content. Geometry is in physical pixels; callers scale it with the document zoom and DPI. Editing, clipboard, selection, accessibility and IME behavior remain the ordinary textbox implementation.
    • font: u32 -- Font identifier registered in the UI text engine.
    • size: i32 -- Font size in 24.8 physical pixels, clamped to 1..512 pixels.
    • line_height: i32 -- Line advance in physical pixels, clamped to 1..2048.
    • baseline: i32 -- Baseline offset from a line's top, clamped to the line height.
    • color: u32 -- Text and caret color, straight ARGB.
    • background: u32 -- Solid editor background, straight ARGB.
    • underline: bool -- Draw an underline beneath every text run.

impl Ui

  • fn textbox_selection(&self, id: u32) -> Option<(usize, usize)> Read the focused textbox's UTF-8 byte selection (anchor, caret), after drawing it. Opt-in API for document editors; ordinary textbox behavior is unchanged.
  • fn textbox_select(&mut self, id: u32, text: &str, anchor: usize, caret: usize) -> bool Select valid UTF-8 byte boundaries in an already focused/drawn textbox. Pass the same current text used by textbox(); returns false without changes for invalid ranges.
  • fn textbox(&mut self, id: u32, placeholder: Option<&str>, buf: &mut String, cap_flags: u32) -> u32 ui_textbox(id, placeholder, buf, cap_flags) -> EV_* (see the module header)
  • fn textbox_icon(&mut self, id: u32, icon: u32, placeholder: Option<&str>, buf: &mut String, cap_flags: u32) -> u32 Single-line text input with a leading semantic icon. The icon reserves its own inset, so pointer selection, caret scrolling and IME share the visible text origin. Multiline inputs ignore the icon and retain their ordinary presentation.
  • fn textbox_presented(&mut self, id: u32, placeholder: Option<&str>, buf: &mut String, cap_flags: u32, mut presentation: TextboxPresentation) -> u32 Edit multiline text on a caller-provided surface without the style's textbox frame. Uses zero text insets and the supplied font, line metrics and colors. The supplied presentation applies only to this call; ordinary textboxes keep their default style. TB_MULTI is implicit; other capacity and textbox flags retain their usual meaning.
  • fn ime_preedit(&self) -> plat::Preedit (E4) the input method's composition in the live window (Preedit: its text and the caret as a byte offset in it), empty when none is going on: a canvas that draws an editable field of its own (ime_enable) draws it at its caret, underlined; the committed text comes through text_take
  • fn ime_enable(&mut self, on: bool, purpose: u32) (E4) an editable field the app draws itself (a web page's input in a canvas) got the focus (on, its purpose IP_*: IP_TEXT, IP_URL, IP_EMAIL, IP_NUMBER, IP_PHONE, IP_PASSWORD, IP_SEARCH) or lost it (off): with it the input method comes while the app tells its caret every frame (ime_pos), and the app draws the composition itself (ime_preedit: Windows hides the method's composition window, its candidates stay at the caret); IP_PASSWORD takes the input method away (Windows) or says so (Wayland). Off: the method goes
  • fn ime_pos(&mut self, x: i32, y: i32, h: i32)
  • fn clip_set_many(&mut self, entries: &[(u32, &[u8])]) -> i32 Publish distinct TEXT/HTML/RTF/DOCX representations of one selection (64 MiB total). Validation precedes replacement; 0 means every representation was published.
  • fn clip_set(&mut self, fmt: u32, data: &[u8]) -> i32 Replace the clipboard with one text, image or rich representation.
  • fn clip_get(&mut self, fmt: u32) -> Option<Vec<u8>> ui_clip_get(fmt) -> the clipboard data (text: UTF-8) or None
  • fn open_folder(&mut self, path: &str) -> i32 ui_open_folder(path) -> 0 / -1: (U2) show a folder (or a file selected in its folder) in the system file manager; headless runs start nothing: they print "ui_open_folder: PATH" and return 0
  • fn open_url(&mut self, url: &str) -> i32 ui_open_url(url) -> 0 / -1: a web page / mailto link in the default application; headless runs print "ui_open_url: URL" and return 0
  • fn number_box(&mut self, id: u32, v: &mut i32, rg: &Range) -> u32 ui_number_box(id, &mut v, &range) -> EV_* (see the module header)
  • fn number_box_f(&mut self, id: u32, v: &mut f64, rf: &RangeF) -> u32 ui_number_box_f(id, &mut v, &range_f) -> EV_* (U1; see the module header)
  • fn nb_live(&mut self, id: u32, v: &mut i32) -> bool nb_live(id, &mut v) -> true when number box id is being edited and the text typed so far is a number different from v: the value (clamped to the range) is stored without reformatting the text -- property rows preview while the user types
  • fn nb_live_f(&mut self, id: u32, v: &mut f64) -> bool nb_live_f(id, &mut v) -> nb_live of an f64 number box (fixed to its RangeF)

ui/edit_spell.rs#

Source: lib/ui/edit_spell.rs

edit_spell -- spell checking in text boxes (edit.rs includes it as `spellbox`)

  TB_SPELL (cap_flags of textbox, TB_MULTI too)   the box checks its words: a red wavy line
       under misspelled ones; a right click on one (or the menu key / Shift+F10 with the
       caret in it) opens a menu: up to 5 suggestions (a click replaces the word, one undo
       step), "Zum Wörterbuch hinzufügen" (the personal word list), "Ignorieren" (this
       session).  Password, read-only and digit boxes are never checked.  The app's own
       context_menu() for the box still opens on words that are not marked.
       Suggestions take a while with large dictionaries (a long German compound up to a
       few tenths of a second): Hunspell's are made on a worker (plat::Job), the menu opens
       at once with "Vorschläge werden gesucht …" and fills when they are there (E_WAKE; a
       menu opened with a key then focuses the first one).  The system's checker answers
       on the UI thread.
  spell_enable(on)            the switch for the whole UI (default on; off: no marks)
  spell_language(tag)         the language, a BCP 47 tag; "" (default) = the user's
                              languages (i18n::user_languages), the first with a checker
  spell_sources(SRC_*)        where checkers come from (spell crate; 0 = the default:
                              everything in window mode, only app:// in headless runs)
  spell_suggest_async(on)     suggestions on a worker (default: window mode yes, headless
                              no, so scripts stay synchronous; a script that switches it on
                              sees the menu waiting, and its `sync` waits for them)
  spell_set(speller)          the app's own checker (spell::Speller)
  speller() -> Option<&mut Speller>   the checker in use (None while loading, or none);
                              loads it when no box asked for it yet
  spell_state() -> SPELL_*    OFF (switched off / not asked yet), LOADING, READY, NONE
  spell_errors(id) -> ranges  the misspelled byte ranges of box id as last checked (of the
                              window being built when it has the box; else the first window's,
                              the main window first, that checked one)

Checking: a box checks when its text changed (a hash of it) or the checker or its word
lists did; word results are cached in the Speller, so typing re-checks only new words.  In
window mode at most BUDGET new words and BUDGET_US of checking per frame (one word at
least; the rest on the timer UIT_SPELL: a long German compound may take milliseconds),
headless all at once.  The word being typed (the caret at its end while typing) is marked
only once the caret leaves it.  The results are kept per window (WinParts::spell): a box of the
same id in two windows is checked by itself, and its menu opens in its own window.
Loading: window mode the system's checker on the UI thread,
Hunspell files on a worker (plat::Job; no marks until it is read), the personal word list
in config://spell (spell::personal_path); headless runs read app:// dictionaries at once
and keep the personal list in memory, so scripts never depend on the machine.

impl Ui

  • fn spell_enable(&mut self, on: bool) the spell checking switch for every TB_SPELL box (default on)
  • fn spell_language(&mut self, tag: &str) the language of the checker: a BCP 47 tag ("de-DE"), "" = the user's languages
  • fn spell_sources(&mut self, sources: u32) where checkers come from (spell::SRC_*; 0 = the default, see the module header)
  • fn spell_set(&mut self, sp: Speller) use the app's own checker
  • fn spell_suggest_async(&mut self, on: bool) suggestions on a worker: on / off (default: on in window mode, off in headless runs)
  • fn spell_state(&self) -> u32 SPELL_OFF / SPELL_LOADING / SPELL_READY / SPELL_NONE
  • fn speller(&mut self) -> Option<&mut Speller> the checker in use (None while it loads or when there is none)
  • fn spell_errors(&self, id: u32) -> Vec<(usize, usize)> the misspelled byte ranges of text box id, as last checked (none while switched off)

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