This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
inputmodal.go
82 lines (72 loc) · 1.98 KB
/
inputmodal.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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// ModalInput is based on Modal from tview, but has an input field instead
type ModalInput struct {
*tview.Form
frame *tview.Frame
text string
done func(string, bool)
}
func NewModalInput() *ModalInput {
form := tview.NewForm()
m := &ModalInput{form, tview.NewFrame(form), "", nil}
m.SetButtonsAlign(tview.AlignCenter).
SetButtonBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
SetButtonTextColor(tview.Styles.PrimaryTextColor).
SetBackgroundColor(tview.Styles.ContrastBackgroundColor).
SetBorderPadding(0, 0, 0, 0)
m.AddInputField("", "", 50, nil, func(text string) {
m.text = text
})
m.AddButton("OK", func() {
if m.done != nil {
m.done(m.text, true) // Passed
}
})
m.AddButton("Cancel", func() {
if m.done != nil {
m.done(m.text, false)
}
})
m.frame.SetBorders(0, 0, 1, 0, 0, 0).
SetBorder(true).
SetBackgroundColor(tview.Styles.ContrastBackgroundColor).
SetBorderPadding(1, 1, 1, 1)
return m
}
// SetValue sets the current value in the item
func (m *ModalInput) SetValue(text string) {
m.Clear(false)
m.AddInputField("", text, 50, nil, func(text string) {
m.text = text
})
}
// SetDoneFunc sets the done func for this input.
// Will be called with the text of the input and a boolean for OK or cancel button.
func (m *ModalInput) SetDoneFunc(handler func(string, bool)) *ModalInput {
m.done = handler
return m
}
// Draw draws this primitive onto the screen.
func (m *ModalInput) Draw(screen tcell.Screen) {
// Calculate the width of this modal.
buttonsWidth := 50
screenWidth, screenHeight := screen.Size()
width := screenWidth / 3
if width < buttonsWidth {
width = buttonsWidth
}
// width is now without the box border.
// Set the modal's position and size.
height := 7
width += 4
x := (screenWidth - width) / 2
y := (screenHeight - height) / 2
m.SetRect(x, y, width, height)
// Draw the frame.
m.frame.SetRect(x, y, width, height)
m.frame.Draw(screen)
}