Skip to content
Myr

Add cmds for toggling mouse mode + deprecate equivalent Program methods

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Also automatically disable mouse mode on exit.

authored by Christian Rocha · committed by Christian Rocha · parent 7c0bbc7 · Browse files · 2 files+67 added−19 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+67 added−19 removed

  • other:+67 added−19 removed

2 files+67 added−19 removed

modified:examples/mouse/main.go+2 added-9 removed
9 unmodified lines
10 tea "github.com/charmbracelet/bubbletea"11)1213func main() {14 p := tea.NewProgram(model{})1516 p.EnterAltScreen()17 defer p.ExitAltScreen()18 p.EnableMouseAllMotion()19 defer p.DisableMouseAllMotion()2021 if err := p.Start(); err != nil {14 if err := tea.NewProgram(model{}).Start(); err != nil {15 log.Fatal(err)16 }17}18
2 unmodified lines
21 mouseEvent tea.MouseEvent22}2324func (m model) Init() tea.Cmd {32 return nil25 return tea.Batch(tea.EnterAltScreen, tea.EnableMouseAllMotion)26}2728func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {29 switch msg := msg.(type) {
23 unmodified lines
modified:tea.go+65 added-10 removed
182 unmodified lines
183func ExitAltScreen() Msg {184 return exitAltScreenMsg{}185}186187// enableMouseCellMotionMsg is a special command that signals to start188// listening for "cell motion" type mouse events (ESC[?1002l). To send an189// enableMouseCellMotionMsg, use the EnableMouseCellMotion command.190type enableMouseCellMotionMsg struct{}191192// EnableMouseCellMotion is a special command that enables mouse click,193// release, wheel, events. Mouse movement events are also captured if a mouse194// button is pressed (i.e., drag events).195func EnableMouseCellMotion() Msg {196 return enableMouseCellMotionMsg{}197}198199// enableMouseAllMotionMsg is a special command that signals to start listening200// for "all motion" type mouse events (ESC[?1003l). To send an201// enableMouseAllMotionMsg, use the EnableMouseAllMotion command.202type enableMouseAllMotionMsg struct{}203204// EnableMouseAllMotion is a special command that enables mouse click, release,205// and wheel events. Mouse movement events are delivered regardless of whether206// a button is pressed. Many modern terminals support this, but not all. If in207// doubt, use EnableMouseCellMotion instead.208func EnableMouseAllMotion() Msg {209 return enableMouseAllMotionMsg{}210}211212// disableMouseMsg is an internal message that that signals to stop listening213// for mouse events. To send a disableMouseMsg, use the DisableMouse command.214type disableMouseMsg struct{}215216// DisableMouse is a special command that stops listening for mouse events.217func DisableMouse() Msg {218 return disableMouseMsg{}219}220221// exitAltScreenMsg in an internal message signals that the program should exit222// alternate screen buffer. You can send a exitAltScreenMsg with ExitAltScreen.223type exitAltScreenMsg struct{}224
10 unmodified lines
235236// HideCursor is a special command for manually instructing Bubble Tea to hide237// the cursor. In some rare cases, certain operations will cause the terminal238// to show the cursor, which is normally hidden for the duration of a Bubble205// Tea program's lifetime. You most likely will not need to use this command.239// Tea program's lifetime. You will most likely not need to use this command.240func HideCursor() Msg {241 return hideCursorMsg{}242}243
178 unmodified lines
422 // Handle special messages423 switch msg.(type) {424 case quitMsg:425 p.ExitAltScreen()426 p.DisableMouseCellMotion()427 p.DisableMouseAllMotion()428 p.renderer.stop()429 close(done)430 return nil431432 case enterAltScreenMsg:433 p.EnterAltScreen()434435 case exitAltScreenMsg:436 p.ExitAltScreen()437438 case enableMouseCellMotionMsg:439 p.EnableMouseCellMotion()440441 case enableMouseAllMotionMsg:442 p.EnableMouseAllMotion()443444 case disableMouseMsg:445 p.DisableMouseCellMotion()446 p.DisableMouseAllMotion()447448 case hideCursorMsg:449 hideCursor(p.output)450 }451
54 unmodified lines
506 }507}508509// EnableMouseCellMotion enables mouse click, release, wheel and motion events461// if a button is pressed.510// if a mouse button is pressed (i.e., drag events).511//512// Deprecated. Use the EnableMouseCellMotion() command instead.513func (p *Program) EnableMouseCellMotion() {514 p.mtx.Lock()515 defer p.mtx.Unlock()516 fmt.Fprintf(p.output, te.CSI+te.EnableMouseCellMotionSeq)517}518468// DisableMouseCellMotion disables Mouse Cell Motion tracking. If you've469// enabled Cell Motion mouse tracking be sure to call this as your program is470// exiting or your users will be very upset!519// DisableMouseCellMotion disables Mouse Cell Motion tracking. This will be520// called automatically when exiting a Bubble Tea program.521//522// Deprecated. Use the DisableMouse() command instead.523func (p *Program) DisableMouseCellMotion() {524 p.mtx.Lock()525 defer p.mtx.Unlock()526 fmt.Fprintf(p.output, te.CSI+te.DisableMouseCellMotionSeq)527}528529// EnableMouseAllMotion enables mouse click, release, wheel and motion events,478// regardless of whether a button is pressed. Many modern terminals support479// this, but not all.530// regardless of whether a mouse button is pressed. Many modern terminals531// support this, but not all.532//533// Deprecated. Use the EnableMouseAllMotion() command instead.534func (p *Program) EnableMouseAllMotion() {535 p.mtx.Lock()536 defer p.mtx.Unlock()537 fmt.Fprintf(p.output, te.CSI+te.EnableMouseAllMotionSeq)538}539486// DisableMouseAllMotion disables All Motion mouse tracking. If you've enabled487// All Motion mouse tracking be sure you call this as your program is exiting488// or your users will be very upset!540// DisableMouseAllMotion disables All Motion mouse tracking. This will be541// called automatically when exiting a Bubble Tea program.542//543// Deprecated. Use the DisableMouse() command instead.544func (p *Program) DisableMouseAllMotion() {545 p.mtx.Lock()546 defer p.mtx.Unlock()547 fmt.Fprintf(p.output, te.CSI+te.DisableMouseAllMotionSeq)
1 unmodified line