Skip to content

Commit

Permalink
pam/adapter: Only draw a model when it's also focused
Browse files Browse the repository at this point in the history
In the tests that we have we now rely on the fact that when something is
on the screen, then it's also possible to type on it, while this is not
really the case in our bubbletea implementation, because we may end up
drawing and only then we focus a view.

Prevent this, by only showing a model when it's also focused (or ready
to draw), so that there's no risk of showing something and not being
able to get its input.
  • Loading branch information
3v1n0 committed Nov 29, 2024
1 parent c1864f2 commit 7d2ffcb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pam/internal/adapter/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type authenticationComponent interface {
Update(msg tea.Msg) (tea.Model, tea.Cmd)
View() string
Focus() tea.Cmd
Focused() bool
Blur()
}

Expand Down Expand Up @@ -348,11 +349,11 @@ func (m *authenticationModel) Update(msg tea.Msg) (authModel authenticationModel

// Focus focuses this model.
func (m *authenticationModel) Focus() tea.Cmd {
m.focused = true

if m.currentModel == nil {
return nil
}

m.focused = true
return m.currentModel.Focus()
}

Expand Down Expand Up @@ -419,6 +420,9 @@ func (m authenticationModel) View() string {
if m.currentModel == nil {
return ""
}
if !m.Focused() {
return ""
}
contents := []string{m.currentModel.View()}

errMsg := m.errorMsg
Expand Down
9 changes: 9 additions & 0 deletions pam/internal/adapter/authmodeselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea
return m, cmd
}

// View renders a text view of the authentication UI.
func (m authModeSelectionModel) View() string {
if !m.Focused() {
return ""
}

return m.Model.View()
}

// Focus focuses this model.
func (m *authModeSelectionModel) Focus() tea.Cmd {
m.focused = true
Expand Down
9 changes: 9 additions & 0 deletions pam/internal/adapter/brokerselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ func (m brokerSelectionModel) Update(msg tea.Msg) (brokerSelectionModel, tea.Cmd
return m, cmd
}

// View renders a text view of the authentication UI.
func (m brokerSelectionModel) View() string {
if !m.Focused() {
return ""
}

return m.Model.View()
}

// Focus focuses this model. It always returns nil.
func (m *brokerSelectionModel) Focus() tea.Cmd {
m.focused = true
Expand Down
5 changes: 5 additions & 0 deletions pam/internal/adapter/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ func (b *buttonModel) Focus() tea.Cmd {
func (b *buttonModel) Blur() {
b.focused = false
}

// Focused returns whether if this model is focused.
func (b buttonModel) Focused() bool {
return b.focused
}
17 changes: 17 additions & 0 deletions pam/internal/adapter/formmodel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adapter

import (
"slices"
"strings"

tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -121,6 +122,10 @@ func (m formModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// View renders a text view of the form.
func (m formModel) View() string {
if !m.Focused() {
return ""
}

var fields []string

if m.label != "" {
Expand Down Expand Up @@ -151,3 +156,15 @@ func (m formModel) Blur() {
}
m.focusableModels[m.focusIndex].Blur()
}

// Focused returns whether if this model is focused.
func (m formModel) Focused() bool {
if len(m.focusableModels) == 0 {
// We consider the model being focused in this case, since there's nothing
// to interact with, but we want to be able to draw.
return true
}
return slices.ContainsFunc(m.focusableModels, func(ac authenticationComponent) bool {
return ac.Focused()
})
}
9 changes: 9 additions & 0 deletions pam/internal/adapter/newpasswordmodel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package adapter

import (
"slices"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -180,6 +182,13 @@ func (m newPasswordModel) Blur() {
m.focusableModels[m.focusIndex].Blur()
}

// Focused returns whether if this model is focused.
func (m newPasswordModel) Focused() bool {
return slices.ContainsFunc(m.focusableModels, func(ac authenticationComponent) bool {
return ac.Focused()
})
}

func (m *newPasswordModel) focusField(increment int) tea.Cmd {
var cmd tea.Cmd
focusableLen := len(m.focusableModels)
Expand Down
6 changes: 6 additions & 0 deletions pam/internal/adapter/qrcodemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@ func (m qrcodeModel) Blur() {
}
m.buttonModel.Blur()
}

// Focused returns whether if this model is focused.
func (m qrcodeModel) Focused() bool {
// This is always considered focused.
return true
}
3 changes: 3 additions & 0 deletions pam/internal/adapter/userselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,8 @@ func (m userSelectionModel) View() string {
if !m.enabled {
return ""
}
if !m.Focused() {
return ""
}
return m.Model.View()
}

0 comments on commit 7d2ffcb

Please sign in to comment.