Dan Anglin
d167039176
This commit adds support for editing an existing card's title and description. The (previously named) input modal has been enhanced to support both creating and editing cards. Part of apollo/pelican#14
115 lines
2.5 KiB
Go
115 lines
2.5 KiB
Go
package ui
|
|
|
|
import (
|
|
"github.com/gdamore/tcell/v2"
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
type cardModalMode int
|
|
|
|
const (
|
|
create cardModalMode = iota
|
|
edit
|
|
)
|
|
|
|
type cardModal struct {
|
|
*tview.Form
|
|
frame *tview.Frame
|
|
title string
|
|
description string
|
|
done func(string, string, bool, cardModalMode)
|
|
mode cardModalMode
|
|
}
|
|
|
|
func newCardModal() *cardModal {
|
|
var (
|
|
background = tcell.ColorBlack.TrueColor()
|
|
buttonBackground = tcell.ColorBlueViolet.TrueColor()
|
|
textColour = tcell.ColorWhite.TrueColor()
|
|
fieldBackground = tcell.ColorGrey.TrueColor()
|
|
labelColour = tcell.ColorGreen.TrueColor()
|
|
|
|
form = tview.NewForm()
|
|
)
|
|
|
|
modal := cardModal{
|
|
Form: form,
|
|
frame: tview.NewFrame(form),
|
|
title: "",
|
|
description: "",
|
|
done: nil,
|
|
mode: create,
|
|
}
|
|
|
|
// Stylise the buttons
|
|
modal.SetButtonsAlign(tview.AlignCenter).
|
|
SetButtonBackgroundColor(buttonBackground).
|
|
SetButtonTextColor(textColour).
|
|
SetBorderPadding(0, 0, 0, 0)
|
|
|
|
// Stylise the form
|
|
modal.SetLabelColor(labelColour).
|
|
SetFieldBackgroundColor(fieldBackground).
|
|
SetFieldTextColor(textColour).
|
|
SetBackgroundColor(background)
|
|
|
|
// Stylise the frame around the form
|
|
modal.frame.SetBorders(0, 0, 1, 0, 0, 0).
|
|
SetBorder(true).
|
|
SetBorderColor(tcell.ColorOrangeRed.TrueColor()).
|
|
SetBackgroundColor(background).
|
|
SetBorderPadding(1, 1, 1, 1)
|
|
|
|
modal.AddButton("Save", func() {
|
|
if modal.done != nil {
|
|
modal.done(modal.title, modal.description, true, modal.mode)
|
|
}
|
|
})
|
|
|
|
modal.AddButton("Cancel", func() {
|
|
if modal.done != nil {
|
|
modal.done(modal.title, modal.description, false, modal.mode)
|
|
}
|
|
})
|
|
|
|
return &modal
|
|
}
|
|
|
|
func (m *cardModal) updateInputFields(title, description string) {
|
|
m.Clear(false)
|
|
m.AddInputField("Title", title, 60, nil, func(text string) {
|
|
m.title = text
|
|
})
|
|
|
|
m.AddTextArea("Description", description, 60, 10, 0, func(text string) {
|
|
m.description = text
|
|
})
|
|
}
|
|
|
|
func (m *cardModal) setDoneFunc(handler func(string, string, bool, cardModalMode)) *cardModal {
|
|
m.done = handler
|
|
|
|
return m
|
|
}
|
|
|
|
func (m *cardModal) Draw(screen tcell.Screen) {
|
|
buttonsWidth := 20
|
|
screenWidth, screenHeight := screen.Size()
|
|
width := screenWidth / 3
|
|
|
|
if width < buttonsWidth {
|
|
width = buttonsWidth
|
|
}
|
|
|
|
height := 20
|
|
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)
|
|
}
|