From df74279db260eae083a6ec508eac2b7a3d8d8ffe Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 26 Jan 2022 16:33:49 +0100 Subject: [PATCH] button: add `momentary` mode for sending messages with no value; prevent button from getting stuck when `on` and `off` are equal in `momentary` and `tap` modes --- CHANGELOG.md | 3 +++ src/client/widgets/basics/button.js | 33 ++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6363b09..95d8ae84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ - bug fixes - broken console since v1.14.0 +- widgets + - button: add `momentary` mode for sending messages with no value; prevent button from getting stuck when `on` and `off` are equal in `momentary` and `tap` modes + - inspector - color picker: inline picker widget (no longer in a modal); show color changes on the fly diff --git a/src/client/widgets/basics/button.js b/src/client/widgets/basics/button.js index 63c65cb5..320119eb 100644 --- a/src/client/widgets/basics/button.js +++ b/src/client/widgets/basics/button.js @@ -30,16 +30,17 @@ class Button extends Widget { }, class_specific: { on: {type: '*', value: 1, help: [ - 'Set to `null` to send send no argument in the osc message', + 'Set to `null` to send send no argument in the osc message. Ignored if `mode` is `momentary`.', ]}, off: {type: '*', value: 0, help: [ - 'Set to `null` to send send no argument in the osc message. Must be different from `on`.', + 'Set to `null` to send send no argument in the osc message. Must be different from `on`. Ignored if `mode` is `momentary` or `tap`.', ]}, - mode: {type: 'string', value: 'toggle', choices: ['toggle', 'push', 'tap'], help: [ + mode: {type: 'string', value: 'toggle', choices: ['toggle', 'push', 'momentary', 'tap'], help: [ 'Interraction mode:', '- `toggle` (classic on/off switch)', '- `push` (press & release)', - '- `tap` (no release)' + '- `momentary` (no release, no value sent)', + '- `tap` (no release, sends `on` as value)' ]}, doubleTap: {type: 'boolean', value: false, help: 'Set to `true` to make the button require a double tap to be pushed instead of a single tap'}, @@ -72,7 +73,7 @@ class Button extends Widget { }, {element: this.widget}) } - var tap = this.getProp('mode') === 'tap', + var tap = this.getProp('mode').match(/momentary|tap/), push = this.getProp('mode') === 'push' || tap if (push) { @@ -150,7 +151,11 @@ class Button extends Widget { if (tap) this.noValueState = true - this.value = this.getProp('off') + if (this.getProp('mode') === 'momentary') { + this.value = null + } else { + this.value = this.getProp('off') + } this.label = html`` @@ -168,7 +173,7 @@ class Button extends Widget { if (typeof v === 'string' && isJSON(v)) { try { - v = JSON.parse(v ) + v = JSON.parse(v) } catch (err) {} } @@ -182,13 +187,21 @@ class Button extends Widget { newstate = 0 + } else if (this.getProp('mode') === 'momentary' && v === null) { + + newstate = 1 + } if (newstate !== undefined) { this.state = newstate - this.container.classList.toggle('on', this.state) - this.value = this.getProp(this.state ? 'on' : 'off') + if (this.getProp('mode').match(/toggle|push/)){ + this.container.classList.toggle('on', this.state) + } + if (this.getProp('mode') !== 'momentary') { + this.value = this.getProp(this.state ? 'on' : 'off') + } if (this.exposeTouchCoords) { if (options.y !== undefined) { @@ -200,7 +213,7 @@ class Button extends Widget { if (options.sync) this.changed(options) // tap mode - if (newstate && this.getProp('mode') === 'tap' && !options.tapRelease) { + if (newstate && this.getProp('mode').match(/momentary|tap/) && !options.tapRelease) { // reset value this.setValue(this.getProp('off'), {sync: false, send: false, tapRelease: true})