feat(tui): chat: clear selection on esc

Ayman Bagabas created

Change summary

internal/tui/components/chat/chat.go              | 9 +++++++--
internal/tui/components/chat/messages/messages.go | 3 +++
2 files changed, 10 insertions(+), 2 deletions(-)

Detailed changes

internal/tui/components/chat/chat.go 🔗

@@ -96,8 +96,13 @@ func (m *messageListCmp) Init() tea.Cmd {
 func (m *messageListCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 	switch msg := msg.(type) {
 	case tea.KeyPressMsg:
-		if key.Matches(msg, messages.CopyKey) && m.listCmp.HasSelection() {
-			return m, m.CopySelectedText(true)
+		if m.listCmp.IsFocused() && m.listCmp.HasSelection() {
+			switch {
+			case key.Matches(msg, messages.CopyKey):
+				return m, m.CopySelectedText(true)
+			case key.Matches(msg, messages.ClearSelectionKey):
+				return m, m.SelectionClear()
+			}
 		}
 	case tea.MouseClickMsg:
 		x := msg.X - 1 // Adjust for padding

internal/tui/components/chat/messages/messages.go 🔗

@@ -28,6 +28,9 @@ import (
 // CopyKey is the key binding for copying message content to the clipboard.
 var CopyKey = key.NewBinding(key.WithKeys("c", "y", "C", "Y"), key.WithHelp("c/y", "copy"))
 
+// ClearSelectionKey is the key binding for clearing the current selection in the chat interface.
+var ClearSelectionKey = key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "clear selection"))
+
 // MessageCmp defines the interface for message components in the chat interface.
 // It combines standard UI model interfaces with message-specific functionality.
 type MessageCmp interface {