« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--backend.js20
-rw-r--r--ui.js7
2 files changed, 26 insertions, 1 deletions
diff --git a/backend.js b/backend.js
index 69aa815..81726c6 100644
--- a/backend.js
+++ b/backend.js
@@ -66,6 +66,7 @@ class QueuePlayer extends EventEmitter {
     this.playingTrack = null
     this.queueGrouplike = {name: 'Queue', isTheQueue: true, items: []}
     this.pauseNextTrack = false
+    this.loopQueueAtEnd = false
     this.playedTrackToEnd = false
     this.timeData = null
 
@@ -433,7 +434,11 @@ class QueuePlayer extends EventEmitter {
     if (playingThisTrack) {
       this.playedTrackToEnd = true
       if (!this.playNext(item)) {
-        this.clearPlayingTrack()
+        if (this.loopQueueAtEnd) {
+          this.playFirst()
+        } else {
+          this.clearPlayingTrack()
+        }
       }
     }
   }
@@ -496,6 +501,15 @@ class QueuePlayer extends EventEmitter {
     return true
   }
 
+  playFirst() {
+    const queue = this.queueGrouplike
+    if (queue.items.length) {
+      this.play(queue.items[0])
+      return true
+    }
+    return false
+  }
+
   clearPlayingTrack() {
     if (this.playingTrack !== null) {
       const oldTrack = this.playingTrack
@@ -557,6 +571,10 @@ class QueuePlayer extends EventEmitter {
     this.pauseNextTrack = !!value
   }
 
+  setLoopQueueAtEnd(value) {
+    this.loopQueueAtEnd = !!value
+  }
+
   get remainingTracks() {
     const index = this.queueGrouplike.items.indexOf(this.playingTrack)
     const length = this.queueGrouplike.items.length
diff --git a/ui.js b/ui.js
index c734a74..5a8e0ad 100644
--- a/ui.js
+++ b/ui.js
@@ -343,6 +343,7 @@ class AppElement extends FocusElement {
           {divider: true},
           playingTrack && {element: this.playingControl},
           {element: this.loopingControl},
+          {element: this.loopQueueControl},
           {element: this.pauseNextControl},
           {element: this.autoDJControl},
           {element: this.volumeSlider},
@@ -398,6 +399,12 @@ class AppElement extends FocusElement {
       getEnabled: () => this.config.canControlPlayback
     })
 
+    this.loopQueueControl = new ToggleControl('Loop queue when it ends?', {
+      setValue: val => this.SQP.setLoopQueueAtEnd(val),
+      getValue: () => this.SQP.loopQueueAtEnd,
+      getEnabled: () => this.config.canControlPlayback
+    })
+
     this.volumeSlider = new SliderElement('Volume', {
       setValue: val => this.SQP.setVolume(val),
       getValue: () => this.SQP.player.volume,