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