item.go

 1package list
 2
 3import (
 4	tea "charm.land/bubbletea/v2"
 5	"charm.land/lipgloss/v2"
 6	"github.com/charmbracelet/x/ansi"
 7)
 8
 9// Item represents a single item in the lazy-loaded list.
10type Item interface {
11	// Render returns the string representation of the item for the given
12	// width.
13	Render(width int) string
14}
15
16// Updatable represents an item that can handle tea.Msg updates (e.g., for
17// animations or interactive state changes).
18type Updatable interface {
19	// Update processes a message and returns an updated item and optional
20	// command. The returned Item should be the same type as the receiver.
21	Update(tea.Msg) (Item, tea.Cmd)
22}
23
24// FocusStylable represents an item that can be styled based on focus state.
25type FocusStylable interface {
26	// FocusStyle returns the style to apply when the item is focused.
27	FocusStyle() lipgloss.Style
28	// BlurStyle returns the style to apply when the item is unfocused.
29	BlurStyle() lipgloss.Style
30}
31
32// HighlightStylable represents an item that can be styled for highlighted regions.
33type HighlightStylable interface {
34	// HighlightStyle returns the style to apply for highlighted regions.
35	HighlightStyle() lipgloss.Style
36}
37
38// MouseClickable represents an item that can handle mouse click events.
39type MouseClickable interface {
40	// HandleMouseClick processes a mouse click event at the given coordinates.
41	// It returns true if the event was handled, false otherwise.
42	HandleMouseClick(btn ansi.MouseButton, x, y int) bool
43}
44
45// FocusAware represents an item that needs to know its focus state for
46// internal rendering decisions.
47type FocusAware interface {
48	// SetFocused is called before Render to inform the item of its focus state.
49	SetFocused(focused bool)
50}
51
52// KeyPressable represents an item that can handle key press events.
53type KeyPressable interface {
54	// HandleKeyPress processes a key press event.
55	// It returns true if the event was handled, false otherwise.
56	HandleKeyPress(msg tea.KeyPressMsg) bool
57}