« get me outta code hell

WIP context menu - 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:18:02 -0300
committerFlorrie <towerofnix@gmail.com>2018-07-25 13:18:02 -0300
commit22af200d04d7f3b5fbfec2a61e6a272f0476e2af (patch)
tree8e94c3ff1450bfdc93a18d2522340aba4218e1d7
parent02c1649d64406b2225189b702fb09c0b48fac856 (diff)
WIP context menu
-rw-r--r--todo.txt2
m---------tui-lib0
-rw-r--r--ui.js77
3 files changed, 79 insertions, 0 deletions
diff --git a/todo.txt b/todo.txt
index 636a804..c042e74 100644
--- a/todo.txt
+++ b/todo.txt
@@ -87,3 +87,5 @@ TODO: Investigate performance issues with very very long ListScrollForms.
 
 TODO: At some point, putting mtui downloads in ~/.mtui - but preferrably with
       a more humanish structure - would be quite nice..!
+
+TODO: Press "M" to show a context menu.
diff --git a/tui-lib b/tui-lib
-Subproject 40431bcfd46c457a1cf271b5eae53a35aa1d0b6
+Subproject 07a667058b9f1ae12e07222b3058d0e2500dd3d
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