charmbracelet / bubbletea

A powerful little TUI framework 🏗
MIT License
25.65k stars 744 forks source link

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

Closed topi314 closed 2 weeks ago

topi314 commented 2 weeks ago

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.

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 commented 2 weeks ago

this actually seems like a duplicate of https://github.com/charmbracelet/bubbletea/issues/1043

therefore closing this