Skip to content
Myr

mouse.go

This file

Path

mouse.go

144 lines · 3.8 KiB · Go

Last changed by

fix: assign MouseButton11 = uv.MouseButton11 (#1754)Bunlong Hengbefore adoption1862dfb

History of this file

Take it

1package tea23import (4	"fmt"56	uv "github.com/charmbracelet/ultraviolet"7)89// MouseButton represents the button that was pressed during a mouse message.10type MouseButton = uv.MouseButton1112// Mouse event buttons13//14// This is based on X11 mouse button codes.15//16//	1 = left button17//	2 = middle button (pressing the scroll wheel)18//	3 = right button19//	4 = turn scroll wheel up20//	5 = turn scroll wheel down21//	6 = push scroll wheel left22//	7 = push scroll wheel right23//	8 = 4th button (aka browser backward button)24//	9 = 5th button (aka browser forward button)25//	1026//	1127//28// Other buttons are not supported.29const (30	MouseNone       = uv.MouseNone31	MouseLeft       = uv.MouseLeft32	MouseMiddle     = uv.MouseMiddle33	MouseRight      = uv.MouseRight34	MouseWheelUp    = uv.MouseWheelUp35	MouseWheelDown  = uv.MouseWheelDown36	MouseWheelLeft  = uv.MouseWheelLeft37	MouseWheelRight = uv.MouseWheelRight38	MouseBackward   = uv.MouseBackward39	MouseForward    = uv.MouseForward40	MouseButton10   = uv.MouseButton1041	MouseButton11   = uv.MouseButton1142)4344// MouseMsg represents a mouse message. This is a generic mouse message that45// can represent any kind of mouse event.46type MouseMsg interface {47	fmt.Stringer4849	// Mouse returns the underlying mouse event.50	Mouse() Mouse51}5253// Mouse represents a Mouse message. Use [MouseMsg] to represent all mouse54// messages.55//56// The X and Y coordinates are zero-based, with (0,0) being the upper left57// corner of the terminal.58//59//	// Catch all mouse events60//	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 events67//	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   int73	Button MouseButton74	Mod    KeyMod75}7677// String returns a string representation of the mouse message.78func (m Mouse) String() (s string) {79	return uv.Mouse(m).String()80}8182// MouseClickMsg represents a mouse button click message.83type MouseClickMsg Mouse8485// String returns a string representation of the mouse click message.86func (e MouseClickMsg) String() string {87	return Mouse(e).String()88}8990// Mouse returns the underlying mouse event. This is a convenience method and91// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse92// event to [Mouse].93func (e MouseClickMsg) Mouse() Mouse {94	return Mouse(e)95}9697// MouseReleaseMsg represents a mouse button release message.98type MouseReleaseMsg Mouse99100// String returns a string representation of the mouse release message.101func (e MouseReleaseMsg) String() string {102	return Mouse(e).String()103}104105// Mouse returns the underlying mouse event. This is a convenience method and106// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse107// event to [Mouse].108func (e MouseReleaseMsg) Mouse() Mouse {109	return Mouse(e)110}111112// MouseWheelMsg represents a mouse wheel message event.113type MouseWheelMsg Mouse114115// String returns a string representation of the mouse wheel message.116func (e MouseWheelMsg) String() string {117	return Mouse(e).String()118}119120// Mouse returns the underlying mouse event. This is a convenience method and121// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse122// event to [Mouse].123func (e MouseWheelMsg) Mouse() Mouse {124	return Mouse(e)125}126127// MouseMotionMsg represents a mouse motion message.128type MouseMotionMsg Mouse129130// 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}138139// Mouse returns the underlying mouse event. This is a convenience method and140// syntactic sugar to satisfy the [MouseMsg] interface, and cast the mouse141// event to [Mouse].142func (e MouseMotionMsg) Mouse() Mouse {143	return Mouse(e)144}