From 577baf306d9335aae5f1a816fd57f8973f3349da Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 17 Feb 2020 17:00:01 -0500 Subject: [PATCH] Remove blink state when changing focus --- examples/inputs/main.go | 6 +++--- input/input.go | 32 ++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/examples/inputs/main.go b/examples/inputs/main.go index a9c0d09..e27498c 100644 --- a/examples/inputs/main.go +++ b/examples/inputs/main.go @@ -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] diff --git a/input/input.go b/input/input.go index 15ef023..84f577b 100644 --- a/input/input.go +++ b/input/input.go @@ -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).