on main help sign in
fix(ui): scroll to expanded item (#2088)
0cbaacdAyman Bagabas
internal/ui/chat/messages.go+3−1
@@ -38,7 +38,9 @@ type Animatable interface {
38
38
39// Expandable is an interface for items that can be expanded or collapsed.
39// Expandable is an interface for items that can be expanded or collapsed.
40type Expandable interface {
40type Expandable interface {
41+ // ToggleExpanded toggles the expanded state of the item. It returns
42+ // whether the item is now expanded.
41− ToggleExpanded()
43+ ToggleExpanded() bool
42}
44}
43
45
44// KeyEventHandler is an interface for items that can handle key events.
46// KeyEventHandler is an interface for items that can handle key events.
internal/ui/chat/tools.go+5−6
@@ -157,6 +157,8 @@ type baseToolMessageItem struct {
157 expandedContent bool
157 expandedContent bool
158}
158}
159
159
160+var _ Expandable = (*baseToolMessageItem)(nil)
161+
160// newBaseToolMessageItem is the internal constructor for base tool message items.
162// newBaseToolMessageItem is the internal constructor for base tool message items.
161func newBaseToolMessageItem(
163func newBaseToolMessageItem(
162 sty *styles.Styles,
164 sty *styles.Styles,
@@ -398,18 +400,15 @@ func (t *baseToolMessageItem) SetSpinningFunc(fn SpinningFunc) {
398}
400}
399
401
400// ToggleExpanded toggles the expanded state of the thinking box.
402// ToggleExpanded toggles the expanded state of the thinking box.
401−func (t *baseToolMessageItem) ToggleExpanded() {
403+func (t *baseToolMessageItem) ToggleExpanded() bool {
402 t.expandedContent = !t.expandedContent
404 t.expandedContent = !t.expandedContent
403 t.clearCache()
405 t.clearCache()
406+ return t.expandedContent
404}
407}
405
408
406// HandleMouseClick implements MouseClickable.
409// HandleMouseClick implements MouseClickable.
407func (t *baseToolMessageItem) HandleMouseClick(btn ansi.MouseButton, x, y int) bool {
410func (t *baseToolMessageItem) HandleMouseClick(btn ansi.MouseButton, x, y int) bool {
408− if btn != ansi.MouseLeft {
411+ return btn == ansi.MouseLeft
409− return false
410− }
411− t.ToggleExpanded()
412− return true
413}
412}
414
413
415// HandleKeyEvent implements KeyEventHandler.
414// HandleKeyEvent implements KeyEventHandler.
internal/ui/model/chat.go+10−2
@@ -438,6 +438,7 @@ func (m *Chat) MessageItem(id string) chat.MessageItem {
438func (m *Chat) ToggleExpandedSelectedItem() {
438func (m *Chat) ToggleExpandedSelectedItem() {
439 if expandable, ok := m.list.SelectedItem().(chat.Expandable); ok {
439 if expandable, ok := m.list.SelectedItem().(chat.Expandable); ok {
440 expandable.ToggleExpanded()
440 expandable.ToggleExpanded()
441+ m.list.ScrollToIndex(m.list.Selected())
441 }
442 }
442}
443}
443
444
@@ -538,8 +539,15 @@ func (m *Chat) HandleDelayedClick(msg DelayedClickMsg) bool {
538 }
539 }
539
540
540 // Execute the click action (e.g., expansion).
541 // Execute the click action (e.g., expansion).
541− if clickable, ok := m.list.SelectedItem().(list.MouseClickable); ok {
542+ selectedItem := m.list.SelectedItem()
543+ if clickable, ok := selectedItem.(list.MouseClickable); ok {
542− return clickable.HandleMouseClick(ansi.MouseButton1, msg.X, msg.Y)
544+ handled := clickable.HandleMouseClick(ansi.MouseButton1, msg.X, msg.Y)
545+ // Toggle expansion if applicable.
546+ if expandable, ok := selectedItem.(chat.Expandable); ok {
547+ expandable.ToggleExpanded()
548+ }
549+ m.list.ScrollToIndex(m.list.Selected())
550+ return handled
543 }
551 }
544
552
545 return false
553 return false