Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mouse: KeyMsg emitted when dragging the mouse with tea.WithMouseCellMotion() enabled #1047

Closed
topi314 opened this issue Jul 3, 2024 · 1 comment

Comments

@topi314
Copy link

topi314 commented Jul 3, 2024

Describe the bug:
When I enable tea.WithMouseCellMotion() and press my left mouse button and then drag sometimes a KeyMsg is emitted.
Example:

tea.KeyMsg{Type:-1, Runes:[]int32{91}, Alt:true, Paste:false}
tea.KeyMsg{Type:-1, Runes:[]int32{60, 51, 50, 59, 51, 57, 59, 55, 77}, Alt:false, Paste:false}

tea.KeyMsg{Type:-1, Runes:[]int32{91}, Alt:true, Paste:false}
tea.KeyMsg{Type:-1, Runes:[]int32{60, 51, 50, 59, 49, 53, 59, 49, 50, 77}, Alt:false, Paste:false}

This seems to stem from the slow computations in the Update method (see artificial 100ms delay), these slow computations should probably handled in a tea.Cmd instead but I find this behavior still weird.

I just did some additional testing and this can also happen with slow View computations.

Setup:
Please complete the following information along with version numbers, if applicable.

  • OS: arch
  • Shell: zsh
  • Terminal Emulator: alacritty
  • Terminal Multiplexer: N/A

To Reproduce:
Steps to reproduce the behavior:

  1. Run the source code below go run main.go
  2. run tail -f debug.log
  3. Click and drag the mouse around
  4. See tea.KeyMsg in logs

Source Code:

package main

import (
	"log"
	"time"

	"github.com/charmbracelet/bubbletea"
)

type model struct{}

func (m model) Init() tea.Cmd {
	return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.MouseMsg:
		time.Sleep(100 * time.Millisecond)
	case tea.KeyMsg:
		log.Printf("key: %#v", msg)

		switch msg.String() {
		case "ctrl+q":
			return m, tea.Quit
		}
	}
	return m, nil
}

func (m model) View() string {
	return "Press Ctrl+Q to quit."
}

func main() {
	logFile, err := tea.LogToFile("debug.log", "test")
	if err != nil {
		log.Panicln("failed to open debug log file:", err)
	}
	defer logFile.Close()

	p := tea.NewProgram(model{}, tea.WithAltScreen(), tea.WithMouseCellMotion())
	if _, err := p.Run(); err != nil {
		log.Panicln("error while running:", err)
	}
}

Expected behavior:
No tea.KeyMsg being emitted

@topi314
Copy link
Author

topi314 commented Jul 4, 2024

this actually seems like a duplicate of charmbracelet/bubbles#567

therefore closing this

@topi314 topi314 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant