From 0cdf9cc05daa617960847ff1b9eb3b4afde74362 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 25 Jul 2018 13:43:48 -0300 Subject: Menu actions --- ui.js | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file 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) { -- cgit 1.3.0-6-gf8a5