2024-01-10 11:20:15 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
type cardModalMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
create cardModalMode = iota
|
|
|
|
edit
|
|
|
|
)
|
|
|
|
|
|
|
|
type cardModal struct {
|
2024-01-10 11:20:15 +00:00
|
|
|
*tview.Form
|
|
|
|
frame *tview.Frame
|
|
|
|
title string
|
|
|
|
description string
|
2024-01-10 18:21:14 +00:00
|
|
|
done func(string, string, bool, cardModalMode)
|
|
|
|
mode cardModalMode
|
2024-01-10 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
func newCardModal() *cardModal {
|
2024-01-10 11:20:15 +00:00
|
|
|
var (
|
|
|
|
background = tcell.ColorBlack.TrueColor()
|
|
|
|
buttonBackground = tcell.ColorBlueViolet.TrueColor()
|
|
|
|
textColour = tcell.ColorWhite.TrueColor()
|
|
|
|
fieldBackground = tcell.ColorGrey.TrueColor()
|
|
|
|
labelColour = tcell.ColorGreen.TrueColor()
|
|
|
|
|
|
|
|
form = tview.NewForm()
|
|
|
|
)
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
modal := cardModal{
|
2024-01-10 11:20:15 +00:00
|
|
|
Form: form,
|
|
|
|
frame: tview.NewFrame(form),
|
|
|
|
title: "",
|
|
|
|
description: "",
|
|
|
|
done: nil,
|
2024-01-10 18:21:14 +00:00
|
|
|
mode: create,
|
2024-01-10 11:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
modal.AddButton("Save", func() {
|
2024-01-10 11:20:15 +00:00
|
|
|
if modal.done != nil {
|
2024-01-10 18:21:14 +00:00
|
|
|
modal.done(modal.title, modal.description, true, modal.mode)
|
2024-01-10 11:20:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
modal.AddButton("Cancel", func() {
|
|
|
|
if modal.done != nil {
|
2024-01-10 18:21:14 +00:00
|
|
|
modal.done(modal.title, modal.description, false, modal.mode)
|
2024-01-10 11:20:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return &modal
|
|
|
|
}
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
func (m *cardModal) updateInputFields(title, description string) {
|
2024-01-10 11:20:15 +00:00
|
|
|
m.Clear(false)
|
2024-01-10 18:21:14 +00:00
|
|
|
m.AddInputField("Title", title, 60, nil, func(text string) {
|
2024-01-10 11:20:15 +00:00
|
|
|
m.title = text
|
|
|
|
})
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
m.AddTextArea("Description", description, 60, 10, 0, func(text string) {
|
2024-01-10 11:20:15 +00:00
|
|
|
m.description = text
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
func (m *cardModal) setDoneFunc(handler func(string, string, bool, cardModalMode)) *cardModal {
|
2024-01-10 11:20:15 +00:00
|
|
|
m.done = handler
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2024-01-10 18:21:14 +00:00
|
|
|
func (m *cardModal) Draw(screen tcell.Screen) {
|
2024-01-10 11:20:15 +00:00
|
|
|
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)
|
|
|
|
}
|