1package model
2
3import (
4 "github.com/charmbracelet/crush/internal/ui/common"
5 "github.com/charmbracelet/crush/internal/ui/list"
6 uv "github.com/charmbracelet/ultraviolet"
7)
8
9// Chat represents the chat UI model that handles chat interactions and
10// messages.
11type Chat struct {
12 com *common.Common
13 list *list.List
14}
15
16// NewChat creates a new instance of [Chat] that handles chat interactions and
17// messages.
18func NewChat(com *common.Common) *Chat {
19 l := list.NewList()
20 l.SetGap(1)
21 return &Chat{
22 com: com,
23 list: l,
24 }
25}
26
27// Height returns the height of the chat view port.
28func (m *Chat) Height() int {
29 return m.list.Height()
30}
31
32// Draw renders the chat UI component to the screen and the given area.
33func (m *Chat) Draw(scr uv.Screen, area uv.Rectangle) {
34 uv.NewStyledString(m.list.Render()).Draw(scr, area)
35}
36
37// SetSize sets the size of the chat view port.
38func (m *Chat) SetSize(width, height int) {
39 m.list.SetSize(width, height)
40}
41
42// Len returns the number of items in the chat list.
43func (m *Chat) Len() int {
44 return m.list.Len()
45}
46
47// PrependItems prepends new items to the chat list.
48func (m *Chat) PrependItems(items ...list.Item) {
49 m.list.PrependItems(items...)
50 m.list.ScrollToIndex(0)
51}
52
53// SetMessages sets the chat messages to the provided list of message items.
54func (m *Chat) SetMessages(msgs ...MessageItem) {
55 items := make([]list.Item, len(msgs))
56 for i, msg := range msgs {
57 items[i] = msg
58 }
59 m.list.SetItems(items...)
60 m.list.ScrollToBottom()
61}
62
63// AppendMessages appends a new message item to the chat list.
64func (m *Chat) AppendMessages(msgs ...MessageItem) {
65 items := make([]list.Item, len(msgs))
66 for i, msg := range msgs {
67 items[i] = msg
68 }
69 m.list.AppendItems(items...)
70}
71
72// AppendItems appends new items to the chat list.
73func (m *Chat) AppendItems(items ...list.Item) {
74 m.list.AppendItems(items...)
75 m.list.ScrollToIndex(m.list.Len() - 1)
76}
77
78// Focus sets the focus state of the chat component.
79func (m *Chat) Focus() {
80 m.list.Focus()
81}
82
83// Blur removes the focus state from the chat component.
84func (m *Chat) Blur() {
85 m.list.Blur()
86}
87
88// ScrollToTop scrolls the chat view to the top.
89func (m *Chat) ScrollToTop() {
90 m.list.ScrollToTop()
91}
92
93// ScrollToBottom scrolls the chat view to the bottom.
94func (m *Chat) ScrollToBottom() {
95 m.list.ScrollToBottom()
96}
97
98// ScrollBy scrolls the chat view by the given number of line deltas.
99func (m *Chat) ScrollBy(lines int) {
100 m.list.ScrollBy(lines)
101}
102
103// ScrollToSelected scrolls the chat view to the selected item.
104func (m *Chat) ScrollToSelected() {
105 m.list.ScrollToSelected()
106}
107
108// SelectedItemInView returns whether the selected item is currently in view.
109func (m *Chat) SelectedItemInView() bool {
110 return m.list.SelectedItemInView()
111}
112
113// SetSelected sets the selected message index in the chat list.
114func (m *Chat) SetSelected(index int) {
115 m.list.SetSelected(index)
116}
117
118// SelectPrev selects the previous message in the chat list.
119func (m *Chat) SelectPrev() {
120 m.list.SelectPrev()
121}
122
123// SelectNext selects the next message in the chat list.
124func (m *Chat) SelectNext() {
125 m.list.SelectNext()
126}
127
128// SelectFirst selects the first message in the chat list.
129func (m *Chat) SelectFirst() {
130 m.list.SelectFirst()
131}
132
133// SelectLast selects the last message in the chat list.
134func (m *Chat) SelectLast() {
135 m.list.SelectLast()
136}
137
138// SelectFirstInView selects the first message currently in view.
139func (m *Chat) SelectFirstInView() {
140 m.list.SelectFirstInView()
141}
142
143// SelectLastInView selects the last message currently in view.
144func (m *Chat) SelectLastInView() {
145 m.list.SelectLastInView()
146}
147
148// HandleMouseDown handles mouse down events for the chat component.
149func (m *Chat) HandleMouseDown(x, y int) {
150 m.list.HandleMouseDown(x, y)
151}
152
153// HandleMouseUp handles mouse up events for the chat component.
154func (m *Chat) HandleMouseUp(x, y int) {
155 m.list.HandleMouseUp(x, y)
156}
157
158// HandleMouseDrag handles mouse drag events for the chat component.
159func (m *Chat) HandleMouseDrag(x, y int) {
160 m.list.HandleMouseDrag(x, y)
161}