2024-01-17 13:43:20 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statusForm struct {
|
|
|
|
*tview.Form
|
|
|
|
frame *tview.Frame
|
|
|
|
nameLabel string
|
2024-01-17 17:10:36 +00:00
|
|
|
done func(string, bool, formMode)
|
|
|
|
mode formMode
|
2024-01-17 13:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:10:36 +00:00
|
|
|
obj.done(name.GetText(), true, obj.mode)
|
2024-01-17 13:43:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
obj.AddButton("Cancel", func() {
|
|
|
|
if obj.done != nil {
|
2024-01-17 17:10:36 +00:00
|
|
|
obj.done("", false, obj.mode)
|
2024-01-17 13:43:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return &obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *statusForm) updateInputFields(name string) {
|
|
|
|
s.Clear(false)
|
|
|
|
s.AddInputField(s.nameLabel, name, 0, nil, nil)
|
|
|
|
}
|
|
|
|
|
2024-01-17 17:10:36 +00:00
|
|
|
func (s *statusForm) setDoneFunc(handler func(string, bool, formMode)) *statusForm {
|
2024-01-17 13:43:20 +00:00
|
|
|
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)
|
|
|
|
}
|