New Implementation of Context Menu

Every InteractiveView should have it's own context Menu. So You set the
contextOpener and contextHandler with the contextKey which is used to
open the contextMenu.
This commit is contained in:
aditya-K2
2023-04-09 04:40:47 +05:30
parent 647cc25fad
commit 2a289e3d1a
3 changed files with 38 additions and 29 deletions

View File

@@ -30,6 +30,9 @@ type interactiveView struct {
visual bool
vrange *_range
baseSel int
contextKey rune
contextHandler func(start, end, selrow int)
contextOpener func()
content func() [][]Content
externalCapture func(e *tcell.EventKey) *tcell.EventKey
View *tview.Table
@@ -63,6 +66,25 @@ func (i *interactiveView) SetExternalCapture(f func(e *tcell.EventKey) *tcell.Ev
i.externalCapture = f
}
func (i *interactiveView) SetContextKey(contextKey rune) {
i.contextKey = contextKey
}
func (i *interactiveView) SetContextOpener(f func()) {
i.contextOpener = f
}
func (i *interactiveView) SetContextHandler(f func(start, end, selrow int)) {
i.contextHandler = f
}
func (i *interactiveView) SelectionHandler(selrow int) {
if i.visual {
i.toggleVisualMode()
}
i.contextHandler(i.vrange.Start, i.vrange.End, selrow)
}
func (i *interactiveView) exitVisualMode() {
if i.vrange.Start < i.baseSel {
i.View.Select(i.vrange.Start, -1)
@@ -160,6 +182,10 @@ func (i *interactiveView) getHandler(s string) func(e *tcell.EventKey) *tcell.Ev
}
return e
},
"openCtx": func(e *tcell.EventKey) *tcell.EventKey {
i.contextOpener()
return nil
},
}
if val, ok := funcMap[s]; ok {
return val
@@ -191,9 +217,9 @@ func (i *interactiveView) capture(e *tcell.EventKey) *tcell.EventKey {
{
return i.getHandler("bottom")(e)
}
case 'C':
case i.contextKey:
{
return i.getHandler("openContextMenu")(e)
return i.getHandler("openCtx")(e)
}
default:
{

View File

@@ -13,10 +13,10 @@ type menu struct {
Menu *tview.Table
title string
content []string
sHandler func(s string)
sHandler func(s int)
}
func newMenu() *menu {
func NewMenu() *menu {
c := &menu{}
menu := tview.NewTable()
@@ -47,11 +47,11 @@ func (c *menu) ContentHandler() {
}
}
func (c *menu) SelectionHandler() func(s string) {
func (c *menu) SelectionHandler() func(s int) {
return c.sHandler
}
func (c *menu) SetSelectionHandler(f func(s string)) {
func (c *menu) SetSelectionHandler(f func(s int)) {
c.sHandler = f
}

View File

@@ -11,12 +11,11 @@ type Main struct {
Root *tview.Pages
}
func NewMain() *Main {
func NewMain(t tview.Primitive) *Main {
m := &Main{}
iv := NewInteractiveView()
Root := tview.NewPages()
Root.AddPage("iview", iv.View, true, true)
Root.AddPage("iview", t, true, true)
m.Root = Root
return m
@@ -25,11 +24,11 @@ func NewMain() *Main {
type CenteredWidget interface {
Primitive() *tview.Table
ContentHandler()
SelectionHandler() func(s string)
SelectionHandler() func(s int)
Size(mw, mh int) (int, int, int, int)
}
func (m *Main) addCenteredWidget(t CenteredWidget) {
func (m *Main) AddCenteredWidget(t CenteredWidget) {
p := *(t.Primitive())
closec := make(chan bool)
currentTime := time.Now().String()
@@ -57,9 +56,8 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
deleteCtx()
return nil
} else if e.Key() == tcell.KeyEnter {
sHandler(
p.GetCell(
p.GetSelection()).Text)
r, _ := p.GetSelection()
sHandler(r)
closeCtx()
return nil
}
@@ -96,18 +94,3 @@ func (m *Main) addCenteredWidget(t CenteredWidget) {
drawCtx()
}
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)
}