diff --git a/CHANGELOG.md b/CHANGELOG.md index 71d227a8..c754d7fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - bug fixes - modal: issues with children's visibility +- scripting + - add `setFocus` function to focus an input widget programmatically + # 1.8.9 - bug fixes diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 9f0a3a75..0f44424a 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -170,6 +170,16 @@ Reference: ---- +#### `setFocus(id)` + +- `id` (optional): widget `id` as a string + +Give focus to a widget (ie input widget). + +*Built-in client only*: tells the operating system to give the focus to the client window + +---- + #### `unfocus()` *Built-in client only*: tells the operating system to give the focus to the window that was focused before. diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index e896534d..007a9b22 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -326,8 +326,28 @@ class ScriptVm extends Vm { } + + this.sandbox.contentWindow.setFocus = (id)=>{ + + if (id) { + var widgets = this.resolveId(id) + + if (widgets.length) { + setTimeout(()=>{ + + widgets[0].widget.focus() + }) + } + } + + // built-in client only: electron will call window.focus() + console.debug('ELECTRON.FOCUS()') + + } + this.sandbox.contentWindow.unfocus = ()=>{ + window.blur() // built-in client only: electron will call window.blur() console.debug('ELECTRON.BLUR()') @@ -354,7 +374,7 @@ class ScriptVm extends Vm { } for (var imports of ['set', 'get', 'getProp', 'getIndex', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', - 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'unfocus', 'setScroll', 'getScroll', 'toolbar']) { + 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'setFocus', 'unfocus', 'setScroll', 'getScroll', 'toolbar']) { this.sanitize(this.sandbox.contentWindow[imports]) } diff --git a/src/server/electron-window.js b/src/server/electron-window.js index 0c76e3a4..f605d6ae 100644 --- a/src/server/electron-window.js +++ b/src/server/electron-window.js @@ -101,6 +101,7 @@ module.exports = function(options={}) { window.webContents.on('console-message', (event, level, message)=>{ if (level === 0 && message === 'ELECTRON.BLUR()') window.blur() + if (level === 0 && message === 'ELECTRON.FOCUS()') window.focus() })