« get me outta code hell

Ctrl+T, ctrl+W; new tabs open adjacent to current tab - 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>2018-07-05 10:01:17 -0300
committerFlorrie <towerofnix@gmail.com>2018-07-05 10:01:17 -0300
commitfe4db4e4f093697b8b35cd63c80c16695fedb2ca (patch)
treee3c363e95ec9a296b0c3145e3657e83c95d06aff /ui.js
parent6e4e3fb76cbceb34338c62fb08394ffdffcdc687 (diff)
Ctrl+T, ctrl+W; new tabs open adjacent to current tab
Diffstat (limited to 'ui.js')
-rw-r--r--ui.js43
1 files changed, 38 insertions, 5 deletions
diff --git a/ui.js b/ui.js
index 3e04f85..4228650 100644
--- a/ui.js
+++ b/ui.js
@@ -148,7 +148,7 @@ class AppElement extends FocusElement {
 
     grouplike = await processSmartPlaylist(grouplike)
 
-    if (newTab) {
+    if (newTab || !this.tabber.currentElement) {
       const grouplikeListing = this.newGrouplikeListing()
       grouplikeListing.loadGrouplike(grouplike)
     } else {
@@ -242,6 +242,10 @@ class AppElement extends FocusElement {
       this.form.updateSelectedElement()
     } else if (keyBuf.equals(Buffer.from([15]))) { // ctrl-O
       this.openPlaylistDialog.open()
+    } else if (keyBuf.equals(Buffer.from([20]))) { // ctrl-T
+      this.cloneCurrentTab()
+    } else if (keyBuf.equals(Buffer.from([23]))) { // ctrl-W
+      this.tabber.closeTab(this.tabber.currentElement)
     } else if (this.tabber.isSelected && keyBuf.equals(Buffer.from(['t'.charCodeAt(0)]))) {
       this.tabber.nextTab()
     } else if (this.tabber.isSelected && keyBuf.equals(Buffer.from(['T'.charCodeAt(0)]))) {
@@ -251,6 +255,12 @@ class AppElement extends FocusElement {
     }
   }
 
+  cloneCurrentTab() {
+    const grouplike = this.tabber.currentElement.grouplike
+    const listing = this.newGrouplikeListing()
+    listing.loadGrouplike(grouplike)
+  }
+
   shuffleQueue() {
     const queue = this.queueGrouplike
     const index = queue.items.indexOf(this.playingTrack) + 1 // This is 0 if no track is playing
@@ -1092,8 +1102,8 @@ class Tabber extends FocusElement {
   }
 
   addTab(element) {
-    this.tabberElements.push(element)
-    this.addChild(element)
+    this.tabberElements.splice(this.currentElementIndex + 1, 0, element)
+    this.addChild(element, this.currentElementIndex + 1)
     this.listElement.buildItems()
   }
 
@@ -1116,6 +1126,25 @@ class Tabber extends FocusElement {
     this.updateVisibleElement()
   }
 
+  closeTab(element) {
+    if (!this.tabberElements.includes(element)) {
+      return
+    }
+
+    const index = this.tabberElements.indexOf(element)
+    this.tabberElements.splice(index, 1)
+    if (index <= this.currentElementIndex) {
+      this.currentElementIndex--
+    }
+
+    // Deliberately update the visible element before removing the child. If we
+    // remove the child first, the isSelected in updateVisibleElement will be
+    // false, so the new currentElement won't actually be root.select()'ed.
+    this.updateVisibleElement()
+    this.removeChild(element)
+    this.listElement.buildItems()
+  }
+
   updateVisibleElement() {
     const len = this.tabberElements.length - 1
     this.currentElementIndex = Math.min(len, Math.max(0, this.currentElementIndex))
@@ -1124,8 +1153,12 @@ class Tabber extends FocusElement {
       el.visible = (i === this.currentElementIndex)
     })
 
-    if (this.isSelected && this.currentElement) {
-      this.root.select(this.currentElement)
+    if (this.isSelected) {
+      if (this.currentElement) {
+        this.root.select(this.currentElement)
+      } else {
+        this.root.select(this)
+      }
     }
 
     this.fixLayout()