Browse Source

add toast for copy

Dax Raad 7 months ago
parent
commit
90d37c98f8
1 changed files with 18 additions and 30 deletions
  1. 18 30
      packages/tui/internal/components/chat/messages.go

+ 18 - 30
packages/tui/internal/components/chat/messages.go

@@ -46,7 +46,7 @@ type messagesComponent struct {
 	tail            bool
 	partCount       int
 	lineCount       int
-	selection       selection
+	selection       *selection
 }
 
 type selection struct {
@@ -56,18 +56,14 @@ type selection struct {
 	endY   int
 }
 
-func (s selection) selecting() bool {
-	return s.startX >= 0 && s.startY >= 0
-}
-
 func (s selection) hasCompleteSelection() bool {
 	return s.startX >= 0 && s.startY >= 0 && s.endX >= 0 && s.endY >= 0
 }
 
-func (s selection) coords(offset int) selection {
+func (s selection) coords(offset int) *selection {
 	// selecting backwards
 	if s.startY > s.endY && s.endY >= 0 {
-		return selection{
+		return &selection{
 			startX: max(0, s.endX-1),
 			startY: s.endY - offset,
 			endX:   s.startX + 1,
@@ -77,7 +73,7 @@ func (s selection) coords(offset int) selection {
 
 	// selecting backwards same line
 	if s.startY == s.endY && s.startX >= s.endX {
-		return selection{
+		return &selection{
 			startY: s.startY - offset,
 			startX: max(0, s.endX-1),
 			endY:   s.endY - offset,
@@ -85,7 +81,7 @@ func (s selection) coords(offset int) selection {
 		}
 	}
 
-	return selection{
+	return &selection{
 		startX: s.startX,
 		startY: s.startY - offset,
 		endX:   s.endX,
@@ -108,7 +104,7 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		slog.Info("mouse", "x", msg.X, "y", msg.Y, "offset", m.viewport.YOffset)
 		y := msg.Y + m.viewport.YOffset
 		if y > 0 {
-			m.selection = selection{
+			m.selection = &selection{
 				startY: y,
 				startX: msg.X,
 				endY:   -1,
@@ -120,8 +116,8 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		}
 
 	case tea.MouseMotionMsg:
-		if m.selection.selecting() {
-			m.selection = selection{
+		if m.selection != nil {
+			m.selection = &selection{
 				startX: m.selection.startX,
 				startY: m.selection.startY,
 				endX:   msg.X + 1,
@@ -131,16 +127,12 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 		}
 
 	case tea.MouseReleaseMsg:
-		if m.selection.hasCompleteSelection() {
-			m.selection = selection{
-				startX: -1,
-				startY: -1,
-				endX:   -1,
-				endY:   -1,
-			}
-			return m, tea.Batch(
-				app.SetClipboard(strings.Join(m.clipboard, "\n")),
+		if m.selection != nil && m.selection.hasCompleteSelection() {
+			m.selection = nil
+			return m, tea.Sequence(
 				m.renderView(),
+				app.SetClipboard(strings.Join(m.clipboard, "\n")),
+				toast.NewSuccessToast("Copied to clipboard"),
 			)
 		}
 	case tea.WindowSizeMsg:
@@ -491,12 +483,14 @@ func (m *messagesComponent) renderView() tea.Cmd {
 
 		final := []string{}
 		clipboard := []string{}
-		selection := m.selection.coords(lipgloss.Height(header) + 1)
-		hasSelection := m.selection.selecting()
+		var selection *selection
+		if m.selection != nil {
+			selection = m.selection.coords(lipgloss.Height(header) + 1)
+		}
 		for _, block := range blocks {
 			lines := strings.Split(block, "\n")
 			for index, line := range lines {
-				if !hasSelection || index == 0 || index == len(lines)-1 {
+				if selection == nil || index == 0 || index == len(lines)-1 {
 					final = append(final, line)
 					continue
 				}
@@ -776,11 +770,5 @@ func NewMessagesComponent(app *app.App) MessagesComponent {
 		showToolDetails: true,
 		cache:           NewPartCache(),
 		tail:            true,
-		selection: selection{
-			startX: -1,
-			startY: -1,
-			endX:   -1,
-			endY:   -1,
-		},
 	}
 }