mouse.go

 1package uv
 2
 3import (
 4	"github.com/charmbracelet/x/ansi"
 5)
 6
 7// MouseButton represents the button that was pressed during a mouse message.
 8type MouseButton = ansi.MouseButton
 9
10// Mouse event buttons
11//
12// This is based on X11 mouse button codes.
13//
14//	1 = left button
15//	2 = middle button (pressing the scroll wheel)
16//	3 = right button
17//	4 = turn scroll wheel up
18//	5 = turn scroll wheel down
19//	6 = push scroll wheel left
20//	7 = push scroll wheel right
21//	8 = 4th button (aka browser backward button)
22//	9 = 5th button (aka browser forward button)
23//	10
24//	11
25//
26// Other buttons are not supported.
27const (
28	MouseNone       = ansi.MouseNone
29	MouseLeft       = ansi.MouseLeft
30	MouseMiddle     = ansi.MouseMiddle
31	MouseRight      = ansi.MouseRight
32	MouseWheelUp    = ansi.MouseWheelUp
33	MouseWheelDown  = ansi.MouseWheelDown
34	MouseWheelLeft  = ansi.MouseWheelLeft
35	MouseWheelRight = ansi.MouseWheelRight
36	MouseBackward   = ansi.MouseBackward
37	MouseForward    = ansi.MouseForward
38	MouseButton10   = ansi.MouseButton10
39	MouseButton11   = ansi.MouseButton11
40)
41
42// Mouse represents a Mouse message. Use [MouseEvent] to represent all mouse
43// messages.
44//
45// The X and Y coordinates are zero-based, with (0,0) being the upper left
46// corner of the terminal.
47//
48//	// Catch all mouse events
49//	switch Event := Event.(type) {
50//	case MouseEvent:
51//	    m := Event.Mouse()
52//	    fmt.Println("Mouse event:", m.X, m.Y, m)
53//	}
54//
55//	// Only catch mouse click events
56//	switch Event := Event.(type) {
57//	case MouseClickEvent:
58//	    fmt.Println("Mouse click event:", Event.X, Event.Y, Event)
59//	}
60type Mouse struct {
61	X, Y   int
62	Button MouseButton
63	Mod    KeyMod
64}
65
66// String returns a string representation of the mouse message.
67func (m Mouse) String() (s string) {
68	if m.Mod.Contains(ModCtrl) {
69		s += "ctrl+"
70	}
71	if m.Mod.Contains(ModAlt) {
72		s += "alt+"
73	}
74	if m.Mod.Contains(ModShift) {
75		s += "shift+"
76	}
77
78	str := m.Button.String()
79	if str == "" {
80		s += "unknown"
81	} else if str != "none" { // motion events don't have a button
82		s += str
83	}
84
85	return s
86}