From 7d20da67d9de1ea5fd919c5d6e6cc11231357e35 Mon Sep 17 00:00:00 2001 From: aditya-K2 Date: Mon, 1 May 2023 12:55:04 +0530 Subject: [PATCH] Refactoring --- ui/app.go | 35 +++++------------------- ui/cover_art.go | 21 -------------- ui/styles.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++ ui/utils.go | 67 +++++++++++++++++++++++++++++++++++++++++++++ ui/view_default.go | 39 -------------------------- 5 files changed, 142 insertions(+), 88 deletions(-) create mode 100644 ui/styles.go create mode 100644 ui/utils.go diff --git a/ui/app.go b/ui/app.go index 3d30bc7..9698ae4 100644 --- a/ui/app.go +++ b/ui/app.go @@ -38,35 +38,14 @@ var ( Flex *tview.Flex ) -func setStyles() { - TrackStyle = config.Config.Colors.Track.Style() - AlbumStyle = config.Config.Colors.Album.Style() - ArtistStyle = config.Config.Colors.Artist.Style() - TimeStyle = config.Config.Colors.Timestamp.Style() - GenreStyle = config.Config.Colors.Genre.Style() - PlaylistNavStyle = config.Config.Colors.PlaylistNav.Style() - NavStyle = config.Config.Colors.Nav.Style() - ContextMenuStyle = config.Config.Colors.ContextMenu.Style() - NotSelectableStyle = config.Config.Colors.Null.Style() +func onConfigChange() { + setStyles() + + setBorderRunes() + if coverArt != nil { coverArt.RefreshState() } - if config.Config.RoundedCorners { - tview.Borders.TopLeft = '╭' - tview.Borders.TopRight = '╮' - tview.Borders.BottomRight = '╯' - tview.Borders.BottomLeft = '╰' - tview.Borders.Vertical = '│' - tview.Borders.Horizontal = '─' - tview.Borders.TopLeftFocus = '╭' - tview.Borders.TopRightFocus = '╮' - tview.Borders.BottomRightFocus = '╯' - tview.Borders.BottomLeftFocus = '╰' - tview.Borders.VerticalFocus = '│' - tview.Borders.HorizontalFocus = '─' - tview.Styles.BorderColorFocus = config.Config.Colors.BorderFocus.Foreground() - tview.Styles.BorderColor = config.Config.Colors.Border.Foreground() - } } @@ -109,8 +88,8 @@ func rectWatcher() { } func NewApplication() *tview.Application { - setStyles() - config.OnConfigChange = setStyles + onConfigChange() + config.OnConfigChange = onConfigChange App = tview.NewApplication() root = NewRoot() diff --git a/ui/cover_art.go b/ui/cover_art.go index 071d96a..b9feb7b 100644 --- a/ui/cover_art.go +++ b/ui/cover_art.go @@ -1,18 +1,15 @@ package ui import ( - "errors" "fmt" "image" "os" - "path/filepath" "github.com/aditya-K2/gspt/config" "github.com/aditya-K2/tview" "github.com/aditya-K2/utils" "github.com/gdamore/tcell/v2" "github.com/nfnt/resize" - "github.com/zmb3/spotify/v2" "gitlab.com/diamondburned/ueberzug-go" ) @@ -52,24 +49,6 @@ func getImg(uri string) (image.Image, error) { return img, nil } -func fileName(a spotify.SimpleAlbum) string { - return fmt.Sprintf(filepath.Join(config.Config.CacheDir, "%s.jpg"), a.ID) -} - -func getFontWidth() (int, int, error) { - w, h, err := ueberzug.GetParentSize() - if err != nil { - return 0, 0, err - } - _, _, rw, rh := root.Root.GetRect() - if rw == 0 || rh == 0 { - return 0, 0, errors.New("Unable to get row width and height") - } - fw := w / rw - fh := h / rh - return fw, fh, nil -} - func (c *CoverArt) RefreshState() { if c.image != nil { c.image.Clear() diff --git a/ui/styles.go b/ui/styles.go new file mode 100644 index 0000000..556b197 --- /dev/null +++ b/ui/styles.go @@ -0,0 +1,68 @@ +package ui + +import ( + "github.com/aditya-K2/gspt/config" + "github.com/aditya-K2/tview" +) + +var ( + borders = map[bool]map[string]rune{ + true: { + "TopLeft": '╭', + "TopRight": '╮', + "BottomRight": '╯', + "BottomLeft": '╰', + "Vertical": '│', + "Horizontal": '─', + "TopLeftFocus": '╭', + "TopRightFocus": '╮', + "BottomRightFocus": '╯', + "BottomLeftFocus": '╰', + "VerticalFocus": '│', + "HorizontalFocus": '─', + }, + false: { + "TopLeft": tview.Borders.TopLeft, + "TopRight": tview.Borders.TopRight, + "BottomRight": tview.Borders.BottomRight, + "BottomLeft": tview.Borders.BottomLeft, + "Vertical": tview.Borders.Vertical, + "Horizontal": tview.Borders.Horizontal, + "TopLeftFocus": tview.Borders.TopLeftFocus, + "TopRightFocus": tview.Borders.TopRightFocus, + "BottomRightFocus": tview.Borders.BottomRightFocus, + "BottomLeftFocus": tview.Borders.BottomLeftFocus, + "VerticalFocus": tview.Borders.VerticalFocus, + "HorizontalFocus": tview.Borders.HorizontalFocus, + }, + } +) + +func setBorderRunes() { + tview.Borders.TopLeft = borders[config.Config.RoundedCorners]["TopLeft"] + tview.Borders.TopRight = borders[config.Config.RoundedCorners]["TopRight"] + tview.Borders.BottomRight = borders[config.Config.RoundedCorners]["BottomRight"] + tview.Borders.BottomLeft = borders[config.Config.RoundedCorners]["BottomLeft"] + tview.Borders.Vertical = borders[config.Config.RoundedCorners]["Vertical"] + tview.Borders.Horizontal = borders[config.Config.RoundedCorners]["Horizontal"] + tview.Borders.TopLeftFocus = borders[config.Config.RoundedCorners]["TopLeftFocus"] + tview.Borders.TopRightFocus = borders[config.Config.RoundedCorners]["TopRightFocus"] + tview.Borders.BottomRightFocus = borders[config.Config.RoundedCorners]["BottomRightFocus"] + tview.Borders.BottomLeftFocus = borders[config.Config.RoundedCorners]["BottomLeftFocus"] + tview.Borders.VerticalFocus = borders[config.Config.RoundedCorners]["VerticalFocus"] + tview.Borders.HorizontalFocus = borders[config.Config.RoundedCorners]["HorizontalFocus"] +} + +func setStyles() { + TrackStyle = config.Config.Colors.Track.Style() + AlbumStyle = config.Config.Colors.Album.Style() + ArtistStyle = config.Config.Colors.Artist.Style() + TimeStyle = config.Config.Colors.Timestamp.Style() + GenreStyle = config.Config.Colors.Genre.Style() + PlaylistNavStyle = config.Config.Colors.PlaylistNav.Style() + NavStyle = config.Config.Colors.Nav.Style() + ContextMenuStyle = config.Config.Colors.ContextMenu.Style() + NotSelectableStyle = config.Config.Colors.Null.Style() + tview.Styles.BorderColorFocus = config.Config.Colors.BorderFocus.Foreground() + tview.Styles.BorderColor = config.Config.Colors.Border.Foreground() +} diff --git a/ui/utils.go b/ui/utils.go new file mode 100644 index 0000000..8e4817b --- /dev/null +++ b/ui/utils.go @@ -0,0 +1,67 @@ +package ui + +import ( + "errors" + "fmt" + "path/filepath" + + "github.com/aditya-K2/gspt/config" + "github.com/aditya-K2/gspt/spt" + "github.com/zmb3/spotify/v2" + "gitlab.com/diamondburned/ueberzug-go" +) + +func openPlaylistMenu(handler func(playlistId spotify.SimplePlaylist)) { + c := NewMenu() + cc := []string{} + // TODO: Better Error Handling + plist, ch := spt.CurrentUserPlaylists() + err := <-ch + if err != nil { + SendNotification(err.Error()) + return + } + for _, v := range *(plist) { + cc = append(cc, v.Name) + } + c.Content(cc) + c.Title("Add to Playlist") + c.SetSelectionHandler(func(sel int) { + handler((*plist)[sel]) + }) + root.AddCenteredWidget(c) +} + +func addToPlaylist(tracks []spotify.ID) { + openPlaylistMenu(func(sp spotify.SimplePlaylist) { + aerr := spt.AddTracksToPlaylist(sp.ID, tracks...) + if aerr != nil { + SendNotification(aerr.Error()) + return + } else { + s := "" + if len(tracks) > 1 { + s = "s" + } + SendNotification("Added %d track%s to %s", len(tracks), s, sp.Name) + } + }) +} + +func fileName(a spotify.SimpleAlbum) string { + return fmt.Sprintf(filepath.Join(config.Config.CacheDir, "%s.jpg"), a.ID) +} + +func getFontWidth() (int, int, error) { + w, h, err := ueberzug.GetParentSize() + if err != nil { + return 0, 0, err + } + _, _, rw, rh := root.Root.GetRect() + if rw == 0 || rh == 0 { + return 0, 0, errors.New("Unable to get row width and height") + } + fw := w / rw + fh := h / rh + return fw, fh, nil +} diff --git a/ui/view_default.go b/ui/view_default.go index bafc0df..4c05773 100644 --- a/ui/view_default.go +++ b/ui/view_default.go @@ -2,9 +2,7 @@ package ui import ( "github.com/aditya-K2/gspt/config" - "github.com/aditya-K2/gspt/spt" "github.com/gdamore/tcell/v2" - "github.com/zmb3/spotify/v2" ) type defView struct { @@ -73,40 +71,3 @@ type DefaultView struct { } func (d *DefaultView) DisableVisualMode() bool { return false } - -func openPlaylistMenu(handler func(playlistId spotify.SimplePlaylist)) { - c := NewMenu() - cc := []string{} - // TODO: Better Error Handling - plist, ch := spt.CurrentUserPlaylists() - err := <-ch - if err != nil { - SendNotification(err.Error()) - return - } - for _, v := range *(plist) { - cc = append(cc, v.Name) - } - c.Content(cc) - c.Title("Add to Playlist") - c.SetSelectionHandler(func(sel int) { - handler((*plist)[sel]) - }) - root.AddCenteredWidget(c) -} - -func addToPlaylist(tracks []spotify.ID) { - openPlaylistMenu(func(sp spotify.SimplePlaylist) { - aerr := spt.AddTracksToPlaylist(sp.ID, tracks...) - if aerr != nil { - SendNotification(aerr.Error()) - return - } else { - s := "" - if len(tracks) > 1 { - s = "s" - } - SendNotification("Added %d track%s to %s", len(tracks), s, sp.Name) - } - }) -}