Loading...
Searching...
No Matches
ValueWriteBack.hpp
1#pragma once
2
3// Writes a port's value back into its bound QML property without clobbering an
4// in-progress edit. Two guards: equality, since the value is usually the echo of
5// the UI's own write; and focus, because avnd republishes every control input of
6// a process when any one changes, so the snapshot can lag a keystroke that has
7// not reached the executor yet. A value suppressed by the focus guard is dropped
8// rather than replayed, being older than what the port already holds.
9
10#include <QMetaType>
11#include <QObject>
12#include <QQmlProperty>
13#include <QVariant>
14
15namespace JS
16{
17
21inline bool targetHasActiveFocus(const QQmlProperty& prop) noexcept
22{
23 auto* obj = prop.object();
24 if(!obj)
25 return false;
26
27 const QVariant focus = obj->property("activeFocus");
28 return focus.typeId() == QMetaType::Bool && focus.toBool();
29}
30
32
33inline bool writeBackUnlessEditing(QQmlProperty& prop, const QVariant& value)
34{
35 if(!prop.isValid())
36 return false;
37
38 // Qt 6 compares numbers across int / float / double here, so a float port
39 // echoing into a `real` property does not count as a change.
40 if(prop.read() == value)
41 return false;
42
43 if(targetHasActiveFocus(prop))
44 return false;
45
46 prop.write(value);
47 return true;
48}
49
50}