From 8ff8350998fe37e2ee617230370382187bf8dda5 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 25 Jul 2018 14:17:05 -0300 Subject: Esc/backspace to close a menu --- ui.js | 15 +++++++++++++-- 1 file 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() { -- cgit 1.3.0-6-gf8a5