widgets: implement some dynamic props

This commit is contained in:
jean-emmanuel 2018-02-05 09:39:00 +01:00
parent 5c5adebabb
commit 67e554c494
7 changed files with 83 additions and 25 deletions

View file

@ -99,11 +99,11 @@ var WidgetManager = class WidgetManager extends EventEmitter {
}
registerWidget(widget, data) {
registerWidget(widget, updatedData) {
ipc.send('addWidget', {
hash: widget.hash,
data: data || {
data: updatedData || {
precision: widget.getProp('precision'),
preArgs: widget.getProp('preArgs'),
split: widget.split,
@ -113,6 +113,16 @@ var WidgetManager = class WidgetManager extends EventEmitter {
}
})
if (updatedData && updatedData.address) {
var address = updatedData.address,
hash = widget.hash
if (address && this.addressRoute[address].indexOf(hash) != -1) this.addressRoute[address].splice(this.addressRoute[address].indexOf(hash), 1)
if (!this.addressRoute[address]) this.addressRoute[address] = []
this.addressRoute[address].push(hash)
}
}
removeWidget(hash) {

View file

@ -47,6 +47,8 @@ module.exports = class Push extends _widgets_base {
this.active = 0
this.lastChanged = 'state'
this.dynamicProps.push('on', 'off', 'norelease')
this.on('draginit',()=>{
if (this.active) return
this.setValuePrivate(this.getProp('on'),{send:true,sync:true})

View file

@ -48,6 +48,8 @@ module.exports = class Toggle extends _widgets_base {
this.state = 0
this.active = false
this.dynamicProps.push('on', 'off')
if (this.getProp('led')) this.container.classList.add('led')

View file

@ -115,25 +115,34 @@ module.exports = class _canvas_base extends _widgets_base {
if (!this.visible || checkColors) {
this.visible = true
this.colors.custom = style.getPropertyValue('--color-custom')
this.colors.text = style.getPropertyValue('--color-text')
this.colors.raised = style.getPropertyValue('--color-raised')
this.colors.bg = style.getPropertyValue('--color-bg')
this.colors.fg = style.getPropertyValue('--color-fg')
this.colors.faded = style.getPropertyValue('--color-faded')
this.colors.light = style.getPropertyValue('--color-light')
this.fontFamily = style.getPropertyValue("font-family")
this.textAlign = style.getPropertyValue("text-align")
this.cacheCanvasStyle()
}
requestAnimationFrame(this.draw.bind(this))
}
cacheCanvasStyle(style){
var style = style || window.getComputedStyle(this.canvas)
this.colors.custom = style.getPropertyValue('--color-custom')
this.colors.text = style.getPropertyValue('--color-text')
this.colors.raised = style.getPropertyValue('--color-raised')
this.colors.bg = style.getPropertyValue('--color-bg')
this.colors.fg = style.getPropertyValue('--color-fg')
this.colors.faded = style.getPropertyValue('--color-faded')
this.colors.light = style.getPropertyValue('--color-light')
this.fontFamily = style.getPropertyValue("font-family")
this.textAlign = style.getPropertyValue("text-align")
this.fontSize = parseFloat(style.getPropertyValue("font-size"))
this.fontWeight = parseFloat(style.getPropertyValue("font-weight"))
this.ctx.font = this.fontWeight + ' ' + this.fontSize + 'px ' + this.fontFamily
this.ctx.textBaseline = "middle"
this.ctx.textAlign = this.textAlign
requestAnimationFrame(this.draw.bind(this))
}
clear() {
@ -164,6 +173,25 @@ module.exports = class _canvas_base extends _widgets_base {
throw new Error('Calling unimplemented draw() method')
}
onPropChanged(propName, options) {
var ret = super.onPropChanged(...arguments)
switch (propName) {
case 'color':
this.cacheCanvasStyle()
this.batchDraw()
return
default:
return ret
}
}
onRemove() {
this.off('resize')
super.onRemove()

View file

@ -310,7 +310,9 @@ module.exports = class _widgets_base extends EventEmitter {
this.cachedProps[propName] = propValue
if (this.onPropChanged(propName, options)) return
if (this.onPropChanged(propName, options)) {
return this.reCreateWidget()
}
widgetManager.trigger(/prop-changed(\..*)?/, [{
id: this.getProp('id'),
@ -333,10 +335,22 @@ module.exports = class _widgets_base extends EventEmitter {
this.setValue(this.getProp('value'), {sync:true, send: options && options.send})
return
case 'color':
this.container.style.setProperty('--color-custom', this.getProp('color') != 'auto' ? this.getProp('color') : 'initial')
return
case 'precision':
case 'address':
case 'preArgs':
case 'target':
case 'noSync':
if (propName == 'precision') this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0))
var data = {}
data[propName] = this.getProp(propName)
widgetManager.registerWidget(this, data)
return
default:
this.reCreateWidget()
return true
}

View file

@ -158,18 +158,16 @@ module.exports = class _sliders_base extends _canvas_base {
}
resizeHandle(event) {
cacheCanvasStyle(style) {
var {style, checkColors} = event
var style = style || window.getComputedStyle(this.canvas)
if (!this.visible || checkColors) {
this.colors.track = style.getPropertyValue('--color-track')
this.colors.gauge = style.getPropertyValue('--color-gauge')
this.colors.knob = style.getPropertyValue('--color-knob')
this.colors.pips = style.getPropertyValue('--color-pips')
}
this.colors.track = style.getPropertyValue('--color-track')
this.colors.gauge = style.getPropertyValue('--color-gauge')
this.colors.knob = style.getPropertyValue('--color-knob')
this.colors.pips = style.getPropertyValue('--color-pips')
super.resizeHandle(...arguments)
super.cacheCanvasStyle(style)
}