Debian Codegraph @

ui/password-modal.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package ui
18
19 import (
20 "fmt"
21 "strings"
22
23 "go.mau.fi/mauview"
•24 "github.com/gdamore/tcell/v2"
25 )
26
27 type PasswordModal struct {
28 mauview.Component
29
30 outputChan chan string
31 cancelChan chan struct{}
32
33 form *mauview.Form
34
35 text *mauview.TextField
36 confirmText *mauview.TextField
37
38 input *mauview.InputField
39 confirmInput *mauview.InputField
40
41 cancel *mauview.Button
42 submit *mauview.Button
43
44 parent *MainView
45 }
46
47 func (view *MainView) AskPassword(title, thing, placeholder string, isNew bool) (string, bool) {
48 pwm := NewPasswordModal(view, title, thing, placeholder, isNew)
49 view.ShowModal(pwm)
50 view.parent.Render()
51 return pwm.Wait()
52 }
53
54 func NewPasswordModal(parent *MainView, title, thing, placeholder string, isNew bool) *PasswordModal {
55 if placeholder == "" {
56 placeholder = "correct horse battery staple"
57 }
58 if thing == "" {
59 thing = strings.ToLower(title)
60 }
61 pwm := &PasswordModal{
62 parent: parent,
63 form: mauview.NewForm(),
64 outputChan: make(chan string, 1),
65 cancelChan: make(chan struct{}, 1),
66 }
67
68 pwm.form.
69 SetColumns([]int{1, 20, 1, 20, 1}).
70 SetRows([]int{1, 1, 1, 0, 0, 0, 1, 1, 1})
71
72 width := 45
73 height := 8
74
75 pwm.text = mauview.NewTextField()
76 if isNew {
77 pwm.text.SetText(fmt.Sprintf("Create a %s", thing))
78 } else {
79 pwm.text.SetText(fmt.Sprintf("Enter the %s", thing))
80 }
81 pwm.input = mauview.NewInputField().
82 SetMaskCharacter('*').
83 SetPlaceholder(placeholder)
84 pwm.form.AddComponent(pwm.text, 1, 1, 3, 1)
85 pwm.form.AddFormItem(pwm.input, 1, 2, 3, 1)
86
87 if isNew {
88 height += 3
89 pwm.confirmInput = mauview.NewInputField().
90 SetMaskCharacter('*').
91 SetPlaceholder(placeholder).
92 SetChangedFunc(pwm.HandleChange)
93 pwm.input.SetChangedFunc(pwm.HandleChange)
94 pwm.confirmText = mauview.NewTextField().SetText(fmt.Sprintf("Confirm %s", thing))
95
96 pwm.form.SetRow(3, 1).SetRow(4, 1).SetRow(5, 1)
97 pwm.form.AddComponent(pwm.confirmText, 1, 4, 3, 1)
98 pwm.form.AddFormItem(pwm.confirmInput, 1, 5, 3, 1)
99 }
100
101 pwm.cancel = mauview.NewButton("Cancel").SetOnClick(pwm.ClickCancel)
102 pwm.submit = mauview.NewButton("Submit").SetOnClick(pwm.ClickSubmit)
103
104 pwm.form.AddFormItem(pwm.submit, 3, 7, 1, 1)
105 pwm.form.AddFormItem(pwm.cancel, 1, 7, 1, 1)
106
107 box := mauview.NewBox(pwm.form).SetTitle(title)
108 center := mauview.Center(box, width, height).SetAlwaysFocusChild(true)
109 center.Focus()
110 pwm.form.FocusNextItem()
111 pwm.Component = center
112
113 return pwm
114 }
115
116 func (pwm *PasswordModal) HandleChange(_ string) {
117 if pwm.input.GetText() == pwm.confirmInput.GetText() {
118 pwm.submit.SetBackgroundColor(mauview.Styles.ContrastBackgroundColor)
119 } else {
120 pwm.submit.SetBackgroundColor(tcell.ColorDefault)
121 }
122 }
123
124 func (pwm *PasswordModal) ClickCancel() {
125 pwm.parent.HideModal()
126 pwm.cancelChan <- struct{}{}
127 }
128
129 func (pwm *PasswordModal) ClickSubmit() {
130 if pwm.confirmInput == nil || pwm.input.GetText() == pwm.confirmInput.GetText() {
131 pwm.parent.HideModal()
132 pwm.outputChan <- pwm.input.GetText()
133 }
134 }
135
136 func (pwm *PasswordModal) Wait() (string, bool) {
137 select {
138 case result := <-pwm.outputChan:
139 return result, true
140 case <-pwm.cancelChan:
141 return "", false
142 }
143 }