From 876c3277b26fc0cbbef6bcce916b1c07f63046bd Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 20 Jan 2022 19:21:42 +0100 Subject: [PATCH] dom: batch dom operations that trigger layout calculation panel: store raw scroll when scrolling and use that value when the editor needs it (avoids many layout trashing) --- package.json | 5 +-- src/client/editor/data-workers.js | 8 ++-- src/client/events/resize.js | 55 +++++++++++++----------- src/client/ui/ui-dragresize.js | 45 ++++++++++++-------- src/client/ui/ui-inspector-field.js | 36 ++++++++++------ src/client/ui/ui-inspector.js | 13 ++++-- src/client/widgets/common/canvas.js | 14 +++--- src/client/widgets/containers/panel.js | 59 ++++++++++++++------------ 8 files changed, 137 insertions(+), 98 deletions(-) diff --git a/package.json b/package.json index 18218bcf..971f2fb3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "open-stage-control", "productName": "Open Stage Control", "description": "Libre and modular OSC / MIDI controller", - "version": "1.14.1", + "version": "1.14.2", "author": { "name": "Jean-Emmanuel Doucet", "email": "jean-emmanuel@ammd.net" @@ -37,14 +37,13 @@ "core-js": "3.11.0", "cpr": "3.0.1", "deep-extend": "0.6.0", - "electron": "15.1.0", - "electron-installer-debian": "3.1.0", "electron-localshortcut": "3.2.1", "electron-packager": "15.2.0", "electron-packager-plugin-non-proprietary-codecs-ffmpeg": "1.0.2", "env-paths": "2.2.1", "eslint": "7.24.0", "exorcist": "2.0.0", + "fastdom": "^1.0.10", "file-saver": "2.0.5", "gyronorm": "2.0.6", "http-auth": "4.1.5", diff --git a/src/client/editor/data-workers.js b/src/client/editor/data-workers.js index c82e215b..82701a21 100644 --- a/src/client/editor/data-workers.js +++ b/src/client/editor/data-workers.js @@ -77,7 +77,7 @@ function updateWidget(widget, options={}) { // save state if (stateManager.queueCounter === 0) { - scrollState = {} + scrollState = {} // scrollbale modals don't store their scroll state in their value } stateManager.incrementQueue() for (let w of removedWidgets) { @@ -89,8 +89,8 @@ function updateWidget(widget, options={}) { stateManager.pushValueState(id, value) if (valueProp !== '' && valueProp !== undefined) stateManager.pushValueOldProp(id, valueProp) } - if (widgetManager.scrollingWidgets.indexOf(w.hash) > -1 && w.scroll) { - var s = w.scroll() + if (w.getProp('scroll') && w.getProp('type') === 'modal') { + var s = w.getScroll() if (s[0] !== 0 || s[1] !== 0) { scrollState[w.getProp('id')] = s } @@ -142,7 +142,7 @@ function updateWidget(widget, options={}) { if (stateManager.queueCounter === 0) { for (let id in scrollState) { for (let w of widgetManager.getWidgetById(id)) { - if (w.scroll) w.scroll(scrollState[id]) + if (w.getProp('scroll')) w.setScroll(scrollState[id]) } } } diff --git a/src/client/events/resize.js b/src/client/events/resize.js index f7cf8f9a..e104bdf8 100644 --- a/src/client/events/resize.js +++ b/src/client/events/resize.js @@ -1,37 +1,42 @@ +var fastdom = require('fastdom') var elements = [] function checkResizes(context, force){ - // Iterate over all elements to which the 'resize' event is bound. - var resizedElems = [], - cachedStyles = [] - for (let i = elements.length - 1; i >= 0; i--) { - var elem = elements[i] + fastdom.measure(()=>{ + // Iterate over all elements to which the 'resize' event is bound. + let resizedElems = [], + cachedStyles = [] + for (let i = elements.length - 1; i >= 0; i--) { + var elem = elements[i] - if (!context.contains(elem)) continue + if (!context.contains(elem)) continue - var style = window.getComputedStyle(elem), - width = parseInt(style['width']) - parseInt(style['padding-left']) - parseInt(style['padding-right']), - height = parseInt(style['height']) - parseInt(style['padding-top']) - parseInt(style['padding-bottom']) + var style = window.getComputedStyle(elem), + width = parseInt(style['width']) - parseInt(style['padding-left']) - parseInt(style['padding-right']), + height = parseInt(style['height']) - parseInt(style['padding-top']) - parseInt(style['padding-bottom']) - if (!width || !height) continue + if (!width || !height) continue - // If element size has changed since the last time, update the element - // data store and trigger the 'resize' event. - if (force || width != elem.resizedataw || height != elem.resizedatah) { - elem.resizedataw = width - elem.resizedatah = height - resizedElems.push(elem) - cachedStyles.push(style) + // If element size has changed since the last time, update the element + // data store and trigger the 'resize' event. + if (force || width != elem.resizedataw || height != elem.resizedatah) { + elem.resizedataw = width + elem.resizedatah = height + resizedElems.push(elem) + cachedStyles.push(style) + } } - } - for (let i = resizedElems.length - 1; i >= 0; i--) { - resizedElems[i]._resize_widget.trigger('resize', { - width: resizedElems[i].resizedataw, - height: resizedElems[i].resizedatah, - style:cachedStyles[i], - stopPropagation: true + fastdom.mutate(()=>{ + for (let i = resizedElems.length - 1; i >= 0; i--) { + resizedElems[i]._resize_widget.trigger('resize', { + width: resizedElems[i].resizedataw, + height: resizedElems[i].resizedatah, + style:cachedStyles[i], + stopPropagation: true + }) + } }) - } + }) } diff --git a/src/client/ui/ui-dragresize.js b/src/client/ui/ui-dragresize.js index ee624325..3ee9c53f 100644 --- a/src/client/ui/ui-dragresize.js +++ b/src/client/ui/ui-dragresize.js @@ -1,6 +1,7 @@ var UiWidget = require('./ui-widget'), keyboardJS = require('keyboardjs/dist/keyboard.min.js'), html = require('nanohtml'), + fastdom = require('fastdom'), Tab, Root class UiDragResize extends UiWidget { @@ -154,9 +155,10 @@ class UiDragResize extends UiWidget { this.initWidth = this.width this.initHeight = this.height - this.widgets[0].parentNode.appendChild(this.container) - this.mounted = true - + fastdom.mutate(()=>{ + this.widgets[0].parentNode.appendChild(this.container) + this.mounted = true + }) } @@ -172,24 +174,33 @@ class UiDragResize extends UiWidget { ] handlesVisibility.push(handlesVisibility[1] && handlesVisibility[2]) // se - for (var i in handlesVisibility) { - this.handles[i].style.display = handlesVisibility[i] ? '' : 'none' - } + fastdom.measure(()=>{ - var lefts = widgets.map(w => w.container.offsetLeft), - tops = widgets.map(w => w.container.offsetTop), - rights = widgets.map(w => w.container.offsetLeft + w.container.offsetWidth), - bottoms = widgets.map(w => w.container.offsetTop + w.container.offsetHeight) + var lefts = widgets.map(w => w.container.offsetLeft), + tops = widgets.map(w => w.container.offsetTop), + rights = widgets.map(w => w.container.offsetLeft + w.container.offsetWidth), + bottoms = widgets.map(w => w.container.offsetTop + w.container.offsetHeight) - this.left = Math.min(...lefts) / PXSCALE - this.top = Math.min(...tops) / PXSCALE - this.width = (Math.max(...rights) - Math.min(...lefts)) / PXSCALE - this.height = (Math.max(...bottoms) - Math.min(...tops)) / PXSCALE + this.left = Math.min(...lefts) / PXSCALE + this.top = Math.min(...tops) / PXSCALE + this.width = (Math.max(...rights) - Math.min(...lefts)) / PXSCALE + this.height = (Math.max(...bottoms) - Math.min(...tops)) / PXSCALE - // this.cssTransform = widget.cssTransform || 'none' - // this.cssTransformOrigin = widget.cssTransformOrigin + fastdom.mutate(()=>{ + + for (var i in handlesVisibility) { + this.handles[i].style.display = handlesVisibility[i] ? '' : 'none' + } + + this.updateCss() + + // this.cssTransform = widget.cssTransform || 'none' + // this.cssTransformOrigin = widget.cssTransformOrigin + + }) + + }) - this.updateCss() } diff --git a/src/client/ui/ui-inspector-field.js b/src/client/ui/ui-inspector-field.js index c35f1951..b9d74d3c 100644 --- a/src/client/ui/ui-inspector-field.js +++ b/src/client/ui/ui-inspector-field.js @@ -11,7 +11,8 @@ var UiWidget = require('./ui-widget'), scriptGlobals = ScriptVm.globals, editors = {}, editorModes = { javascript: require('brace/mode/javascript') - } + }, + fastdom = require('fastdom') function createEditor(name, language, syntaxChecker) { var el = html`
` @@ -156,23 +157,34 @@ class UiInspectorField extends UiWidget { colorData = this.widget.constructor.cssVariables.find(x=>x.js === this.name), val = 'transparent' - try { - val = chroma(style.getPropertyValue(colorData.css).trim()).hex('rgba') - } catch(e) {} + fastdom.measure(()=>{ + try { + val = chroma(style.getPropertyValue(colorData.css).trim()).hex('rgba') + } catch(e) {} + fastdom.mutate(()=>{ + this.container.appendChild(html` + + `) + }) - let picker = html`` - - this.container.appendChild(picker) + }) } else if (stringValue === 'auto' && this.widget.constructor.cssVariables.some(x=>x.js === this.name)) { - let style = window.getComputedStyle(this.widget.container), - data = this.widget.constructor.cssVariables.find(x=>x.js === this.name), - val = style.getPropertyValue(data.css).trim() + fastdom.measure(()=>{ + let style = window.getComputedStyle(this.widget.container), + data = this.widget.constructor.cssVariables.find(x=>x.js === this.name), + val = style.getPropertyValue(data.css).trim() - if (data.toJs) val = data.toJs(val) + if (data.toJs) val = data.toJs(val) + fastdom.mutate(()=>{ + this.container.appendChild(html` +
${val}
+ `) + }) + + }) - this.container.appendChild(html`
${val}
`) } diff --git a/src/client/ui/ui-inspector.js b/src/client/ui/ui-inspector.js index d5f21aa2..a2d22638 100644 --- a/src/client/ui/ui-inspector.js +++ b/src/client/ui/ui-inspector.js @@ -5,7 +5,8 @@ var UiWidget = require('./ui-widget'), morph = require('nanomorph'), {defaults} = require('../widgets/'), html = require('nanohtml'), - raw = require('nanohtml/raw') + raw = require('nanohtml/raw'), + fastdom = require('fastdom') @@ -56,10 +57,16 @@ class UiInspector extends UiWidget { var name = node.getAttribute('name') + var textarea = DOM.get(this.container, `textarea[name="${name}"]`) if (textarea) { - textarea[0].value = !node.classList.contains('on') - DOM.dispatchEvent(textarea[0], 'change') + fastdom.measure(()=>{ + var bool = !node.classList.contains('on') + fastdom.mutate(()=>{ + textarea[0].value = bool + DOM.dispatchEvent(textarea[0], 'change') + }) + }) } } else if (node.tagName === 'OSC-INSPECTOR-COLOR') { diff --git a/src/client/widgets/common/canvas.js b/src/client/widgets/common/canvas.js index ab616669..c43cce39 100644 --- a/src/client/widgets/common/canvas.js +++ b/src/client/widgets/common/canvas.js @@ -1,7 +1,8 @@ var Widget = require('./widget'), resize = require('../../events/resize'), canvasQueue = require('./queue'), - html = require('nanohtml') + html = require('nanohtml'), + fastdom = require('fastdom') class Canvas extends Widget { @@ -69,11 +70,12 @@ class Canvas extends Widget { this.height = height this.width = width - this.canvas.setAttribute('width', width * ratio) - this.canvas.setAttribute('height', height * ratio) - - this.container.style.setProperty('--widget-height', this.height + 'rem') - this.container.style.setProperty('--widget-width', this.width + 'rem') + // fastdom.mutate(()=>{ + this.canvas.setAttribute('width', width * ratio) + this.canvas.setAttribute('height', height * ratio) + this.container.style.setProperty('--widget-height', this.height + 'rem') + this.container.style.setProperty('--widget-width', this.width + 'rem') + // }) this.parsersLocalScope.height = this.height this.parsersLocalScope.width = this.width diff --git a/src/client/widgets/containers/panel.js b/src/client/widgets/containers/panel.js index 0c3b0a84..b8bd5f69 100644 --- a/src/client/widgets/containers/panel.js +++ b/src/client/widgets/containers/panel.js @@ -134,28 +134,30 @@ class Panel extends Container() { } if (this.props.type !== 'modal') { - this.value = [0, 0] - this.scrollWidth = 1 - this.scrollHeight = 1 - setTimeout(()=>{ - this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth - this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight - }) this.scrollTimeout = null - this.widget.addEventListener('scroll', ()=>{ - this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth - this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight - var x = this.widget.scrollLeft / this.scrollWidth || 0, - y = this.widget.scrollTop / this.scrollHeight || 0 - if (iOS13) { - if (this.iosScrollbars.horizontal._scrollable) this.iosScrollbars.horizontal.setValue(x) - if (this.iosScrollbars.vertical._scrollable) this.iosScrollbars.vertical.setValue(y) - } - this.setValue([x, y], {sync: true, send:true}) - }) - } + this.scroll = [0, 0] + this.scrollWidth = 1 + this.scrollHeight = 1 + setTimeout(()=>{ + this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth + this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight + }) + this.widget.addEventListener('scroll', ()=>{ + this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth + this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight + this.scroll = [this.widget.scrollLeft, this.widget.scrollTop] + var x = this.widget.scrollLeft / this.scrollWidth || 0, + y = this.widget.scrollTop / this.scrollHeight || 0 + if (iOS13) { + if (this.iosScrollbars.horizontal._scrollable) this.iosScrollbars.horizontal.setValue(x) + if (this.iosScrollbars.vertical._scrollable) this.iosScrollbars.vertical.setValue(y) + } + if (this.props.type !== 'modal') { + this.setValue([x, y], {sync: true, send:true}) + } + }) } @@ -293,8 +295,7 @@ class Panel extends Container() { this.value = v clearTimeout(this.scrollTimeout) this.scrollTimeout = setTimeout(()=>{ - this.widget.scrollLeft = v[0] * this.scrollWidth - this.widget.scrollTop = v[1] * this.scrollHeight + this.setScroll(v[0] * this.scrollWidth, v[1] * this.scrollHeight) }) if (options.send) this.sendValue() @@ -302,14 +303,16 @@ class Panel extends Container() { } } - scroll(s) { + getScroll() { - if (!s) { - return [this.widget.scrollLeft, this.widget.scrollTop] - } else { - if (s[0] !== undefined) this.widget.scrollLeft = s[0] - if (s[1] !== undefined) this.widget.scrollTop = s[1] - } + return this.scroll + + } + + setScroll(x, y) { + + if (x !== undefined) this.widget.scrollLeft = this.scroll[0] = x + if (y !== undefined) this.widget.scrollTop = this.scroll[1] = y }