-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support focus-blur events (#1122)
* feat: support focus-blur events This backports @caarlos0 focus-blur changes from v2-exp to v1 #1081 Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * Update key_sequences.go Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: go mod tidy --------- Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> Co-authored-by: Christian Rocha <christian@rocha.is>
- Loading branch information
1 parent
a5437ac
commit e58efab
Showing
16 changed files
with
265 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
// A simple program that handled losing and acquiring focus. | ||
|
||
import ( | ||
"log" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func main() { | ||
p := tea.NewProgram(model{ | ||
// assume we start focused... | ||
focused: true, | ||
reporting: true, | ||
}, tea.WithReportFocus()) | ||
if _, err := p.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type model struct { | ||
focused bool | ||
reporting bool | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.FocusMsg: | ||
m.focused = true | ||
case tea.BlurMsg: | ||
m.focused = false | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "t": | ||
m.reporting = !m.reporting | ||
case "ctrl+c", "q": | ||
return m, tea.Quit | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func (m model) View() string { | ||
s := "Hi. Focus report is currently " | ||
if m.reporting { | ||
s += "enabled" | ||
} else { | ||
s += "disabled" | ||
} | ||
s += ".\n\n" | ||
|
||
if m.reporting { | ||
if m.focused { | ||
s += "This program is currently focused!" | ||
} else { | ||
s += "This program is currently blurred!" | ||
} | ||
} | ||
return s + "\n\nTo quit sooner press ctrl-c, or t to toggle focus reporting...\n" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package tea | ||
|
||
// FocusMsg represents a terminal focus message. | ||
// This occurs when the terminal gains focus. | ||
type FocusMsg struct{} | ||
|
||
// BlurMsg represents a terminal blur message. | ||
// This occurs when the terminal loses focus. | ||
type BlurMsg struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.