Add mouse support to the FullscreenRenderer
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Vlastimil Ovčáčík · committed by Junegunn Choi · parent ca43f95 · Browse files · 2 files·+52 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+52 added−14 removed
- other:+52 added−14 removed
Provenance
- Commit
0ff8854- Tree
730a120- Author
Vlastimil Ovčáčík <vovcacik@github.ovcacik.org>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ca43f95
2 files changed+52 added−14 removed
modified:src/tui/tcell.go+38 added-4 removed
183 unmodified lines184 return Event{Resize, 0, nil}185 186 // process mouse events:187 case *tcell.EventMouse:188+ // mouse down events have zeroed buttons, so we can't use them189+ // mouse up event consists of two events, 1. (main) event with modifier and other metadata, 2. event with zeroed buttons190+ // so mouse click is three consecutive events, but the first and last are indistinguishable from movement events (with released buttons)191+ // dragging has same structure, it only repeats the middle (main) event appropriately192 x, y := ev.Position()189- button := ev.Buttons()193 mod := ev.Modifiers() != 0191- if button&tcell.WheelDown != 0 {194+195+ // since we dont have mouse down events (unlike LightRenderer), we need to track state in prevButton196+ prevButton, button := r.prevMouseButton, ev.Buttons()197+ r.prevMouseButton = button198+ drag := prevButton == button199+200+ switch {201+ case button&tcell.WheelDown != 0:202 return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, false, mod}}193- } else if button&tcell.WheelUp != 0 {203+ case button&tcell.WheelUp != 0:204 return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, false, mod}}195- } else if runtime.GOOS != "windows" {205+ case button&tcell.Button1 != 0 && !drag:206+ // all potential double click events put their 'line' coordinate in the clickY array207+ // double click event has two conditions, temporal and spatial, the first is checked here208+ now := time.Now()209+ if now.Sub(r.prevDownTime) < doubleClickDuration {210+ r.clickY = append(r.clickY, y)211+ } else {212+ r.clickY = []int{y}213+ }214+ r.prevDownTime = now215+216+ // detect double clicks (also check for spatial condition)217+ n := len(r.clickY)218+ double := n > 1 && r.clickY[n-2] == r.clickY[n-1]219+ if double {220+ // make sure two consecutive double clicks require four clicks221+ r.clickY = []int{}222+ }223+224+ // fire single or double click event225+ return Event{Mouse, 0, &MouseEvent{y, x, 0, true, !double, double, mod}}226+ case button&tcell.Button2 != 0 && !drag:227+ return Event{Mouse, 0, &MouseEvent{y, x, 0, false, true, false, mod}}228+ case runtime.GOOS != "windows":229+230 // double and single taps on Windows don't quite work due to231 // the console acting on the events and not allowing us232 // to consume them.233 485 unmodified linesmodified:src/tui/tui.go+14 added-10 removed
3 unmodified lines4 "fmt"5 "os"6 "strconv"7 "time"8+9+ "github.com/gdamore/tcell"10 )11 12 // Types of user action13 type EventType int384 unmodified lines398 Erase()399 }400 401 type FullscreenRenderer struct {400- theme *ColorTheme401- mouse bool402- forceBlack bool403- prevDownTime time.Time404- clickY []int402+ theme *ColorTheme403+ mouse bool404+ forceBlack bool405+ prevDownTime time.Time406+ prevMouseButton tcell.ButtonMask407+ clickY []int408 }409 410 func NewFullscreenRenderer(theme *ColorTheme, forceBlack bool, mouse bool) Renderer {411 r := &FullscreenRenderer{409- theme: theme,410- mouse: mouse,411- forceBlack: forceBlack,412- prevDownTime: time.Unix(0, 0),413- clickY: []int{}}412+ theme: theme,413+ mouse: mouse,414+ forceBlack: forceBlack,415+ prevDownTime: time.Unix(0, 0),416+ prevMouseButton: tcell.ButtonNone,417+ clickY: []int{}}418 return r419 }420 421 var (208 unmodified lines