mirror of
https://github.com/aditya-K2/gspt.git
synced 2026-01-09 13:58:05 -05:00
Better ContentHandler implementation
This commit is contained in:
39
ui/menu.go
39
ui/menu.go
@@ -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 }
|
||||
|
||||
20
ui/root.go
20
ui/root.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user