From ccfd1ef7373dc59a27e32ceadb91882e19d1fd96 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Fri, 19 Apr 2024 11:41:02 +0200 Subject: [PATCH] [BasicUI] Fix slider behaviour and implement two different modes Fix #2525 Also related to openhab/openhab-core#4084 and openhab/openhab-core#3430 Two behaviour modes are now supported. - If releaseOnly attribute is set, the new value is sent to the item only when the slider is released. - If releaseOnly attribute is unset, new values are sent to the item while moving the slider. Events are sent at a certain frequency, this frequency is defined by the sendFrequency attribute if set or every 200 ms by default. Signed-off-by: Laurent Garnier --- .../basic/internal/render/SliderRenderer.java | 1 + .../src/main/resources/snippets/slider.html | 3 +- .../org.openhab.ui.basic/web-src/smarthome.js | 64 +++++++++++-------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java index 8f8d02cadb..08347c5ca5 100644 --- a/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java +++ b/bundles/org.openhab.ui.basic/src/main/java/org/openhab/ui/basic/internal/render/SliderRenderer.java @@ -83,6 +83,7 @@ public EList renderWidget(Widget w, StringBuilder sb, String sitemap) th snippet = preprocessSnippet(snippet, w); snippet = snippet.replace("%frequency%", frequency); + snippet = snippet.replace("%release_only%", s.isReleaseOnly() ? "true" : "flase"); snippet = snippet.replace("%switch%", s.isSwitchEnabled() ? "1" : "0"); snippet = snippet.replace("%unit%", unit == null ? "" : unit); snippet = snippet.replace("%minValue%", minValueOf(s)); diff --git a/bundles/org.openhab.ui.basic/src/main/resources/snippets/slider.html b/bundles/org.openhab.ui.basic/src/main/resources/snippets/slider.html index db25da5fe7..ef90d856e9 100644 --- a/bundles/org.openhab.ui.basic/src/main/resources/snippets/slider.html +++ b/bundles/org.openhab.ui.basic/src/main/resources/snippets/slider.html @@ -21,7 +21,8 @@ data-icon-color="%icon_color%" > unchanged command"); + return; + } + + var command = value; if (_t.unit) { command = command + " " + _t.unit; } + window.console.log("emitEvent " + command); _t.parentNode.dispatchEvent(createEvent("control-change", { item: _t.item, value: command })); + lastSentCmd = value; } _t.debounceProxy = new DebounceProxy(function() { - emitEvent(); - }, 200); + emitEvent(_t.input.value); + }, _t.sendFrequency); _t.setValuePrivate = function(value, itemState) { + window.console.log("setValuePrivate " + value); if (_t.hasValue) { _t.valueNode.innerHTML = value; } @@ -2425,32 +2434,34 @@ _t.valueNode.style.color = smarthome.UI.adjustColorToTheme(_t.valueColor); }; - var - unlockTimeout = null; - function onChange() { + window.console.log("onChange " + _t.input.value); + _t.debounceProxy.finish(); + emitEvent(_t.input.value); + } + + function onInput() { + if (_t.releaseOnly) { + return; + } + window.console.log("onInput " + _t.input.value); _t.debounceProxy.call(); } function onChangeStart() { + window.console.log("onChangeStart releaseOnly " + _t.releaseOnly + " sendFrequency " + _t.sendFrequency); if (unlockTimeout !== null) { clearTimeout(unlockTimeout); } _t.locked = true; - smarthome.changeListener.pause(); + lastSentCmd = null; } function onChangeEnd() { - // mouseUp is fired earlier than the value is changed - // quite a dirty hack, but I don't see any other way - _t.debounceProxy.call(); - setTimeout(function() { - smarthome.changeListener.resume(); - }, 5); + window.console.log("onChangeEnd"); unlockTimeout = setTimeout(function() { _t.locked = false; }, 300); - _t.debounceProxy.finish(); } var @@ -2459,6 +2470,7 @@ [ _t.input, "mousedown", onChangeStart ], [ _t.input, "touchend", onChangeEnd ], [ _t.input, "mouseup", onChangeEnd ], + [ _t.input, "input", onInput ], [ _t.input, "change", onChange ] ];