pelican/internal/ui/statusform.go
Dan Anglin c1bb834a7f
All checks were successful
/ test (pull_request) Successful in 34s
/ lint (pull_request) Successful in 46s
feat(ui): add support for creating status columns
This commit adds support for creating new status columns. When the user
is in the 'board edit' mode, they can press the 'c' key to create a new
column. When a new column is created it automatically assumes the last
position on the board. We will add support later on to allow the user to
re-arrange the columns on the board.

Part of apollo/pelican#22
2024-01-17 17:10:36 +00:00

104 lines
2.2 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, formMode)
mode formMode
}
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.mode)
}
})
obj.AddButton("Cancel", func() {
if obj.done != nil {
obj.done("", false, obj.mode)
}
})
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, formMode)) *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)
}