on main help sign in
feat(ui): add mouse click handling to lazy list items
commitafb546733555e78a5970c544062e72e9c4f4360b authorAyman Bagabas <ayman.bagabas@gmail.com> dateWed 10 Dec 2025, 12:10 (1 year ago) parent065f339
3 files changed+22−4
internal/ui/lazylist/item.go+11−1 internal/ui/lazylist/list.go+7 internal/ui/model/items.go+4−3
internal/ui/lazylist/item.go+11−1
@@ -1,6 +1,9 @@
11package lazylist
22
3−import "charm.land/lipgloss/v2"
3+import (
4+ "charm.land/lipgloss/v2"
5+ "github.com/charmbracelet/x/ansi"
6+)
47
58// Item represents a single item in the lazy-loaded list.
69type Item interface {
@@ -22,3 +25,10 @@ type HighlightStylable interface {
2225 // HighlightStyle returns the style to apply for highlighted regions.
2326 HighlightStyle() lipgloss.Style
2427}
28+
29+// MouseClickable represents an item that can handle mouse click events.
30+type MouseClickable interface {
31+ // HandleMouseClick processes a mouse click event at the given coordinates.
32+ // It returns true if the event was handled, false otherwise.
33+ HandleMouseClick(btn ansi.MouseButton, x, y int) bool
34+}
internal/ui/lazylist/list.go+7
@@ -5,6 +5,7 @@ import (
55 "strings"
66
77 "charm.land/lipgloss/v2"
8+ "github.com/charmbracelet/x/ansi"
89)
910
1011// List represents a list of items that can be lazily rendered. A list is
@@ -550,6 +551,12 @@ func (l *List) HandleMouseDown(x, y int) bool {
550551 // Select the clicked item
551552 l.SetSelected(itemIdx)
552553
554+ if clickable, ok := l.items[itemIdx].(MouseClickable); ok {
555+ clickable.HandleMouseClick(ansi.MouseButton1, x, itemY)
556+ l.items[itemIdx] = clickable.(Item)
557+ l.invalidateItem(itemIdx)
558+ }
559+
553560 return true
554561}
555562
internal/ui/model/items.go+4−3
@@ -351,6 +351,7 @@ type SectionHeaderItem struct {
351351 duration time.Duration
352352 isSectionHeader bool
353353 sty *styles.Styles
354+ content string
354355}
355356
356357// NewSectionHeaderItem creates a new section header item.
@@ -387,13 +388,13 @@ func (s *SectionHeaderItem) BlurStyle() lipgloss.Style {
387388
388389// Render implements lazylist.Item.
389390func (s *SectionHeaderItem) Render(width int) string {
390− content := s.sty.Chat.Message.SectionHeader.Render(fmt.Sprintf("%s %s %s",
391+ content := fmt.Sprintf("%s %s %s",
391392 s.sty.Subtle.Render(styles.ModelIcon),
392393 s.sty.Muted.Render(s.modelName),
393394 s.sty.Subtle.Render(s.duration.String()),
394− ))
395+ )
395396
396− return content
397+ return s.sty.Chat.Message.SectionHeader.Render(content)
397398}
398399
399400// GetMessageItems extracts [MessageItem]s from a [message.Message]. It returns