handle input on text change & padding

This commit is contained in:
王劲鹏 2021-05-27 10:56:58 +08:00 committed by osborn
parent 143e52ab63
commit 2782552b41
11 changed files with 108 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include "DoricPanel.h" #include "DoricPanel.h"
#include "utils/DoricDialogBridge.h" #include "utils/DoricDialogBridge.h"
#include "utils/DoricImageBridge.h" #include "utils/DoricImageBridge.h"
#include "utils/DoricInputBridge.h"
#include "utils/DoricMouseAreaBridge.h" #include "utils/DoricMouseAreaBridge.h"
#include "utils/DoricUtils.h" #include "utils/DoricUtils.h"
@ -102,4 +103,6 @@ void DoricDemoBridge::navigate(QVariant route) {
context->setContextProperty("dialogBridge", dialogBridge); context->setContextProperty("dialogBridge", dialogBridge);
DoricImageBridge *imageBridge = new DoricImageBridge(); DoricImageBridge *imageBridge = new DoricImageBridge();
context->setContextProperty("imageBridge", imageBridge); context->setContextProperty("imageBridge", imageBridge);
DoricInputBridge *inputBridge = new DoricInputBridge();
context->setContextProperty("inputBridge", inputBridge);
} }

View File

@ -59,6 +59,7 @@ SOURCES += \
utils/DoricContextHolder.cpp \ utils/DoricContextHolder.cpp \
utils/DoricDialogBridge.cpp \ utils/DoricDialogBridge.cpp \
utils/DoricImageBridge.cpp \ utils/DoricImageBridge.cpp \
utils/DoricInputBridge.cpp \
utils/DoricLayouts.cpp \ utils/DoricLayouts.cpp \
utils/DoricMouseAreaBridge.cpp \ utils/DoricMouseAreaBridge.cpp \
widget/flex/FlexLayout.cpp \ widget/flex/FlexLayout.cpp \
@ -140,6 +141,7 @@ HEADERS += \
utils/DoricCountDownLatch.h \ utils/DoricCountDownLatch.h \
utils/DoricDialogBridge.h \ utils/DoricDialogBridge.h \
utils/DoricImageBridge.h \ utils/DoricImageBridge.h \
utils/DoricInputBridge.h \
utils/DoricLayouts.h \ utils/DoricLayouts.h \
utils/DoricMouseAreaBridge.h \ utils/DoricMouseAreaBridge.h \
utils/DoricNetworkService.h \ utils/DoricNetworkService.h \

View File

@ -5,6 +5,8 @@ import "util.mjs" as Util
import "gravity.mjs" as Gravity import "gravity.mjs" as Gravity
TextArea { TextArea {
property var wrapper
property var uuid: Util.uuidv4() property var uuid: Util.uuidv4()
property var tag: "Input" property var tag: "Input"
@ -62,6 +64,7 @@ TextArea {
onTextChanged: { onTextChanged: {
console.log(tag, uuid + " onTextChanged: " + this.text) console.log(tag, uuid + " onTextChanged: " + this.text)
inputBridge.onTextChange(wrapper, this.text)
} }
property var borderWidth: 0 property var borderWidth: 0

View File

@ -32,7 +32,35 @@ void DoricInputNode::blend(QQuickItem *view, QString name, QJsonValue prop) {
view->setProperty("placeholderText", prop.toString()); view->setProperty("placeholderText", prop.toString());
} else if (name == "textAlignment") { } else if (name == "textAlignment") {
view->setProperty("textAlignment", prop.toInt()); 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 { } else {
DoricViewNode::blend(view, name, prop); 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);
}
}

View File

@ -6,12 +6,20 @@
#include "DoricViewNode.h" #include "DoricViewNode.h"
class DORIC_EXPORT DoricInputNode : public DoricViewNode { class DORIC_EXPORT DoricInputNode : public DoricViewNode {
private:
QString onTextChangeId;
QString onFocusChangeId;
public: public:
using DoricViewNode::DoricViewNode; using DoricViewNode::DoricViewNode;
QQuickItem *build() override; QQuickItem *build() override;
virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override; virtual void blend(QQuickItem *view, QString name, QJsonValue prop) override;
QSizeF sizeThatFits(QSizeF size);
void onTextChange(QString text);
}; };
#endif // DORICINPUTNODE_H #endif // DORICINPUTNODE_H

View File

@ -242,3 +242,8 @@ void DoricViewNode::onClick() {
QVariantList args; QVariantList args;
callJSResponse(clickFunction, args); callJSResponse(clickFunction, args);
} }
QSizeF DoricViewNode::sizeThatFits(QSizeF size) {
return QSizeF(qMin(size.width(), mView->width()),
qMin(size.height(), mView->height()));
}

View File

@ -84,5 +84,7 @@ public:
std::shared_ptr<DoricAsyncResult> pureCallJSResponse(QString funcId, std::shared_ptr<DoricAsyncResult> pureCallJSResponse(QString funcId,
QVariantList args); QVariantList args);
QSizeF sizeThatFits(QSizeF size);
}; };
#endif // DORICVIEWNODE_H #endif // DORICVIEWNODE_H

View File

@ -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<DoricInputNode *>(object);
inputNode->onTextChange(text);
}
void DoricInputBridge::onFocusChange(QString pointer) {
QObject *object = (QObject *)(pointer.toULongLong());
DoricInputNode *inputNode = dynamic_cast<DoricInputNode *>(object);
}

View File

@ -0,0 +1,22 @@
#ifndef DORICINPUTBRIDGE_H
#define DORICINPUTBRIDGE_H
#include <QObject>
#include <QVariant>
#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

View File

@ -1,4 +1,5 @@
#include "DoricLayouts.h" #include "DoricLayouts.h"
#include "shader/DoricInputNode.h"
#include "shader/DoricScrollerNode.h" #include "shader/DoricScrollerNode.h"
DoricLayouts::DoricLayouts(QObject *parent) : QObject(parent) { DoricLayouts::DoricLayouts(QObject *parent) : QObject(parent) {
@ -76,15 +77,19 @@ void DoricLayouts::setMarginBottom(qreal marginBottom) {
void DoricLayouts::setPaddingLeft(qreal paddingLeft) { void DoricLayouts::setPaddingLeft(qreal paddingLeft) {
this->paddingLeft = paddingLeft; this->paddingLeft = paddingLeft;
} }
qreal DoricLayouts::getPaddingLeft() { return this->paddingLeft; }
void DoricLayouts::setPaddingTop(qreal paddingTop) { void DoricLayouts::setPaddingTop(qreal paddingTop) {
this->paddingTop = paddingTop; this->paddingTop = paddingTop;
} }
qreal DoricLayouts::getPaddingTop() { return this->paddingTop; }
void DoricLayouts::setPaddingRight(qreal paddingRight) { void DoricLayouts::setPaddingRight(qreal paddingRight) {
this->paddingRight = paddingRight; this->paddingRight = paddingRight;
} }
qreal DoricLayouts::getPaddingRight() { return this->paddingRight; }
void DoricLayouts::setPaddingBottom(qreal paddingBottom) { void DoricLayouts::setPaddingBottom(qreal paddingBottom) {
this->paddingBottom = paddingBottom; this->paddingBottom = paddingBottom;
} }
qreal DoricLayouts::getPaddingBottom() { return this->paddingBottom; }
void DoricLayouts::setWeight(int weight) { this->weight = weight; } void DoricLayouts::setWeight(int weight) { this->weight = weight; }
@ -252,6 +257,11 @@ void DoricLayouts::measureUndefinedContent(QSizeF targetSize) {
(QObject *)(this->view->property("wrapper").toULongLong()); (QObject *)(this->view->property("wrapper").toULongLong());
DoricScrollerNode *viewNode = dynamic_cast<DoricScrollerNode *>(object); DoricScrollerNode *viewNode = dynamic_cast<DoricScrollerNode *>(object);
measuredSize = viewNode->sizeThatFits(targetSize); measuredSize = viewNode->sizeThatFits(targetSize);
} else if (tag == "Input") {
QObject *object =
(QObject *)(this->view->property("wrapper").toULongLong());
DoricInputNode *viewNode = dynamic_cast<DoricInputNode *>(object);
measuredSize = viewNode->sizeThatFits(targetSize);
} else { } else {
qreal actualWidth = this->view->width(); qreal actualWidth = this->view->width();
qreal actualHeight = this->view->height(); qreal actualHeight = this->view->height();
@ -268,6 +278,10 @@ void DoricLayouts::measureUndefinedContent(QSizeF targetSize) {
setMeasuredHeight(measuredSize.height() + this->paddingTop + setMeasuredHeight(measuredSize.height() + this->paddingTop +
this->paddingBottom); this->paddingBottom);
} }
this->contentWidth = measuredSize.width();
this->contentHeight = measuredSize.height();
} }
void DoricLayouts::measureStackContent(QSizeF targetSize) { void DoricLayouts::measureStackContent(QSizeF targetSize) {
qreal contentWidth = 0, contentHeight = 0; qreal contentWidth = 0, contentHeight = 0;

View File

@ -65,9 +65,13 @@ public:
void setMarginBottom(qreal marginBottom); void setMarginBottom(qreal marginBottom);
void setPaddingLeft(qreal paddingLeft); void setPaddingLeft(qreal paddingLeft);
qreal getPaddingLeft();
void setPaddingTop(qreal paddingTop); void setPaddingTop(qreal paddingTop);
qreal getPaddingTop();
void setPaddingRight(qreal paddingRight); void setPaddingRight(qreal paddingRight);
qreal getPaddingRight();
void setPaddingBottom(qreal paddingBottom); void setPaddingBottom(qreal paddingBottom);
qreal getPaddingBottom();
void setWeight(int weight); void setWeight(int weight);