Skip to content

Commit

Permalink
reafactor: rofi event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
davidborzek committed Jul 19, 2024
1 parent 0ea191a commit 40b5e5b
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 481 deletions.
49 changes: 17 additions & 32 deletions internal/views/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type albumView struct {

func NewAlbumView(app *app.App) View {
r := rofi.App{
KBCustom: []string{
Keybindings: []string{
app.Config.Keybindings.PlayAlbum,
app.Config.Keybindings.AddToQueue,
app.Config.Keybindings.PlayTrack,
Expand Down Expand Up @@ -66,34 +66,6 @@ func (view *albumView) playAlbum(uri ...string) {
}
}

func (view *albumView) handleSelection(selection *rofi.Row, code int) {
if code == rofi.Escape || selection.Title == rofi.Back {
view.parent.Show()
return
}

if code == rofi.KBCustom1 {
view.playAlbum()
return
}

if code == rofi.KBCustom2 {
view.addToQueue(selection.Value)
return
}

if code == rofi.KBCustom3 {
view.playTrack(selection.Value)
return
}

if code > 0 {
return
}

view.playAlbum(selection.Value)
}

func (view *albumView) setPrompt() {
view.rofi.Prompt = format.FormatTitle(view.album.Name, view.album.Artists[0].Name)
}
Expand Down Expand Up @@ -127,13 +99,26 @@ func (view *albumView) Show(payload ...interface{}) {
view.setPrompt()
view.setRows()

result, code, err := view.rofi.Show()
evt, err := view.rofi.Run()
if err != nil {
log.Fatalln(err.Error())
}

view.handleSelection(result, code)

switch evt := evt.(type) {
case rofi.BackEvent, rofi.CancelledEvent:
view.parent.Show()
case rofi.KeyEvent:
switch evt.Key {
case view.app.Config.Keybindings.PlayAlbum:
view.playAlbum()
case view.app.Config.Keybindings.AddToQueue:
view.addToQueue(evt.Selection.Value)
case view.app.Config.Keybindings.PlayTrack:
view.playTrack(evt.Selection.Value)
}
case rofi.SelectedEvent:
view.playAlbum(evt.Selection.Value)
}
}

func (view *albumView) SetParent(parent View) {
Expand Down
48 changes: 17 additions & 31 deletions internal/views/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,6 @@ func (view *devicesView) getCurrentDevice() string {
return msg
}

func (view *devicesView) handleSelection(selection *rofi.Row, code int) {
if code == rofi.Escape {
view.parent.Show()
return
}

if code > 0 {
return
}

if selection.Title == rofi.Back {
view.parent.Show()
return
}

view.app.Config.Device = config.SpotifyDevice{
ID: selection.Value,
Name: selection.Title,
}

if err := view.app.Config.Write(); err != nil {
selectDeviceError(err)
return
}

view.app.Player.SetDevice(selection.Value)
view.Show()
}

func (view *devicesView) Show(payload ...interface{}) {
rows, err := view.getDevices()
if err != nil {
Expand All @@ -110,13 +81,28 @@ func (view *devicesView) Show(payload ...interface{}) {
view.rofi.Message = msg
view.rofi.Rows = rows

result, code, err := view.rofi.Show()
evt, err := view.rofi.Run()
if err != nil {
log.Fatalln(err.Error())
}

view.handleSelection(result, code)
switch evt := evt.(type) {
case rofi.BackEvent, rofi.CancelledEvent:
view.parent.Show()
case rofi.SelectedEvent:
view.app.Config.Device = config.SpotifyDevice{
ID: evt.Selection.Value,
Name: evt.Selection.Title,
}

if err := view.app.Config.Write(); err != nil {
selectDeviceError(err)
return
}

view.app.Player.SetDevice(evt.Selection.Value)
view.Show()
}
}

func (view *devicesView) SetParent(parent View) {
Expand Down
79 changes: 27 additions & 52 deletions internal/views/liked_tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type likedTracksView struct {
func NewLikedTracksView(app *app.App, title string) View {
r := rofi.App{
Prompt: title,
KBCustom: []string{
Keybindings: []string{
app.Config.Keybindings.NextPage,
app.Config.Keybindings.PreviousPage,
app.Config.Keybindings.AddToQueue,
Expand Down Expand Up @@ -76,55 +76,6 @@ func (view *likedTracksView) addToQueue(uri string) {
if err != nil {
addQueueError(err)
}

view.Show()
}

func (view *likedTracksView) handleSelection(selection *rofi.Row, code int) {
if code == rofi.Escape {
view.page = 1
view.parent.Show()
return
}

if code == rofi.KBCustom1 {
if view.page < view.totalPages {
view.page += 1
}

view.Show()
return
}

if code == rofi.KBCustom2 {
if view.page > 1 {
view.page -= 1
}

view.Show()
return
}

if code == rofi.KBCustom3 {
view.addToQueue(selection.Value)
return
}

if code > 0 {
return
}

if selection.Title == rofi.Back {
view.parent.Show()
return
}

err := view.app.Player.PlayTrack(selection.Value)

if err != nil {
playTrackError(err)
return
}
}

func (view *likedTracksView) Show(payload ...interface{}) {
Expand All @@ -137,12 +88,36 @@ func (view *likedTracksView) Show(payload ...interface{}) {
view.rofi.Prompt = fmt.Sprintf("%s %d/%d", view.title, view.page, view.totalPages)
view.rofi.Rows = rows

result, code, err := view.rofi.Show()
evt, err := view.rofi.Run()
if err != nil {
log.Fatalln(err.Error())
}

view.handleSelection(result, code)
switch evt := evt.(type) {
case rofi.BackEvent, rofi.CancelledEvent:
view.page = 1
view.parent.Show()
case rofi.KeyEvent:
switch evt.Key {
case view.app.Config.Keybindings.NextPage:
if view.page < view.totalPages {
view.page += 1
}
case view.app.Config.Keybindings.PreviousPage:
if view.page > 1 {
view.page -= 1
}
case view.app.Config.Keybindings.AddToQueue:
view.addToQueue(evt.Selection.Value)
}

view.Show()
case rofi.SelectedEvent:
err := view.app.Player.PlayTrack(evt.Selection.Value)
if err != nil {
playTrackError(err)
}
}
}

func (view *likedTracksView) SetParent(parent View) {
Expand Down
118 changes: 49 additions & 69 deletions internal/views/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewMainView(app *app.App) View {

r := rofi.App{
IgnoreCase: true,
KBCustom: []string{
Keybindings: []string{
app.Config.Keybindings.TogglePauseResume,
app.Config.Keybindings.NextTrack,
app.Config.Keybindings.PreviousTrack,
Expand Down Expand Up @@ -178,82 +178,62 @@ func (view *mainView) buildPlayerMessage() string {
return currentlyPlaying
}

func (view *mainView) handleSelection(selection *rofi.Row, code int) {
if code == rofi.KBCustom1 {
if err := view.app.Player.PlayPause(); err != nil {
playPauseError(err)
}
view.Show()
return
}

if code == rofi.KBCustom2 {
if err := view.app.Player.Next(); err != nil {
skipTrackError(err)
}
view.Show()
return
}

if code == rofi.KBCustom3 {
if err := view.app.Player.Previous(); err != nil {
previousTrackError(err)
}
view.Show()
return
}

if code == rofi.KBCustom4 {
if err := view.app.Player.ToggleRepeat(); err != nil {
updatePlayerError(err)
}
view.Show()
return
}

if code == rofi.KBCustom5 {
if err := view.app.Player.ToggleShuffle(); err != nil {
updatePlayerError(err)
}
view.Show()
return
}

if code > 0 {
return
}

switch selection.Value {
case playerViewID:
view.playerView.Show()
case devicesViewID:
view.devicesView.Show()
case searchViewID:
view.searchView.Show()
case likedTracksViewID:
view.likedTracksView.Show()
case queueViewID:
view.queueView.Show()
case recentlyPlayedViewID:
view.recentlyPlayedView.Show()
case savedAlbumsViewID:
view.savedAlbumsView.Show()
default:
view.searchTracksView.SetQuery(selection.Title)
view.searchTracksView.Show()
}
}

func (view *mainView) Show(payload ...interface{}) {
msg := view.buildPlayerMessage()
view.rofi.Prompt = msg

selection, code, err := view.rofi.Show()
evt, err := view.rofi.Run()
if err != nil {
log.Fatalln(err.Error())
}

view.handleSelection(selection, code)
switch evt := evt.(type) {
case rofi.KeyEvent:
switch evt.Key {
case view.app.Config.Keybindings.TogglePauseResume:
if err := view.app.Player.PlayPause(); err != nil {
playPauseError(err)
}
case view.app.Config.Keybindings.NextTrack:
if err := view.app.Player.Next(); err != nil {
skipTrackError(err)
}
case view.app.Config.Keybindings.PreviousTrack:
if err := view.app.Player.Previous(); err != nil {
previousTrackError(err)
}
case view.app.Config.Keybindings.ToggleRepeat:
if err := view.app.Player.ToggleRepeat(); err != nil {
updatePlayerError(err)
}
case view.app.Config.Keybindings.ToggleShuffle:
if err := view.app.Player.ToggleShuffle(); err != nil {
updatePlayerError(err)
}
}

view.Show()
case rofi.SelectedEvent:
switch evt.Selection.Value {
case playerViewID:
view.playerView.Show()
case devicesViewID:
view.devicesView.Show()
case searchViewID:
view.searchView.Show()
case likedTracksViewID:
view.likedTracksView.Show()
case queueViewID:
view.queueView.Show()
case recentlyPlayedViewID:
view.recentlyPlayedView.Show()
case savedAlbumsViewID:
view.savedAlbumsView.Show()
default:
view.searchTracksView.SetQuery(evt.Selection.Title)
view.searchTracksView.Show()
}
}
}

func (view *mainView) SetParent(parent View) {
Expand Down
Loading

0 comments on commit 40b5e5b

Please sign in to comment.