From f6f56398e3476421fe637e6313bae72c5191eeba Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 19 Oct 2021 11:36:47 +0200 Subject: [PATCH] panel: expose scroll position in value if it contains widgets and scroll is true --- CHANGELOG.md | 5 + package.json | 2 +- resources/docs/docs/widgets/scripting.md | 10 +- src/client/widgets/containers/panel.js | 118 ++++++++++++++--------- 4 files changed, 89 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c17865a2..4144e4b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.11.0 + +- widgets + - when a panel contains widgets and has `scroll` set to `true`, its value will be used to represent the scroll position for x-axis and y-axis between 0 and 1. + ## 1.10.3 - bug fixes diff --git a/package.json b/package.json index 5c44af4d..9470f948 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.10.3", + "version": "1.11.0", "author": { "name": "Jean-Emmanuel Doucet", "email": "jean-emmanuel@ammd.net" diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index fec3bc39..50bde8af 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -234,7 +234,11 @@ Gives focus to a widget (ie input widget). #### `getScroll(id)` - `id`: widget `id` as a string. -Returns the scroll state of a container as a `[x, y]` array. +Returns the scroll position of a container as a `[x, y]` array. + +**Deprecated** + +Scrollable panels expose their scroll position normalized between 0 and 1 with their value. ---- @@ -245,6 +249,10 @@ Returns the scroll state of a container as a `[x, y]` array. Sets the scroll state of a container. +**Deprecated** + +Scrollable panels expose their scroll position normalized between 0 and 1 with their value. + ---- #### `toolbar(i1, i2, ...)` diff --git a/src/client/widgets/containers/panel.js b/src/client/widgets/containers/panel.js index 05fc3896..4b06961c 100644 --- a/src/client/widgets/containers/panel.js +++ b/src/client/widgets/containers/panel.js @@ -35,9 +35,9 @@ class Panel extends Container() { verticalTabs: {type: 'boolean', value: false, help: 'Set to `true` to display for vertical tab layout'}, }, value: { - value: {type: 'integer', value: '', help: [ - 'Defines currently selected tab in the widget', - 'A tab can be opened only by setting its parent\'s value' + value: {type: 'integer|array', value: '', help: [ + 'If the panel contains tabs, its value defines which tab is selected selected (by index, starting with 0).', + 'If the panel contains widgets and `scroll` is `true`, its value is an array that contains the scrolling state between 0 and 1 for the x and y axis. ' ]} }, children: { @@ -85,54 +85,72 @@ class Panel extends Container() { this.container.classList.add('contains-widgets') - if (iOS13 && this.getProp('scroll')) { + if (this.getProp('scroll')) { - faderDefaults = faderDefaults || Fader.defaults()._props() + if (iOS13) { - this.iosScrollbars = {} - for (let dir of ['vertical', 'horizontal']) { - this.iosScrollbars[dir] = new Fader({props:{ - ...faderDefaults, - design:'compact', - horizontal: dir === 'horizontal', - range: dir === 'horizontal' ? {min:0, max:1} : {min:1, max:0} - }, parent: this}) - this.iosScrollbars[dir].container.classList.add('not-editable') - this.iosScrollbars[dir].container.classList.add('ios-scrollbar') - this.iosScrollbars[dir].container.classList.add(dir) - this.iosScrollbars[dir]._scrollable = false - this.container.appendChild(this.iosScrollbars[dir].container) - } - this.widget.addEventListener('scroll', ()=>{ - if (this.iosScrollbars.horizontal._scrollable) this.iosScrollbars.horizontal.setValue(this.widget.scrollLeft / (this.widget.scrollWidth - this.widget.clientWidth)) - if (this.iosScrollbars.vertical._scrollable) this.iosScrollbars.vertical.setValue(this.widget.scrollTop / (this.widget.scrollHeight - this.widget.clientHeight)) - }) - this.iosScrollbars.horizontal.on('change', (e)=>{ - e.stopPropagation = true - this.widget.scrollLeft = parseInt(e.widget.getValue() * (this.widget.scrollWidth - this.widget.clientWidth)) - }) - this.iosScrollbars.vertical.on('change', (e)=>{ - e.stopPropagation = true - this.widget.scrollTop = parseInt(e.widget.getValue() * (this.widget.scrollHeight - this.widget.clientHeight)) - }) + faderDefaults = faderDefaults || Fader.defaults()._props() - this.checkScrollBars = ()=>{ - this.iosScrollbars.horizontal.container.style.setProperty('--knob-size', parseInt(this.widget.clientWidth * this.widget.clientWidth / this.widget.scrollWidth) + 'px') - this.iosScrollbars.vertical.container.style.setProperty('--knob-size', parseInt(this.widget.clientHeight * this.widget.clientHeight / this.widget.scrollHeight) + 'px') - this.iosScrollbars.horizontal._scrollable = this.container.classList.toggle('has-ios-scrollbar-h', this.widget.scrollWidth > this.widget.clientWidth) - this.iosScrollbars.vertical._scrollable = this.container.classList.toggle('has-ios-scrollbar-v', this.widget.scrollHeight > this.widget.clientHeight) - this.iosScrollbars.vertical.container.classList.toggle('double-scrollbar', this.iosScrollbars.horizontal._scrollable && this.iosScrollbars.vertical._scrollable) - if (this.iosScrollbars.horizontal._scrollable) resize.check(this.iosScrollbars.horizontal.container, true) - if (this.iosScrollbars.vertical._scrollable) resize.check(this.iosScrollbars.vertical.container, true) + this.iosScrollbars = {} + for (let dir of ['vertical', 'horizontal']) { + this.iosScrollbars[dir] = new Fader({props:{ + ...faderDefaults, + design:'compact', + horizontal: dir === 'horizontal', + range: dir === 'horizontal' ? {min:0, max:1} : {min:1, max:0} + }, parent: this}) + this.iosScrollbars[dir].container.classList.add('not-editable') + this.iosScrollbars[dir].container.classList.add('ios-scrollbar') + this.iosScrollbars[dir].container.classList.add(dir) + this.iosScrollbars[dir]._scrollable = false + this.container.appendChild(this.iosScrollbars[dir].container) + } + this.iosScrollbars.horizontal.on('change', (e)=>{ + e.stopPropagation = true + this.widget.scrollLeft = parseInt(e.widget.getValue() * (this.widget.scrollWidth - this.widget.clientWidth)) + }) + this.iosScrollbars.vertical.on('change', (e)=>{ + e.stopPropagation = true + this.widget.scrollTop = parseInt(e.widget.getValue() * (this.widget.scrollHeight - this.widget.clientHeight)) + }) + + this.checkScrollBars = ()=>{ + this.iosScrollbars.horizontal.container.style.setProperty('--knob-size', parseInt(this.widget.clientWidth * this.widget.clientWidth / this.widget.scrollWidth) + 'px') + this.iosScrollbars.vertical.container.style.setProperty('--knob-size', parseInt(this.widget.clientHeight * this.widget.clientHeight / this.widget.scrollHeight) + 'px') + this.iosScrollbars.horizontal._scrollable = this.container.classList.toggle('has-ios-scrollbar-h', this.widget.scrollWidth > this.widget.clientWidth) + this.iosScrollbars.vertical._scrollable = this.container.classList.toggle('has-ios-scrollbar-v', this.widget.scrollHeight > this.widget.clientHeight) + this.iosScrollbars.vertical.container.classList.toggle('double-scrollbar', this.iosScrollbars.horizontal._scrollable && this.iosScrollbars.vertical._scrollable) + if (this.iosScrollbars.horizontal._scrollable) resize.check(this.iosScrollbars.horizontal.container, true) + if (this.iosScrollbars.vertical._scrollable) resize.check(this.iosScrollbars.vertical.container, true) - this.iosScrollbars.horizontal.batchDraw() + this.iosScrollbars.horizontal.batchDraw() + } + + this.on('resize', (event)=>{ + this.checkScrollBars() + }, {element: this.widget}) + } - this.on('resize', (event)=>{ - this.checkScrollBars() - }, {element: this.widget}) + if (this.props.type !== 'modal') { + this.scrollWidth = 1 + this.scrollHeight = 1 + 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, + y = this.widget.scrollTop / this.scrollHeight + 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}) + }) + + } } @@ -250,7 +268,8 @@ class Panel extends Container() { } setValue(v, options={}) { - if (this.tabs.length && typeof v == 'number' && v >= 0 && v < this.tabs.length) { + + if (this.childrenType === 'tab' && typeof v == 'number' && v >= 0 && v < this.tabs.length) { for (let i in this.tabs) { if (i != v) this.tabs[i].hide() @@ -264,6 +283,17 @@ class Panel extends Container() { if (options.send) this.sendValue() if (options.sync) this.changed(options) + } else if (this.childrenType === 'widget' && Array.isArray(v) && v.length === 2 && this.props.type !== 'modal') { + + this.value = v + clearTimeout(this.scrollTimeout) + this.scrollTimeout = setTimeout(()=>{ + this.widget.scrollLeft = v[0] * this.scrollWidth + this.widget.scrollTop = v[1] * this.scrollHeight + }) + + if (options.send) this.sendValue() + if (options.sync) this.changed(options) } }