Skip to content

stoa-predict: popup at the text cursor with arrow-key navigation (Wayland input method) - #208

Merged
VictorGSchneider merged 1 commit into
mainfrom
claude/tender-dirac-0bc2zn
Oct 22, 2026
Merged

VictorGSchneider merged 1 commit into
mainfrom
claude/tender-dirac-0bc2zn

Conversation

@VictorGSchneider

Copy link
Copy Markdown
Owner

What

Text prediction becomes a Wayland input method (input-method-v2, the protocol fcitx uses). This answers three requests:

Request Before Now
Popup above/below where you type fixed strip at the bottom of the screen compositor places it at the text cursor
↓/↑ into the popup, ←/→ to move not possible ↓/↑ enter (and leave), ←/→ move, Enter/Tab accept, Esc dismiss; click still works
Emojis never inserted inserted

Why an input method: a shell panel can't see another app's caret, and it can't take arrow keys without stealing focus. An input method gets both from the compositor.

How it works (scripts/stoa-predict.py)

  • Popup: an input-method popup surface, drawn with cairo + Pango into shm buffers using the Stoa palette (StoaCustom.json if present). It follows the output scale and shows a ↓ hint until the popup is entered.
  • Keys: a keyboard grab sees every key. Keys the popup doesn't use are forwarded through the input method's virtual keyboard. niri and sway route those past the grab: src/handlers/virtual_keyboard.rs says so explicitly, and fcitx relies on the same path.
  • Prefix: comes from text-input surrounding_text. In terminals, which send none, it comes from the keys themselves via xkbcommon (ctypes, no extra package), so abnt2 and ç work.
  • Inserting text: commit_string, preceded by delete_surrounding_text. In terminals it is preceded by BackSpaces, using the keycode looked up in the current keymap. Case follows what was typed (Stoi → Stoical).
  • Bindings: generated once into ~/.cache from XMLs shipped in scripts/stoa-predict-protocols/ (MIT, from wlroots). No Arch package installs these two protocols.

Why the emojis never went in

Two causes in the old wtype path, both reproduced and measured:

  • without a UTF-8 locale, wtype refuses any non-ASCII input (Failed to deencode input argv);
  • typing a single character right after wtype creates its keyboard dropped it 9 times out of 15.

commit_string avoids both.

Plumbing

  • Noctalia plugin: keeps only its service (start/stop/notify). panel.luau is gone, and plugin_api drops back to 9.
  • stoa-predict.sh: toggle | start | stop | run | kill. kill waits for the old instance to disconnect, because the compositor refuses a second input method while the first is still connected.
  • Packages: post-install swaps python-evdev for python-pywayland python-cairo python-gobject. The input group stays, relabelled: libinput-gestures needs it, prediction no longer does.
  • Brave (binds + .desktop) and Obsidian (~/.config/obsidian/user-flags.conf): get --enable-wayland-ime --wayland-text-input-version=3, so Chromium talks to the input method.

Bugs caught while testing

  • SIGTERM ignored when idle: a Python SIGTERM handler never runs while libwayland sits in poll() (it restarts on EINTR). The daemon survived toggle-off, and the next start was refused. Now SIGTERM keeps its default action; the compositor cleans up on disconnect.
  • cffi swallows sys.exit(): the "another input method is running" path kept running. Fatal exits now use os._exit.
  • Pidfile overwrite: a refused second instance used to overwrite and then delete the pidfile of the instance holding the input method. It is now written only after registration and removed only by its owner.

Verification

Headless sway 1.10 (built here, since sway 1.9 doesn't draw IME popups), with a GTK4 text view, foot, and a test virtual keyboard carrying a full br/abnt2 keymap:

  • Popup placement: the popup appears right under the caret; screenshots show the passive state and ↓ → selection.
  • GTK words: the Stoi → ↓ → Enter gives the Stoical .
  • GTK emoji: Fir → ↓ ← Enter gives Fir🔥.
  • Esc: dismisses the popup, and typing continues.
  • Terminal (foot): go stoi → ↓ Enter gives 4×DEL + stoic .
  • Whole chain: Noctalia 5.3.0 + plugin → stoa-predict toggle → service → daemon, typing into GTK and foot. Toggling off leaves no daemon, and text then goes in plain.
  • Controller: run → run again (takes over) → kill, with no refusal and the pidfile removed.
  • Repo checks: tests/run.sh 18 passed; ruff, luac5.4, shellcheck and noctalia plugins lint are clean.

Not tested here: niri and Hyprland themselves, which don't run headless. niri's source supports the IME popup (PopupKind::InputMethod) and the grab passthrough. A real Brave/Obsidian has not been tried either.


Generated by Claude Code

The suggestions now sit in a popup next to the text cursor, and the
arrow keys work: Down/Up enter the popup, Left/Right move, Enter or Tab
accepts, Esc dismisses. Neither is possible from a shell panel, which
cannot see another app's caret nor take keys without stealing focus,
so the daemon is now an input method (input-method-v2, as fcitx):

- the compositor places its popup surface at the caret (cairo + Pango
  into shm buffers, Stoa palette);
- a keyboard grab sees each key; keys it does not use are forwarded
  through its virtual keyboard, which niri and sway route past the grab;
- the word before the cursor comes from text-input surrounding text, or
  from the keys themselves in terminals, which send none;
- the choice is committed with commit_string (+ delete_surrounding_text,
  or BackSpaces looked up in the current keymap for terminals).

That also fixes emoji, which never went in: wtype refused non-ASCII
without a UTF-8 locale and dropped a lone keystroke 9 times in 15.
evdev and the input group are no longer needed for prediction.

The Noctalia plugin keeps only its service (start/stop/notify); the
panel is gone. Brave and Obsidian get --enable-wayland-ime
--wayland-text-input-version=3 so Chromium talks to the input method.
SIGTERM keeps its default action: a Python handler never runs while
libwayland sits in poll(), which left the daemon alive on toggle-off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CrdbH4MCHhLKimzdRSzXZp
@VictorGSchneider VictorGSchneider self-assigned this Oct 22, 2026
@VictorGSchneider
VictorGSchneider merged commit 5e06095 into main Oct 22, 2026
3 checks passed
@VictorGSchneider
VictorGSchneider deleted the claude/tender-dirac-0bc2zn branch October 22, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants