|
| 1 | +// Copyright (c) 2026 Keymaster Team |
| 2 | +// Keymaster - SSH key management system |
| 3 | +// This source code is licensed under the MIT license found in the LICENSE file. |
| 4 | +package formelement |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/charmbracelet/bubbles/help" |
| 8 | + "github.com/charmbracelet/bubbles/key" |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | + "github.com/charmbracelet/lipgloss" |
| 11 | + "github.com/charmbracelet/x/ansi" |
| 12 | + "github.com/toeirei/keymaster/ui/tui/helpers/form" |
| 13 | + "github.com/toeirei/keymaster/ui/tui/util" |
| 14 | + "github.com/toeirei/keymaster/ui/tui/util/keys" |
| 15 | +) |
| 16 | + |
| 17 | +// *[Popup] implements [form.FormElement] |
| 18 | +var _ form.FormElement = (*Popup[any])(nil) |
| 19 | + |
| 20 | +type popupReturnValueMsg[T any] struct{ value T } |
| 21 | + |
| 22 | +type Popup[T any] struct { |
| 23 | + Label string |
| 24 | + fnOpenPopup func(returnValue func(value T) tea.Cmd) tea.Cmd |
| 25 | + fnToString func(value T) string |
| 26 | + |
| 27 | + DisabledStyle lipgloss.Style |
| 28 | + BlurredStyle lipgloss.Style |
| 29 | + FocusedStyle lipgloss.Style |
| 30 | + |
| 31 | + value T |
| 32 | + focused bool |
| 33 | + Disabled bool |
| 34 | +} |
| 35 | + |
| 36 | +func NewPopup[T any]( |
| 37 | + label string, |
| 38 | + fnOpenPopup func(returnValue func(value T) tea.Cmd) tea.Cmd, |
| 39 | + fnToString func(value T) string, |
| 40 | +) form.FormElement { |
| 41 | + return &Popup[T]{ |
| 42 | + Label: label, |
| 43 | + fnOpenPopup: fnOpenPopup, |
| 44 | + fnToString: fnToString, |
| 45 | + |
| 46 | + DisabledStyle: lipgloss.NewStyle(). |
| 47 | + Foreground(lipgloss.Color("240")), |
| 48 | + BlurredStyle: lipgloss.NewStyle(). |
| 49 | + Foreground(lipgloss.Color("240")), |
| 50 | + FocusedStyle: lipgloss.NewStyle(). |
| 51 | + Foreground(lipgloss.Color("205")). |
| 52 | + Bold(true), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (p *Popup[T]) Focus(parentKeyMap help.KeyMap) tea.Cmd { |
| 57 | + p.focused = true |
| 58 | + return util.AnnounceKeyMapCmd(parentKeyMap, keys.KeyBindingList{keys.Open()}) |
| 59 | +} |
| 60 | + |
| 61 | +func (p *Popup[T]) Blur() { p.focused = false } |
| 62 | + |
| 63 | +func (p *Popup[T]) Get() any { return p.value } |
| 64 | + |
| 65 | +func (p *Popup[T]) Init() (tea.Cmd, keys.KeyBindingList) { return nil, nil } |
| 66 | + |
| 67 | +func (p *Popup[T]) Reset() { p.value = util.NewZero[T]() } |
| 68 | + |
| 69 | +func (p *Popup[T]) Set(value any) { |
| 70 | + if value, ok := value.(T); ok { |
| 71 | + p.value = value |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (p *Popup[T]) Update(msg tea.Msg) (tea.Cmd, form.Action) { |
| 76 | + switch msg := msg.(type) { |
| 77 | + case popupReturnValueMsg[T]: |
| 78 | + p.value = msg.value |
| 79 | + case tea.KeyMsg: |
| 80 | + if p.Disabled { |
| 81 | + break |
| 82 | + } |
| 83 | + |
| 84 | + switch { |
| 85 | + case key.Matches(msg, keys.Open()): |
| 86 | + return p.fnOpenPopup( |
| 87 | + func(value T) tea.Cmd { return util.TeaMsgToCmd(popupReturnValueMsg[T]{value}) }, |
| 88 | + ), form.ActionNone |
| 89 | + case key.Matches(msg, keys.DownArrow(), keys.RightArrow()): |
| 90 | + return nil, form.ActionNext |
| 91 | + case key.Matches(msg, keys.UpArrow(), keys.LeftArrow()): |
| 92 | + return nil, form.ActionPrev |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return nil, form.ActionNone |
| 97 | +} |
| 98 | + |
| 99 | +func (p *Popup[T]) View(width int, eager bool) string { |
| 100 | + var style lipgloss.Style |
| 101 | + if p.Disabled { |
| 102 | + style = p.DisabledStyle |
| 103 | + } else if p.focused { |
| 104 | + style = p.FocusedStyle |
| 105 | + } else { |
| 106 | + style = p.BlurredStyle |
| 107 | + } |
| 108 | + |
| 109 | + label := ansi.Truncate(p.Label, width, "…") |
| 110 | + content := ansi.Truncate(p.fnToString(p.value), width-4, "…") |
| 111 | + |
| 112 | + return lipgloss.JoinVertical(lipgloss.Left, style.Render(label), "[ "+content+" ]") |
| 113 | +} |
| 114 | + |
| 115 | +func (p *Popup[T]) Focusable() bool { return !p.Disabled } |
0 commit comments