scripting: add getToolbar() and make toolbar() accept string id

This commit is contained in:
jean-emmanuel 2025-12-10 09:50:05 +01:00
parent 67669d4ff2
commit fab78f98a4
4 changed files with 119 additions and 33 deletions

View file

@ -25,6 +25,8 @@
- scripting
- user scripts are longer protected against infinite loop and the main window object may be reached (it's not supported to break out of scripting context, but resources are no longer wasted to prevent it)
- `toolbar()` can now target menu actions by id instead of index
- `getToolbar()` function added (returns the state of a menu entry toggle)
- midi
- add `no_auto_note_off` option

View file

@ -347,19 +347,39 @@ Scrollable panels expose their scroll position normalized between 0 and 1 with t
----
#### `toolbar(i1, i2, ...)`
- `iX`: menu entry index
#### `toolbar(id)`
- `id` menu entry id (string)
Triggers toolbar action at specified index.
Triggers toobar action if currently available (ie accessible from the menu). `id` can be one of the following:
!!! example "Examples"
- `session-new`,`session-open`, `session-save`, `session-save-as`, `session-fragment`, `session-backup`, `session-import`, `session-export`
- `state-store`, `state-recall`, `state-send`, `state-open`, `state-save`, `state-save-as`, `state-import`, `state-export`
- `editor-enabled`, `editor-grid`, `editor-tree`, `editor-inspector`, `editor-percents`
- `console-enabled`, `console-clear`
- `fullscreen`, `notifictions`, `prevent-sleep`, `virtual-keyboard`
Some actions will only be triggered if initiated with a physical user interaction, check the console for errors if it doesn't work as expected.
=== "Legacy syntax: `toolbar(i1, i2, ...)`"
- `iX`: menu entry index
Triggers toolbar action at specified index.
!!! example "Examples"
- `toolbar(0, 0)` -> Open a new session
- `toolbar(4)` -> Toggle full screen
Some actions will only be triggered if initiated with a physical user interaction, check the console for errors if it doesn't work as expected.
----
#### `getToolbar(id)`
- `id` menu entry id (string)
Returns `true` if the toolbar entry exists and is currently activated.
#### `openUrl(url)`
- `url`: http(s) url

View file

@ -23,6 +23,7 @@ var menuEntries = [
{
label: locales('session_new'),
action: sessionManager.create.bind(sessionManager),
id: 'session-new'
},
{
separator: true
@ -31,6 +32,7 @@ var menuEntries = [
label: locales('file_open'),
action: sessionManager.browse.bind(sessionManager),
shortcut: 'mod + o',
id: 'session-open'
},
{
label: locales('file_open_recent'),
@ -44,18 +46,21 @@ var menuEntries = [
label: locales('file_save'),
action: sessionManager.save.bind(sessionManager),
shortcut: 'mod + s',
class: ()=>{return sessionManager.session === null ? 'disabled' :''}
class: ()=>{return sessionManager.session === null ? 'disabled' :''},
id: 'session-save'
},
{
label: locales('file_save_as'),
action: sessionManager.saveAs.bind(sessionManager),
shortcut: 'mod + shift + s',
class: ()=>{return sessionManager.session === null ? 'disabled' :''}
class: ()=>{return sessionManager.session === null ? 'disabled' :''},
id: 'session-save-as'
},
{
label: locales('fragment_mode'),
class: ()=>{return 'toggle ' + (sessionManager.saveMode === 'fragment' ? 'on' : 'off')},
action: ()=>{sessionManager.setSaveMode(sessionManager.saveMode === 'session' ? 'fragment' : 'session')}
action: ()=>{sessionManager.setSaveMode(sessionManager.saveMode === 'session' ? 'fragment' : 'session')},
id: 'session-fragment'
},
{
separator: true
@ -64,7 +69,8 @@ var menuEntries = [
label: locales('file_backup'),
action: sessionManager.saveBackup.bind(sessionManager),
shortcut: 'mod + b',
class: ()=>{return sessionManager.session === null || !sessionManager.sessionPath ? 'disabled' :''}
class: ()=>{return sessionManager.session === null || !sessionManager.sessionPath ? 'disabled' :''},
id: 'session-backup'
},
{
separator: true
@ -72,11 +78,13 @@ var menuEntries = [
{
label: locales('file_import'),
action: sessionManager.import.bind(sessionManager),
id: 'session-import'
},
{
label: locales('file_export'),
action: sessionManager.export.bind(sessionManager),
class: ()=>{return sessionManager.session === null ? 'disabled' :''}
class: ()=>{return sessionManager.session === null ? 'disabled' :''},
id: 'session-export'
},
]
@ -88,12 +96,14 @@ var menuEntries = [
action: [
{
label: locales('state_store'),
action: stateManager.quickSave.bind(stateManager)
action: stateManager.quickSave.bind(stateManager),
id: 'state-store'
},
{
label: locales('state_recall'),
class: ()=>{return stateManager.quickState === null ? 'disabled' : ''},
action: stateManager.quickLoad.bind(stateManager)
action: stateManager.quickLoad.bind(stateManager),
id: 'state-recall'
},
{
label: locales('state_send'),
@ -103,7 +113,8 @@ var menuEntries = [
icon: 'sliders-h',
message: locales('state_sendsuccess')
})
}
},
id: 'state-send'
},
{
separator: true
@ -111,14 +122,17 @@ var menuEntries = [
{
label: locales('file_open'),
action: stateManager.browse.bind(stateManager),
id: 'state-open'
},
{
label: locales('file_save'),
action: stateManager.save.bind(stateManager),
id: 'state-save'
},
{
label: locales('file_save_as'),
action: stateManager.saveAs.bind(stateManager),
id: 'state-save-as'
},
{
separator: true
@ -126,10 +140,12 @@ var menuEntries = [
{
label: locales('file_import'),
action: stateManager.import.bind(stateManager),
id: 'state-import'
},
{
label: locales('file_export'),
action: stateManager.export.bind(stateManager),
id: 'state-export'
},
]
},
@ -145,7 +161,8 @@ var menuEntries = [
if (editor.enabled) editor.disable()
else editor.enable()
},
shortcut: 'mod + e'
shortcut: 'mod + e',
id: 'editor-enabled'
},
{
separator: true
@ -154,22 +171,22 @@ var menuEntries = [
label: locales('editor_grid'),
class: ()=>{return 'toggle ' + (editor.grid ? 'on' : 'off')},
action: editor.toggleGrid.bind(editor),
shortcut: 'mod + g'
shortcut: 'mod + g',
id: 'editor-grid'
},
{
label: locales('editor_tree'),
class: ()=>{return 'toggle ' + (leftUiSidePanel.minimized ? 'off' : 'on')},
action: ()=>{return leftUiSidePanel.minimized ? leftUiSidePanel.restore() : leftUiSidePanel.minimize()},
shortcut: 'mod + t'
shortcut: 'mod + t',
id: 'editor-tree'
},
{
label: locales('editor_inspector'),
class: ()=>{return 'toggle ' + (rightUiSidePanel.minimized ? 'off' : 'on')},
action: ()=>{return rightUiSidePanel.minimized ? rightUiSidePanel.restore() : rightUiSidePanel.minimize()},
shortcut: 'mod + i'
shortcut: 'mod + i',
id: 'editor-inspector'
},
{
separator: true
@ -177,7 +194,8 @@ var menuEntries = [
{
label: locales('editor_percents'),
class: ()=>{return 'toggle ' + (editor.usePercents ? 'on' : 'off')},
action: ()=>{editor.usePercents = !editor.usePercents}
action: ()=>{editor.usePercents = !editor.usePercents},
id: 'editor-percents'
}
]
},
@ -196,7 +214,8 @@ var menuEntries = [
uiConsole.minimize()
}
},
shortcut: 'mod + k'
shortcut: 'mod + k',
id: 'console-enabled'
},
{
separator: true
@ -204,7 +223,9 @@ var menuEntries = [
{
label: locales('console_clear'),
action: uiConsole.clear.bind(uiConsole),
shortcut: 'mod + l'
shortcut: 'mod + l',
id: 'console-clear'
},
]
},
@ -218,14 +239,16 @@ var menuEntries = [
action: ()=>{
if (fullscreen.isEnabled) fullscreen.toggle()
},
shortcut: 'f11'
shortcut: 'f11',
id: 'fullscreen'
},
{
label: locales('notifications'),
class: ()=>{return 'toggle ' + (notifications.visible ? 'on' : 'off')},
action: ()=>{
notifications.setVisibility(!notifications.visible)
}
},
id: 'notifications'
}
]
@ -264,7 +287,8 @@ if (device.is('mobile') || device.is('tablet')) {
noSleep.disable()
}
}
}
},
id: 'prevent-sleep'
})
} else {
@ -274,7 +298,8 @@ if (device.is('mobile') || device.is('tablet')) {
class: ()=>{return 'toggle ' + (VIRTUAL_KEYBOARD ? 'on' : 'off')},
action: ()=>{
setVIRTUAL_KEYBOARD(!VIRTUAL_KEYBOARD)
}
},
id: 'virtual-keyboard'
})
}

View file

@ -476,6 +476,19 @@ class ScriptVm extends Vm {
}
function getToolbarActionById(id, entries) {
for (var a of entries) {
if (a.id == id) {
return a
}
if (Array.isArray(a.action)) {
var ret = getToolbarActionById(id, a.action)
if (ret) return ret
}
}
}
this.sandbox.contentWindow.toolbar = (...args)=>{
this.checkContext('toolbar')
@ -486,16 +499,42 @@ class ScriptVm extends Vm {
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 args[0] == 'string') {
action = getToolbarActionById(args[0], action)
if (action) action = action.action // get callback
if (action && typeof action.class == 'function') {
if (action.class().match(/disabled/)) action = null
}
} else {
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()
}
this.sandbox.contentWindow.getToolbar = (id)=>{
this.checkContext('toolbar')
var options = this.getValueOptions()
if (!options.send) return
var action = getToolbarActionById(id, toolbar.entries.filter(x=>!x.separator))
if (action && typeof action.class == 'function') return action.class().split(' ').includes('on')
}
this.sandbox.contentWindow.openUrl = (url)=>{
this.checkContext('openUrl')
@ -591,8 +630,8 @@ class ScriptVm extends Vm {
this.globals = {}
for (var imports of ['set', 'get', 'getProp', 'getIndex', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage',
'setInterval', 'clearInterval', 'setTimeout', 'cthislearTimeout', 'setFocus', 'unfocus' , 'setFocusable', 'setScroll', 'getScroll', 'toolbar',
'openUrl', 'getVar', 'setVar', 'runAs', 'reload', 'Image', 'updateCanvas', 'getNavigator', 'browseFile']) {
'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'setFocus', 'unfocus' , 'setFocusable', 'setScroll', 'getScroll', 'toolbar',
'getToolbar', 'openUrl', 'getVar', 'setVar', 'runAs', 'reload', 'Image', 'updateCanvas', 'getNavigator', 'browseFile']) {
this.globals[imports] = true
}