Remove blink state when changing focus

This commit is contained in:
Christian Rocha 2020-02-17 17:00:01 -05:00
parent f087c8260a
commit 577baf306d
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 29 additions and 9 deletions

View File

@ -29,7 +29,7 @@ type Model struct {
func initialize() (tea.Model, tea.Cmd) {
n := input.DefaultModel()
n.Placeholder = "Name"
n.Focus = true
n.Focus()
e := input.DefaultModel()
e.Placeholder = "Email"
@ -75,10 +75,10 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
for i := 0; i < len(inputs); i++ {
if i == m.index {
inputs[i].Focus = true
inputs[i].Focus()
continue
}
inputs[i].Focus = false
inputs[i].Blur()
}
m.nameInput = inputs[0]

View File

@ -19,13 +19,33 @@ type Model struct {
// Focus indicates whether user input focus should be on this input
// component. When false, don't blink and ignore keyboard input.
Focus bool
focus bool
blink bool
pos int
colorProfile termenv.Profile
}
// Focused returns the focus state on the model
func (m Model) Focused() bool {
if m.focus {
return true
}
return false
}
// Focus sets the focus state on the model
func (m *Model) Focus() {
m.focus = true
m.blink = false
}
// Blur removes the focus state on the model
func (m *Model) Blur() {
m.focus = false
m.blink = true
}
type CursorBlinkMsg struct{}
func DefaultModel() Model {
@ -36,16 +56,16 @@ func DefaultModel() Model {
Placeholder: "",
PlaceholderColor: "240",
CursorColor: "",
Focus: false,
blink: false,
focus: false,
blink: true,
pos: 0,
colorProfile: termenv.ColorProfile(),
}
}
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
if !m.Focus {
if !m.focus {
m.blink = true
return m, nil
}
@ -140,7 +160,7 @@ func placeholderView(m Model) string {
)
// Cursor
if (!m.Focus || m.blink) && m.PlaceholderColor != "" {
if m.blink && m.PlaceholderColor != "" {
v += cursorView(
termenv.String(p[:1]).
Foreground(color(c)).
@ -161,7 +181,7 @@ func placeholderView(m Model) string {
// Style the cursor
func cursorView(s string, m Model) string {
if !m.Focus || m.blink {
if m.blink {
return s
} else if m.CursorColor != "" {
return termenv.String(s).