« get me outta code hell

Menu actions - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-07-25 13:43:48 -0300
committerFlorrie <towerofnix@gmail.com>2018-07-25 13:43:48 -0300
commit0cdf9cc05daa617960847ff1b9eb3b4afde74362 (patch)
tree5994f77e97b06cef4eaa1b1b07bad363b88565d2
parentc6109e10e52c1dba3989be4b993c67830969338a (diff)
Menu actions
-rw-r--r--ui.js49
1 files changed, 38 insertions, 11 deletions
diff --git a/ui.js b/ui.js
index 273f43d..b42c24e 100644
--- a/ui.js
+++ b/ui.js
@@ -25,6 +25,9 @@ const fs = require('fs')
 const { promisify } = require('util')
 const writeFile = promisify(fs.writeFile)
 
+const playNow = Symbol()
+const playIfNothingElse = Symbol()
+
 class AppElement extends FocusElement {
   constructor() {
     super()
@@ -120,6 +123,7 @@ class AppElement extends FocusElement {
     grouplikeListing.on('select (enter)', item => handleSelectFromMain(item))
     grouplikeListing.on('select (space)', item => this.handleSpacePressed(
       () => handleSelectFromMain(item)))
+    grouplikeListing.on('play', item => this.queueGrouplikeItem(item, playNow))
     grouplikeListing.on('queue (at end)', item => this.queueGrouplikeItem(item))
     grouplikeListing.on('queue (shuffled)', item => this.shuffleQueueGrouplikeItem(item))
     grouplikeListing.on('queue (play next)', item => this.queueGrouplikeItem(item, true, this.playingTrack))
@@ -439,7 +443,7 @@ class AppElement extends FocusElement {
     this.player.kill()
   }
 
-  async queueGrouplikeItem(topItem, play = true, afterItem = null) {
+  async queueGrouplikeItem(topItem, play = playIfNothingElse, afterItem = null) {
     const newTrackIndex = this.queueGrouplike.items.length
 
     const recursivelyAddTracks = item => {
@@ -478,7 +482,10 @@ class AppElement extends FocusElement {
 
     // This is the first new track, if a group was queued.
     const newTrack = this.queueGrouplike.items[newTrackIndex]
-    if (play && !this.playingTrack && newTrack) {
+    if (
+      play === playNow ||
+      (play === playIfNothingElse && !this.playingTrack && newTrack)
+    ) {
       this.playGrouplikeItem(newTrack, false)
     }
   }
@@ -771,7 +778,7 @@ class GrouplikeListingElement extends FocusElement {
     if (this.grouplike.items.length) {
       for (const item of this.grouplike.items) {
         const itemElement = new GrouplikeItemElement(item, this.recordStore)
-        for (const evtName of ['download', 'remove (backspace)', 'remove (x)', 'mark', 'select (space)', 'select (enter)', 'queue (at end)', 'queue (shuffled)', 'queue (play next)', 'menu']) {
+        for (const evtName of ['download', 'remove (backspace)', 'remove (x)', 'mark', 'select (space)', 'select (enter)', 'play', 'queue (at end)', 'queue (shuffled)', 'queue (play next)', 'menu']) {
           itemElement.on(evtName, (...data) => this.emit(evtName, item, ...data))
         }
         form.addInput(itemElement)
@@ -962,9 +969,9 @@ class GrouplikeItemElement extends Button {
         x: this.absLeft,
         y: this.absTop + 1,
         items: [
-          'Play',
-          'Play next',
-          'Play at end',
+          {label: 'Play', action: () => this.emit('play')},
+          {label: 'Play next', action: () => this.emit('queue (play next)')},
+          {label: 'Play at end', action: () => this.emit('queue (at end)')}
         ]
       })
     }
@@ -1448,17 +1455,25 @@ class ContextMenu extends FocusElement {
   }
 
   show({x = 0, y = 0, items}) {
-    for (const input of this.form.inputs) {
-      this.form.removeInput(input)
-    }
+    const selectedBefore = this.root.selectedElement
+
+    this.clearItems()
 
     this.x = x
     this.y = y
     this.visible = true
 
     // TODO: Actions, that sorta thing
-    for (const item of items) {
-      this.form.addInput(new Button(item))
+    for (const { label, action } of items) {
+      const button = new Button(label)
+      if (action) {
+        button.on('pressed', () => {
+          this.root.select(selectedBefore)
+          this.close()
+          action()
+        })
+      }
+      this.form.addInput(button)
     }
 
     this.fixLayout()
@@ -1466,6 +1481,18 @@ class ContextMenu extends FocusElement {
     this.form.firstInput()
   }
 
+  close() {
+    this.clearItems()
+    this.visible = false
+  }
+
+  clearItems() {
+    for (const input of this.form.inputs) {
+      this.form.removeInput(input)
+    }
+  }
+
+
   fixLayout() {
     let width = 10
     for (const input of this.form.inputs) {