diff --git a/doric-Qt/example/app/demo/DoricDemoBridge.cpp b/doric-Qt/example/app/demo/DoricDemoBridge.cpp index 128b6c64..384aecdd 100644 --- a/doric-Qt/example/app/demo/DoricDemoBridge.cpp +++ b/doric-Qt/example/app/demo/DoricDemoBridge.cpp @@ -8,6 +8,7 @@ #include "utils/DoricImageBridge.h" #include "utils/DoricInputBridge.h" #include "utils/DoricMouseAreaBridge.h" +#include "utils/DoricSwitchBridge.h" #include "utils/DoricUtils.h" DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} @@ -108,4 +109,6 @@ void DoricDemoBridge::navigate(QVariant route) { context->setContextProperty("imageBridge", imageBridge); DoricInputBridge *inputBridge = new DoricInputBridge(); context->setContextProperty("inputBridge", inputBridge); + DoricSwitchBridge *switchBridge = new DoricSwitchBridge(); + context->setContextProperty("switchBridge", switchBridge); } diff --git a/doric-Qt/example/doric/doric.pro b/doric-Qt/example/doric/doric.pro index 783f2ee4..7fca89d1 100644 --- a/doric-Qt/example/doric/doric.pro +++ b/doric-Qt/example/doric/doric.pro @@ -63,6 +63,7 @@ SOURCES += \ utils/DoricInputBridge.cpp \ utils/DoricLayouts.cpp \ utils/DoricMouseAreaBridge.cpp \ + utils/DoricSwitchBridge.cpp \ widget/flex/FlexLayout.cpp \ widget/flex/FlexLayoutConfig.cpp \ widget/flex/FlexLayoutService.cpp \ @@ -148,6 +149,7 @@ HEADERS += \ utils/DoricMouseAreaBridge.h \ utils/DoricNetworkService.h \ utils/DoricObjectFactory.h \ + utils/DoricSwitchBridge.h \ utils/DoricThreadMode.h \ utils/DoricUtils.h \ widget/flex/FlexLayout.h \ diff --git a/doric-Qt/example/doric/resources/switch.qml b/doric-Qt/example/doric/resources/switch.qml index 8e53ebdb..0c0491c7 100644 --- a/doric-Qt/example/doric/resources/switch.qml +++ b/doric-Qt/example/doric/resources/switch.qml @@ -28,5 +28,6 @@ Switch { } else { this.indicator.children[0].color = offTintColor } + switchBridge.onSwitch(wrapper, checked) } } diff --git a/doric-Qt/example/doric/shader/DoricSwitchNode.cpp b/doric-Qt/example/doric/shader/DoricSwitchNode.cpp index 533d0955..331ff6ca 100644 --- a/doric-Qt/example/doric/shader/DoricSwitchNode.cpp +++ b/doric-Qt/example/doric/shader/DoricSwitchNode.cpp @@ -25,7 +25,7 @@ void DoricSwitchNode::blend(QQuickItem *view, QString name, QJsonValue prop) { view->setProperty("checked", prop.toBool()); checkByCodeToggle = false; } else if (name == "onSwitch") { - + onSwitchFuncId = prop.toString(); } else if (name == "offTintColor") { view->setProperty( "offTintColor", @@ -41,3 +41,13 @@ void DoricSwitchNode::blend(QQuickItem *view, QString name, QJsonValue prop) { DoricViewNode::blend(view, name, prop); } } + +void DoricSwitchNode::onSwitch(bool checked) { + if (checkByCodeToggle) + return; + if (!onSwitchFuncId.isEmpty()) { + QVariantList args; + args.append(checked); + callJSResponse(onSwitchFuncId, args); + } +} diff --git a/doric-Qt/example/doric/shader/DoricSwitchNode.h b/doric-Qt/example/doric/shader/DoricSwitchNode.h index f6551abd..aad626ee 100644 --- a/doric-Qt/example/doric/shader/DoricSwitchNode.h +++ b/doric-Qt/example/doric/shader/DoricSwitchNode.h @@ -10,12 +10,16 @@ class DORIC_EXPORT DoricSwitchNode : public DoricViewNode { private: bool checkByCodeToggle = false; + QString onSwitchFuncId; + public: using DoricViewNode::DoricViewNode; QQuickItem *build() override; virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override; + + void onSwitch(bool checked); }; #endif // DORICSWITCHNODE_H diff --git a/doric-Qt/example/doric/utils/DoricSwitchBridge.cpp b/doric-Qt/example/doric/utils/DoricSwitchBridge.cpp new file mode 100644 index 00000000..dd2c3cec --- /dev/null +++ b/doric-Qt/example/doric/utils/DoricSwitchBridge.cpp @@ -0,0 +1,12 @@ +#include "DoricSwitchBridge.h" + +#include "shader/DoricSwitchNode.h" + +DoricSwitchBridge::DoricSwitchBridge(QObject *parent) : QObject(parent) {} + +void DoricSwitchBridge::onSwitch(QString pointer, bool checked) { + QObject *object = (QObject *)(pointer.toULongLong()); + DoricSwitchNode *switchNode = dynamic_cast(object); + + switchNode->onSwitch(checked); +} diff --git a/doric-Qt/example/doric/utils/DoricSwitchBridge.h b/doric-Qt/example/doric/utils/DoricSwitchBridge.h new file mode 100644 index 00000000..7cb1d923 --- /dev/null +++ b/doric-Qt/example/doric/utils/DoricSwitchBridge.h @@ -0,0 +1,19 @@ +#ifndef DORICSWITCHBRIDGE_H +#define DORICSWITCHBRIDGE_H + +#include +#include + +#include "DoricExport.h" + +class DORIC_EXPORT DoricSwitchBridge : public QObject { + Q_OBJECT +public: + explicit DoricSwitchBridge(QObject *parent = nullptr); + + Q_INVOKABLE + void onSwitch(QString pointer, bool checked); +signals: +}; + +#endif // DORICSWITCHBRIDGE_H