|
| 1 | +package ui |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/askovpen/gossiped/pkg/config" |
| 5 | + "github.com/askovpen/gossiped/pkg/nodelist" |
| 6 | + "github.com/gdamore/tcell/v2" |
| 7 | + "github.com/rivo/tview" |
| 8 | +) |
| 9 | + |
| 10 | +// ModalNodeList is a centered message window used to inform the user or prompt them |
| 11 | +type ModalNodeList struct { |
| 12 | + *tview.Box |
| 13 | + table *tview.Table |
| 14 | + frame *tview.Frame |
| 15 | + textColor tcell.Color |
| 16 | + title string |
| 17 | + done func(buttonIndex int) |
| 18 | +} |
| 19 | + |
| 20 | +// NewModalNodeList returns a new modal message window. |
| 21 | +func NewModalNodeList() *ModalNodeList { |
| 22 | + defFg, defBg, _ := config.StyleDefault.Decompose() |
| 23 | + m := &ModalNodeList{ |
| 24 | + Box: tview.NewBox().SetBackgroundColor(defBg), |
| 25 | + textColor: defFg, |
| 26 | + } |
| 27 | + borderFg, _, borderAttr := config.GetElementStyle(config.ColorAreaAreaListModal, config.ColorElementBorder).Decompose() |
| 28 | + headerStyle := config.GetElementStyle(config.ColorAreaAreaListModal, config.ColorElementHeader) |
| 29 | + selectionStyle := config.GetElementStyle(config.ColorAreaAreaListModal, config.ColorElementSelection) |
| 30 | + itemStyle := config.GetElementStyle(config.ColorAreaAreaListModal, config.ColorElementItem) |
| 31 | + fgItem, bgItem, attrItem := itemStyle.Decompose() |
| 32 | + fgHeader, bgHeader, attrHeader := headerStyle.Decompose() |
| 33 | + m.table = tview.NewTable(). |
| 34 | + SetFixed(1, 0). |
| 35 | + SetBordersColor(borderFg). |
| 36 | + SetSelectable(true, false). |
| 37 | + SetSelectedStyle(selectionStyle). |
| 38 | + SetSelectedFunc(func(row int, column int) { |
| 39 | + m.done(row) |
| 40 | + }) |
| 41 | + m.frame = tview.NewFrame(m.table).SetBorders(0, 0, 1, 0, 0, 0) |
| 42 | + m.frame.SetBackgroundColor(defBg) |
| 43 | + m.table.SetBackgroundColor(defBg) |
| 44 | + m.frame.SetBorder(true). |
| 45 | + SetTitleAlign(tview.AlignLeft). |
| 46 | + SetBorderAttributes(borderAttr). |
| 47 | + SetBorderColor(borderFg). |
| 48 | + SetBorderPadding(0, 0, 1, 1) |
| 49 | + m.table.SetCell( |
| 50 | + 0, 0, tview.NewTableCell(" Address"). |
| 51 | + SetTextColor(fgHeader).SetBackgroundColor(bgHeader).SetAttributes(attrHeader). |
| 52 | + SetSelectable(false)) |
| 53 | + m.table.SetCell( |
| 54 | + 0, 1, tview.NewTableCell("Sysop"). |
| 55 | + SetTextColor(fgHeader).SetBackgroundColor(bgHeader).SetAttributes(attrHeader). |
| 56 | + SetSelectable(false)) |
| 57 | + m.table.SetCell( |
| 58 | + 0, 2, tview.NewTableCell("City"). |
| 59 | + SetTextColor(fgHeader).SetBackgroundColor(bgHeader).SetAttributes(attrHeader). |
| 60 | + SetSelectable(false)) |
| 61 | + m.table.SetCell( |
| 62 | + 0, 3, tview.NewTableCell("BBS"). |
| 63 | + SetTextColor(fgHeader).SetBackgroundColor(bgHeader).SetAttributes(attrHeader). |
| 64 | + SetExpansion(1). |
| 65 | + SetSelectable(false)) |
| 66 | + for i, node := range nodelist.Nodelist { |
| 67 | + m.table.SetCell(i+1, 0, tview.NewTableCell(node.Address.String()). |
| 68 | + SetTextColor(fgItem).SetBackgroundColor(bgItem).SetAttributes(attrItem)) |
| 69 | + m.table.SetCell(i+1, 1, tview.NewTableCell(node.Sysop). |
| 70 | + SetTextColor(fgItem).SetBackgroundColor(bgItem).SetAttributes(attrItem)) |
| 71 | + m.table.SetCell(i+1, 2, tview.NewTableCell(node.City). |
| 72 | + SetTextColor(fgItem).SetBackgroundColor(bgItem).SetAttributes(attrItem)) |
| 73 | + m.table.SetCell(i+1, 3, tview.NewTableCell(node.BBS). |
| 74 | + SetTextColor(fgItem).SetBackgroundColor(bgItem).SetAttributes(attrItem)) |
| 75 | + } |
| 76 | + return m |
| 77 | +} |
| 78 | + |
| 79 | +// SetTextColor sets the color of the message text. |
| 80 | +func (m *ModalNodeList) SetTextColor(color tcell.Color) *ModalNodeList { |
| 81 | + m.textColor = color |
| 82 | + return m |
| 83 | +} |
| 84 | + |
| 85 | +// SetDoneFunc sets a handler which is called when one of the buttons was |
| 86 | +// pressed. It receives the index of the button as well as its label text. The |
| 87 | +// handler is also called when the user presses the Escape key. The index will |
| 88 | +// then be negative and the label text an emptry string. |
| 89 | +func (m *ModalNodeList) SetDoneFunc(handler func(buttonIndex int)) *ModalNodeList { |
| 90 | + m.done = handler |
| 91 | + return m |
| 92 | +} |
| 93 | + |
| 94 | +// SetText sets the message text of the window. The text may contain line |
| 95 | +// breaks. Note that words are wrapped, too, based on the final size of the |
| 96 | +// window. |
| 97 | +func (m *ModalNodeList) SetText(text string) *ModalNodeList { |
| 98 | + m.title = text |
| 99 | + style := config.GetElementStyle(config.ColorAreaAreaListModal, config.ColorElementTitle) |
| 100 | + m.frame.SetTitle(config.FormatTextWithStyle(text, style)) |
| 101 | + return m |
| 102 | +} |
| 103 | + |
| 104 | +// AddButtons adds buttons to the window. There must be at least one button and |
| 105 | +// a "done" handler so the window can be closed again. |
| 106 | + |
| 107 | +// Focus is called when this primitive receives focus. |
| 108 | +func (m *ModalNodeList) Focus(delegate func(p tview.Primitive)) { |
| 109 | + //delegate(m.form) |
| 110 | + delegate(m.table) |
| 111 | +} |
| 112 | + |
| 113 | +// HasFocus returns whether or not this primitive has focus. |
| 114 | +func (m *ModalNodeList) HasFocus() bool { |
| 115 | + //return m.form.HasFocus() |
| 116 | + return m.table.HasFocus() |
| 117 | +} |
| 118 | + |
| 119 | +// Draw draws this primitive onto the screen. |
| 120 | +func (m *ModalNodeList) Draw(screen tcell.Screen) { |
| 121 | + width, height := screen.Size() |
| 122 | + height -= 7 |
| 123 | + m.frame.Clear() |
| 124 | + x := 0 |
| 125 | + y := 6 |
| 126 | + m.SetRect(x, y, width, height) |
| 127 | + |
| 128 | + // Draw the frame. |
| 129 | + m.frame.SetRect(x, y, width, height) |
| 130 | + m.frame.Draw(screen) |
| 131 | +} |
| 132 | + |
| 133 | +// InputHandler handle input |
| 134 | +func (m *ModalNodeList) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) { |
| 135 | + return m.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) { |
| 136 | + if m.HasFocus() { |
| 137 | + switch event.Key() { |
| 138 | + case tcell.KeyEscape: |
| 139 | + m.done(-1) |
| 140 | + } |
| 141 | + if handler := m.table.InputHandler(); handler != nil { |
| 142 | + handler(event, setFocus) |
| 143 | + } |
| 144 | + return |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments