/
signin.go
151 lines (129 loc) 路 3.12 KB
/
signin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package view
import (
"github.com/dyng/ramen/internal/view/format"
"github.com/dyng/ramen/internal/view/style"
"github.com/dyng/ramen/internal/view/util"
"github.com/ethereum/go-ethereum/log"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type SignInDialog struct {
*tview.InputField
app *App
display bool
lastFocus tview.Primitive
spinner *util.Spinner
}
func NewSignInDialog(app *App) *SignInDialog {
d := &SignInDialog{
app: app,
display: false,
spinner: util.NewSpinner(app.Application),
}
// setup layout
d.initLayout()
return d
}
func (d *SignInDialog) initLayout() {
s := d.app.config.Style()
input := tview.NewInputField()
input.SetFieldWidth(80)
input.SetBorder(true)
input.SetBorderColor(s.DialogBorderColor)
input.SetTitle(style.Padding("Private Key"))
input.SetTitleColor(s.FgColor)
input.SetLabel(" ")
input.SetMaskCharacter('*')
input.SetFieldBackgroundColor(s.DialogBgColor)
input.SetDoneFunc(d.handleKey)
d.InputField = input
}
func (d *SignInDialog) handleKey(key tcell.Key) {
switch key {
case tcell.KeyEnter:
// start spinner
d.Loading()
privateKey := d.GetText()
if privateKey == "" {
return
}
go func() {
signer, err := d.app.service.GetSigner(privateKey)
signer.UpdateBalance() // populate balance cache
d.app.QueueUpdateDraw(func() {
if err != nil {
d.Finished()
log.Error("Failed to create signer", "error", err)
d.app.root.NotifyError(format.FineErrorMessage("Failed to create signer", err))
} else {
d.app.root.SignIn(signer)
d.Finished()
}
})
}()
case tcell.KeyEsc:
d.Hide()
}
}
func (d *SignInDialog) Show() {
if !d.display {
// save last focused element
d.lastFocus = d.app.GetFocus()
d.Display(true)
d.app.SetFocus(d)
}
}
func (d *SignInDialog) Hide() {
if d.display {
d.Display(false)
d.app.SetFocus(d.lastFocus)
}
}
// Loading will set the location of spinner and show it
func (d *SignInDialog) Loading() {
d.setSpinnerRect()
d.spinner.StartAndShow()
}
// Finished will stop and hide spinner, as well as close current dialog
func (d *SignInDialog) Finished() {
d.spinner.StopAndHide()
d.Hide()
}
func (d *SignInDialog) Clear() {
d.InputField.SetText("")
}
func (d *SignInDialog) Display(display bool) {
d.display = display
}
func (d *SignInDialog) IsDisplay() bool {
return d.display
}
// Draw implements tview.Primitive
func (d *SignInDialog) Draw(screen tcell.Screen) {
if d.display {
d.InputField.Draw(screen)
}
d.spinner.Draw(screen)
}
func (d *SignInDialog) SetCentral(x int, y int, width int, height int) {
inputWidth, inputHeight := d.inputSize()
if inputWidth > width-2 {
inputWidth = width - 2
}
if inputHeight > height-2 {
inputHeight = height
}
ws := (width - inputWidth) / 2
hs := (height - inputHeight) / 2
d.InputField.SetRect(x+ws, y+hs, inputWidth, inputHeight)
}
func (d *SignInDialog) inputSize() (int, int) {
width := len(d.GetLabel()) + d.GetFieldWidth()
height := d.GetFieldHeight() + 2
return width, height
}
func (d *SignInDialog) setSpinnerRect() {
x, y, _, _ := d.GetInnerRect()
sx := x + len(d.GetText()) + 1
d.spinner.SetRect(sx, y, 0, 0)
}