Simple NavMenu implementation

This commit is contained in:
aditya-K2
2023-04-11 02:38:46 +05:30
parent 45acf4ba24
commit 79ed0d2608
2 changed files with 47 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
package ui
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
@@ -16,7 +18,7 @@ var (
type Application struct {
App *tview.Application
Main *interactiveView
Navbar *tview.Table
NavMenu *NavMenu
SearchBar *tview.Box
ProgressBar *tview.Box
Root *Root
@@ -42,16 +44,21 @@ func NewApplication() *Application {
mains.SetContextHandler(GetCurrentView().ContextHandler)
mains.SetExternalCapture(GetCurrentView().ExternalInputCapture)
Navbar := tview.NewTable()
NavMenu := newNavMenu([]navItem{
{"Albums", NewAction(func(e *tcell.EventKey) *tcell.EventKey { fmt.Println("Albums"); return nil }, nil)},
{"Artists", NewAction(func(e *tcell.EventKey) *tcell.EventKey { fmt.Println("Artists"); return nil }, nil)},
{"Liked Songs", NewAction(func(e *tcell.EventKey) *tcell.EventKey { fmt.Println("Liked Songs"); return nil }, nil)},
{"Recently Played", NewAction(func(e *tcell.EventKey) *tcell.EventKey { fmt.Println("Recently Played"); return nil }, nil)},
})
imagePreviewer := tview.NewBox()
imagePreviewer.SetBorder(true)
Navbar.SetBackgroundColor(tcell.ColorDefault)
NavMenu.Table.SetBackgroundColor(tcell.ColorDefault)
imagePreviewer.SetBackgroundColor(tcell.ColorDefault)
Navbar.SetBorder(true)
Navbar.SetSelectable(true, false)
NavMenu.Table.SetBorder(true)
NavMenu.Table.SetSelectable(true, false)
done := func(s bool, err error) {
if s {
@@ -76,10 +83,11 @@ func NewApplication() *Application {
}
playlistNav.MapActions(map[tcell.Key]string{
tcell.KeyEnter: "openEntry",
tcell.KeyCtrlP: "playEntry",
})
searchNavFlex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(Navbar, 6, 3, false).
AddItem(NavMenu.Table, 6, 3, false).
AddItem(playlistNav.Table, 0, 6, false).
AddItem(imagePreviewer, 9, 3, false)
@@ -102,7 +110,7 @@ func NewApplication() *Application {
Ui = &Application{
App: App,
Main: mains,
Navbar: Navbar,
NavMenu: NavMenu,
SearchBar: searchbar,
ProgressBar: pBar,
Root: Main,

View File

@@ -6,6 +6,38 @@ import (
"github.com/rivo/tview"
)
type NavMenu struct {
Table *tview.Table
m []navItem
}
type navItem struct {
name string
action *Action
}
func newNavMenu(m []navItem) *NavMenu {
T := tview.NewTable()
n := &NavMenu{T, m}
T.SetDrawFunc(func(tcell.Screen, int, int, int, int) (int, int, int, int) {
for k := range n.m {
T.SetCell(k, 0,
GetCell(n.m[k].name, tcell.StyleDefault.Foreground(tcell.ColorRed)))
}
return T.GetInnerRect()
})
T.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if e.Key() == tcell.KeyEnter {
r, _ := T.GetSelection()
if r < len(n.m) {
return (*n.m[r].action).Func()(e)
}
}
return e
})
return n
}
type PlaylistNav struct {
Table *tview.Table
Playlists *spt.UserPlaylists