pelican/internal/ui/modalinput.go

117 lines
2.4 KiB
Go
Raw Normal View History

package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type modalInput struct {
*tview.Form
frame *tview.Frame
title string
description string
done func(string, string, bool)
}
func newModalInput() *modalInput {
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 := modalInput{
Form: form,
frame: tview.NewFrame(form),
title: "",
description: "",
done: nil,
}
// 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("Create card", func() {
if modal.done != nil {
modal.done(modal.title, modal.description, true)
}
modal.reset()
})
modal.AddButton("Cancel", func() {
if modal.done != nil {
modal.done(modal.title, modal.description, false)
}
modal.reset()
})
modal.addInputFields()
modal.frame.SetTitle(" New Card ")
return &modal
}
func (m *modalInput) reset() {
m.Clear(false)
m.addInputFields()
}
func (m *modalInput) addInputFields() {
m.AddInputField("Title", "", 60, nil, func(text string) {
m.title = text
})
m.AddTextArea("Description", "", 60, 10, 0, func(text string) {
m.description = text
})
}
func (m *modalInput) SetDoneFunc(handler func(string, string, bool)) *modalInput {
m.done = handler
return m
}
func (m *modalInput) 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)
}