xonecas.io
62159
Sy

symb / files

symb / internal / tui / mouse.go

7.0 KB
1package tui
2
3import (
4	"time"
5
6	tea "charm.land/bubbletea/v2"
7	"charm.land/lipgloss/v2"
8)
9
10// ---------------------------------------------------------------------------
11// Mouse filter — throttle high-frequency events at program level.
12// ---------------------------------------------------------------------------
13
14var lastMouseEvent time.Time
15
16// MouseEventFilter rate-limits wheel and motion events (15 ms).
17// Pass to tea.WithFilter. Never drops clicks or releases.
18func MouseEventFilter(_ tea.Model, msg tea.Msg) tea.Msg {
19	switch msg.(type) {
20	case tea.MouseWheelMsg, tea.MouseMotionMsg:
21		now := time.Now()
22		if now.Sub(lastMouseEvent) < 15*time.Millisecond {
23			return nil
24		}
25		lastMouseEvent = now
26	}
27	return msg
28}
29
30// ---------------------------------------------------------------------------
31// Mouse handling — dialog-first when we add dialogs, then focus-based.
32// Coordinate translation via layout rects.
33// ---------------------------------------------------------------------------
34
35// mouseXY extracts X, Y from any mouse message via the MouseMsg interface.
36func mouseXY(msg tea.MouseMsg) (int, int) {
37	m := msg.Mouse()
38	return m.X, m.Y
39}
40
41func (m Model) handleMouse(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
42	x, y := mouseXY(msg)
43
44	// --- Input: translate coords to component-local -------------------------
45	if inRect(x, y, m.layout.input) {
46		translated := m.translateMouse(msg, m.layout.input.Min.X, m.layout.input.Min.Y)
47		var cmd tea.Cmd
48		m.agentInput, cmd = m.agentInput.Update(translated)
49		return m, cmd
50	}
51
52	// --- Conversation: scroll + selection -----------------------------------
53	if inRect(x, y, m.layout.conv) {
54		return m, m.handleConvMouse(msg, x, y)
55	}
56
57	return m, nil
58}
59
60// handleConvMouse handles mouse events within the conversation pane.
61func (m *Model) handleConvMouse(msg tea.MouseMsg, x, y int) tea.Cmd {
62	lines := m.wrappedConvLines()
63	totalLines := len(lines)
64
65	switch ev := msg.(type) {
66	case tea.MouseClickMsg:
67		if ev.Button == tea.MouseLeft && totalLines > 0 {
68			cp := m.convPosFromScreen(x, y, totalLines)
69			m.convDragging = true
70			m.convSel = &convSelection{anchor: cp, active: cp}
71			m.agentInput.ClearSelection()
72		}
73	case tea.MouseMotionMsg:
74		m.handleConvMotion(x, y, totalLines)
75	case tea.MouseReleaseMsg:
76		return m.handleConvRelease(x, y, totalLines)
77	case tea.MouseWheelMsg:
78		m.handleConvWheel(ev, totalLines)
79	}
80	return nil
81}
82
83func (m *Model) handleConvMotion(x, y, totalLines int) {
84	if m.convDragging && m.convSel != nil && totalLines > 0 {
85		m.convSel.active = m.convPosFromScreen(x, y, totalLines)
86	} else if !m.convDragging && m.convSel != nil {
87		m.convSel = nil
88	}
89
90}
91
92func (m *Model) handleConvRelease(x, y, totalLines int) tea.Cmd {
93	m.convDragging = false
94	if m.convSel != nil && m.convSel.empty() {
95		cp := m.convPosFromScreen(x, y, totalLines)
96		m.convSel = nil
97		// Ignore clicks on empty space past the last wrapped line.
98		startLine := m.visibleStartLine()
99		screenLine := startLine + (y - m.layout.conv.Min.Y)
100		if screenLine >= totalLines {
101			return nil
102		}
103		return m.handleConvClick(cp.line, cp.col)
104	}
105	return nil
106}
107
108func (m *Model) handleConvWheel(ev tea.MouseWheelMsg, totalLines int) {
109	convH := m.layout.conv.Dy()
110	if ev.Button == tea.MouseWheelUp {
111		maxScroll := totalLines - convH
112		if maxScroll < 0 {
113			maxScroll = 0
114		}
115		m.scrollOffset = min(m.scrollOffset+5, maxScroll)
116	} else if ev.Button == tea.MouseWheelDown {
117		m.scrollOffset = max(m.scrollOffset-5, 0)
118	}
119}
120
121// convPosFromScreen converts screen x,y to a convPos.
122func (m *Model) convPosFromScreen(x, y, totalLines int) convPos {
123	startLine := m.visibleStartLine()
124	clickedLine := startLine + (y - m.layout.conv.Min.Y)
125	if clickedLine < 0 {
126		clickedLine = 0
127	}
128	if clickedLine >= totalLines {
129		clickedLine = totalLines - 1
130	}
131	clickedCol := x - m.layout.conv.Min.X
132	if clickedCol < 0 {
133		clickedCol = 0
134	}
135	return convPos{line: clickedLine, col: clickedCol}
136}
137
138// translateMouse offsets a mouse message's coordinates for child components.
139func (m Model) translateMouse(msg tea.MouseMsg, offX, offY int) tea.Msg {
140	switch ev := msg.(type) {
141	case tea.MouseClickMsg:
142		ev.X -= offX
143		ev.Y -= offY
144		return ev
145	case tea.MouseMotionMsg:
146		ev.X -= offX
147		ev.Y -= offY
148		return ev
149	case tea.MouseReleaseMsg:
150		ev.X -= offX
151		ev.Y -= offY
152		return ev
153	case tea.MouseWheelMsg:
154		ev.X -= offX
155		ev.Y -= offY
156		return ev
157	}
158	return msg
159}
160
161// isClickableLine returns true if the wrapped line at lineIdx is clickable.
162// Tool result entries (first line only, which has the [view] button) and undo
163// entries are always clickable.
164func (m *Model) isClickableLine(lineIdx int) bool {
165	m.wrappedConvLines() // ensures convLineSource is also fresh
166	src := m.convLineSource
167	if lineIdx < 0 || lineIdx >= len(src) {
168		return false
169	}
170	entryIdx := src[lineIdx]
171	if entryIdx < 0 || entryIdx >= len(m.convEntries) {
172		return false
173	}
174	entry := m.convEntries[entryIdx]
175	switch entry.kind {
176	case entryToolResult:
177		// Only the first wrapped line (containing [view]) is clickable.
178		if lineIdx > 0 && src[lineIdx-1] == entryIdx {
179			return false
180		}
181		return true
182	case entryUndo:
183		return true
184	case entryToolDiag, entryToolCall, entrySeparator:
185		return false
186	default:
187		return false
188	}
189}
190
191// handleConvClick resolves a click on a wrapped conversation line.
192// Tool result [view] buttons open the relevant content in the editor.
193// Undo buttons trigger an undo.
194func (m *Model) handleConvClick(wrappedLine, col int) tea.Cmd {
195	m.wrappedConvLines() // ensure convLineSource is fresh
196	src := m.convLineSource
197	if wrappedLine < 0 || wrappedLine >= len(src) {
198		return nil
199	}
200	entryIdx := src[wrappedLine]
201	if entryIdx < 0 || entryIdx >= len(m.convEntries) {
202		return nil
203	}
204	entry := m.convEntries[entryIdx]
205
206	switch entry.kind {
207	case entryUndo:
208		if m.isClickOnCenteredLabel(entry.display, col) {
209			return func() tea.Msg { return undoMsg{} }
210		}
211		return nil
212
213	case entryToolResult:
214		// Only trigger on the [view] label at the end of the line.
215		if m.isClickOnViewLabel(entry.display, col) {
216			return m.handleToolResultView(entry)
217		}
218		return nil
219
220	case entryToolDiag, entryToolCall, entrySeparator:
221		return nil
222
223	default:
224		return nil
225	}
226}
227
228// isClickOnCenteredLabel checks whether a column falls within a centered
229// label's visible text area in the conversation pane.
230func (m *Model) isClickOnCenteredLabel(display string, col int) bool {
231	rw := m.convWidth()
232	lw := lipgloss.Width(display)
233	pad := (rw - lw) / 2
234	if pad < 0 {
235		pad = 0
236	}
237	return col >= pad && col < pad+lw
238}
239
240// isClickOnViewLabel checks whether a column falls on the "view" label
241// at the end of a tool result line.
242func (m *Model) isClickOnViewLabel(display string, col int) bool {
243	const viewLabel = "view"
244	lw := lipgloss.Width(display)
245	viewStart := lw - len(viewLabel)
246	if viewStart < 0 {
247		viewStart = 0
248	}
249	return col >= viewStart && col < lw
250}
251
252// handleToolResultView returns a Cmd that opens the tool view modal.
253func (m *Model) handleToolResultView(entry convEntry) tea.Cmd {
254	title := entry.toolName
255	if title == "" {
256		title = "Tool Result"
257	}
258	return func() tea.Msg {
259		return openToolViewMsg{title: title, content: entry.full}
260	}
261}