widgets: add scrollbar widget

This commit is contained in:
jean-emmanuel 2025-09-21 13:22:07 +02:00
parent de62a7ca0e
commit a95697045a
5 changed files with 199 additions and 8 deletions

View file

@ -112,6 +112,7 @@ class Panel extends Container() {
}
this.scroll = [0, 0]
this.scrollThumb = [1, 1]
this.scrollWidth = 1
this.scrollHeight = 1
this.settingScroll = false
@ -119,11 +120,13 @@ class Panel extends Container() {
fastdom.measure(()=>{
this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth
this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight
this.scrollThumb = [this.widget.clientWidth / this.widget.scrollWidth || 1, this.widget.clientHeight / this.widget.scrollHeight || 1]
})
this.on('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]
this.scroll = [this.widget.scrollLeft / this.scrollWidth || 0, this.widget.scrollTop / this.scrollHeight || 0]
this.scrollThumb = [this.widget.clientWidth / this.widget.scrollWidth || 1, this.widget.clientHeight / this.widget.scrollHeight || 1]
if (this.settingScroll) {
clearTimeout(this.settingScrollEnd)
this.settingScrollEnd = setTimeout(()=>{
@ -133,8 +136,8 @@ class Panel extends Container() {
this.scripts.onScroll.run({
value: this.value,
event: {
x: this.widget.scrollLeft / this.scrollWidth || 0,
y: this.widget.scrollTop / this.scrollHeight || 0
x: this.scroll[0],
y: this.scroll[1]
}
}, {sync: true, send: true})
}

View file

@ -45,6 +45,7 @@ import fader from './sliders/fader.mjs'
import knob from './sliders/knob.mjs'
import encoder from './sliders/encoder.mjs'
import range from './sliders/range.mjs'
import scrollbar from './sliders/scrollbar.mjs'
// scripts
import script from './scripts/script-widget.mjs'
@ -100,6 +101,7 @@ export const widgets = {
knob,
encoder,
range,
scrollbar,
// scripts
script,
@ -114,7 +116,7 @@ export const categories = {
'Graphs':['plot','eq','visualizer'],
'Indicators':['led', 'text'],
'Pads':['xy','rgb','multixy', 'canvas'],
'Sliders':['fader','knob', 'encoder', 'range'],
'Sliders':['fader','knob', 'encoder', 'range', 'scrollbar'],
'Scripts':['script', 'variable'],
}

View file

@ -119,9 +119,9 @@ class Fader extends Slider {
}
shouldDrag(e) {
shouldDrag(e, testZone) {
var zone = this.getProp('touchZone'),
var zone = testZone || this.getProp('touchZone'),
design = this.getProp('design'),
horizontal = this.getProp('horizontal'),
snap = this.getProp('snap')
@ -309,7 +309,7 @@ class Fader extends Slider {
// flat knob
this.ctx.globalAlpha = 1
this.ctx.globalAlpha = this.cssVars.alphaKnob || 1
this.ctx.fillStyle = this.cssVars.colorKnob
this.ctx.beginPath()

View file

@ -0,0 +1,184 @@
import Fader from './fader.mjs'
import StaticProperties from '../mixins/static_properties.mjs'
import {clip} from '../utils.mjs'
import widgetManager from '../../managers/widgets.mjs'
export default class Scrollbar extends StaticProperties(Fader, {
range: {min: 0, max: 1},
touchZone: 'all',
alphaFillOn: 0,
snap: false,
design: 'compact'
}) {
static defaults() {
return super.defaults().extend({
style: {
_separator_fader_style: 'Scrollbar style',
design: null,
knobSize: null,
// colorKnob: null,
pips: null,
dashed: null,
gradient: null,
},
class_specific: {
snap: null,
touchZone: null,
spring: null,
doubleTap: null,
range: null,
logScale: null,
steps: null,
origin: null,
widgetId: {type: 'string', value: '', help: '`id` of the container widget to control'},
}
})
}
constructor(options) {
super(options)
this.scrollTarget = null
this.thumbSize = 1
if (!this.getProp('horizontal')) {
this.rangeVals[0] = 1
this.rangeVals[1] = 0
}
widgetManager.on('widget-created', (e)=>{
if (this.scrollTarget) return
var {id, widget} = e
if (id === this.getProp('widgetId') && widget.getProp('scroll')) {
this.unbindTarget()
this.bindTarget(widget)
}
}, {context: this})
}
draginitHandle(e) {
// super.draginitHandle(...arguments)
if (!e.traversing && !this.shouldDrag(e)) {
e.cancelDragEvent = true
return
}
this.percent = clip(this.percent,[0,100])
// snap when not touching the knob zone
if (this.shouldDrag(e, 'knob')) return
this.percent = this.getProp('horizontal')?
(e.offsetX - this.gaugePadding) / (this.width - this.gaugePadding * 2) * 100:
(this.height - e.offsetY - this.gaugePadding) / (this.height - this.gaugePadding * 2) * 100
this.setValue(this.percentToValue(this.percent), {send: true, sync: true, dragged: true})
}
mousewheelHandle(e) {
e.preventDefault()
e.stopPropagation()
var increment
if (this.getProp('horizontal')) {
increment = e.deltaY / this.scrollTarget.scrollWidth * 100
} else {
increment = -e.deltaY / this.scrollTarget.scrollHeight * 100
}
this.percent = clip(this.percent + increment, [0,100])
this.setValue(this.percentToValue(this.percent), {sync: true, send: true, dragged: true})
}
cacheCanvasStyle(style){
super.cacheCanvasStyle(style)
var size = this.getProp('horizontal') ? this.width : this.height
this.cssVars.knobSize = this.thumbSize * size
this.cssVars.alphaKnob = 0.25
}
unbindTarget() {
if (!this.scrollTarget) return
this.scrollTarget.off(undefined, undefined, this)
this.scrollTarget = null
}
bindTarget(target) {
if (!target) return
this.scrollTarget = target
// listen for cloneTarget's deletion
// if it is just edited, its recreation will be caught by the global 'widget-created' event handler
this.scrollTarget.on('widget-removed', (e)=>{
if (this.scrollTarget === e.widget) {
this.unbindTarget()
}
}, {context: this})
var onScroll = ()=>{
if (!this.scrollTarget) return
var index = this.getProp('horizontal') ? 0 : 1,
size = this.getProp('horizontal') ? this.width : this.height
this.thumbSize = this.scrollTarget.scrollThumb[index]
this.cssVars.knobSize = this.thumbSize * size
this.setValue(this.scrollTarget.scroll[index], {fromPanel:true, script:true})
}
this.scrollTarget.on('scroll', onScroll)
setTimeout(()=>{
onScroll()
})
}
setValue(v, options={}) {
super.setValue(v, options)
if (!options.fromPanel && this.scrollTarget) {
if (this.getProp('horizontal')) {
this.scrollTarget.setScroll(this.value, undefined, true)
} else {
this.scrollTarget.setScroll(undefined, this.value, true)
}
}
}
onRemove(){
this.unbindTarget()
super.onRemove()
}
}

View file

@ -1,4 +1,5 @@
.fader-container,
.scrollbar-container,
.knob-container,
.encoder-container,
.range-container {
@ -13,6 +14,7 @@
}
.scrollbar-container,
.fader-container,
.range-container {
@ -22,7 +24,7 @@
--widget-padding: 1rem;
--knob-size: 1rem;
--line-width: 1rem;
canvas {
text-align: left;
}