Skip to content

feat(ui): add toggle_mouse action to suspend mouse tracking at runtime - #435

Merged
umputun merged 5 commits into
umputun:masterfrom
mikhail-angelov:toggle-mouse
Nov 5, 2026
Merged

umputun merged 5 commits into
umputun:masterfrom
mikhail-angelov:toggle-mouse

Conversation

@mikhail-angelov

Copy link
Copy Markdown
Contributor

Problem

revdiff captures plain drag via mouse tracking, so terminal-native text selection is unavailable while tracking is on. The current options are all-or-nothing at startup:

  • keep tracking on — no drag selection. The terminal bypass modifiers documented in the README differ per terminal and are unavailable in some embedded clients entirely: we run revdiff inside herdr (a terminal workspace) whose client implements neither shift-drag nor any other bypass, so with tracking on there is no way to copy diff text at all.
  • run with --no-mouse — selection works, but wheel scrolling and click navigation are gone for the entire session.

How this solves it

A new keymap action toggle_mouse (default m, remappable through the keybindings file, shown in the help overlay and documented in the README) flips tracking at runtime:

  • suspending hands the mouse back to the terminal, so plain drag selects text natively;
  • a second press restores wheel and click handling;
  • works in both directions, including enabling from a --no-mouse start.

Implementation notes: tracking state moves from the immutable startup config to a mutable model field, and the $EDITOR / output-flush suspend paths restore the current mode rather than the startup flag, so a suspended session stays suspended across editor round-trips. Suspending emits bubbletea's DisableMouse, resuming emits EnableMouseCellMotion.

Testing

  • go test ./... green except one app/diff blame test (TestGit_FileBlame_UsesIndexForStagedDiffs) that fails identically on a clean master checkout locally — environment-dependent, unrelated to this change;
  • golangci-lint run clean;
  • manual: suspend/resume round-trips verified in a raw pty and inside herdr, including editor suspend while suspended and resume from a --no-mouse start.

mouse tracking captures plain drag, so terminal-native text selection is
unavailable while it is on; --no-mouse is startup-only and gives up wheel
and click handling entirely. toggle_mouse (default m) flips tracking at
runtime: suspending hands the mouse back to the terminal for drag
selection, a second press restores wheel and click.

the tracking state moves to a mutable model field so the editor and
output-flush suspend paths restore the current mode rather than the
startup flag. works in both directions, including enabling from a
--no-mouse start.
@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

Opened #436 with the problem statement per CONTRIBUTING (this PR went up first — happy to rework per discussion there).

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the toggle, but as an opt-in action with no default key. The terminal's own selection modifiers (Shift, Option or Ctrl+Shift, depending on the terminal) already cover plain terminals. The case this helps is a multiplexer like herdr, where turning tracking off lets it select inside revdiff's pane only. Users who need it can add map m toggle_mouse to their keybindings.

changes:

  • drop the m default from defaultBindings and replace the binding test entry with an assertion that toggle_mouse is unbound by default
  • add a status-bar indicator, so a suspended mouse is visible: an entry in statusIconForAction plus a row in statusModeIcons, lit while tracking is off. Show it only when the action is bound (len(m.keymap.KeysFor(keymap.ActionToggleMouse)) > 0), so users who never map it don't get a new icon. Extend TestBuildHelpSpec_StatusIconsOnToggleRows with an explicitly bound toggle_mouse, since the default keymap won't have it
  • tests for the toggle from both startup states (tracking on and --no-mouse), and for the editor, source editor and post-flush callbacks after toggling off or on. Check that each callback restores the current setting, not the startup one. A hand-built completion message doesn't pin the change
  • remove cfg.mouseTracking (app/ui/model.go:325 and its initializer at :938), nothing reads it now
  • app/ui/editor.go:96 still says "originally enabled", and the handleMouseToggle comment describes the implementation. // handleMouseToggle toggles mouse tracking. is enough

docs are optional. README, site/docs.html and the plugin's references/usage.md should mention the action with a map example, but I'll accept the PR without them and add the docs myself.

@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

I'm ok with the toggle, but as an opt-in action with no default key. The terminal's own selection modifiers (Shift, Option or Ctrl+Shift, depending on the terminal) already cover plain terminals. The case this helps is a multiplexer like herdr, where turning tracking off lets it select inside revdiff's pane only. Users who need it can add map m toggle_mouse to their keybindings.

changes:

* drop the `m` default from `defaultBindings` and replace the binding test entry with an assertion that `toggle_mouse` is unbound by default

* add a status-bar indicator, so a suspended mouse is visible: an entry in `statusIconForAction` plus a row in `statusModeIcons`, lit while tracking is off. Show it only when the action is bound (`len(m.keymap.KeysFor(keymap.ActionToggleMouse)) > 0`), so users who never map it don't get a new icon. Extend `TestBuildHelpSpec_StatusIconsOnToggleRows` with an explicitly bound `toggle_mouse`, since the default keymap won't have it

* tests for the toggle from both startup states (tracking on and `--no-mouse`), and for the editor, source editor and post-flush callbacks after toggling off or on. Check that each callback restores the current setting, not the startup one. A hand-built completion message doesn't pin the change

* remove `cfg.mouseTracking` (`app/ui/model.go:325` and its initializer at :938), nothing reads it now

* `app/ui/editor.go:96` still says "originally enabled", and the `handleMouseToggle` comment describes the implementation. `// handleMouseToggle toggles mouse tracking.` is enough

docs are optional. README, site/docs.html and the plugin's references/usage.md should mention the action with a map example, but I'll accept the PR without them and add the docs myself.

yes, I'll update PR. BTW: not of those options (Shift, Option or Ctrl+Shift ...) are working in native MacOs terminal for text selection.

…ent-state restore

- no default binding: plain terminals select through their own modifiers;
  users map the action explicitly (test asserts it stays unbound)
- status-bar indicator shown only when the action is bound, lit while
  tracking is suspended
- completion handlers (editor, source editor, post-flush) read the current
  tracking state instead of a restoreMouse flag carried through completion
  messages, so the new tests drive the real toggle from both startup states
  instead of hand-built messages; cfg.mouseTracking is gone
- README documents the action with a map example
@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

Pushed the reworked branch (84e2ce1):

  • toggle_mouse has no default binding; a test asserts it stays unbound, and the README/help show a map m toggle_mouse example.
  • Status-bar indicator: m glyph, shown only when the action is bound, lit while tracking is suspended. TestBuildHelpSpec_StatusIconsOnToggleRows covers the explicitly bound case.
  • The restoreMouse flag is gone from editorFinishedMsg / sourceEditorFinishedMsg / postFlushFinishedMsg — the completion handlers read the current tracking state directly, so a session suspended via toggle_mouse stays suspended across editor and flush round-trips. With the flag gone, nothing is carried by hand-built messages: the new tests toggle the model and drive the real completion handlers from both startup states (tracking on and --no-mouse).
  • cfg.mouseTracking removed; stale "originally enabled" comment replaced.
  • README updated; left site/docs.html and the plugin's references/usage.md to you as offered.

go test ./... and golangci-lint run are clean.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest of the last round checks out, two small things left.

  • app/ui/model.go:1521 - the new tests call handleMouseToggle() directly and discard its command, so nothing pins which command the toggle sends or that a bound key reaches it. Swapping tea.DisableMouse for tea.EnableMouseCellMotion at :1526, or dropping keymap.ActionToggleMouse from the dispatchAction case at :1165, keeps the whole app/ui suite green. Fix: one test that builds the model with NewModel(ModelConfig{MouseTracking: ...}) for both startup states (like output_test.go:169), binds a key to toggle_mouse, sends it through Update, checks mouseTracking and the exact command, then presses it again and checks the reverse.
  • app/ui/mouse.go:155 - the handleMouse godoc says the handler never runs with --no-mouse, which is no longer true once such a session toggles tracking on. Fix: something like "mouse events arrive only while terminal mouse tracking is enabled".

@umputun

umputun commented Nov 5, 2026

Copy link
Copy Markdown
Owner

BTW: not of those options (Shift, Option or Ctrl+Shift ...) are working in native MacOs terminal for text selection.

Just use the proper terminal, and you will be able to select with the mouse. Also, you may not even need a multiplexer at all ;)

…sg constructors

the original completion plumbing and tests stay verbatim: restoreMouse
remains on editorFinishedMsg / sourceEditorFinishedMsg / postFlushFinishedMsg
and the handlers keep reading it. the exec closures now build their messages
through small constructors that snapshot the current tracking state, so the
toggle-flow tests pin the wiring without hand-built messages and without
touching existing behavior.
@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

Reworked per the minimal/incremental preference: the existing restoreMouse plumbing and every pre-existing test are back verbatim — handlers still read the flag from the completion messages, --no-mouse behavior unchanged.

The only production change beyond the original feature is that the three exec closures now build their completion messages through tiny constructors (newEditorFinishedMsg / newSourceEditorFinishedMsg / newPostFlushFinishedMsg) that snapshot the current tracking state. That seam lets the new toggle-flow tests (tracking on → toggled off, --no-mouse start → toggled on, for editor / source editor / post-flush) pin the wiring without hand-built messages, while old code paths and tests stay untouched. cfg.mouseTracking is still removed as it has no readers left.

go test ./... and golangci-lint run are clean.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't ask for the 22c4587 rework. It brings back the restoreMouse plumbing 84e2ce1 removed, behaves the same, and adds three constructors and message fields. Pls revert it and keep the 84e2ce1 handlers reading m.mouseTracking.

both items from my last review are still open:

  • app/ui/model.go:1521 - nothing pins the toggle itself. Swapping tea.DisableMouse for tea.EnableMouseCellMotion at :1526, or dropping keymap.ActionToggleMouse from the dispatchAction case at :1165, still keeps the app/ui suite green on 22c4587. Fix: one test that builds the model with NewModel(ModelConfig{MouseTracking: ...}) for both startup states, binds a key to toggle_mouse, sends it through Update, checks mouseTracking and the exact command, then presses it again and checks the reverse.
  • app/ui/mouse.go:155 - the handleMouse godoc still says the handler never runs with --no-mouse. Fix: something like "mouse events arrive only while terminal mouse tracking is enabled".

@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

I didn't ask for the 22c4587 rework.

you absolutely right, my bad, i'll fix it.

…odoc

the toggle-flow tests called handleMouseToggle directly and discarded its
command, so a swapped emitted command or a dropped dispatchAction case kept
the suite green. the new test binds a key to toggle_mouse, sends it through
Update from both startup states, and asserts mouseTracking plus the exact
command on each press.

handleMouse godoc claimed the handler never runs under --no-mouse, which is
no longer true once such a session toggles tracking on.
@mikhail-angelov

Copy link
Copy Markdown
Contributor Author

Done:

  • Reverted 22c4587 — the handlers are back to reading m.mouseTracking directly, with no constructors or restored message fields.
  • New TestToggleMouse_KeyThroughUpdate builds the model via NewModel/testNewModel with MouseTracking true and false, binds a key to toggle_mouse, sends it through Update, and asserts both mouseTracking and the exact command (DisableMouse / EnableMouseCellMotion) on each press — so a swapped command or a dropped dispatchAction case now fails the suite.
  • handleMouse godoc now says mouse events arrive only while terminal mouse tracking is enabled (a --no-mouse session can toggle it on).

go test ./... and golangci-lint run clean.

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@umputun
umputun merged commit a1ad12a into umputun:master Nov 5, 2026
3 checks passed
umputun added a commit that referenced this pull request Nov 5, 2026
mirror the README toggle_mouse text into site/docs.html and both plugin
usage.md copies, add the action to both plugin config.md action lists, and
drop the comments #435 added to its tests.
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