From 22af200d04d7f3b5fbfec2a61e6a272f0476e2af Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 25 Jul 2018 13:18:02 -0300 Subject: WIP context menu --- todo.txt | 2 ++ tui-lib | 2 +- ui.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) 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 index 40431bc..07a6670 160000 --- a/tui-lib +++ b/tui-lib @@ -1 +1 @@ -Subproject commit 40431bcfd46c457a1cf271b5eae53a35aa1d0b6b +Subproject commit 07a667058b9f1ae12e07222b3058d0e2500dd3dc 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 -- cgit 1.3.0-6-gf8a5