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 "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);
}

View File

@ -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 \

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -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()));
}

View File

@ -84,5 +84,7 @@ public:
std::shared_ptr<DoricAsyncResult> pureCallJSResponse(QString funcId,
QVariantList args);
QSizeF sizeThatFits(QSizeF size);
};
#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 "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<DoricScrollerNode *>(object);
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 {
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;

View File

@ -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);