« get me outta code hell

Shift+up/down to select multiple items at once - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/ui.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-08-25 14:01:02 -0300
committerFlorrie <towerofnix@gmail.com>2019-08-25 14:01:02 -0300
commitf5ec52beb8afd5490ed854fddf6f5744dc31fe83 (patch)
tree4a990297b8921debe08634beb36da54c646033b0 /ui.js
parent0021f66a44f5d851841249f6b74ca2f4acaa6e10 (diff)
Shift+up/down to select multiple items at once
Dragging works too, as implemented earlier.
Diffstat (limited to 'ui.js')
-rw-r--r--ui.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/ui.js b/ui.js
index 13f115b..0d17fb8 100644
--- a/ui.js
+++ b/ui.js
@@ -79,6 +79,8 @@ const keyBindings = [
   ['isClearQueue', 'c'],
   ['isFocusMenubar', ';'],
   ['isFocusLabels', 'l'], // *** conflicts with isToggleLoop!!! must change this
+  ['isSelectUp', telc.isShiftUp],
+  ['isSelectDown', telc.isShiftDown],
 
   // Number pad
   ['isUp', '8'],
@@ -1204,6 +1206,8 @@ class GrouplikeListingElement extends Form {
   }
 
   selectNone() {
+    // nb: this is unrelated to the actual track selection system!
+    // just clears the form selection
     this.pathElement.showItem(null)
     this.form.curIndex = 0
     this.form.scrollItems = 0
@@ -1413,9 +1417,24 @@ class GrouplikeListingForm extends ListScrollForm {
 
     this.app = app
     this.dragInputs = []
+    this.selectMode = null
+    this.keyboardDragDirection = null
     this.captureTab = false
   }
 
+  keyPressed(keyBuf) {
+    if (input.isSelectUp(keyBuf)) {
+      this.selectUp()
+    } else if (input.isSelectDown(keyBuf)) {
+      this.selectDown()
+    } else {
+      if (telc.isUp(keyBuf) || telc.isDown(keyBuf)) {
+        this.keyboardDragDirection = null
+      }
+      return super.keyPressed(keyBuf)
+    }
+  }
+
   set curIndex(newIndex) {
     this._curIndex = newIndex
     this.emit('selected input', this.inputs[this.curIndex])
@@ -1521,6 +1540,64 @@ class GrouplikeListingForm extends ListScrollForm {
       }
     }
   }
+
+  selectUp() {
+    this.handleKeyboardSelect(-1)
+  }
+
+  selectDown() {
+    this.handleKeyboardSelect(+1)
+  }
+
+  handleKeyboardSelect(direction) {
+    const move = () => {
+      if (direction === +1) {
+        this.nextInput()
+      } else {
+        this.previousInput()
+      }
+    }
+
+    const getItem = () => {
+      const input = this.inputs[this.curIndex]
+      if (input instanceof InteractiveGrouplikeItemElement) {
+        return input.item
+      } else {
+        return null
+      }
+    }
+
+    if (!this.keyboardDragDirection) {
+      const item = getItem()
+      if (!item) {
+        return
+      }
+      this.keyboardDragDirection = direction
+      this.oldMarkedItems = this.app.markGrouplike.items.slice()
+      if (this.app.markGrouplike.items.includes(item)) {
+        this.selectMode = 'deselect'
+      } else {
+        this.selectMode = 'select'
+      }
+      this.dragEnteredRange(item)
+    }
+
+    if (direction === this.keyboardDragDirection) {
+      move()
+      const item = getItem()
+      if (!item) {
+        return
+      }
+      this.dragEnteredRange(item)
+    } else {
+      const item = getItem()
+      if (!item) {
+        return
+      }
+      this.dragLeftRange(item)
+      move()
+    }
+  }
 }
 
 class BasicGrouplikeItemElement extends Button {