104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"github.com/gdamore/tcell/v2"
|
||
|
"github.com/rivo/tview"
|
||
|
)
|
||
|
|
||
|
type statusForm struct {
|
||
|
*tview.Form
|
||
|
frame *tview.Frame
|
||
|
nameLabel string
|
||
|
done func(string, bool)
|
||
|
}
|
||
|
|
||
|
func newStatusForm() *statusForm {
|
||
|
var (
|
||
|
background = tcell.ColorBlack.TrueColor()
|
||
|
buttonBackground = tcell.ColorBlueViolet.TrueColor()
|
||
|
textColour = tcell.ColorWhite.TrueColor()
|
||
|
fieldBackground = tcell.ColorGrey.TrueColor()
|
||
|
labelColour = tcell.ColorGreen.TrueColor()
|
||
|
|
||
|
form = tview.NewForm()
|
||
|
)
|
||
|
|
||
|
obj := statusForm{
|
||
|
Form: form,
|
||
|
frame: tview.NewFrame(form),
|
||
|
done: nil,
|
||
|
nameLabel: "Name",
|
||
|
}
|
||
|
|
||
|
// Stylise the buttons
|
||
|
obj.SetButtonsAlign(tview.AlignCenter).
|
||
|
SetButtonBackgroundColor(buttonBackground).
|
||
|
SetButtonTextColor(textColour).
|
||
|
SetBorderPadding(0, 0, 0, 0)
|
||
|
|
||
|
// Stylise the form
|
||
|
obj.SetLabelColor(labelColour).
|
||
|
SetFieldBackgroundColor(fieldBackground).
|
||
|
SetFieldTextColor(textColour).
|
||
|
SetBackgroundColor(background)
|
||
|
|
||
|
// Stylise the frame around the form
|
||
|
obj.frame.SetBorders(0, 0, 1, 0, 0, 0).
|
||
|
SetBorder(true).
|
||
|
SetBorderColor(tcell.ColorOrangeRed.TrueColor()).
|
||
|
SetBackgroundColor(background).
|
||
|
SetBorderPadding(1, 1, 1, 1)
|
||
|
|
||
|
obj.AddButton("Save", func() {
|
||
|
if obj.done != nil {
|
||
|
name, ok := obj.GetFormItemByLabel(obj.nameLabel).(*tview.InputField)
|
||
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
obj.done(name.GetText(), true)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
obj.AddButton("Cancel", func() {
|
||
|
if obj.done != nil {
|
||
|
obj.done("", false)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return &obj
|
||
|
}
|
||
|
|
||
|
func (s *statusForm) updateInputFields(name string) {
|
||
|
s.Clear(false)
|
||
|
s.AddInputField(s.nameLabel, name, 0, nil, nil)
|
||
|
}
|
||
|
|
||
|
func (s *statusForm) setDoneFunc(handler func(string, bool)) *statusForm {
|
||
|
s.done = handler
|
||
|
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *statusForm) Draw(screen tcell.Screen) {
|
||
|
buttonsWidth := 20
|
||
|
screenWidth, screenHeight := screen.Size()
|
||
|
width := screenWidth / 3
|
||
|
|
||
|
if width < buttonsWidth {
|
||
|
width = buttonsWidth
|
||
|
}
|
||
|
|
||
|
height := 20
|
||
|
width += 4
|
||
|
|
||
|
// Set the form's position and size.
|
||
|
x := (screenWidth - width) / 2
|
||
|
y := (screenHeight - height) / 2
|
||
|
s.SetRect(x, y, width, height)
|
||
|
|
||
|
// Draw the frame.
|
||
|
s.frame.SetRect(x, y, width, height)
|
||
|
s.frame.Draw(screen)
|
||
|
}
|