Skip to content

Commit

Permalink
fix: program renderer commands (#1030)
Browse files Browse the repository at this point in the history
Using program commands like p.EnableMouseCellMotion and p.SetWindowTitle
_before_ the program starts can panic the application since `renderer`
hasn't been initialized yet. Use program options to enable and set these
options if `renderer` is not initialized.

Fixes: #1029
  • Loading branch information
aymanbagabas authored May 30, 2024
1 parent 5331b83 commit eb78a8c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
36 changes: 31 additions & 5 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ type disableBracketedPasteMsg struct{}
func (p *Program) EnterAltScreen() {
if p.renderer != nil {
p.renderer.enterAltScreen()
} else {
p.startupOptions |= withAltScreen
}
}

Expand All @@ -160,6 +162,8 @@ func (p *Program) EnterAltScreen() {
func (p *Program) ExitAltScreen() {
if p.renderer != nil {
p.renderer.exitAltScreen()
} else {
p.startupOptions &^= withAltScreen
}
}

Expand All @@ -168,15 +172,23 @@ func (p *Program) ExitAltScreen() {
//
// Deprecated: Use the WithMouseCellMotion ProgramOption instead.
func (p *Program) EnableMouseCellMotion() {
p.renderer.enableMouseCellMotion()
if p.renderer != nil {
p.renderer.enableMouseCellMotion()
} else {
p.startupOptions |= withMouseCellMotion
}
}

// DisableMouseCellMotion disables Mouse Cell Motion tracking. This will be
// called automatically when exiting a Bubble Tea program.
//
// Deprecated: The mouse will automatically be disabled when the program exits.
func (p *Program) DisableMouseCellMotion() {
p.renderer.disableMouseCellMotion()
if p.renderer != nil {
p.renderer.disableMouseCellMotion()
} else {
p.startupOptions &^= withMouseCellMotion
}
}

// EnableMouseAllMotion enables mouse click, release, wheel and motion events,
Expand All @@ -185,18 +197,32 @@ func (p *Program) DisableMouseCellMotion() {
//
// Deprecated: Use the WithMouseAllMotion ProgramOption instead.
func (p *Program) EnableMouseAllMotion() {
p.renderer.enableMouseAllMotion()
if p.renderer != nil {
p.renderer.enableMouseAllMotion()
} else {
p.startupOptions |= withMouseAllMotion
}
}

// DisableMouseAllMotion disables All Motion mouse tracking. This will be
// called automatically when exiting a Bubble Tea program.
//
// Deprecated: The mouse will automatically be disabled when the program exits.
func (p *Program) DisableMouseAllMotion() {
p.renderer.disableMouseAllMotion()
if p.renderer != nil {
p.renderer.disableMouseAllMotion()
} else {
p.startupOptions &^= withMouseAllMotion
}
}

// SetWindowTitle sets the terminal window title.
//
// Deprecated: Use the SetWindowTitle command instead.
func (p *Program) SetWindowTitle(title string) {
p.renderer.setWindowTitle(title)
if p.renderer != nil {
p.renderer.setWindowTitle(title)
} else {
p.startupTitle = title
}
}
7 changes: 7 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ type Program struct {
// treated as bits. These options can be set via various ProgramOptions.
startupOptions startupOptions

// startupTitle is the title that will be set on the terminal when the
// program starts.
startupTitle string

inputType inputType

ctx context.Context
Expand Down Expand Up @@ -490,6 +494,9 @@ func (p *Program) Run() (Model, error) {
}

// Honor program startup options.
if p.startupTitle != "" {
p.renderer.setWindowTitle(p.startupTitle)
}
if p.startupOptions&withAltScreen != 0 {
p.renderer.enterAltScreen()
}
Expand Down

0 comments on commit eb78a8c

Please sign in to comment.