summaryrefslogtreecommitdiff
path: root/internal/client/ui/auth/field
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2024-11-07 18:00:35 +0200
committerKyren223 <Kyren223@proton.me>2024-11-07 18:00:35 +0200
commite29b7ade2f402bf89ca60bcb4a5598310ff809d4 (patch)
treef4f151985a0735afbaf3a88792187f4352315093 /internal/client/ui/auth/field
parentdd3fd8c0a771f44dc9f0657e3f8e5b262682b785 (diff)
feat: continued to improve the UI
Diffstat (limited to 'internal/client/ui/auth/field')
-rw-r--r--internal/client/ui/auth/field/field.go100
1 files changed, 86 insertions, 14 deletions
diff --git a/internal/client/ui/auth/field/field.go b/internal/client/ui/auth/field/field.go
index 623cdd6..4c6ca12 100644
--- a/internal/client/ui/auth/field/field.go
+++ b/internal/client/ui/auth/field/field.go
@@ -9,25 +9,36 @@ import (
)
type Model struct {
- Input textinput.Model
+ Input textinput.Model
+ Header string
+ Visisble bool
+ width int
+
+ reveal bool
+ revealIcon string
+ concealIcon string
+ icon string
+
Style lipgloss.Style
FocusedStyle lipgloss.Style
BlurredStyle lipgloss.Style
ErrorStyle lipgloss.Style
- Header string
+ HeaderStyle lipgloss.Style
}
func New(width int) Model {
- t := textinput.New()
- t.Width = width
- t.CharLimit = width
- t.Prompt = ""
+ input := textinput.New()
+ input.Prompt = ""
+ input.Width = width
m := Model{
- Input: t,
+ width: width,
+ Visisble: true,
+ Input: input,
}
m.Blur()
+ m.SetRevealed(true)
return m
}
@@ -37,21 +48,29 @@ func (m Model) Init() tea.Cmd {
}
func (m Model) View() string {
+ if !m.Visisble {
+ return ""
+ }
+
style := m.Style
- error := ""
+ header := m.HeaderStyle.Render(m.Header)
if m.Input.Err != nil {
- error = m.Input.Err.Error()
+ error := m.Input.Err.Error()
style = style.BorderForeground(m.ErrorStyle.GetForeground())
+ header = lipgloss.NewStyle().
+ MaxWidth(m.width).
+ Render(header + m.ErrorStyle.Render(" - "+error))
}
- field := ui.AddBorderHeader(m.Header, 1, style, m.Input.View())
- error = m.ErrorStyle.MaxWidth(lipgloss.Width(field)).Render(error)
-
- // return lipgloss.JoinVertical(lipgloss.Left, borderTop, field, error)
- return lipgloss.JoinVertical(lipgloss.Left, field, error)
+ field := ui.AddBorderHeader(header, 1, style, m.Input.View()+m.icon)
+ return field
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
+ if !m.Visisble {
+ return m, nil
+ }
+
var cmd tea.Cmd
m.Input, cmd = m.Input.Update(msg)
return m, cmd
@@ -68,3 +87,56 @@ func (m *Model) Blur() {
m.Input.PromptStyle = m.BlurredStyle
m.Input.TextStyle = m.BlurredStyle
}
+
+func (m *Model) SetWidth(width int) {
+ m.width = width
+ m.recalculateWidth()
+}
+
+func (m Model) Width() int {
+ return m.width
+}
+
+func (m *Model) SetRevealed(revealed bool) {
+ m.reveal = revealed
+ if m.reveal {
+ m.Input.EchoMode = textinput.EchoNormal
+ m.icon = m.revealIcon
+ } else {
+ m.Input.EchoMode = textinput.EchoPassword
+ m.icon = m.concealIcon
+ }
+ m.recalculateWidth()
+}
+
+func (m Model) Revealed() bool {
+ return m.reveal
+}
+
+func (m *Model) SetRevealIcon(icon string) {
+ m.revealIcon = icon
+ if m.reveal {
+ m.icon = icon
+ }
+ m.recalculateWidth()
+}
+
+func (m Model) RevealIcon() string {
+ return m.revealIcon
+}
+
+func (m *Model) SetConcealIcon(icon string) {
+ m.concealIcon = icon
+ if !m.reveal {
+ m.icon = icon
+ }
+ m.recalculateWidth()
+}
+
+func (m Model) ConcealIcon() string {
+ return m.concealIcon
+}
+
+func (m *Model) recalculateWidth() {
+ m.Input.Width = m.width - lipgloss.Width(m.icon)
+}