Repository navigation
Commit 5e06095
authored
stoa-predict: popup at the text cursor with arrow-key navigation (Wayland input method) (#208)
## 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](https://claude.ai/code/session_01CrdbH4MCHhLKimzdRSzXZp)_12 files changed
Lines changed: 1347 additions & 510 deletions
File tree
- config
- hypr
- niri
- scripts
- stoa-predict-protocols
- setup
- theme/noctalia-plugins/stoa-predict
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
171 | | - | |
| 171 | + | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
| 156 | + | |
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
629 | 629 | | |
630 | 630 | | |
631 | 631 | | |
632 | | - | |
| 632 | + | |
633 | 633 | | |
634 | 634 | | |
635 | 635 | | |
| |||
640 | 640 | | |
641 | 641 | | |
642 | 642 | | |
643 | | - | |
| 643 | + | |
644 | 644 | | |
645 | 645 | | |
646 | 646 | | |
647 | | - | |
| 647 | + | |
648 | 648 | | |
649 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
650 | 660 | | |
651 | 661 | | |
652 | 662 | | |
| |||
0 commit comments