chanding the architecure, removing the views ability to emit or listen to events
I liked my idea better but I recon I will hit less obstacles this way
This commit is contained in:
@@ -2,16 +2,12 @@ package views
|
||||
|
||||
import (
|
||||
"esdi/tui/internal/dom"
|
||||
"esdi/tui/internal/events"
|
||||
"esdi/tui/internal/ui"
|
||||
"slices"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func AddAndShowPage(
|
||||
bus *events.Bus,
|
||||
doc *dom.DOM,
|
||||
pages *tview.Pages,
|
||||
page *dom.UINode,
|
||||
changeFocus bool,
|
||||
@@ -23,11 +19,6 @@ func AddAndShowPage(
|
||||
pages.SwitchToPage(page.ID)
|
||||
}
|
||||
|
||||
// Set focus to our new tool
|
||||
if changeFocus {
|
||||
bus.Emit(ui.ChangeFocusEv{Target: page.Self})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
layoutToolFlexID = "layout-tool-flex"
|
||||
layoutToolTreeID = "layout-tool-tree"
|
||||
layoutToolActionPagesID = "layout-tool-action-pages"
|
||||
LayoutToolFlexID = "layout-tool-flex"
|
||||
LayoutToolTreeID = "layout-tool-tree"
|
||||
LayoutToolActionPagesID = "layout-tool-action-pages"
|
||||
)
|
||||
|
||||
type windowReference struct {
|
||||
@@ -155,7 +155,7 @@ func layoutToolTreeViewEvCapture(bus *events.Bus, doc *dom.DOM,
|
||||
return func(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch event.Key() {
|
||||
case tcell.KeyEsc:
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(deviceAPIListID)})
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(DeviceAPIListID)})
|
||||
}
|
||||
|
||||
switch event.Rune() {
|
||||
@@ -198,34 +198,12 @@ func layoutToolTreeViewEvCapture(bus *events.Bus, doc *dom.DOM,
|
||||
}
|
||||
}
|
||||
|
||||
func layoutToolUIOnSelect(bus *events.Bus, doc *dom.DOM) {
|
||||
bus.Emit(ui.LogEv{Log: "Opening layout tool UI\n"})
|
||||
var err error
|
||||
|
||||
// Get the api pages
|
||||
apiToolPages := doc.GetElemByID(apiToolPagesID).(*tview.Pages)
|
||||
if apiToolPages == nil {
|
||||
bus.Emit(ui.LogEv{Log: " Failed to retrive apiToolPages UI\n"})
|
||||
return
|
||||
}
|
||||
|
||||
bus.Emit(ui.LogEv{
|
||||
Log: fmt.Sprintf(" Checking if `%s` UINode already exists\n", layoutToolFlexID),
|
||||
})
|
||||
layoutToolUINode := doc.GetNodeByID(layoutToolFlexID)
|
||||
if layoutToolUINode == nil {
|
||||
layoutToolUINode, err = buildLayoutFlexComponent(bus, doc)
|
||||
if err != nil {
|
||||
bus.Emit(ui.LogEv{
|
||||
Log: fmt.Sprintf(" Failed to build layout tool UI: %s\n", err.Error()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
AddAndShowPage(bus, doc, apiToolPages, layoutToolUINode, true)
|
||||
}
|
||||
|
||||
func layoutToolTreeViewOnChange(bus *events.Bus, doc *dom.DOM) func(node *tview.TreeNode) {
|
||||
// Set focus to our new tool
|
||||
// if changeFocus {
|
||||
// bus.Emit(ui.ChangeFocusEv{Target: page.Self})
|
||||
// }
|
||||
|
||||
return func(node *tview.TreeNode) {
|
||||
// Here we want to change the current existing form on the action pages
|
||||
nodeRef := node.GetReference()
|
||||
@@ -240,7 +218,7 @@ func layoutToolTreeViewOnChange(bus *events.Bus, doc *dom.DOM) func(node *tview.
|
||||
Log: fmt.Sprintf("changing to -> %d | %s\n", nodeWinRef.ID, nodeWinRef.Form.ID),
|
||||
})
|
||||
|
||||
pages := doc.GetElemByID(layoutToolActionPagesID)
|
||||
pages := doc.GetElemByID(LayoutToolActionPagesID)
|
||||
AddAndShowPage(bus, doc, pages.(*tview.Pages), nodeWinRef.Form, false)
|
||||
}
|
||||
}
|
||||
@@ -270,8 +248,8 @@ func buildLayoutTreeComponent(bus *events.Bus, doc *dom.DOM) (*dom.UINode, error
|
||||
layoutTree.SetChangedFunc(layoutToolTreeViewOnChange(bus, doc))
|
||||
layoutTree.SetSelectedFunc(layoutToolTreeViewOnSelect(bus))
|
||||
|
||||
layoutTreeUINode, err := doc.NewUINode(layoutToolTreeID,
|
||||
doc.GetElemByID(rightFlexID), layoutTree)
|
||||
layoutTreeUINode, err := doc.NewUINode(LayoutToolTreeID,
|
||||
doc.GetElemByID(RightFlexID), layoutTree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -296,9 +274,9 @@ func buildLayoutActionPagesComponent(bus *events.Bus, doc *dom.DOM) (*dom.UINode
|
||||
emptyPage.SetBorder(true).SetTitle("-- Tool Area --")
|
||||
fmt.Fprintf(emptyPage, "No tool selected")
|
||||
|
||||
actionPages := tview.NewPages().AddPage(emptyPageName, emptyPage, true, true)
|
||||
actionPages := tview.NewPages().AddPage(EmptyPageName, emptyPage, true, true)
|
||||
|
||||
actionPagesUINode, err := doc.NewUINode(layoutToolActionPagesID,
|
||||
actionPagesUINode, err := doc.NewUINode(LayoutToolActionPagesID,
|
||||
doc.GetElemByID("right-flex"), actionPages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -321,8 +299,8 @@ func buildLayoutFlexComponent(bus *events.Bus, doc *dom.DOM) (*dom.UINode, error
|
||||
}
|
||||
|
||||
layoutToolFlex := tview.NewFlex().SetDirection(tview.FlexRow)
|
||||
layoutToolFlexNode, err := doc.NewUINode(layoutToolFlexID,
|
||||
doc.GetElemByID(apiToolPagesID), layoutToolFlex)
|
||||
layoutToolFlexNode, err := doc.NewUINode(LayoutToolFlexID,
|
||||
doc.GetElemByID(APIToolPagesID), layoutToolFlex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ func createNewWindowForm(bus *events.Bus, doc *dom.DOM) {
|
||||
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||
switch ev.Key() {
|
||||
case tcell.KeyEscape:
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(layoutToolFlexID)})
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(LayoutToolFlexID)})
|
||||
}
|
||||
|
||||
return ev
|
||||
@@ -138,13 +138,13 @@ func createNewWindowForm(bus *events.Bus, doc *dom.DOM) {
|
||||
formNode = doc.GetNodeByID("new-window-form")
|
||||
if formNode == nil {
|
||||
formNode, err = doc.NewUINode("new-window-form",
|
||||
doc.GetElemByID(layoutToolActionPagesID), form)
|
||||
doc.GetElemByID(LayoutToolActionPagesID), form)
|
||||
if err != nil {
|
||||
panic("failed to create UI node for the new window form: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
AddAndShowPage(bus, doc, doc.GetElemByID(layoutToolActionPagesID).(*tview.Pages),
|
||||
AddAndShowPage(bus, doc, doc.GetElemByID(LayoutToolActionPagesID).(*tview.Pages),
|
||||
formNode, true)
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ func windowInfoForm(bus *events.Bus, doc *dom.DOM, idx int16,
|
||||
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||
switch ev.Key() {
|
||||
case tcell.KeyEscape:
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(layoutToolTreeID)})
|
||||
bus.Emit(ui.ChangeFocusEv{Target: doc.GetElemByID(LayoutToolTreeID)})
|
||||
}
|
||||
|
||||
return ev
|
||||
@@ -264,7 +264,7 @@ func windowInfoForm(bus *events.Bus, doc *dom.DOM, idx int16,
|
||||
var formNode *dom.UINode
|
||||
elemID := windowInfoPageID(idx)
|
||||
formNode = doc.GetNodeByID(elemID)
|
||||
layoutToolActionPagesElem := doc.GetElemByID(layoutToolActionPagesID)
|
||||
layoutToolActionPagesElem := doc.GetElemByID(LayoutToolActionPagesID)
|
||||
if formNode == nil {
|
||||
formNode, err = doc.NewUINode(elemID, layoutToolActionPagesElem, form)
|
||||
if err != nil {
|
||||
|
||||
@@ -104,7 +104,7 @@ func windowManipulationEvCapture(
|
||||
switch ev.Key() {
|
||||
case tcell.KeyEscape:
|
||||
bus.Emit(ui.ChangeFocusEv{
|
||||
Target: doc.GetElemByID(layoutToolFlexID),
|
||||
Target: doc.GetElemByID(LayoutToolFlexID),
|
||||
})
|
||||
return ev
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func windowManipulationTool(bus *events.Bus, doc *dom.DOM, idx int16) {
|
||||
var boxNode *dom.UINode
|
||||
|
||||
// delete the currently existing move-window-box
|
||||
actionPages := doc.GetElemByID(layoutToolActionPagesID).(*tview.Pages)
|
||||
actionPages := doc.GetElemByID(LayoutToolActionPagesID).(*tview.Pages)
|
||||
actionPages.RemovePage("move-window-box")
|
||||
|
||||
boxNode = doc.GetNodeByID("move-window-box")
|
||||
@@ -154,7 +154,7 @@ func windowManipulationTool(bus *events.Bus, doc *dom.DOM, idx int16) {
|
||||
|
||||
boxNode, err = doc.NewUINode(
|
||||
"move-window-box",
|
||||
doc.GetElemByID(layoutToolActionPagesID),
|
||||
doc.GetElemByID(LayoutToolActionPagesID),
|
||||
box,
|
||||
)
|
||||
|
||||
|
||||
@@ -5,113 +5,111 @@ import (
|
||||
"fmt"
|
||||
|
||||
"esdi/tui/internal/dom"
|
||||
"esdi/tui/internal/events"
|
||||
"esdi/tui/internal/ui"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
const (
|
||||
mainFlexID = "main-flex"
|
||||
apiToolPagesID = "api-tool-pages"
|
||||
rightFlexID = "right-flex"
|
||||
outputPaneID = "output-window"
|
||||
deviceAPIListID = "device-api-list"
|
||||
MainFlexID = "main-flex"
|
||||
APIToolPagesID = "api-tool-pages"
|
||||
RightFlexID = "right-flex"
|
||||
OutputPaneID = "output-window"
|
||||
DeviceAPIListID = "device-api-list"
|
||||
)
|
||||
|
||||
const (
|
||||
emptyPageName = "empty-page"
|
||||
EmptyPageName = "empty-page"
|
||||
)
|
||||
|
||||
func buildRightSidePages(bus *events.Bus, doc *dom.DOM) (*dom.UINode, error) {
|
||||
type DeviceAPIToolView struct {
|
||||
Pages *tview.Pages
|
||||
ChangedFn func()
|
||||
}
|
||||
|
||||
func NewDeviceAPIToolView() *DeviceAPIToolView {
|
||||
// We need to build a set of pages with an empty page
|
||||
emptyPage := tview.NewTextView().
|
||||
SetTextAlign(tview.AlignCenter).
|
||||
SetChangedFunc(func() {
|
||||
bus.Emit(ui.RedrawEv{})
|
||||
// bus.Emit(ui.RedrawEv{})
|
||||
})
|
||||
emptyPage.SetBorder(true).SetTitle("-- Tool Area --")
|
||||
fmt.Fprintf(emptyPage, "No Tool Selected")
|
||||
|
||||
apiToolPages := tview.NewPages().AddPage(emptyPageName, emptyPage, true, true)
|
||||
apiToolPages := tview.NewPages().AddPage(EmptyPageName, emptyPage, true, true)
|
||||
|
||||
apiToolPagesNode, err := doc.NewUINode(apiToolPagesID, doc.GetElemByID(rightFlexID),
|
||||
apiToolPages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return &DeviceAPIToolView{
|
||||
Pages: apiToolPages,
|
||||
ChangedFn: func() {}, // blank function to be hooked on
|
||||
}
|
||||
|
||||
return apiToolPagesNode, nil
|
||||
}
|
||||
|
||||
func buildRightSideFlex(bus *events.Bus, doc *dom.DOM) (*dom.UINode, error) {
|
||||
apiToolPagesNode, err := buildRightSidePages(bus, doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// This will be the right side flex
|
||||
flex := tview.NewFlex().SetDirection(tview.FlexRow)
|
||||
flexNode, err := doc.NewUINode(rightFlexID, doc.GetElemByID(mainFlexID), flex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// outputWin := doc.GetElemByID(outputPaneID)
|
||||
// if outputWin == nil {
|
||||
// panic("failed to attach output win to UI")
|
||||
// }
|
||||
|
||||
flex.
|
||||
AddItem(apiToolPagesNode.Self, 0, 5, false)
|
||||
// AddItem(outputWin, 0, 2, false)
|
||||
|
||||
return flexNode, nil
|
||||
type DeviceAPIListView struct {
|
||||
List *tview.List
|
||||
InputCapture func(*tcell.EventKey) *tcell.EventKey
|
||||
ItemOnSelect func()
|
||||
}
|
||||
|
||||
func BuildMainFlex(bus *events.Bus, doc *dom.DOM) (*tview.Flex, error) {
|
||||
func NewDeviceAPIListView() *DeviceAPIListView {
|
||||
deviceAPIList := tview.NewList().
|
||||
AddItem("layout", "build a layout for CDashDisplay", 0, func() {
|
||||
// layoutToolUIOnSelect(bus, doc)
|
||||
})
|
||||
deviceAPIList.SetBorder(true).SetTitle("list")
|
||||
|
||||
return &DeviceAPIListView{
|
||||
List: deviceAPIList,
|
||||
InputCapture: func(ev *tcell.EventKey) *tcell.EventKey { return ev },
|
||||
ItemOnSelect: func() {}, // blank functions, they have to be hooked on by the controller
|
||||
}
|
||||
}
|
||||
|
||||
type DeviceAPIView struct {
|
||||
MainFlex *tview.Flex
|
||||
DevAPIList *DeviceAPIListView
|
||||
DevAPIToolView *DeviceAPIToolView
|
||||
OutputWindow *OutputWinView
|
||||
}
|
||||
|
||||
func (dl *DeviceAPIListView) AddItem(name, description string, onSelect func()) {
|
||||
dl.List.AddItem(name, description, 0, onSelect)
|
||||
}
|
||||
|
||||
func NewDeviceAPIView(doc *dom.DOM) (*DeviceAPIView, error) {
|
||||
// To build the main view we must set the DOM root
|
||||
mainFlex := tview.NewFlex().SetDirection(tview.FlexColumn)
|
||||
mainFlexUINode, err := doc.NewUINode(mainFlexID, nil, mainFlex)
|
||||
mainFlexUINode, err := doc.NewUINode(MainFlexID, nil, mainFlex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
doc.SetRoot(mainFlexUINode)
|
||||
|
||||
deviceAPIList := tview.NewList().
|
||||
AddItem("layout", "build a layout for CDashDisplay", 0, func() {
|
||||
layoutToolUIOnSelect(bus, doc)
|
||||
})
|
||||
deviceAPIList.SetBorder(true).SetTitle("list").
|
||||
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||
switch ev.Rune() {
|
||||
case 'r':
|
||||
bus.Emit(ui.FindCDashDisplay{})
|
||||
}
|
||||
return ev
|
||||
})
|
||||
|
||||
apiListWindowUINode, err := doc.NewUINode(deviceAPIListID, doc.GetRootElem(),
|
||||
deviceAPIList)
|
||||
deviceAPIList := NewDeviceAPIListView()
|
||||
apiListWindowUINode, err := doc.NewUINode(DeviceAPIListID, doc.GetRootElem(),
|
||||
deviceAPIList.List)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rightSideFlex, err := buildRightSideFlex(bus, doc)
|
||||
apiToolPages := NewDeviceAPIToolView()
|
||||
apiToolPagesNode, err := doc.NewUINode(APIToolPagesID, doc.GetElemByID(RightFlexID),
|
||||
apiToolPages.Pages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mainFlex.
|
||||
AddItem(apiListWindowUINode.Self, 0, 1, false).
|
||||
AddItem(rightSideFlex.Self, 0, 4, false)
|
||||
AddItem(apiToolPagesNode.Self, 0, 4, false)
|
||||
|
||||
// Output window
|
||||
err = BuildOutputWindow(bus, doc)
|
||||
|
||||
outputWin := NewOutputWinView()
|
||||
_, err = doc.NewUINode("output-window", nil, outputWin.TextArea)
|
||||
if err != nil {
|
||||
panic("failed to create output window")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Flex with debug window
|
||||
@@ -119,8 +117,13 @@ func BuildMainFlex(bus *events.Bus, doc *dom.DOM) (*tview.Flex, error) {
|
||||
flexWithOutputWindow := tview.NewFlex().SetDirection(tview.FlexRow)
|
||||
flexWithOutputWindow.
|
||||
AddItem(mainFlex, 0, 5, true).
|
||||
AddItem(doc.GetElemByID(outputPaneID), 0, 2, false)
|
||||
AddItem(doc.GetElemByID(OutputPaneID), 0, 2, false)
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
return flexWithOutputWindow, nil
|
||||
return &DeviceAPIView{
|
||||
MainFlex: flexWithOutputWindow,
|
||||
DevAPIList: deviceAPIList,
|
||||
DevAPIToolView: apiToolPages,
|
||||
OutputWindow: outputWin,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1,38 +1,32 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"esdi/tui/internal/dom"
|
||||
"esdi/tui/internal/events"
|
||||
"esdi/tui/internal/ui"
|
||||
"fmt"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func bindOutputWindowEvents(
|
||||
bus *events.Bus,
|
||||
output *tview.TextView,
|
||||
) {
|
||||
bus.On(ui.PrintLogEv{}, func(e any) {
|
||||
le, _ := e.(ui.PrintLogEv)
|
||||
fmt.Fprintf(output, le.Log)
|
||||
})
|
||||
// func bindOutputWindowEvents(
|
||||
// bus *events.Bus,
|
||||
// output *tview.TextView,
|
||||
// ) {
|
||||
// bus.On(ui.PrintLogEv{}, func(e any) {
|
||||
// le, _ := e.(ui.PrintLogEv)
|
||||
// fmt.Fprintf(output, le.Log)
|
||||
// })
|
||||
// }
|
||||
|
||||
type OutputWinView struct {
|
||||
TextArea *tview.TextView
|
||||
}
|
||||
|
||||
func BuildOutputWindow(bus *events.Bus, doc *dom.DOM) error {
|
||||
func NewOutputWinView() *OutputWinView {
|
||||
var outputWin *tview.TextView
|
||||
outputWin = tview.NewTextView().SetChangedFunc(func() {
|
||||
bus.Emit(ui.RedrawEv{})
|
||||
outputWin.ScrollToEnd()
|
||||
})
|
||||
|
||||
outputWin.SetBorder(true).SetTitle("DebugWindow")
|
||||
_, err := doc.NewUINode("output-window", nil, outputWin)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
return &OutputWinView{
|
||||
TextArea: outputWin,
|
||||
}
|
||||
|
||||
bindOutputWindowEvents(bus, outputWin)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func streamingWindow(bus *events.Bus, doc *dom.DOM) {
|
||||
var boxNode *dom.UINode
|
||||
|
||||
// delete the currently existing streaming-box
|
||||
actionPages := doc.GetElemByID(layoutToolActionPagesID).(*tview.Pages)
|
||||
actionPages := doc.GetElemByID(LayoutToolActionPagesID).(*tview.Pages)
|
||||
actionPages.RemovePage(streamingBoxID)
|
||||
|
||||
// Get the currently exisiting streaming box in the dom so we can delete it
|
||||
@@ -54,7 +54,7 @@ func streamingWindow(bus *events.Bus, doc *dom.DOM) {
|
||||
// Register this streaming box as an UINode
|
||||
boxNode, err = doc.NewUINode(
|
||||
streamingBoxID,
|
||||
doc.GetElemByID(layoutToolActionPagesID),
|
||||
doc.GetElemByID(LayoutToolActionPagesID),
|
||||
box,
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user