scripting: add setFocus(id)

This commit is contained in:
bordun 2021-03-01 10:41:55 +01:00
parent b5446c08ac
commit e9a829b323
4 changed files with 35 additions and 1 deletions

View file

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

View file

@ -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.

View file

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

View file

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