mirror of
https://github.com/aditya-K2/gspt.git
synced 2026-01-09 13:58:05 -05:00
Better Implementation for Selection Handler
This commit is contained in:
15
ui/menu.go
15
ui/menu.go
@@ -6,9 +6,10 @@ import (
|
||||
)
|
||||
|
||||
type menu struct {
|
||||
Menu *tview.Table
|
||||
title string
|
||||
content []string
|
||||
Menu *tview.Table
|
||||
title string
|
||||
content []string
|
||||
sHandler func(s string)
|
||||
}
|
||||
|
||||
func newMenu() *menu {
|
||||
@@ -43,8 +44,12 @@ func (c *menu) ContentHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *menu) SelectionHandler(s string) {
|
||||
c.content = append(c.content, s)
|
||||
func (c *menu) SelectionHandler() func(s string) {
|
||||
return c.sHandler
|
||||
}
|
||||
|
||||
func (c *menu) SetSelectionHandler(f func(s string)) {
|
||||
c.sHandler = f
|
||||
}
|
||||
|
||||
func (c *menu) Primitive() *tview.Table { return c.Menu }
|
||||
|
||||
10
ui/root.go
10
ui/root.go
@@ -25,7 +25,7 @@ func NewMain() *Main {
|
||||
type CenteredWidget interface {
|
||||
Primitive() *tview.Table
|
||||
ContentHandler()
|
||||
SelectionHandler(s string)
|
||||
SelectionHandler() func(s string)
|
||||
Size(mw, mh int) (int, int, int, int)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
|
||||
p := *(t.Primitive())
|
||||
closec := make(chan bool)
|
||||
currentTime := time.Now().String()
|
||||
sHandler := t.SelectionHandler()
|
||||
_, _, w, h := m.Root.GetRect()
|
||||
|
||||
closeCtx := func() {
|
||||
@@ -56,7 +57,7 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
|
||||
deleteCtx()
|
||||
return nil
|
||||
} else if e.Key() == tcell.KeyEnter {
|
||||
t.SelectionHandler(
|
||||
sHandler(
|
||||
p.GetCell(
|
||||
p.GetSelection()).Text)
|
||||
closeCtx()
|
||||
@@ -98,10 +99,15 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
|
||||
|
||||
func (m *Main) OpenContextMenu() {
|
||||
c := newMenu()
|
||||
content := []string{}
|
||||
c.Content([]string{
|
||||
"Hello",
|
||||
"Bitches",
|
||||
"whatisup"})
|
||||
c.Title("Add to Playlist")
|
||||
sHandler := func(s string) {
|
||||
content = append(content, s)
|
||||
}
|
||||
c.SetSelectionHandler(sHandler)
|
||||
m.addCenteredWidget(c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user