From ffc338260956b1a44eaa6524210906d9d92209b9 Mon Sep 17 00:00:00 2001 From: bordun Date: Thu, 10 Sep 2020 09:02:08 +0200 Subject: [PATCH] scripting: expose toolbar menu actions --- CHANGELOG.md | 5 +++++ resources/docs/docs/widgets/scripting.md | 12 ++++++++++++ src/client/ui/ui-toolbar.js | 1 + src/client/widgets/scripts/script-vm.js | 25 ++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca9f03ce..1f8d6bca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## next + +- scripting + - expose toolbar menu actions + ## 1.2.0 - bug fixes diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 0864b8a3..63829cc6 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -146,3 +146,15 @@ Returns the scroll state of a container as a `[x, y]` array. - `y`: vertical scroll, `undefined` to use current value Sets the scroll state of a container. + +---- + +#### `toolbar(i1, i2, ...)` +- `iX`: menu entry index + +Trigger toolbar action at specified index. Examples: + +- `toolbar(0, 0)` -> Open a new session +- `toolbar(3)` -> Toggle full screen + +Actions will only be triggered if initiated with a user interaction. Fullscreen cannot be toggled with a simulated interaction (i.e. using `/SET`) diff --git a/src/client/ui/ui-toolbar.js b/src/client/ui/ui-toolbar.js index d6962716..95fb7b8f 100644 --- a/src/client/ui/ui-toolbar.js +++ b/src/client/ui/ui-toolbar.js @@ -215,6 +215,7 @@ class UiToolbar extends UiWidget { position: [40, 1] }) this.opened = false + this.entries = menuEntries this.menu.bindShortcuts(menuEntries) ipc.on('sessionList', (data)=>{ diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index d45664cd..0c1a1452 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -3,7 +3,8 @@ var widgetManager = require('../../managers/widgets'), cache = require('../../managers/cache'), {deepCopy} = require('../../utils'), {urlParser} = require('../utils'), - Vm = require('../vm') + Vm = require('../vm'), + toolbar class ScriptVm extends Vm { @@ -312,8 +313,28 @@ class ScriptVm extends Vm { } + this.sandbox.contentWindow.toolbar = (...args)=>{ + + var options = this.getValueOptions() + + if (!options.send) return + + toolbar = toolbar || require('../../ui/ui-toolbar') + + var action = toolbar.entries.filter(x=>!x.separator) + + for (var i of args) { + if (action[i]) action = action[i].action + if (!Array.isArray(action)) break + action = action.filter(x=>!x.separator) + } + + if (typeof action === 'function') action() + + } + for (var imports of ['set', 'get', 'getProp', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', - 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'unfocus', 'setScroll', 'getScroll']) { + 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'unfocus', 'setScroll', 'getScroll', 'toolbar']) { this.sanitize(this.sandbox.contentWindow[imports]) }