« get me outta code hell

Esc/backspace to close a 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 14:17:05 -0300
committerFlorrie <towerofnix@gmail.com>2018-07-25 14:17:05 -0300
commit8ff8350998fe37e2ee617230370382187bf8dda5 (patch)
tree3f7e8b3df2a36a79ee3abf4c41882ac465f83c2c
parente90f95077be409333d0e7d67c84c84352245fae2 (diff)
Esc/backspace to close a menu
-rw-r--r--ui.js15
1 files changed, 13 insertions, 2 deletions
diff --git a/ui.js b/ui.js
index e6ae644..7b1d330 100644
--- a/ui.js
+++ b/ui.js
@@ -1461,7 +1461,10 @@ class ContextMenu extends FocusElement {
   }
 
   show({x = 0, y = 0, items}) {
-    const selectedBefore = this.root.selectedElement
+    // This *should* work with a menu action which opens the menu again,
+    // because the selected element will be restored before the menu is
+    // opened the second time.
+    this.selectedBefore = this.root.selectedElement
 
     this.clearItems()
 
@@ -1474,7 +1477,6 @@ class ContextMenu extends FocusElement {
       const button = new Button(label)
       if (action) {
         button.on('pressed', () => {
-          this.root.select(selectedBefore)
           this.close()
           action()
         })
@@ -1487,9 +1489,18 @@ class ContextMenu extends FocusElement {
     this.form.firstInput()
   }
 
+  keyPressed(keyBuf) {
+    if (telc.isEscape(keyBuf) || telc.isBackspace(keyBuf)) {
+      this.close()
+    } else {
+      super.keyPressed(keyBuf)
+    }
+  }
+
   close() {
     this.clearItems()
     this.visible = false
+    this.root.select(this.selectedBefore)
   }
 
   clearItems() {