« get me outta code hell

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:
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 20f0167..7dbb05b 100644
--- a/ui.js
+++ b/ui.js
@@ -87,6 +87,16 @@ class AppElement extends FocusElement {
 
     this.alertDialog = new AlertDialog()
     this.setupDialog(this.alertDialog)
+
+    /* Ignore this comment mostly :)  (Because menu isn't a child of pane,
+       so we can append it to the app right away. Helps w/ handling ^C and
+       stuff too.)
+    // If the program were embedded, this.menu should probably be set to the
+    // global menu object for that app (and everything should work fine).
+    // As is, remember to append app.menu to root.
+    */
+    this.menu = new ContextMenu()
+    this.addChild(this.menu)
   }
 
   selected() {
@@ -348,6 +358,16 @@ class AppElement extends FocusElement {
       this.tabber.nextTab()
     } else if (this.tabber.isSelected && keyBuf.equals(Buffer.from(['T'.charCodeAt(0)]))) {
       this.tabber.previousTab()
+    } else if (telc.isCharacter(keyBuf, 'm')) {
+      this.menu.show({
+        x: this.w - 10, y: this.h + 30,
+        items: [
+          'Nice.',
+          'Oh yeah.',
+          'Ooooh yeah.',
+          'N' + 'i'.repeat(200) + 'ce.'
+        ]
+      })
     } else {
       super.keyPressed(keyBuf)
     }
@@ -1413,4 +1433,61 @@ class TabberListItem extends FocusElement {
   }
 }
 
+class ContextMenu extends FocusElement {
+  constructor() {
+    super()
+
+    this.pane = new Pane()
+    this.addChild(this.pane)
+
+    this.form = new ListScrollForm()
+    this.pane.addChild(this.form)
+
+    this.visible = false
+  }
+
+  show({x = 0, y = 0, items}) {
+    for (const input of this.form.inputs) {
+      this.form.removeInput(input)
+    }
+
+    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))
+    }
+
+    this.fixLayout()
+
+    this.form.firstInput()
+  }
+
+  fixLayout() {
+    let width = 10
+    for (const input of this.form.inputs) {
+      input.fixLayout()
+      width = Math.max(width, input.w)
+    }
+
+    let height = Math.min(10, this.form.inputs.length)
+
+    width += 2 // Space for the pane border
+    height += 2
+    this.w = width
+    this.h = height
+
+    this.fitToParent()
+
+    this.pane.fillParent()
+    this.form.fillParent()
+  }
+
+  selected() {
+    this.root.select(this.form)
+  }
+}
+
 module.exports.AppElement = AppElement