forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.go
More file actions
144 lines (124 loc) Β· 3.81 KB
/
mouse.go
File metadata and controls
144 lines (124 loc) Β· 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package tea
import (
"fmt"
"github.com/charmbracelet/x/input"
)
// MouseButton represents the button that was pressed during a mouse message.
type MouseButton = input.MouseButton
// Mouse event buttons
//
// This is based on X11 mouse button codes.
//
// 1 = left button
// 2 = middle button (pressing the scroll wheel)
// 3 = right button
// 4 = turn scroll wheel up
// 5 = turn scroll wheel down
// 6 = push scroll wheel left
// 7 = push scroll wheel right
// 8 = 4th button (aka browser backward button)
// 9 = 5th button (aka browser forward button)
// 10
// 11
//
// Other buttons are not supported.
const (
MouseNone = input.MouseNone
MouseLeft = input.MouseLeft
MouseMiddle = input.MouseMiddle
MouseRight = input.MouseRight
MouseWheelUp = input.MouseWheelUp
MouseWheelDown = input.MouseWheelDown
MouseWheelLeft = input.MouseWheelLeft
MouseWheelRight = input.MouseWheelRight
MouseBackward = input.MouseBackward
MouseForward = input.MouseForward
MouseButton10 = input.MouseButton10
MouseButton11
)
// MouseMsg represents a mouse message. This is a generic mouse message that
// can represent any kind of mouse event.
type MouseMsg interface {
fmt.Stringer
// Mouse returns the underlying mouse event.
Mouse() Mouse
}
// Mouse represents a Mouse message. Use [MouseMsg] to represent all mouse
// messages.
//
// The X and Y coordinates are zero-based, with (0,0) being the upper left
// corner of the terminal.
//
// // Catch all mouse events
// switch msg := msg.(type) {
// case MouseMsg:
// m := msg.Mouse()
// fmt.Println("Mouse event:", m.X, m.Y, m)
// }
//
// // Only catch mouse click events
// switch msg := msg.(type) {
// case MouseClickMsg:
// fmt.Println("Mouse click event:", msg.X, msg.Y, msg)
// }
type Mouse struct {
X, Y int
Button MouseButton
Mod KeyMod
}
// String returns a string representation of the mouse message.
func (m Mouse) String() (s string) {
return input.Mouse(m).String()
}
// MouseClickMsg represents a mouse button click message.
type MouseClickMsg Mouse
// String returns a string representation of the mouse click message.
func (e MouseClickMsg) String() string {
return Mouse(e).String()
}
// Mouse returns the underlying mouse event. This is a convenience method and
// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse
// event to [Mouse].
func (e MouseClickMsg) Mouse() Mouse {
return Mouse(e)
}
// MouseReleaseMsg represents a mouse button release message.
type MouseReleaseMsg Mouse
// String returns a string representation of the mouse release message.
func (e MouseReleaseMsg) String() string {
return Mouse(e).String()
}
// Mouse returns the underlying mouse event. This is a convenience method and
// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse
// event to [Mouse].
func (e MouseReleaseMsg) Mouse() Mouse {
return Mouse(e)
}
// MouseWheelMsg represents a mouse wheel message event.
type MouseWheelMsg Mouse
// String returns a string representation of the mouse wheel message.
func (e MouseWheelMsg) String() string {
return Mouse(e).String()
}
// Mouse returns the underlying mouse event. This is a convenience method and
// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse
// event to [Mouse].
func (e MouseWheelMsg) Mouse() Mouse {
return Mouse(e)
}
// MouseMotionMsg represents a mouse motion message.
type MouseMotionMsg Mouse
// String returns a string representation of the mouse motion message.
func (e MouseMotionMsg) String() string {
m := Mouse(e)
if m.Button != 0 {
return m.String() + "+motion"
}
return m.String() + "motion"
}
// Mouse returns the underlying mouse event. This is a convenience method and
// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse
// event to [Mouse].
func (e MouseMotionMsg) Mouse() Mouse {
return Mouse(e)
}