Better Implementation for Selection Handler

This commit is contained in:
aditya-K2
2023-01-26 05:30:31 +05:30
parent 32ecd96ff9
commit 19d35d231c
2 changed files with 18 additions and 7 deletions

View File

@@ -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 }

View File

@@ -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)
}