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
Provenance
- Commit
5c150eb- Tree
b4a7e38- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
7c0bbc7
2 files changed+67 added−19 removed
modified:examples/mouse/main.go+2 added-9 removed
9 unmodified lines10 tea "github.com/charmbracelet/bubbletea"11 )12 13 func main() {14- p := tea.NewProgram(model{})15-16- p.EnterAltScreen()17- defer p.ExitAltScreen()18- p.EnableMouseAllMotion()19- defer p.DisableMouseAllMotion()20-21- if err := p.Start(); err != nil {14+ if err := tea.NewProgram(model{}).Start(); err != nil {15 log.Fatal(err)16 }17 }18 2 unmodified lines21 mouseEvent tea.MouseEvent22 }23 24 func (m model) Init() tea.Cmd {32- return nil25+ return tea.Batch(tea.EnterAltScreen, tea.EnableMouseAllMotion)26 }27 28 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {29 switch msg := msg.(type) {23 unmodified linesmodified:tea.go+65 added-10 removed
182 unmodified lines183 func ExitAltScreen() Msg {184 return exitAltScreenMsg{}185 }186 187+// 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.190+type enableMouseCellMotionMsg struct{}191+192+// 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).195+func EnableMouseCellMotion() Msg {196+ return enableMouseCellMotionMsg{}197+}198+199+// 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.202+type enableMouseAllMotionMsg struct{}203+204+// 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.208+func EnableMouseAllMotion() Msg {209+ return enableMouseAllMotionMsg{}210+}211+212+// disableMouseMsg is an internal message that that signals to stop listening213+// for mouse events. To send a disableMouseMsg, use the DisableMouse command.214+type disableMouseMsg struct{}215+216+// DisableMouse is a special command that stops listening for mouse events.217+func DisableMouse() Msg {218+ return disableMouseMsg{}219+}220+221 // exitAltScreenMsg in an internal message signals that the program should exit222 // alternate screen buffer. You can send a exitAltScreenMsg with ExitAltScreen.223 type exitAltScreenMsg struct{}224 10 unmodified lines235 236 // 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.240 func HideCursor() Msg {241 return hideCursorMsg{}242 }243 178 unmodified lines422 // 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 nil431+432 case enterAltScreenMsg:433 p.EnterAltScreen()434+435 case exitAltScreenMsg:436 p.ExitAltScreen()437+438+ case enableMouseCellMotionMsg:439+ p.EnableMouseCellMotion()440+441+ case enableMouseAllMotionMsg:442+ p.EnableMouseAllMotion()443+444+ case disableMouseMsg:445+ p.DisableMouseCellMotion()446+ p.DisableMouseAllMotion()447+448 case hideCursorMsg:449 hideCursor(p.output)450 }451 54 unmodified lines506 }507 }508 509 // 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.513 func (p *Program) EnableMouseCellMotion() {514 p.mtx.Lock()515 defer p.mtx.Unlock()516 fmt.Fprintf(p.output, te.CSI+te.EnableMouseCellMotionSeq)517 }518 468-// 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.523 func (p *Program) DisableMouseCellMotion() {524 p.mtx.Lock()525 defer p.mtx.Unlock()526 fmt.Fprintf(p.output, te.CSI+te.DisableMouseCellMotionSeq)527 }528 529 // 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.534 func (p *Program) EnableMouseAllMotion() {535 p.mtx.Lock()536 defer p.mtx.Unlock()537 fmt.Fprintf(p.output, te.CSI+te.EnableMouseAllMotionSeq)538 }539 486-// 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.544 func (p *Program) DisableMouseAllMotion() {545 p.mtx.Lock()546 defer p.mtx.Unlock()547 fmt.Fprintf(p.output, te.CSI+te.DisableMouseAllMotionSeq)1 unmodified line