87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"github.com/gdamore/tcell/v2"
|
||
|
"github.com/rivo/tview"
|
||
|
)
|
||
|
|
||
|
type modalInput struct {
|
||
|
*tview.Form
|
||
|
frame *tview.Frame
|
||
|
text string
|
||
|
done func(string, bool)
|
||
|
}
|
||
|
|
||
|
func NewModalInput() *modalInput {
|
||
|
form := tview.NewForm()
|
||
|
m := modalInput{
|
||
|
Form: form,
|
||
|
frame: tview.NewFrame(form),
|
||
|
text: "",
|
||
|
done: 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)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
func (m *modalInput) SetValue(text string) {
|
||
|
m.Clear(false)
|
||
|
m.AddInputField("", text, 50, nil, func(text string) {
|
||
|
m.text = text
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (m *modalInput) SetDoneFunc(handler func(string, bool)) *modalInput {
|
||
|
m.done = handler
|
||
|
return m
|
||
|
}
|
||
|
|
||
|
func (m *modalInput) Draw(screen tcell.Screen) {
|
||
|
buttonsWidth := 50
|
||
|
screenWidth, screenHeight := screen.Size()
|
||
|
width := screenWidth / 3
|
||
|
|
||
|
if width < buttonsWidth {
|
||
|
width = buttonsWidth
|
||
|
}
|
||
|
|
||
|
height := 7
|
||
|
width += 4
|
||
|
|
||
|
// Set the modal's position and size.
|
||
|
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)
|
||
|
}
|