Better Focusing Model

Previously I was using the InsidePlaylist and InsideSearchView boolean
values which was a very hacky and unscalable way. Instead I am using a
focus map which can be queried to check which view has focus. Also the
Pages Implementation is kind of on hold because it has a lot of problems
for e.g resizing doesn't seem to work as I imagined. I am keeping that
Idea on hold right now.
This commit is contained in:
aditya-K2 2021-12-22 23:47:18 +05:30
parent 5031477cf8
commit 33603e1450
2 changed files with 47 additions and 32 deletions

58
main.go
View File

@ -19,16 +19,14 @@ import (
) )
var ( var (
CONN *mpd.Client CONN *mpd.Client
UI *ui.Application UI *ui.Application
Notify *ui.NotificationServer Notify *ui.NotificationServer
RENDERER *render.Renderer RENDERER *render.Renderer
Volume int64 Volume int64
Random bool Random bool
Repeat bool Repeat bool
InsidePlaylist = true ArtistTree map[string]map[string]map[string]string
InsideSearchView = false
ArtistTree map[string]map[string]map[string]string
) )
func main() { func main() {
@ -41,6 +39,8 @@ func main() {
} }
defer CONN.Close() defer CONN.Close()
ui.GenerateFocusMap()
client.SetConnection(CONN) client.SetConnection(CONN)
ui.SetConnection(CONN) ui.SetConnection(CONN)
render.SetConnection(CONN) render.SetConnection(CONN)
@ -78,11 +78,11 @@ func main() {
var SearchContentSlice []interface{} var SearchContentSlice []interface{}
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 { if ui.HasFocus("Playlist") {
client.UpdatePlaylist(UI.ExpandedView) client.UpdatePlaylist(UI.ExpandedView)
} else if InsideSearchView { } else if ui.HasFocus("SearchView") {
client.UpdateSearchView(UI.ExpandedView, SearchContentSlice) client.UpdateSearchView(UI.ExpandedView, SearchContentSlice)
} else { } else if ui.HasFocus("FileBrowser") {
client.Update(dirTree.Children, UI.ExpandedView) client.Update(dirTree.Children, UI.ExpandedView)
} }
return UI.ExpandedView.GetInnerRect() return UI.ExpandedView.GetInnerRect()
@ -91,7 +91,7 @@ func main() {
var FuncMap = map[string]func(){ var FuncMap = map[string]func(){
"showChildrenContent": func() { "showChildrenContent": func() {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
if !InsidePlaylist && !InsideSearchView { if ui.HasFocus("FileBrowser") {
if len(dirTree.Children[r].Children) == 0 { if len(dirTree.Children[r].Children) == 0 {
id, _ := CONN.AddId(dirTree.Children[r].AbsolutePath, -1) id, _ := CONN.AddId(dirTree.Children[r].AbsolutePath, -1)
CONN.PlayId(id) CONN.PlayId(id)
@ -99,9 +99,9 @@ func main() {
client.Update(dirTree.Children[r].Children, UI.ExpandedView) client.Update(dirTree.Children[r].Children, UI.ExpandedView)
dirTree = &dirTree.Children[r] dirTree = &dirTree.Children[r]
} }
} else if InsidePlaylist { } else if ui.HasFocus("Playlist") {
CONN.Play(r) CONN.Play(r)
} else if InsideSearchView { } else if ui.HasFocus("SearchView") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
client.AddToPlaylist(SearchContentSlice[r], true) client.AddToPlaylist(SearchContentSlice[r], true)
} }
@ -110,7 +110,7 @@ func main() {
client.TogglePlayBack() client.TogglePlayBack()
}, },
"showParentContent": func() { "showParentContent": func() {
if !InsidePlaylist && !InsideSearchView { if ui.HasFocus("FileBrowser") {
if dirTree.Parent != nil { if dirTree.Parent != nil {
client.Update(dirTree.Parent.Children, UI.ExpandedView) client.Update(dirTree.Parent.Children, UI.ExpandedView)
dirTree = dirTree.Parent dirTree = dirTree.Parent
@ -122,16 +122,16 @@ func main() {
}, },
"clearPlaylist": func() { "clearPlaylist": func() {
CONN.Clear() CONN.Clear()
Notify.Send("PlayList Cleared") Notify.Send("Playlist Cleared")
}, },
"previousSong": func() { "previousSong": func() {
CONN.Previous() CONN.Previous()
}, },
"addToPlaylist": func() { "addToPlaylist": func() {
if !InsidePlaylist && !InsideSearchView { if ui.HasFocus("FileBrowser") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Add(dirTree.Children[r].AbsolutePath) CONN.Add(dirTree.Children[r].AbsolutePath)
} else if InsideSearchView { } else if ui.HasFocus("SearchView") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
client.AddToPlaylist(SearchContentSlice[r], false) client.AddToPlaylist(SearchContentSlice[r], false)
} }
@ -165,25 +165,20 @@ func main() {
CONN.SetVolume(int(Volume)) CONN.SetVolume(int(Volume))
}, },
"navigateToFiles": func() { "navigateToFiles": func() {
InsidePlaylist = false ui.SetFocus("FileBrowser")
InsideSearchView = false
UI.Navbar.Select(1, 0) UI.Navbar.Select(1, 0)
client.Update(dirTree.Children, UI.ExpandedView) client.Update(dirTree.Children, UI.ExpandedView)
}, },
"navigateToPlaylist": func() { "navigateToPlaylist": func() {
InsidePlaylist = true ui.SetFocus("Playlist")
InsideSearchView = false
UI.Navbar.Select(0, 0) UI.Navbar.Select(0, 0)
client.UpdatePlaylist(UI.ExpandedView) client.UpdatePlaylist(UI.ExpandedView)
}, },
"navigateToMostPlayed": func() { "navigateToMostPlayed": func() {
InsideSearchView = false
InsidePlaylist = false
UI.Navbar.Select(2, 0) UI.Navbar.Select(2, 0)
}, },
"navigateToSearch": func() { "navigateToSearch": func() {
InsideSearchView = true ui.SetFocus("SearchView")
InsidePlaylist = false
UI.Navbar.Select(3, 0) UI.Navbar.Select(3, 0)
}, },
"quit": func() { "quit": func() {
@ -201,7 +196,7 @@ func main() {
Notify.Send("Database Updated") Notify.Send("Database Updated")
}, },
"deleteSongFromPlaylist": func() { "deleteSongFromPlaylist": func() {
if InsidePlaylist { if ui.HasFocus("Playlist") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Delete(r, -1) CONN.Delete(r, -1)
} }
@ -242,8 +237,7 @@ func main() {
UI.SearchBar.SetDoneFunc(func(e tcell.Key) { UI.SearchBar.SetDoneFunc(func(e tcell.Key) {
if e == tcell.KeyEnter { if e == tcell.KeyEnter {
UI.ExpandedView.Select(0, 0) UI.ExpandedView.Select(0, 0)
InsideSearchView = true ui.SetFocus("SearchView")
InsidePlaylist = false
SearchContentSlice = nil SearchContentSlice = nil
SearchContentSlice, err = client.GenerateContentSlice(UI.SearchBar.GetText()) SearchContentSlice, err = client.GenerateContentSlice(UI.SearchBar.GetText())
if err != nil { if err != nil {
@ -255,7 +249,7 @@ func main() {
} }
} }
if e == tcell.KeyEscape { if e == tcell.KeyEscape {
InsideSearchView = false ui.FocusMap["SearchView"] = false
UI.App.SetFocus(UI.ExpandedView) UI.App.SetFocus(UI.ExpandedView)
} }
}) })

21
ui/focus.go Normal file
View File

@ -0,0 +1,21 @@
package ui
var FocusMap map[string]bool
func GenerateFocusMap() {
FocusMap = make(map[string]bool)
FocusMap["Playlist"] = true
FocusMap["FileBrowser"] = false
FocusMap["SearchView"] = false
}
func HasFocus(s string) bool {
return FocusMap[s]
}
func SetFocus(s string) {
for k := range FocusMap {
FocusMap[k] = false
}
FocusMap[s] = true
}