scripting: expose toolbar menu actions

This commit is contained in:
bordun 2020-09-10 09:02:08 +02:00
parent d853b5e38c
commit c017abae58
4 changed files with 41 additions and 2 deletions

View file

@ -1,5 +1,10 @@
# Changelog
## next
- scripting
- expose toolbar menu actions
## 1.2.0
- bug fixes

View file

@ -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`)

View file

@ -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)=>{

View file

@ -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])
}