chat.go

  1package model
  2
  3import (
  4	tea "charm.land/bubbletea/v2"
  5	"github.com/charmbracelet/crush/internal/ui/anim"
  6	"github.com/charmbracelet/crush/internal/ui/chat"
  7	"github.com/charmbracelet/crush/internal/ui/common"
  8	"github.com/charmbracelet/crush/internal/ui/list"
  9	uv "github.com/charmbracelet/ultraviolet"
 10	"github.com/charmbracelet/x/ansi"
 11)
 12
 13// Chat represents the chat UI model that handles chat interactions and
 14// messages.
 15type Chat struct {
 16	com      *common.Common
 17	list     *list.List
 18	idInxMap map[string]int // Map of message IDs to their indices in the list
 19
 20	// Animation visibility optimization: track animations paused due to items
 21	// being scrolled out of view. When items become visible again, their
 22	// animations are restarted.
 23	pausedAnimations map[string]struct{}
 24
 25	// Mouse state
 26	mouseDown     bool
 27	mouseDownItem int // Item index where mouse was pressed
 28	mouseDownX    int // X position in item content (character offset)
 29	mouseDownY    int // Y position in item (line offset)
 30	mouseDragItem int // Current item index being dragged over
 31	mouseDragX    int // Current X in item content
 32	mouseDragY    int // Current Y in item
 33}
 34
 35// NewChat creates a new instance of [Chat] that handles chat interactions and
 36// messages.
 37func NewChat(com *common.Common) *Chat {
 38	c := &Chat{
 39		com:              com,
 40		idInxMap:         make(map[string]int),
 41		pausedAnimations: make(map[string]struct{}),
 42	}
 43	l := list.NewList()
 44	l.SetGap(1)
 45	l.RegisterRenderCallback(c.applyHighlightRange)
 46	c.list = l
 47	c.mouseDownItem = -1
 48	c.mouseDragItem = -1
 49	return c
 50}
 51
 52// Height returns the height of the chat view port.
 53func (m *Chat) Height() int {
 54	return m.list.Height()
 55}
 56
 57// Draw renders the chat UI component to the screen and the given area.
 58func (m *Chat) Draw(scr uv.Screen, area uv.Rectangle) {
 59	uv.NewStyledString(m.list.Render()).Draw(scr, area)
 60}
 61
 62// SetSize sets the size of the chat view port.
 63func (m *Chat) SetSize(width, height int) {
 64	m.list.SetSize(width, height)
 65}
 66
 67// Len returns the number of items in the chat list.
 68func (m *Chat) Len() int {
 69	return m.list.Len()
 70}
 71
 72// SetMessages sets the chat messages to the provided list of message items.
 73func (m *Chat) SetMessages(msgs ...chat.MessageItem) {
 74	m.idInxMap = make(map[string]int)
 75	m.pausedAnimations = make(map[string]struct{})
 76
 77	items := make([]list.Item, len(msgs))
 78	for i, msg := range msgs {
 79		m.idInxMap[msg.ID()] = i
 80		items[i] = msg
 81	}
 82	m.list.SetItems(items...)
 83	m.list.ScrollToBottom()
 84}
 85
 86// AppendMessages appends a new message item to the chat list.
 87func (m *Chat) AppendMessages(msgs ...chat.MessageItem) {
 88	items := make([]list.Item, len(msgs))
 89	indexOffset := m.list.Len()
 90	for i, msg := range msgs {
 91		m.idInxMap[msg.ID()] = indexOffset + i
 92		items[i] = msg
 93	}
 94	m.list.AppendItems(items...)
 95}
 96
 97// Animate animates items in the chat list. Only propagates animation messages
 98// to visible items to save CPU. When items are not visible, their animation ID
 99// is tracked so it can be restarted when they become visible again.
100func (m *Chat) Animate(msg anim.StepMsg) tea.Cmd {
101	idx, ok := m.idInxMap[msg.ID]
102	if !ok {
103		return nil
104	}
105
106	animatable, ok := m.list.ItemAt(idx).(chat.Animatable)
107	if !ok {
108		return nil
109	}
110
111	// Check if item is currently visible.
112	startIdx, endIdx := m.list.VisibleItemIndices()
113	isVisible := idx >= startIdx && idx <= endIdx
114
115	if !isVisible {
116		// Item not visible - pause animation by not propagating.
117		// Track it so we can restart when it becomes visible.
118		m.pausedAnimations[msg.ID] = struct{}{}
119		return nil
120	}
121
122	// Item is visible - remove from paused set and animate.
123	delete(m.pausedAnimations, msg.ID)
124	return animatable.Animate(msg)
125}
126
127// RestartPausedVisibleAnimations restarts animations for items that were paused
128// due to being scrolled out of view but are now visible again.
129func (m *Chat) RestartPausedVisibleAnimations() tea.Cmd {
130	if len(m.pausedAnimations) == 0 {
131		return nil
132	}
133
134	startIdx, endIdx := m.list.VisibleItemIndices()
135	var cmds []tea.Cmd
136
137	for id := range m.pausedAnimations {
138		idx, ok := m.idInxMap[id]
139		if !ok {
140			// Item no longer exists.
141			delete(m.pausedAnimations, id)
142			continue
143		}
144
145		if idx >= startIdx && idx <= endIdx {
146			// Item is now visible - restart its animation.
147			if animatable, ok := m.list.ItemAt(idx).(chat.Animatable); ok {
148				if cmd := animatable.StartAnimation(); cmd != nil {
149					cmds = append(cmds, cmd)
150				}
151			}
152			delete(m.pausedAnimations, id)
153		}
154	}
155
156	if len(cmds) == 0 {
157		return nil
158	}
159	return tea.Batch(cmds...)
160}
161
162// Focus sets the focus state of the chat component.
163func (m *Chat) Focus() {
164	m.list.Focus()
165}
166
167// Blur removes the focus state from the chat component.
168func (m *Chat) Blur() {
169	m.list.Blur()
170}
171
172// ScrollToTopAndAnimate scrolls the chat view to the top and returns a command to restart
173// any paused animations that are now visible.
174func (m *Chat) ScrollToTopAndAnimate() tea.Cmd {
175	m.list.ScrollToTop()
176	return m.RestartPausedVisibleAnimations()
177}
178
179// ScrollToBottomAndAnimate scrolls the chat view to the bottom and returns a command to
180// restart any paused animations that are now visible.
181func (m *Chat) ScrollToBottomAndAnimate() tea.Cmd {
182	m.list.ScrollToBottom()
183	return m.RestartPausedVisibleAnimations()
184}
185
186// ScrollByAndAnimate scrolls the chat view by the given number of line deltas and returns
187// a command to restart any paused animations that are now visible.
188func (m *Chat) ScrollByAndAnimate(lines int) tea.Cmd {
189	m.list.ScrollBy(lines)
190	return m.RestartPausedVisibleAnimations()
191}
192
193// ScrollToSelectedAndAnimate scrolls the chat view to the selected item and returns a
194// command to restart any paused animations that are now visible.
195func (m *Chat) ScrollToSelectedAndAnimate() tea.Cmd {
196	m.list.ScrollToSelected()
197	return m.RestartPausedVisibleAnimations()
198}
199
200// SelectedItemInView returns whether the selected item is currently in view.
201func (m *Chat) SelectedItemInView() bool {
202	return m.list.SelectedItemInView()
203}
204
205// SetSelected sets the selected message index in the chat list.
206func (m *Chat) SetSelected(index int) {
207	m.list.SetSelected(index)
208}
209
210// SelectPrev selects the previous message in the chat list.
211func (m *Chat) SelectPrev() {
212	m.list.SelectPrev()
213}
214
215// SelectNext selects the next message in the chat list.
216func (m *Chat) SelectNext() {
217	m.list.SelectNext()
218}
219
220// SelectFirst selects the first message in the chat list.
221func (m *Chat) SelectFirst() {
222	m.list.SelectFirst()
223}
224
225// SelectLast selects the last message in the chat list.
226func (m *Chat) SelectLast() {
227	m.list.SelectLast()
228}
229
230// SelectFirstInView selects the first message currently in view.
231func (m *Chat) SelectFirstInView() {
232	m.list.SelectFirstInView()
233}
234
235// SelectLastInView selects the last message currently in view.
236func (m *Chat) SelectLastInView() {
237	m.list.SelectLastInView()
238}
239
240// GetMessageItem returns the message item at the given id.
241func (m *Chat) GetMessageItem(id string) chat.MessageItem {
242	idx, ok := m.idInxMap[id]
243	if !ok {
244		return nil
245	}
246	item, ok := m.list.ItemAt(idx).(chat.MessageItem)
247	if !ok {
248		return nil
249	}
250	return item
251}
252
253// ToggleExpandedSelectedItem expands the selected message item if it is expandable.
254func (m *Chat) ToggleExpandedSelectedItem() {
255	if expandable, ok := m.list.SelectedItem().(chat.Expandable); ok {
256		expandable.ToggleExpanded()
257	}
258}
259
260// HandleMouseDown handles mouse down events for the chat component.
261func (m *Chat) HandleMouseDown(x, y int) bool {
262	if m.list.Len() == 0 {
263		return false
264	}
265
266	itemIdx, itemY := m.list.ItemIndexAtPosition(x, y)
267	if itemIdx < 0 {
268		return false
269	}
270
271	m.mouseDown = true
272	m.mouseDownItem = itemIdx
273	m.mouseDownX = x
274	m.mouseDownY = itemY
275	m.mouseDragItem = itemIdx
276	m.mouseDragX = x
277	m.mouseDragY = itemY
278
279	// Select the item that was clicked
280	m.list.SetSelected(itemIdx)
281
282	if clickable, ok := m.list.SelectedItem().(list.MouseClickable); ok {
283		return clickable.HandleMouseClick(ansi.MouseButton1, x, itemY)
284	}
285
286	return true
287}
288
289// HandleMouseUp handles mouse up events for the chat component.
290func (m *Chat) HandleMouseUp(x, y int) bool {
291	if !m.mouseDown {
292		return false
293	}
294
295	// TODO: Handle the behavior when mouse is released after a drag selection
296	// (e.g., copy selected text to clipboard)
297
298	m.mouseDown = false
299	return true
300}
301
302// HandleMouseDrag handles mouse drag events for the chat component.
303func (m *Chat) HandleMouseDrag(x, y int) bool {
304	if !m.mouseDown {
305		return false
306	}
307
308	if m.list.Len() == 0 {
309		return false
310	}
311
312	itemIdx, itemY := m.list.ItemIndexAtPosition(x, y)
313	if itemIdx < 0 {
314		return false
315	}
316
317	m.mouseDragItem = itemIdx
318	m.mouseDragX = x
319	m.mouseDragY = itemY
320
321	return true
322}
323
324// ClearMouse clears the current mouse interaction state.
325func (m *Chat) ClearMouse() {
326	m.mouseDown = false
327	m.mouseDownItem = -1
328	m.mouseDragItem = -1
329}
330
331// applyHighlightRange applies the current highlight range to the chat items.
332func (m *Chat) applyHighlightRange(idx, selectedIdx int, item list.Item) list.Item {
333	if hi, ok := item.(list.Highlightable); ok {
334		// Apply highlight
335		startItemIdx, startLine, startCol, endItemIdx, endLine, endCol := m.getHighlightRange()
336		sLine, sCol, eLine, eCol := -1, -1, -1, -1
337		if idx >= startItemIdx && idx <= endItemIdx {
338			if idx == startItemIdx && idx == endItemIdx {
339				// Single item selection
340				sLine = startLine
341				sCol = startCol
342				eLine = endLine
343				eCol = endCol
344			} else if idx == startItemIdx {
345				// First item - from start position to end of item
346				sLine = startLine
347				sCol = startCol
348				eLine = -1
349				eCol = -1
350			} else if idx == endItemIdx {
351				// Last item - from start of item to end position
352				sLine = 0
353				sCol = 0
354				eLine = endLine
355				eCol = endCol
356			} else {
357				// Middle item - fully highlighted
358				sLine = 0
359				sCol = 0
360				eLine = -1
361				eCol = -1
362			}
363		}
364
365		hi.Highlight(sLine, sCol, eLine, eCol)
366		return hi.(list.Item)
367	}
368
369	return item
370}
371
372// getHighlightRange returns the current highlight range.
373func (m *Chat) getHighlightRange() (startItemIdx, startLine, startCol, endItemIdx, endLine, endCol int) {
374	if m.mouseDownItem < 0 {
375		return -1, -1, -1, -1, -1, -1
376	}
377
378	downItemIdx := m.mouseDownItem
379	dragItemIdx := m.mouseDragItem
380
381	// Determine selection direction
382	draggingDown := dragItemIdx > downItemIdx ||
383		(dragItemIdx == downItemIdx && m.mouseDragY > m.mouseDownY) ||
384		(dragItemIdx == downItemIdx && m.mouseDragY == m.mouseDownY && m.mouseDragX >= m.mouseDownX)
385
386	if draggingDown {
387		// Normal forward selection
388		startItemIdx = downItemIdx
389		startLine = m.mouseDownY
390		startCol = m.mouseDownX
391		endItemIdx = dragItemIdx
392		endLine = m.mouseDragY
393		endCol = m.mouseDragX
394	} else {
395		// Backward selection (dragging up)
396		startItemIdx = dragItemIdx
397		startLine = m.mouseDragY
398		startCol = m.mouseDragX
399		endItemIdx = downItemIdx
400		endLine = m.mouseDownY
401		endCol = m.mouseDownX
402	}
403
404	return startItemIdx, startLine, startCol, endItemIdx, endLine, endCol
405}