doesnt look as cool, but for sure feels simpler
This commit is contained in:
@@ -2,274 +2,165 @@ package views
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"esdi/cdashdisplay"
|
||||
"esdi/tui/internal/dom"
|
||||
"esdi/tui/internal/events"
|
||||
"esdi/tui/internal/models"
|
||||
"esdi/tui/internal/ui"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
// Validate the form inputs
|
||||
func validateFormInputs(x, y, w, h,
|
||||
title, prev, titleSize, textSize string,
|
||||
showID bool) (models.Window, error) {
|
||||
xValue, err := strconv.ParseUint(x, 10, 64)
|
||||
if err != nil {
|
||||
return models.Window{}, err
|
||||
}
|
||||
yValue, err := strconv.ParseUint(y, 10, 64)
|
||||
if err != nil {
|
||||
return models.Window{}, err
|
||||
}
|
||||
widthValue, err := strconv.ParseUint(w, 10, 64)
|
||||
if err != nil {
|
||||
return models.Window{}, err
|
||||
}
|
||||
heightValue, err := strconv.ParseUint(h, 10, 64)
|
||||
if err != nil {
|
||||
return models.Window{}, err
|
||||
}
|
||||
titleSizeValue, err := strconv.ParseUint(titleSize, 10, 64)
|
||||
if err != nil {
|
||||
return models.Window{}, err
|
||||
}
|
||||
textSizeValue, err := strconv.ParseUint(textSize, 10, 64)
|
||||
if err != nil {
|
||||
return models.Window{}, err
|
||||
}
|
||||
|
||||
showIDValue := cdashdisplay.ShowIDFalse
|
||||
if showID {
|
||||
showIDValue = cdashdisplay.ShowIDTrue
|
||||
}
|
||||
|
||||
return models.Window{
|
||||
X: uint16(xValue),
|
||||
Y: uint16(yValue),
|
||||
Width: uint16(widthValue),
|
||||
Height: uint16(heightValue),
|
||||
ShowID: showIDValue,
|
||||
TitleSize: uint8(titleSizeValue),
|
||||
TextSize: uint8(textSizeValue),
|
||||
PreviewValue: prev,
|
||||
Title: title,
|
||||
}, nil
|
||||
type CDashDisplayWindowFormView struct {
|
||||
Form *tview.Form
|
||||
X *tview.InputField
|
||||
Y *tview.InputField
|
||||
Width *tview.InputField
|
||||
Height *tview.InputField
|
||||
Title *tview.InputField
|
||||
PreviewValue *tview.InputField
|
||||
ShowID *tview.Checkbox
|
||||
WinType *tview.DropDown
|
||||
TitleSize *tview.DropDown
|
||||
TextSize *tview.DropDown
|
||||
}
|
||||
|
||||
func createNewWindowForm(bus *events.Bus, doc *dom.DOM) {
|
||||
var err error
|
||||
func NewCDashDisplayWindowFormView() *CDashDisplayWindowFormView {
|
||||
view := &CDashDisplayWindowFormView{}
|
||||
|
||||
x0 := tview.NewInputField().SetLabel("x")
|
||||
y0 := tview.NewInputField().SetLabel("y")
|
||||
width := tview.NewInputField().SetLabel("width")
|
||||
height := tview.NewInputField().SetLabel("height")
|
||||
title := tview.NewInputField().SetLabel("title")
|
||||
previewValue := tview.NewInputField().SetLabel("preview value")
|
||||
showID := tview.NewCheckbox().SetLabel("show ID").SetChecked(true)
|
||||
winType := tview.NewDropDown().SetLabel("type").
|
||||
SetOptions([]string{"string", "bar"}, func(s string, id int) {
|
||||
// Here we have to add extra options for the configuration of default
|
||||
// values and so on
|
||||
}).
|
||||
SetCurrentOption(0)
|
||||
titleSize := tview.NewDropDown().SetLabel("Title Size").
|
||||
SetOptions([]string{"1", "2", "3", "4", "5", "6", "7", "8"}, func(s string, id int) {
|
||||
}).
|
||||
SetCurrentOption(0)
|
||||
textSize := tview.NewDropDown().SetLabel("Text Size").
|
||||
SetOptions([]string{"1", "2", "3", "4", "5", "6", "7", "8"}, func(s string, id int) {
|
||||
}).
|
||||
view.X = tview.NewInputField().SetLabel("x")
|
||||
view.Y = tview.NewInputField().SetLabel("y")
|
||||
view.Width = tview.NewInputField().SetLabel("width")
|
||||
view.Height = tview.NewInputField().SetLabel("height")
|
||||
view.Title = tview.NewInputField().SetLabel("title")
|
||||
view.PreviewValue = tview.NewInputField().SetLabel("preview value")
|
||||
view.ShowID = tview.NewCheckbox().SetLabel("show ID").SetChecked(true)
|
||||
view.WinType = tview.NewDropDown().SetLabel("type").
|
||||
SetOptions([]string{"string", "bar"}, func(s string, id int) {}).
|
||||
SetCurrentOption(0)
|
||||
view.TitleSize = tview.NewDropDown().SetLabel("Title Size").SetCurrentOption(0)
|
||||
for k := range 20 {
|
||||
view.TitleSize.AddOption(fmt.Sprintf("%d", k+1), blankDropdownOptionCallback)
|
||||
}
|
||||
view.TitleSize.SetCurrentOption(0)
|
||||
|
||||
form := tview.NewForm().
|
||||
AddFormItem(x0).
|
||||
AddFormItem(y0).
|
||||
AddFormItem(width).
|
||||
AddFormItem(height).
|
||||
AddFormItem(title).
|
||||
AddFormItem(previewValue).
|
||||
AddFormItem(showID).
|
||||
AddFormItem(winType).
|
||||
AddFormItem(titleSize).
|
||||
AddFormItem(textSize).
|
||||
AddButton("Create", func() {
|
||||
_, titleSizeTxt := titleSize.GetCurrentOption()
|
||||
_, textSizeTxt := textSize.GetCurrentOption()
|
||||
// Validate the inputs
|
||||
window, err := validateFormInputs(
|
||||
x0.GetText(),
|
||||
y0.GetText(),
|
||||
width.GetText(),
|
||||
height.GetText(),
|
||||
title.GetText(),
|
||||
previewValue.GetText(),
|
||||
titleSizeTxt,
|
||||
textSizeTxt,
|
||||
showID.IsChecked(),
|
||||
)
|
||||
view.TextSize = tview.NewDropDown().SetLabel("Text Size")
|
||||
for k := range 20 {
|
||||
view.TextSize.AddOption(fmt.Sprintf("%d", k+1), blankDropdownOptionCallback)
|
||||
}
|
||||
view.TitleSize.SetCurrentOption(0)
|
||||
|
||||
if err != nil {
|
||||
bus.Emit(ui.LogEv{Log: fmt.Sprintf("failed to parse form: %s\n", err.Error())})
|
||||
return
|
||||
}
|
||||
view.Form = tview.NewForm().
|
||||
AddFormItem(view.X).
|
||||
AddFormItem(view.Y).
|
||||
AddFormItem(view.Width).
|
||||
AddFormItem(view.Height).
|
||||
AddFormItem(view.Title).
|
||||
AddFormItem(view.PreviewValue).
|
||||
AddFormItem(view.ShowID).
|
||||
AddFormItem(view.WinType).
|
||||
AddFormItem(view.TitleSize).
|
||||
AddFormItem(view.TextSize)
|
||||
|
||||
bus.Emit(ui.LogEv{Log: fmt.Sprintf("showID: %d\n", window.ShowID)})
|
||||
bus.Emit(ui.CreateWindowEv{Window: window})
|
||||
})
|
||||
view.Form.SetBorder(true).
|
||||
SetTitle("New Window Form").
|
||||
SetTitleAlign(tview.AlignLeft)
|
||||
// Need to inject the event capture later
|
||||
|
||||
form.SetBorder(true).
|
||||
SetTitle("new window form").
|
||||
SetTitleAlign(tview.AlignLeft).
|
||||
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||
switch ev.Key() {
|
||||
case tcell.KeyEscape:
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(LayoutToolFlexID)})
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
return ev
|
||||
})
|
||||
type CreateWindowFormView struct {
|
||||
Form *CDashDisplayWindowFormView
|
||||
CreateBtn *tview.Button
|
||||
}
|
||||
|
||||
var formNode *dom.UINode
|
||||
formNode = doc.GetNodeByID("new-window-form")
|
||||
if formNode == nil {
|
||||
formNode, err = doc.NewUINode("new-window-form",
|
||||
doc.GetElemByID(LayoutToolActionPagesID), form)
|
||||
if err != nil {
|
||||
panic("failed to create UI node for the new window form: " + err.Error())
|
||||
}
|
||||
func NewCreateWindowFormView() *CreateWindowFormView {
|
||||
view := &CreateWindowFormView{
|
||||
Form: NewCDashDisplayWindowFormView(),
|
||||
}
|
||||
|
||||
AddAndShowPage(doc.GetElemByID(LayoutToolActionPagesID).(*tview.Pages), formNode, true)
|
||||
view.CreateBtn = tview.NewButton("Create")
|
||||
// Need to inject the button functionality later
|
||||
|
||||
view.Form.Form.AddButton("Create", func() {})
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
func windowInfoPageID(idx int16) string {
|
||||
return fmt.Sprintf("layout-tool-win-info-%d", idx)
|
||||
type WindowFormView struct {
|
||||
Form *CDashDisplayWindowFormView
|
||||
UpdateBtn *tview.Button
|
||||
}
|
||||
|
||||
func windowInfoForm(bus *events.Bus, doc *dom.DOM, idx int16,
|
||||
win *cdashdisplay.UIWindow) *dom.UINode {
|
||||
var err error
|
||||
func NewWindowFormView(win *cdashdisplay.UIWindow) *WindowFormView {
|
||||
view := &WindowFormView{
|
||||
Form: NewCDashDisplayWindowFormView(),
|
||||
}
|
||||
|
||||
x0 := tview.NewInputField().SetLabel("x").
|
||||
SetText(fmt.Sprintf("%d", win.Dims.X0))
|
||||
y0 := tview.NewInputField().SetLabel("y").
|
||||
SetText(fmt.Sprintf("%d", win.Dims.Y0))
|
||||
width := tview.NewInputField().SetLabel("width").
|
||||
SetText(fmt.Sprintf("%d", win.Dims.Width))
|
||||
height := tview.NewInputField().SetLabel("height").
|
||||
SetText(fmt.Sprintf("%d", win.Dims.Height))
|
||||
title := tview.NewInputField().SetLabel("title").
|
||||
SetText(win.Title.String())
|
||||
previewValue := tview.NewInputField().SetLabel("preview value").
|
||||
SetText(win.Opts.PreviewValue.String())
|
||||
showID := tview.NewCheckbox().SetLabel("show ID").SetChecked(true)
|
||||
winType := tview.NewDropDown().SetLabel("type").
|
||||
SetOptions([]string{"string", "bar"}, func(s string, id int) {
|
||||
// Here we have to add extra options for the configuration of default
|
||||
// values and so on
|
||||
}).
|
||||
SetCurrentOption(0)
|
||||
// NOTE: put this thing into a loop that generate the numbers or something yo
|
||||
titleSize := tview.NewDropDown().SetLabel("Title Size").
|
||||
SetOptions([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
|
||||
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"},
|
||||
func(s string, id int) {
|
||||
}).
|
||||
SetCurrentOption(int(win.Decor.TitleSize))
|
||||
textSize := tview.NewDropDown().SetLabel("Text Size").
|
||||
SetOptions([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
|
||||
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"},
|
||||
func(s string, id int) {
|
||||
}).
|
||||
SetCurrentOption(int(win.Decor.TextSize))
|
||||
|
||||
form := tview.NewForm().
|
||||
AddFormItem(x0).
|
||||
AddFormItem(y0).
|
||||
AddFormItem(width).
|
||||
AddFormItem(height).
|
||||
AddFormItem(title).
|
||||
AddFormItem(previewValue).
|
||||
AddFormItem(showID).
|
||||
AddFormItem(titleSize).
|
||||
AddFormItem(textSize).
|
||||
AddFormItem(winType).
|
||||
AddButton("Update", func() {
|
||||
bus.Emit(ui.LogEv{Log: "update event was created\n"})
|
||||
|
||||
_, titleSizeTxt := titleSize.GetCurrentOption()
|
||||
_, textSizeTxt := textSize.GetCurrentOption()
|
||||
// Validate the inputs
|
||||
window, err := validateFormInputs(
|
||||
x0.GetText(),
|
||||
y0.GetText(),
|
||||
width.GetText(),
|
||||
height.GetText(),
|
||||
title.GetText(),
|
||||
previewValue.GetText(),
|
||||
titleSizeTxt,
|
||||
textSizeTxt,
|
||||
showID.IsChecked(),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
bus.Emit(ui.LogEv{Log: fmt.Sprintf("failed to parse form: %s\n", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
bus.Emit(ui.LogEv{Log: fmt.Sprintf("showID: %d\n", window.ShowID)})
|
||||
bus.Emit(ui.LogEv{Log: "sending update window ev\n"})
|
||||
bus.Emit(ui.UpdateWindowEv{ID: idx, Window: window})
|
||||
})
|
||||
|
||||
form.SetBorder(true).
|
||||
SetTitle(fmt.Sprintf("Info - %s [%2d]", win.Title.String(), idx)).
|
||||
SetTitleAlign(tview.AlignLeft).
|
||||
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||
switch ev.Key() {
|
||||
case tcell.KeyEscape:
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(LayoutToolTreeID)})
|
||||
}
|
||||
|
||||
return ev
|
||||
})
|
||||
view.UpdateBtn = tview.NewButton("Update")
|
||||
// Need to inject the button functionality later
|
||||
// AddButton("Update", func() {
|
||||
// bus.Emit(ui.LogEv{Log: "update event was created\n"})
|
||||
//
|
||||
// _, titleSizeTxt := titleSize.GetCurrentOption()
|
||||
// _, textSizeTxt := textSize.GetCurrentOption()
|
||||
// // Validate the inputs
|
||||
// window, err := validateFormInputs(
|
||||
// x0.GetText(),
|
||||
// y0.GetText(),
|
||||
// width.GetText(),
|
||||
// height.GetText(),
|
||||
// title.GetText(),
|
||||
// previewValue.GetText(),
|
||||
// titleSizeTxt,
|
||||
// textSizeTxt,
|
||||
// showID.IsChecked(),
|
||||
// )
|
||||
//
|
||||
// if err != nil {
|
||||
// bus.Emit(ui.LogEv{Log: fmt.Sprintf("failed to parse form: %s\n", err.Error())})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// bus.Emit(ui.LogEv{Log: fmt.Sprintf("showID: %d\n", window.ShowID)})
|
||||
// bus.Emit(ui.LogEv{Log: "sending update window ev\n"})
|
||||
// bus.Emit(ui.UpdateWindowEv{ID: idx, Window: window})
|
||||
// })
|
||||
|
||||
// In case the window is moved we need to update this form
|
||||
bus.On(ui.WindowMovedEv{}, func(e any) {
|
||||
data := e.(ui.WindowMovedEv)
|
||||
// Only update the fields if the event is for this thing
|
||||
if data.ID != idx {
|
||||
return
|
||||
}
|
||||
// bus.On(ui.WindowMovedEv{}, func(e any) {
|
||||
// data := e.(ui.WindowMovedEv)
|
||||
// // Only update the fields if the event is for this thing
|
||||
// if data.ID != idx {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // Get the current window-info-page
|
||||
// elem := doc.GetElemByID(windowInfoPageID(data.ID))
|
||||
// if elem == nil {
|
||||
// bus.Emit(ui.LogEv{Log: "could't get hold of info page"})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// x0.SetText(fmt.Sprintf("%d", data.Dims.X0))
|
||||
// x0.SetText(fmt.Sprintf("%d", data.Dims.Y0))
|
||||
// width.SetText(fmt.Sprintf("%d", data.Dims.Width))
|
||||
// height.SetText(fmt.Sprintf("%d", data.Dims.Height))
|
||||
// })
|
||||
|
||||
// Get the current window-info-page
|
||||
elem := doc.GetElemByID(windowInfoPageID(data.ID))
|
||||
if elem == nil {
|
||||
bus.Emit(ui.LogEv{Log: "could't get hold of info page"})
|
||||
return
|
||||
}
|
||||
// Set the fields data
|
||||
view.Form.X.SetText(fmt.Sprintf("%d", win.Dims.X0))
|
||||
view.Form.Y.SetText(fmt.Sprintf("%d", win.Dims.Y0))
|
||||
view.Form.Width.SetText(fmt.Sprintf("%d", win.Dims.Width))
|
||||
view.Form.Height.SetText(fmt.Sprintf("%d", win.Dims.Height))
|
||||
view.Form.Title.SetText(win.Title.String())
|
||||
view.Form.PreviewValue.SetText(win.Opts.PreviewValue.String())
|
||||
view.Form.ShowID.SetChecked(win.Opts.ShowID == 1)
|
||||
view.Form.WinType.SetCurrentOption(0) // NOTE: this needs to set the correct option
|
||||
view.Form.TitleSize.SetCurrentOption(int(win.Decor.TitleSize))
|
||||
view.Form.TextSize.SetCurrentOption(int(win.Decor.TextSize))
|
||||
|
||||
x0.SetText(fmt.Sprintf("%d", data.Dims.X0))
|
||||
x0.SetText(fmt.Sprintf("%d", data.Dims.Y0))
|
||||
width.SetText(fmt.Sprintf("%d", data.Dims.Width))
|
||||
height.SetText(fmt.Sprintf("%d", data.Dims.Height))
|
||||
})
|
||||
return view
|
||||
}
|
||||
|
||||
var formNode *dom.UINode
|
||||
elemID := windowInfoPageID(idx)
|
||||
formNode = doc.GetNodeByID(elemID)
|
||||
layoutToolActionPagesElem := doc.GetElemByID(LayoutToolActionPagesID)
|
||||
if formNode == nil {
|
||||
formNode, err = doc.NewUINode(elemID, layoutToolActionPagesElem, form)
|
||||
if err != nil {
|
||||
panic("failed to create UI node for the new window form: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return formNode
|
||||
func WindowInfoPageID(idx int16) string {
|
||||
return fmt.Sprintf("layout-tool-win-info-%d", idx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user