From 7c2a85f7d8567647fa0612b0cc837e0ebfe1b0dd Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 27 Feb 2025 15:44:01 +0100 Subject: [PATCH] fader: add touchZone property --- CHANGELOG.md | 3 ++ src/client/widgets/sliders/fader.js | 53 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f7aa4a1..94d94677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - ui: global zoom (CmdOrCtrl + scroll / + / - / 0) issues and conflits with native browser zoom; - ui: limit ctrl + scroll zoom speed +- widgets + - fader: add `touchZone` property to allow restricting interaction to a part of the widget + ## 1.27.5 - bug fixes diff --git a/src/client/widgets/sliders/fader.js b/src/client/widgets/sliders/fader.js index 98b50bb9..d7d6870f 100644 --- a/src/client/widgets/sliders/fader.js +++ b/src/client/widgets/sliders/fader.js @@ -30,6 +30,12 @@ class Fader extends Slider { }, class_specific: { snap: {type: 'boolean', value: false, help: 'By default, dragging the widget will modify it\'s value starting from its last value. Setting this to `true` will make it snap directly to the mouse/touch position'}, + touchZone: {type: 'string', value: 'any', choices: ['any', 'knob', 'gauge'], help: [ + 'Restrict interaction to a part of the widget:', + '- `all`: touching the widget anywhere will start an interaction', + '- `knob`: touching the knob will start an interaction', + '- `gauge`: touching anywhere in the knob\'s moving range will start an interaction' + ]}, spring: {type: 'boolean', value: false, help: 'When set to `true`, the widget will go back to its `default` value when released'}, doubleTap: {type: 'boolean', value: false, help: [ 'Set to `true` to make the fader reset to its `default` value when receiving a double tap.', @@ -77,6 +83,11 @@ class Fader extends Slider { super.draginitHandle(...arguments) + if (!this.shouldDrag(e)) { + e.cancelDragEvent = true + return + } + this.percent = clip(this.percent,[0,100]) if (!(e.traversing || this.getProp('snap')) || e.ctrlKey) return @@ -104,6 +115,48 @@ class Fader extends Slider { } + shouldDrag(e) { + + var zone = this.getProp('dragZone'), + design = this.getProp('design'), + horizontal = this.getProp('horizontal') + + if (zone == 'any' || design == 'compact') return true + + var x = e.offsetX, + y = e.offsetY, + percent = this.steps ? this.valueToPercent(this.value) : this.percent, + d = Math.round(this.percentToCoord(percent)), + m = horizontal ? this.height / 2 : this.width / 2, + knobHeight = this.cssVars.knobSize, + knobWidth = design ? knobHeight : knobHeight * .6 + + if (horizontal) [knobWidth, knobHeight] = [knobHeight, knobWidth] + + // add extra tolerance pixels + knobWidth += 2 * PXSCALE + knobHeight += 2 * PXSCALE + + var knobX = (horizontal ? d : m) - knobWidth / 2, + knobY = (horizontal ? m : d) - knobHeight / 2, + condition + + if (horizontal) { + condition = ((x >= knobX && x <= knobX + knobWidth) || zone == 'gauge') && + y >= knobY && y <= knobY + knobHeight + } else { + condition = x >= knobX && x <= knobX + knobWidth && + ((y >= knobY && y <= knobY + knobHeight) || zone == 'gauge') + } + + if (condition) { + return true + } else { + return false + } + + } + percentToCoord(percent) { if (this.getProp('horizontal')) {