From e2fd4c117e2ed1e5a066d7adce3d1301607c3bf2 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 13 Nov 2021 16:40:33 +0530 Subject: [PATCH 1/5] Using Pages so that Notifications can be displayed Pages allow overlaying of primitives. --- App.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/App.go b/App.go index 0ef4492..82d4006 100644 --- a/App.go +++ b/App.go @@ -13,6 +13,7 @@ type Application struct { Navbar *tview.Table searchBar *tview.Table pBar *progressBar + Pages *tview.Pages } func newApplication(r *Renderer) *Application { @@ -51,8 +52,11 @@ func newApplication(r *Renderer) *Application { expandedView.SetBorderPadding(1, 1, 1, 1).SetBorder(true) expandedView.SetSelectable(true, false) + rootPages := tview.NewPages() + rootPages.AddPage("Main", mainFlex, true, true) + App := tview.NewApplication() - App.SetRoot(mainFlex, true).SetFocus(expandedView) + App.SetRoot(rootPages, true).SetFocus(expandedView) return &Application{ App: App, @@ -60,6 +64,7 @@ func newApplication(r *Renderer) *Application { Navbar: Navbar, searchBar: searchBar, pBar: pBar, + Pages: rootPages, } } From dbfc61703774c40a0bc35696e9eaffcfc78adda1 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 13 Nov 2021 16:57:54 +0530 Subject: [PATCH 2/5] Rename Application Fields --- App.go | 12 ++++++------ main.go | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/App.go b/App.go index 82d4006..5c38a17 100644 --- a/App.go +++ b/App.go @@ -9,10 +9,10 @@ var IMG_X, IMG_Y, IMG_W, IMG_H int type Application struct { App *tview.Application - expandedView *tview.Table + ExpandedView *tview.Table Navbar *tview.Table - searchBar *tview.Table - pBar *progressBar + SearchBar *tview.Table + ProgressBar *progressBar Pages *tview.Pages } @@ -60,10 +60,10 @@ func newApplication(r *Renderer) *Application { return &Application{ App: App, - expandedView: expandedView, + ExpandedView: expandedView, Navbar: Navbar, - searchBar: searchBar, - pBar: pBar, + SearchBar: searchBar, + ProgressBar: pBar, Pages: rootPages, } diff --git a/main.go b/main.go index b23014d..8ebe916 100644 --- a/main.go +++ b/main.go @@ -39,31 +39,31 @@ func main() { fileMap, err := CONN.GetFiles() dirTree := generateDirectoryTree(fileMap) - UpdatePlaylist(UI.expandedView) + UpdatePlaylist(UI.ExpandedView) _v, _ := CONN.Status() Volume, _ = strconv.ParseInt(_v["volume"], 10, 64) Random, _ = strconv.ParseBool(_v["random"]) Repeat, _ = strconv.ParseBool(_v["repeat"]) - UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { + UI.ExpandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) { if InsidePlaylist { - UpdatePlaylist(UI.expandedView) + UpdatePlaylist(UI.ExpandedView) } else { - Update(dirTree.children, UI.expandedView) + Update(dirTree.children, UI.ExpandedView) } - return UI.expandedView.GetInnerRect() + return UI.ExpandedView.GetInnerRect() }) var FUNC_MAP = map[string]func(){ "showChildrenContent": func() { - r, _ := UI.expandedView.GetSelection() + r, _ := UI.ExpandedView.GetSelection() if !InsidePlaylist { if len(dirTree.children[r].children) == 0 { id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1) CONN.PlayId(id) } else { - Update(dirTree.children[r].children, UI.expandedView) + Update(dirTree.children[r].children, UI.ExpandedView) dirTree = &dirTree.children[r] } } else { @@ -76,7 +76,7 @@ func main() { "showParentContent": func() { if !InsidePlaylist { if dirTree.parent != nil { - Update(dirTree.parent.children, UI.expandedView) + Update(dirTree.parent.children, UI.ExpandedView) dirTree = dirTree.parent } } @@ -87,7 +87,7 @@ func main() { "clearPlaylist": func() { CONN.Clear() if InsidePlaylist { - UpdatePlaylist(UI.expandedView) + UpdatePlaylist(UI.ExpandedView) } }, "previousSong": func() { @@ -95,7 +95,7 @@ func main() { }, "addToPlaylist": func() { if !InsidePlaylist { - r, _ := UI.expandedView.GetSelection() + r, _ := UI.ExpandedView.GetSelection() CONN.Add(dirTree.children[r].absolutePath) } }, @@ -130,12 +130,12 @@ func main() { "navigateToFiles": func() { InsidePlaylist = false UI.Navbar.Select(1, 0) - Update(dirTree.children, UI.expandedView) + Update(dirTree.children, UI.ExpandedView) }, "navigateToPlaylist": func() { InsidePlaylist = true UI.Navbar.Select(0, 0) - UpdatePlaylist(UI.expandedView) + UpdatePlaylist(UI.ExpandedView) }, "navigateToMostPlayed": func() { InsidePlaylist = false @@ -155,7 +155,7 @@ func main() { }, "deleteSongFromPlaylist": func() { if InsidePlaylist { - r, _ := UI.expandedView.GetSelection() + r, _ := UI.ExpandedView.GetSelection() CONN.Delete(r, -1) } }, @@ -163,7 +163,7 @@ func main() { config.GenerateKeyMap(FUNC_MAP) - UI.expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { + UI.ExpandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { if val, ok := config.KEY_MAP[int(e.Rune())]; ok { FUNC_MAP[val]() return nil From 29439fa0de884dfcf0f75bd9abf501a0e0f63f5d Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 13 Nov 2021 23:52:16 +0530 Subject: [PATCH 3/5] Simple Implementation of notification Mechanism. Using a Notification Server that will keep listening for notifications and will render the notification upon receiving read notification.go for more information --- notification.go | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 notification.go diff --git a/notification.go b/notification.go new file mode 100644 index 0000000..9c0cd63 --- /dev/null +++ b/notification.go @@ -0,0 +1,88 @@ +package main + +import ( + "time" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +/* Notification Primitive */ +type Notification struct { + *tview.Box + Text string +} + +/* Get A Pointer to A Notification Struct */ +func NewNotification(s string) *Notification { + return &Notification{ + Box: tview.NewBox(), + Text: s, + } +} + +/* Draw Function for the Notification Primitive */ +func (self *Notification) Draw(screen tcell.Screen) { + termDetails := getWidth() + + var ( + COL int = int(termDetails.Col) + TEXTLENGTH int = len(self.Text) + HEIGHT int = 3 + TEXTPOSITION int = 2 + ) + + self.Box.SetBackgroundColor(tcell.GetColor("#15191a")) + self.SetRect(COL-(TEXTLENGTH+7), 1, TEXTLENGTH+4, HEIGHT) + self.DrawForSubclass(screen, self.Box) + tview.Print(screen, self.Text, + COL-(TEXTLENGTH+5), TEXTPOSITION, TEXTLENGTH, + tview.AlignCenter, tcell.GetColor("#ffffff")) +} + +/* Notification Server : Not an actual Server*/ +type NotificationServer struct { + c chan string +} + +/* Get A Pointer to a NotificationServer Struct */ +func NewNotificationServer() *NotificationServer { + return &NotificationServer{ + c: make(chan string), + } +} + +/* This Method Starts the go routine for the NotificationServer */ +func (self *NotificationServer) Start() { + go NotificationRoutine(self.c, "EMPTY NOTIFICATION") +} + +/* The Notification Server is just a string channel and the NotificationRoutine +the channel is used to receive the Notification Data through the Send Function +The Channel keeps listening for the Notification when it receives a Notification it checks if it +is Empty or not if it is an empty Notification it calls the NotificationRoutine with the empty routine else +it will call the go routine that renders the Notification for the Notification Interval and agains start listening +for the notfications sort of works like a que */ +func NotificationRoutine(c chan string, s string) { + if s != "EMPTY NOTIFICATION" { + go func() { + currentTime := time.Now().String() + UI.Pages.AddPage(currentTime, NewNotification(s), false, true) + UI.App.SetFocus(UI.ExpandedView) + time.Sleep(time.Second * 1) + UI.Pages.RemovePage(currentTime) + UI.App.SetFocus(UI.ExpandedView) + }() + } + NewNotification := <-c + if NewNotification == "EMPTY NOTIFICATION" { + NotificationRoutine(c, "EMPTY NOTIFICATION") + } else { + NotificationRoutine(c, NewNotification) + } +} + +/* Sends the Notification to the Notification Server */ +func (self NotificationServer) Send(text string) { + self.c <- text +} From b01deba4840ac2e9fe1a9688191507d37ff628a9 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sat, 13 Nov 2021 23:53:59 +0530 Subject: [PATCH 4/5] Utilising the Notification Server Following Changes have been made: 1. UI is now global 2. Simple Notifications are sent when some events are called. --- main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 8ebe916..8fdbcd1 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( ) var CONN *mpd.Client +var UI *Application var Volume int64 var Random bool var Repeat bool @@ -34,7 +35,7 @@ func main() { r.Start("stop") } - UI := newApplication(r) + UI = newApplication(r) fileMap, err := CONN.GetFiles() dirTree := generateDirectoryTree(fileMap) @@ -55,6 +56,9 @@ func main() { return UI.ExpandedView.GetInnerRect() }) + notificationServer := NewNotificationServer() + notificationServer.Start() + var FUNC_MAP = map[string]func(){ "showChildrenContent": func() { r, _ := UI.ExpandedView.GetSelection() @@ -85,6 +89,7 @@ func main() { CONN.Next() }, "clearPlaylist": func() { + notificationServer.Send("PlayList Cleared") CONN.Clear() if InsidePlaylist { UpdatePlaylist(UI.ExpandedView) @@ -145,6 +150,7 @@ func main() { UI.App.Stop() }, "stop": func() { + notificationServer.Send("Playback Stopped") CONN.Stop() }, "updateDB": func() { From 8f995c235796ebb574c9b5dbee732ad31538a4b1 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Sun, 14 Nov 2021 01:31:17 +0530 Subject: [PATCH 5/5] Minor changes --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 8fdbcd1..05a0aee 100644 --- a/main.go +++ b/main.go @@ -89,11 +89,11 @@ func main() { CONN.Next() }, "clearPlaylist": func() { - notificationServer.Send("PlayList Cleared") CONN.Clear() if InsidePlaylist { UpdatePlaylist(UI.ExpandedView) } + notificationServer.Send("PlayList Cleared") }, "previousSong": func() { CONN.Previous() @@ -150,14 +150,15 @@ func main() { UI.App.Stop() }, "stop": func() { - notificationServer.Send("Playback Stopped") CONN.Stop() + notificationServer.Send("Playback Stopped") }, "updateDB": func() { _, err = CONN.Update("") if err != nil { panic(err) } + notificationServer.Send("Database Updated") }, "deleteSongFromPlaylist": func() { if InsidePlaylist {