From 2782552b41bb835356e1ce9c4fc00ea27d4048b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 27 May 2021 10:56:58 +0800 Subject: [PATCH] handle input on text change & padding --- doric-Qt/example/app/demo/DoricDemoBridge.cpp | 3 ++ doric-Qt/example/doric/doric.pro | 2 ++ doric-Qt/example/doric/resources/input.qml | 3 ++ .../example/doric/shader/DoricInputNode.cpp | 28 +++++++++++++++++++ .../example/doric/shader/DoricInputNode.h | 8 ++++++ .../example/doric/shader/DoricViewNode.cpp | 5 ++++ doric-Qt/example/doric/shader/DoricViewNode.h | 2 ++ .../example/doric/utils/DoricInputBridge.cpp | 17 +++++++++++ .../example/doric/utils/DoricInputBridge.h | 22 +++++++++++++++ doric-Qt/example/doric/utils/DoricLayouts.cpp | 14 ++++++++++ doric-Qt/example/doric/utils/DoricLayouts.h | 4 +++ 11 files changed, 108 insertions(+) create mode 100644 doric-Qt/example/doric/utils/DoricInputBridge.cpp create mode 100644 doric-Qt/example/doric/utils/DoricInputBridge.h diff --git a/doric-Qt/example/app/demo/DoricDemoBridge.cpp b/doric-Qt/example/app/demo/DoricDemoBridge.cpp index fbd5a976..87370209 100644 --- a/doric-Qt/example/app/demo/DoricDemoBridge.cpp +++ b/doric-Qt/example/app/demo/DoricDemoBridge.cpp @@ -6,6 +6,7 @@ #include "DoricPanel.h" #include "utils/DoricDialogBridge.h" #include "utils/DoricImageBridge.h" +#include "utils/DoricInputBridge.h" #include "utils/DoricMouseAreaBridge.h" #include "utils/DoricUtils.h" @@ -102,4 +103,6 @@ void DoricDemoBridge::navigate(QVariant route) { context->setContextProperty("dialogBridge", dialogBridge); DoricImageBridge *imageBridge = new DoricImageBridge(); context->setContextProperty("imageBridge", imageBridge); + DoricInputBridge *inputBridge = new DoricInputBridge(); + context->setContextProperty("inputBridge", inputBridge); } diff --git a/doric-Qt/example/doric/doric.pro b/doric-Qt/example/doric/doric.pro index cbc501a7..033f3a81 100644 --- a/doric-Qt/example/doric/doric.pro +++ b/doric-Qt/example/doric/doric.pro @@ -59,6 +59,7 @@ SOURCES += \ utils/DoricContextHolder.cpp \ utils/DoricDialogBridge.cpp \ utils/DoricImageBridge.cpp \ + utils/DoricInputBridge.cpp \ utils/DoricLayouts.cpp \ utils/DoricMouseAreaBridge.cpp \ widget/flex/FlexLayout.cpp \ @@ -140,6 +141,7 @@ HEADERS += \ utils/DoricCountDownLatch.h \ utils/DoricDialogBridge.h \ utils/DoricImageBridge.h \ + utils/DoricInputBridge.h \ utils/DoricLayouts.h \ utils/DoricMouseAreaBridge.h \ utils/DoricNetworkService.h \ diff --git a/doric-Qt/example/doric/resources/input.qml b/doric-Qt/example/doric/resources/input.qml index 2eb622ab..26aee6ba 100644 --- a/doric-Qt/example/doric/resources/input.qml +++ b/doric-Qt/example/doric/resources/input.qml @@ -5,6 +5,8 @@ import "util.mjs" as Util import "gravity.mjs" as Gravity TextArea { + property var wrapper + property var uuid: Util.uuidv4() property var tag: "Input" @@ -62,6 +64,7 @@ TextArea { onTextChanged: { console.log(tag, uuid + " onTextChanged: " + this.text) + inputBridge.onTextChange(wrapper, this.text) } property var borderWidth: 0 diff --git a/doric-Qt/example/doric/shader/DoricInputNode.cpp b/doric-Qt/example/doric/shader/DoricInputNode.cpp index 2ae33407..eafd20a4 100644 --- a/doric-Qt/example/doric/shader/DoricInputNode.cpp +++ b/doric-Qt/example/doric/shader/DoricInputNode.cpp @@ -32,7 +32,35 @@ void DoricInputNode::blend(QQuickItem *view, QString name, QJsonValue prop) { view->setProperty("placeholderText", prop.toString()); } else if (name == "textAlignment") { view->setProperty("textAlignment", prop.toInt()); + } else if (name == "onTextChange") { + this->onTextChangeId = prop.toString(); + } else if (name == "onFocusChange") { + this->onFocusChangeId = prop.toString(); + } else if (name == "padding") { + DoricViewNode::blend(view, name, prop); + view->setProperty("leftPadding", prop["left"].toDouble()); + view->setProperty("rightPadding", prop["right"].toDouble()); + view->setProperty("topPadding", prop["top"].toDouble()); + view->setProperty("bottomPadding", prop["bottom"].toDouble()); } else { DoricViewNode::blend(view, name, prop); } } + +QSizeF DoricInputNode::sizeThatFits(QSizeF size) { + DoricLayouts *layout = + (DoricLayouts *)mView->property("doricLayout").toULongLong(); + + QSizeF ret = DoricViewNode::sizeThatFits(size); + return QSizeF( + ret.width() - layout->getPaddingLeft() - layout->getPaddingRight(), + ret.height() - layout->getPaddingTop() - layout->getPaddingBottom()); +} + +void DoricInputNode::onTextChange(QString text) { + if (!onTextChangeId.isEmpty()) { + QVariantList args; + args.append(text); + callJSResponse(onTextChangeId, args); + } +} diff --git a/doric-Qt/example/doric/shader/DoricInputNode.h b/doric-Qt/example/doric/shader/DoricInputNode.h index 9cac63e5..cc12171c 100644 --- a/doric-Qt/example/doric/shader/DoricInputNode.h +++ b/doric-Qt/example/doric/shader/DoricInputNode.h @@ -6,12 +6,20 @@ #include "DoricViewNode.h" class DORIC_EXPORT DoricInputNode : public DoricViewNode { +private: + QString onTextChangeId; + QString onFocusChangeId; + public: using DoricViewNode::DoricViewNode; QQuickItem *build() override; virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override; + + QSizeF sizeThatFits(QSizeF size); + + void onTextChange(QString text); }; #endif // DORICINPUTNODE_H diff --git a/doric-Qt/example/doric/shader/DoricViewNode.cpp b/doric-Qt/example/doric/shader/DoricViewNode.cpp index 0c1bf939..80a3d7a9 100644 --- a/doric-Qt/example/doric/shader/DoricViewNode.cpp +++ b/doric-Qt/example/doric/shader/DoricViewNode.cpp @@ -242,3 +242,8 @@ void DoricViewNode::onClick() { QVariantList args; callJSResponse(clickFunction, args); } + +QSizeF DoricViewNode::sizeThatFits(QSizeF size) { + return QSizeF(qMin(size.width(), mView->width()), + qMin(size.height(), mView->height())); +} diff --git a/doric-Qt/example/doric/shader/DoricViewNode.h b/doric-Qt/example/doric/shader/DoricViewNode.h index 74db1e3b..840ed21b 100644 --- a/doric-Qt/example/doric/shader/DoricViewNode.h +++ b/doric-Qt/example/doric/shader/DoricViewNode.h @@ -84,5 +84,7 @@ public: std::shared_ptr pureCallJSResponse(QString funcId, QVariantList args); + + QSizeF sizeThatFits(QSizeF size); }; #endif // DORICVIEWNODE_H diff --git a/doric-Qt/example/doric/utils/DoricInputBridge.cpp b/doric-Qt/example/doric/utils/DoricInputBridge.cpp new file mode 100644 index 00000000..f4861792 --- /dev/null +++ b/doric-Qt/example/doric/utils/DoricInputBridge.cpp @@ -0,0 +1,17 @@ +#include "DoricInputBridge.h" + +#include "shader/DoricInputNode.h" + +DoricInputBridge::DoricInputBridge(QObject *parent) : QObject(parent) {} + +void DoricInputBridge::onTextChange(QString pointer, QString text) { + QObject *object = (QObject *)(pointer.toULongLong()); + DoricInputNode *inputNode = dynamic_cast(object); + + inputNode->onTextChange(text); +} + +void DoricInputBridge::onFocusChange(QString pointer) { + QObject *object = (QObject *)(pointer.toULongLong()); + DoricInputNode *inputNode = dynamic_cast(object); +} diff --git a/doric-Qt/example/doric/utils/DoricInputBridge.h b/doric-Qt/example/doric/utils/DoricInputBridge.h new file mode 100644 index 00000000..f752232e --- /dev/null +++ b/doric-Qt/example/doric/utils/DoricInputBridge.h @@ -0,0 +1,22 @@ +#ifndef DORICINPUTBRIDGE_H +#define DORICINPUTBRIDGE_H + +#include +#include + +#include "DoricExport.h" + +class DORIC_EXPORT DoricInputBridge : public QObject { + Q_OBJECT +public: + explicit DoricInputBridge(QObject *parent = nullptr); + + Q_INVOKABLE + void onTextChange(QString pointer, QString text); + + Q_INVOKABLE + void onFocusChange(QString pointer); +signals: +}; + +#endif // DORICINPUTBRIDGE_H diff --git a/doric-Qt/example/doric/utils/DoricLayouts.cpp b/doric-Qt/example/doric/utils/DoricLayouts.cpp index b6501dd6..19bb5697 100644 --- a/doric-Qt/example/doric/utils/DoricLayouts.cpp +++ b/doric-Qt/example/doric/utils/DoricLayouts.cpp @@ -1,4 +1,5 @@ #include "DoricLayouts.h" +#include "shader/DoricInputNode.h" #include "shader/DoricScrollerNode.h" DoricLayouts::DoricLayouts(QObject *parent) : QObject(parent) { @@ -76,15 +77,19 @@ void DoricLayouts::setMarginBottom(qreal marginBottom) { void DoricLayouts::setPaddingLeft(qreal paddingLeft) { this->paddingLeft = paddingLeft; } +qreal DoricLayouts::getPaddingLeft() { return this->paddingLeft; } void DoricLayouts::setPaddingTop(qreal paddingTop) { this->paddingTop = paddingTop; } +qreal DoricLayouts::getPaddingTop() { return this->paddingTop; } void DoricLayouts::setPaddingRight(qreal paddingRight) { this->paddingRight = paddingRight; } +qreal DoricLayouts::getPaddingRight() { return this->paddingRight; } void DoricLayouts::setPaddingBottom(qreal paddingBottom) { this->paddingBottom = paddingBottom; } +qreal DoricLayouts::getPaddingBottom() { return this->paddingBottom; } void DoricLayouts::setWeight(int weight) { this->weight = weight; } @@ -252,6 +257,11 @@ void DoricLayouts::measureUndefinedContent(QSizeF targetSize) { (QObject *)(this->view->property("wrapper").toULongLong()); DoricScrollerNode *viewNode = dynamic_cast(object); measuredSize = viewNode->sizeThatFits(targetSize); + } else if (tag == "Input") { + QObject *object = + (QObject *)(this->view->property("wrapper").toULongLong()); + DoricInputNode *viewNode = dynamic_cast(object); + measuredSize = viewNode->sizeThatFits(targetSize); } else { qreal actualWidth = this->view->width(); qreal actualHeight = this->view->height(); @@ -268,6 +278,10 @@ void DoricLayouts::measureUndefinedContent(QSizeF targetSize) { setMeasuredHeight(measuredSize.height() + this->paddingTop + this->paddingBottom); } + + this->contentWidth = measuredSize.width(); + + this->contentHeight = measuredSize.height(); } void DoricLayouts::measureStackContent(QSizeF targetSize) { qreal contentWidth = 0, contentHeight = 0; diff --git a/doric-Qt/example/doric/utils/DoricLayouts.h b/doric-Qt/example/doric/utils/DoricLayouts.h index 47558f62..6df1ab7f 100644 --- a/doric-Qt/example/doric/utils/DoricLayouts.h +++ b/doric-Qt/example/doric/utils/DoricLayouts.h @@ -65,9 +65,13 @@ public: void setMarginBottom(qreal marginBottom); void setPaddingLeft(qreal paddingLeft); + qreal getPaddingLeft(); void setPaddingTop(qreal paddingTop); + qreal getPaddingTop(); void setPaddingRight(qreal paddingRight); + qreal getPaddingRight(); void setPaddingBottom(qreal paddingBottom); + qreal getPaddingBottom(); void setWeight(int weight);