From b42787cf29fc694321d373acab1a7c3a79678f84 Mon Sep 17 00:00:00 2001 From: tdwd <111124579+tdwd@users.noreply.github.com> Date: Fri, 9 Oct 2026 12:39:42 +0200 Subject: [PATCH 1/2] Move the dashboard's mouse handling to dashmouse.go mouse.go keeps the routing and the session view. dashboardMouse, dashboardHit, click and wheel move unchanged, with their comments, so the next change to the pane's wheel does not push mouse.go past 200 lines. --- docs/architecture.md | 3 +- internal/ui/dashmouse.go | 102 ++++++++++++++++++++++++++++++++++++++ internal/ui/mouse.go | 104 ++------------------------------------- 3 files changed, 108 insertions(+), 101 deletions(-) create mode 100644 internal/ui/dashmouse.go diff --git a/docs/architecture.md b/docs/architecture.md index 3f9e6a8..3a361cd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -87,7 +87,8 @@ internal/ui the Bubble Tea program, split by job: app.go Update, View — the shell input.go who receives a keystroke mouse.go where a click or a wheel notch lands; - hittest.go records what each cell stands for + hittest.go records what each cell stands for, + dashmouse.go acts on the dashboard keyroutes.go where a key goes once modals decline it keys.go the binding table; ptykeys.go encodes to bytes selection.go the sidebar's cursor diff --git a/internal/ui/dashmouse.go b/internal/ui/dashmouse.go new file mode 100644 index 0000000..ed7fd68 --- /dev/null +++ b/internal/ui/dashmouse.go @@ -0,0 +1,102 @@ +package ui + +// Mouse on the dashboard: which column and row a press lands on, and what a +// click or a wheel notch does there. mouse.go routes the press here. + +import tea "github.com/charmbracelet/bubbletea" + +// dashboardMouse acts on a press over the dashboard. Every press is recorded +// in lastPress, a wheel notch and a miss included, and handleKey clears it, so +// only two presses on one row with nothing between them count as a second +// click. +func (m Model) dashboardMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) { + col, t, ok := m.dashboardHit(msg.X, msg.Y) + again := t == m.lastPress + m.lastPress = target{} + if !ok { + return m, nil + } + switch msg.Button { + case tea.MouseButtonLeft: + m.lastPress = t + return m.click(t, again) + case tea.MouseButtonWheelUp: + m.wheel(col, -1) + case tea.MouseButtonWheelDown: + m.wheel(col, 1) + } + return m, nil +} + +// dashboardHit resolves a cell of the dashboard to the column it is in and the +// target it stands for. The target is hitNone on a heading, a blank, a marker +// or a column's top border. ok is false outside both columns: on the header +// and the footer. +func (m Model) dashboardHit(x, y int) (col column, t target, ok bool) { + l := m.dashboardLayout() + row := y - l.top + if row < -columnBorderRows || row >= l.rows || x < 0 || x >= m.width { + return 0, target{}, false + } + if x < l.navW { + lines, rows, _ := m.projectListWindow(l.navW, l.rows) + if row >= 0 && rows[row] >= 0 { + t = lines[rows[row]].at(x) + } + return colProjects, t, true + } + lines := m.detailLines(l.detailW) + if row >= 0 && row < len(lines) { + t = lines[row].at(x - l.navW - detailIndent) + } + return colContent, t, true +} + +// click acts on a left click. A click on a row puts the cursor and the focus +// there. A second press in a row on that same row opens it, as ↵ would: the +// first press already put the cursor and the focus on it, and nothing but a +// press or a key can move them in between, and each of those clears or +// replaces lastPress. +// +// Having the cursor is not enough on its own. At launch the first project +// already has the cursor and the focus, and opening it on a first click would +// start an agent from a click meant only to select. No double-click timing is +// involved, so there is none to get wrong. +func (m Model) click(t target, again bool) (tea.Model, tea.Cmd) { + switch t.kind { + case hitProject: + if again { + return m.openFromDashboard() + } + m.focus = colProjects + if m.projectIx != t.index { + m.selectProject(t.index) + } + case hitSession: + if again { + return m.openFromDashboard() + } + m.focus, m.listIx = colContent, t.index + case hitTab: + if m.tabIx != t.index { + m.selectTab(t.index) + } + m.focusContent() + } + return m, nil +} + +// wheel moves the cursor in the column under the pointer, as ↑ and ↓ move it in +// the focused one, and focuses that column so the keys then drive the list that +// just moved. +// +// An empty sessions list has nothing to move, so it takes no focus, and says +// nothing either. Unlike a click, a notch is often not aimed, and a notice +// stays in the footer until another replaces it. +func (m *Model) wheel(col column, delta int) { + if col == colContent && !m.hasSessionList() { + return + } + m.focus = col + m.moveDashboard(delta) +} diff --git a/internal/ui/mouse.go b/internal/ui/mouse.go index b60e3aa..f369829 100644 --- a/internal/ui/mouse.go +++ b/internal/ui/mouse.go @@ -1,9 +1,9 @@ package ui -// Mouse routing: where an event lands and what it does there. The agent pane -// takes the wheel as arrow keys. The dashboard and the session sidebar take -// clicks and the wheel, and resolve them against the lines their builders drew -// (hittest.go), never against a second copy of the layout. +// Mouse routing, and the mouse on the session view. The agent pane takes the +// wheel as arrow keys. The dashboard (dashmouse.go) and the session sidebar +// take clicks and the wheel, and resolve them against the lines their builders +// drew (hittest.go), never against a second copy of the layout. import tea "github.com/charmbracelet/bubbletea" @@ -93,99 +93,3 @@ func (m *Model) scrollPane(b tea.MouseButton) { m.sendToAgent([]byte("\x1b[B")) } } - -// dashboardMouse acts on a press over the dashboard. Every press is recorded -// in lastPress, a wheel notch and a miss included, and handleKey clears it, so -// only two presses on one row with nothing between them count as a second -// click. -func (m Model) dashboardMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) { - col, t, ok := m.dashboardHit(msg.X, msg.Y) - again := t == m.lastPress - m.lastPress = target{} - if !ok { - return m, nil - } - switch msg.Button { - case tea.MouseButtonLeft: - m.lastPress = t - return m.click(t, again) - case tea.MouseButtonWheelUp: - m.wheel(col, -1) - case tea.MouseButtonWheelDown: - m.wheel(col, 1) - } - return m, nil -} - -// dashboardHit resolves a cell of the dashboard to the column it is in and the -// target it stands for. The target is hitNone on a heading, a blank, a marker -// or a column's top border. ok is false outside both columns: on the header -// and the footer. -func (m Model) dashboardHit(x, y int) (col column, t target, ok bool) { - l := m.dashboardLayout() - row := y - l.top - if row < -columnBorderRows || row >= l.rows || x < 0 || x >= m.width { - return 0, target{}, false - } - if x < l.navW { - lines, rows, _ := m.projectListWindow(l.navW, l.rows) - if row >= 0 && rows[row] >= 0 { - t = lines[rows[row]].at(x) - } - return colProjects, t, true - } - lines := m.detailLines(l.detailW) - if row >= 0 && row < len(lines) { - t = lines[row].at(x - l.navW - detailIndent) - } - return colContent, t, true -} - -// click acts on a left click. A click on a row puts the cursor and the focus -// there. A second press in a row on that same row opens it, as ↵ would: the -// first press already put the cursor and the focus on it, and nothing but a -// press or a key can move them in between, and each of those clears or -// replaces lastPress. -// -// Having the cursor is not enough on its own. At launch the first project -// already has the cursor and the focus, and opening it on a first click would -// start an agent from a click meant only to select. No double-click timing is -// involved, so there is none to get wrong. -func (m Model) click(t target, again bool) (tea.Model, tea.Cmd) { - switch t.kind { - case hitProject: - if again { - return m.openFromDashboard() - } - m.focus = colProjects - if m.projectIx != t.index { - m.selectProject(t.index) - } - case hitSession: - if again { - return m.openFromDashboard() - } - m.focus, m.listIx = colContent, t.index - case hitTab: - if m.tabIx != t.index { - m.selectTab(t.index) - } - m.focusContent() - } - return m, nil -} - -// wheel moves the cursor in the column under the pointer, as ↑ and ↓ move it in -// the focused one, and focuses that column so the keys then drive the list that -// just moved. -// -// An empty sessions list has nothing to move, so it takes no focus, and says -// nothing either. Unlike a click, a notch is often not aimed, and a notice -// stays in the footer until another replaces it. -func (m *Model) wheel(col column, delta int) { - if col == colContent && !m.hasSessionList() { - return - } - m.focus = col - m.moveDashboard(delta) -} From 8f7595f0e6d026467c4fbe6ef87c57a7a0656964 Mon Sep 17 00:00:00 2001 From: tdwd <111124579+tdwd@users.noreply.github.com> Date: Fri, 9 Oct 2026 12:41:51 +0200 Subject: [PATCH 2/2] Send the wheel to an agent that tracks the mouse as a wheel report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A wheel notch over the pane went to the agent as the ↑ or ↓ key. cathode with mouse capture on, its default, reads ↑ as the previous prompt, so the wheel recalled history instead of scrolling until /mouse turned capture off, and that had to be done again in every session. The runner now follows the agent's mouse modes through the emulator's mode callbacks, a full reset included. An agent that set a tracking mode gets a wheel report at the pane cell under the pointer, SGR if it set ?1006 and X10 if not. Any other agent still gets ↑ or ↓. Runner.Wheel encodes the report and writes it to the PTY as a key is written. The emulator's SendMouse writes to its reply pipe while holding the emulator lock, so an agent that stopped reading its input could freeze every render, and its X10 encoder writes a coordinate of 95 or more as two UTF-8 bytes. The notch is clamped to the pane rows on screen, and a terminal too short to draw the pane sends nothing. sendToAgent becomes a wrapper over toAgent, so the wheel reports a failed write the way a key does. --- README.md | 4 +- docs/architecture.md | 20 ++++++++- internal/agent/agent.go | 4 ++ internal/agent/mouse.go | 81 ++++++++++++++++++++++++++++++++++++ internal/agent/mouse_test.go | 79 +++++++++++++++++++++++++++++++++++ internal/ui/input.go | 15 +++++-- internal/ui/mouse.go | 43 +++++++++++++------ internal/ui/mouse_test.go | 63 ++++++++++++++++++++++++++++ 8 files changed, 290 insertions(+), 19 deletions(-) create mode 100644 internal/agent/mouse.go create mode 100644 internal/agent/mouse_test.go diff --git a/README.md b/README.md index df5d6a9..b93092c 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,9 @@ column under the pointer. The session sidebar works the same way. A click on a card selects that session, as `^g j` does, so moving from one running agent to another keeps the keyboard attached. A second click opens it. The wheel over the sidebar -moves the cursor, and over the pane it goes to the agent. +moves the cursor, and over the pane it goes to the agent. An agent that +tracks the mouse, as cathode does with mouse capture on, gets it as a wheel +event. Any other agent gets `↑` and `↓`. | Command — press `ctrl+g` first | | |---|---| diff --git a/docs/architecture.md b/docs/architecture.md index 3a361cd..2e4a64c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -67,6 +67,7 @@ internal/agent PTY + vt emulator per session <- the subtle package agent.go Runner, Start, Stop pty.go the two pumps, writes, resize, teardown render.go the cell walk + mouse.go the agent's mouse modes, and the wheel status.go working / idle / exited, and how quiet env.go what a hosted agent must not inherit headless.go one turn with no pane, and what it cost @@ -734,6 +735,23 @@ that fits is shown whole. A click selects through `landOn`, as `^g j` does, so a click from one live agent to another stays attached. The wheel over the sidebar moves its cursor, and over the pane it still goes to the agent. +The wheel reaches the agent in the form the agent asked for (`Runner.Wheel`). +The emulator reports each mode change through a callback, so the runner knows +when the agent has set a mouse-tracking mode. An agent that has set one gets +a wheel report at the pane cell under the pointer: SGR if the agent also set +mode 1006, X10 if not, including when it asked for another encoding. Any +other agent gets `↑` or `↓`. Sending the key to an agent that tracks the +mouse is wrong: cathode with mouse capture on reads `↑` as its previous +prompt, so the wheel recalled history instead of scrolling until `/mouse` +turned capture off. + +`Runner.Wheel` encodes the report itself and writes it to the PTY as a key is +written. The emulator's `SendMouse` would be shorter and is not safe here. It +writes to the reply pipe while holding the emulator lock, so an agent that +stopped reading its input would block it, then the read pump, then every +render. Its X10 encoder also writes a coordinate of 95 or more as two UTF-8 +bytes. + The session view is held to the terminal's height for the same reason the dashboard is. The agent's terminal never drops under five rows (`paneSize`), and the pane used to be drawn at that height, so a terminal shorter than eight @@ -752,7 +770,7 @@ untouched. A corrupt `state.json` fails the launch rather than starting empty, because starting empty invites duplicate worktrees over sessions we cannot see. Closing a session leaves its worktree on disk and says where. `model.fault` holds errors the user must see and is never auto-cleared. Every write into an -agent pane goes through `sendToAgent`, so no keystroke, literal `^g` or wheel +agent pane goes through `toAgent`, so no keystroke, literal `^g` or wheel notch can drop a failed write. Reporting is not the same as abandoning. `ui.formProblem` puts a failure *into* diff --git a/internal/agent/agent.go b/internal/agent/agent.go index abdcd7e..7f90a64 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -16,6 +16,7 @@ import ( "sync" "time" + "github.com/charmbracelet/x/ansi" "github.com/charmbracelet/x/vt" "github.com/creack/pty" ) @@ -74,6 +75,7 @@ type Runner struct { started time.Time exited bool exitErr error + mouse map[ansi.Mode]bool // mouse modes the agent has set; see trackModes } // Start launches the agent process attached to a new PTY. @@ -114,7 +116,9 @@ func Start(cfg Config) (*Runner, error) { ptmx: ptmx, term: term, started: time.Now(), + mouse: map[ansi.Mode]bool{}, } + r.trackModes() go r.pump() go r.respond() return r, nil diff --git a/internal/agent/mouse.go b/internal/agent/mouse.go new file mode 100644 index 0000000..7753d1c --- /dev/null +++ b/internal/agent/mouse.go @@ -0,0 +1,81 @@ +package agent + +// Mouse input to the agent: which mouse reports the agent has asked for, and +// the wheel notches Deck sends it in the form it asked for. + +import ( + "slices" + + "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/x/vt" +) + +// mouseTracking lists the modes in which a terminal reports wheel notches as +// mouse events. They are the modes the emulator's own SendMouse acts on. +var mouseTracking = []ansi.Mode{ + ansi.ModeMouseX10, + ansi.ModeMouseNormal, + ansi.ModeMouseHighlight, + ansi.ModeMouseButtonEvent, + ansi.ModeMouseAnyEvent, +} + +// x10Limit is the last 0-based cell an X10 report can carry: a coordinate is +// one byte, offset by 33, so 222 is the most that fits in 255. +const x10Limit = 255 - 33 + +// trackModes keeps r.mouse current from the emulator's mode changes: the +// tracking modes and SGR encoding (?1006). A full reset (RIS) sets every mode +// again through the same path, so the record cannot outlive a reset. It must +// be called before the read pump starts. +func (r *Runner) trackModes() { + set := func(on bool) func(ansi.Mode) { + return func(mode ansi.Mode) { + if mode == ansi.ModeMouseExtSgr || slices.Contains(mouseTracking, mode) { + r.mu.Lock() + r.mouse[mode] = on + r.mu.Unlock() + } + } + } + r.term.SetCallbacks(vt.Callbacks{EnableMode: set(true), DisableMode: set(false)}) +} + +// Wheel sends one wheel notch at cell (x, y) of the agent's screen. +// +// An agent that asked for mouse reports gets a wheel report, which it reads as +// a scroll. The report is SGR if the agent set ?1006 and X10 otherwise, even +// if it asked for another encoding (?1005, ?1015, ?1016) instead. Any other +// agent gets the ↑ or ↓ key, so a pane with no mouse tracking still moves +// under the wheel. Sending the key to an agent that tracks the mouse is wrong: +// cathode, with mouse capture on, reads ↑ as the previous prompt from its +// history, not as a scroll. +// +// The report is encoded here and written as a key is, not sent through the +// emulator's SendMouse. SendMouse writes to the reply pipe while holding the +// emulator lock, so an agent that stopped reading its input would block it, +// then the read pump, then every Render. Its X10 encoder also writes a +// coordinate of 95 or more as two UTF-8 bytes. +func (r *Runner) Wheel(up bool, x, y int) error { + r.mu.Lock() + tracking, sgr := false, r.mouse[ansi.ModeMouseExtSgr] + for _, t := range mouseTracking { + tracking = tracking || r.mouse[t] + } + r.mu.Unlock() + + button, key := ansi.MouseWheelDown, "\x1b[B" + if up { + button, key = ansi.MouseWheelUp, "\x1b[A" + } + b := ansi.EncodeMouseButton(button, false, false, false, false) + switch { + case !tracking: + return r.Write([]byte(key)) + case sgr: + return r.Write([]byte(ansi.MouseSgr(b, x, y, false))) + default: + x, y = min(x, x10Limit), min(y, x10Limit) + return r.Write([]byte{0x1b, '[', 'M', b + 32, byte(x + 33), byte(y + 33)}) + } +} diff --git a/internal/agent/mouse_test.go b/internal/agent/mouse_test.go new file mode 100644 index 0000000..69817c8 --- /dev/null +++ b/internal/agent/mouse_test.go @@ -0,0 +1,79 @@ +package agent + +import ( + "fmt" + "strings" + "testing" + "time" +) + +// TestWheelUsesTheFormTheAgentAskedFor: an agent that set a mouse-tracking +// mode gets the wheel as a mouse report at the cell given, SGR if it also set +// ?1006 and X10 if not. One that never set it, cleared it, or was reset gets +// the ↑ or ↓ key. cathode with mouse capture on reads ↑ as prompt history, so +// sending it the key would recall an old prompt instead of scrolling. +// +// The script prints the bytes it read as decimals, so the test compares bytes +// rather than what a screen makes of them, and then says whether anything +// followed: a report and a key together would still recall cathode's history. +// It reads with dd, one byte at a time, because BSD head reads ahead and would +// swallow whatever followed in the same write. +func TestWheelUsesTheFormTheAgentAskedFor(t *testing.T) { + const sgr = `\033[?1000h\033[?1006h` + for name, tc := range map[string]struct { + modes string // what the agent prints before it reads + up bool + x, y int + want string + }{ + "sgr, up": {sgr, true, 3, 4, "\x1b[<64;4;5M"}, + "x10 compatibility": {`\033[?9h\033[?1006h`, true, 3, 4, "\x1b[<64;4;5M"}, + "highlight tracking": {`\033[?1001h\033[?1006h`, true, 3, 4, "\x1b[<64;4;5M"}, + "button-event": {`\033[?1002h\033[?1006h`, true, 3, 4, "\x1b[<64;4;5M"}, + "any-event": {`\033[?1003h\033[?1006h`, true, 3, 4, "\x1b[<64;4;5M"}, + "sgr, down": {sgr, false, 3, 4, "\x1b[<65;4;5M"}, + "x10": {`\033[?1000h`, true, 3, 4, "\x1b[M`$%"}, + "x10 past its range": {`\033[?1000h`, true, 300, 4, "\x1b[M`\xff%"}, + "x10 below its range": {`\033[?1000h`, true, 3, 300, "\x1b[M`$\xff"}, + "tracking cleared": {sgr + `\033[?1000l`, true, 3, 4, "\x1b[A"}, + "reset (RIS)": {sgr + `\033c`, false, 3, 4, "\x1b[B"}, + "never on": {``, true, 3, 4, "\x1b[A"}, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + script := fmt.Sprintf(`stty -echo -icanon; printf '%s'; echo READY; `+ + `dd bs=1 count=%d 2>/dev/null | od -An -tu1 | tr -s ' \n' ' '; echo END; `+ + `if IFS= read -r -t 1 -n 1 rest; then echo EXTRA; else echo CLEAN; fi; sleep 5`, + tc.modes, len(tc.want)) + r, err := Start(Config{ + Command: "bash", + Args: []string{"--norc", "--noprofile", "-c", script}, + Dir: t.TempDir(), + Width: 80, + Height: 24, + }) + if err != nil { + t.Fatalf("start: %v", err) + } + defer r.Stop() + + if !screenContains(t, r, "READY", 5*time.Second) { + t.Fatal("the script never got ready") + } + if err := r.Wheel(tc.up, tc.x, tc.y); err != nil { + t.Fatalf("wheel: %v", err) + } + var want strings.Builder + for _, b := range []byte(tc.want) { + fmt.Fprintf(&want, " %d", b) + } + want.WriteString(" END") + if !screenContains(t, r, want.String(), 5*time.Second) { + t.Errorf("want%s on screen:\n%s", want.String(), strings.Join(r.Render(false, nil, nil), "\n")) + } + if !screenContains(t, r, "CLEAN", 5*time.Second) { + t.Errorf("the agent got more than the one form:\n%s", strings.Join(r.Render(false, nil, nil), "\n")) + } + }) + } +} diff --git a/internal/ui/input.go b/internal/ui/input.go index 9aaaf3e..286ade2 100644 --- a/internal/ui/input.go +++ b/internal/ui/input.go @@ -8,6 +8,8 @@ import ( "fmt" tea "github.com/charmbracelet/bubbletea" + + "github.com/tripledownab/deck/internal/agent" ) func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { @@ -64,15 +66,20 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { return m.sessionKey(msg) } -// sendToAgent writes to the attached agent. Every path that types into the -// pane comes through here, so none of them can drop a failed write: it is -// reported, and the attachment goes with it. +// sendToAgent writes bytes to the attached agent. func (m *Model) sendToAgent(b []byte) { + m.toAgent(func(r *agent.Runner) error { return r.Write(b) }) +} + +// toAgent runs send against the attached agent. Every path that sends input +// to the pane comes through here, so none of them can drop a failed write: it +// is reported, and the attachment goes with it. +func (m *Model) toAgent(send func(*agent.Runner) error) { r := m.currentRunner() if r == nil || !m.attached { return } - if err := r.Write(b); err != nil { + if err := send(r); err != nil { m.fault = fmt.Errorf("send to agent: %w", err) m.attached = false } diff --git a/internal/ui/mouse.go b/internal/ui/mouse.go index f369829..48be77f 100644 --- a/internal/ui/mouse.go +++ b/internal/ui/mouse.go @@ -1,11 +1,16 @@ package ui // Mouse routing, and the mouse on the session view. The agent pane takes the -// wheel as arrow keys. The dashboard (dashmouse.go) and the session sidebar -// take clicks and the wheel, and resolve them against the lines their builders -// drew (hittest.go), never against a second copy of the layout. +// wheel in the form the agent asked for. The dashboard (dashmouse.go) and the +// session sidebar take clicks and the wheel, and resolve them against the +// lines their builders drew (hittest.go), never against a second copy of the +// layout. -import tea "github.com/charmbracelet/bubbletea" +import ( + tea "github.com/charmbracelet/bubbletea" + + "github.com/tripledownab/deck/internal/agent" +) // handleMouse routes a mouse event. // @@ -46,7 +51,7 @@ func (m Model) sessionMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) { again := t == m.lastPress m.lastPress = target{} if !over { - m.scrollPane(msg.Button) + m.scrollPane(msg) return m, nil } switch msg.Button { @@ -83,13 +88,25 @@ func (m Model) sidebarHit(x, y int) (t target, over bool) { return t, true } -// scrollPane sends a wheel notch to the attached agent as the ↑ or ↓ key. What -// that does is up to the agent. A pane that is not attached is not listening. -func (m *Model) scrollPane(b tea.MouseButton) { - switch b { - case tea.MouseButtonWheelUp: - m.sendToAgent([]byte("\x1b[A")) - case tea.MouseButtonWheelDown: - m.sendToAgent([]byte("\x1b[B")) +// scrollPane sends a wheel notch to the attached agent, in the form the agent +// asked for (see Runner.Wheel). What it does then is up to the agent. A pane +// that is not attached is not listening. +// +// The notch carries the pane cell under the pointer. A notch over the chrome +// around the pane is clamped to the nearest pane cell on screen. A terminal +// too short to draw the pane sends nothing. +func (m *Model) scrollPane(msg tea.MouseMsg) { + up := msg.Button == tea.MouseButtonWheelUp + if !up && msg.Button != tea.MouseButtonWheelDown { + return + } + sidebarW, bodyH := m.layout() + w, h := m.paneSize() + h = min(h, bodyH) // the rows drawn, which a short terminal cuts below paneSize + if h <= 0 { + return } + x := clamp(msg.X-sidebarW-paneChromeCols, 0, w-1) + y := clamp(msg.Y-sessionBodyTop, 0, h-1) + m.toAgent(func(r *agent.Runner) error { return r.Wheel(up, x, y) }) } diff --git a/internal/ui/mouse_test.go b/internal/ui/mouse_test.go index 8ffc704..ced109e 100644 --- a/internal/ui/mouse_test.go +++ b/internal/ui/mouse_test.go @@ -416,6 +416,69 @@ func TestWritesToAnExitedPaneAreReported(t *testing.T) { } } +// TestWheelOverThePaneReportsThePaneCell: an agent that tracks the mouse gets +// the wheel at the cell of its own screen under the pointer, not at the cell +// of Deck's frame. A notch over the chrome around the pane lands on the +// nearest pane cell on screen, and a press that is not a notch sends nothing. +// +// After the press the test writes a Z, and the script prints everything that +// came before it. A press that sent nothing prints GOT[], with no timeout in +// the way. +func TestWheelOverThePaneReportsThePaneCell(t *testing.T) { + for name, tc := range map[string]struct { + height int // the terminal's + button tea.MouseButton + dx, dy int // cells right of and below the pane's first cell + want string + }{ + "over the pane": {30, tea.MouseButtonWheelUp, 3, 4, "GOT[[<64;4;5M]"}, + "down over the pane": {30, tea.MouseButtonWheelDown, 3, 4, "GOT[[<65;4;5M]"}, + "over the pane rule": {30, tea.MouseButtonWheelUp, -paneChromeCols, 4, "GOT[[<64;1;5M]"}, + "over the top bar": {30, tea.MouseButtonWheelUp, 3, -sessionBodyTop, "GOT[[<64;4;1M]"}, + "a left click": {30, tea.MouseButtonLeft, 3, 4, "GOT[]"}, + // Six rows draw three of the pane's five. The footer is below the + // third, and clamps to it rather than to a row not on screen. + "over a short terminal's footer": {6, tea.MouseButtonWheelUp, 3, 3, "GOT[[<64;4;3M]"}, + // Three rows draw no pane at all, so a notch has no cell to report. + "on a terminal with no pane rows": {3, tea.MouseButtonWheelUp, 3, 0, "GOT[]"}, + } { + t.Run(name, func(t *testing.T) { + st := mouseState(1, 1) + st.Sessions[0].Dir = t.TempDir() + m := sized(New(st, "bash", nil), 120, tc.height) + r := startAgent(t, st.Sessions[0].Dir, `stty -echo -icanon; printf '\033[?1000h\033[?1006h'; `+ + `echo READY; IFS= read -r -d Z s; printf 'GOT[%s]\n' "${s#?}"; sleep 5`) + m.runners[st.Sessions[0].ID] = r + m.screen, m.attached = screenSession, true + + screen := func() string { return ansi.Strip(strings.Join(r.Render(false, nil, nil), "\n")) } + waitFor := func(want string) bool { + for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); { + if strings.Contains(screen(), want) { + return true + } + time.Sleep(25 * time.Millisecond) + } + return false + } + if !waitFor("READY") { + t.Fatal("the script never got ready") + } + sidebarW, _ := m.layout() + got := press(m, tc.button, sidebarW+paneChromeCols+tc.dx, sessionBodyTop+tc.dy) + if got.fault != nil { + t.Fatalf("fault: %v", got.fault) + } + if err := r.Write([]byte("Z")); err != nil { + t.Fatalf("write the sentinel: %v", err) + } + if !waitFor(tc.want) { + t.Errorf("want %s on screen:\n%s", tc.want, screen()) + } + }) + } +} + // TestDoubleClickInAScrolledListOpensTheRowClicked: the list must not move // under the pointer between two presses. It used to re-centre on every cursor // move, so the second press landed on whichever project had scrolled there