Better ContentHandler implementation

This commit is contained in:
aditya-K2
2023-01-26 05:05:59 +05:30
parent 31b53b3b3a
commit 32ecd96ff9
2 changed files with 31 additions and 28 deletions

View File

@@ -1,45 +1,54 @@
package ui
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type menu struct {
Menu *tview.Table
_s []string
Menu *tview.Table
title string
content []string
}
func newMenu() *menu {
c := &menu{}
ctxMenu := tview.NewTable()
ctxMenu.SetBorder(true)
ctxMenu.SetSelectable(true, false)
c.Menu = ctxMenu
menu := tview.NewTable()
menu.SetBorder(true)
menu.SetSelectable(true, false)
c.Menu = menu
return c
}
func (c *menu) Size(mw, mh int) (int, int, int, int) {
cslice := c.ContentHandler()
cheight := len(cslice) + 3
cheight := len(c.content) + 3
cwidth := 30
epx := 4
return mw/2 - (cwidth/2 + epx), (mh/2 - (cheight/2 + epx)), cwidth, cheight
}
func (c *menu) ContentHandler() []string {
return []string{
"Hello",
"Bye",
func (c *menu) ContentHandler() {
if c.title != "" {
c.Menu.SetCell(0, 0,
GetCell(c.title, tcell.StyleDefault.
Foreground(tcell.ColorWhite).
Bold(true)).SetSelectable(false))
}
for k := range c.content {
c.Menu.SetCell(k+1, 0,
GetCell(c.content[k], defaultstyle))
}
}
func (c *menu) SelectionHandler(s string) {
c._s = append(c._s, s)
c.content = append(c.content, s)
}
func (c menu) Title() string { return "Add to Playlist: " }
func (c *menu) Primitive() *tview.Table { return c.Menu }
func (c *menu) Content(s []string) { c.content = s }
func (c *menu) Title(s string) { c.title = s }

View File

@@ -24,14 +24,12 @@ func NewMain() *Main {
type CenteredWidget interface {
Primitive() *tview.Table
ContentHandler() []string
ContentHandler()
SelectionHandler(s string)
Title() string
Size(mw, mh int) (int, int, int, int)
}
func (m *Main) addCenteredWidget(t CenteredWidget) {
cslice := t.ContentHandler()
p := *(t.Primitive())
closec := make(chan bool)
currentTime := time.Now().String()
@@ -68,16 +66,7 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
}
p.SetInputCapture(capture)
if t.Title() != "" {
p.SetCell(0, 0,
GetCell(t.Title(), tcell.StyleDefault.
Foreground(tcell.ColorWhite).
Bold(true)).SetSelectable(false))
}
for k := range cslice {
p.SetCell(k+1, 0,
GetCell(cslice[k], defaultstyle))
}
t.ContentHandler()
resizeHandler := func() {
dur := 500
@@ -109,5 +98,10 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
func (m *Main) OpenContextMenu() {
c := newMenu()
c.Content([]string{
"Hello",
"Bitches",
"whatisup"})
c.Title("Add to Playlist")
m.addCenteredWidget(c)
}