add sizeThatFits in scroller node

This commit is contained in:
王劲鹏 2021-04-19 21:17:40 +08:00 committed by osborn
parent 77e32d1f08
commit 5ba5f5ce6c
3 changed files with 30 additions and 7 deletions

View File

@ -13,8 +13,6 @@ QQuickItem *DoricScrollerNode::build() {
QQuickItem *item = qobject_cast<QQuickItem *>(component.create()); QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
this->createLayouts(item); this->createLayouts(item);
getLayouts()->setLayoutType(DoricLayoutType::DoricStack);
item->setProperty("wrapper", QString::number((qint64)this)); item->setProperty("wrapper", QString::number((qint64)this));
return item; return item;
} }
@ -109,3 +107,13 @@ void DoricScrollerNode::blendSubNode(QJsonValue subProperties) {
mChildNode->blend(subProperties["props"]); mChildNode->blend(subProperties["props"]);
} }
} }
QSizeF DoricScrollerNode::sizeThatFits(QSizeF size) {
DoricLayouts *layout = (DoricLayouts *)mChildNode->getNodeView()
->property("doricLayout")
.toULongLong();
layout->apply(size);
return QSizeF(qMin(size.width(), layout->getMeasuredWidth()),
qMin(size.height(), layout->getMeasuredHeight()));
}

View File

@ -23,6 +23,8 @@ public:
virtual void afterBlended(QJsonValue jsValue) override; virtual void afterBlended(QJsonValue jsValue) override;
virtual void requestLayout() override; virtual void requestLayout() override;
QSizeF sizeThatFits(QSizeF size);
}; };
#endif // DORICSCROLLERNODE_H #endif // DORICSCROLLERNODE_H

View File

@ -1,4 +1,5 @@
#include "DoricLayouts.h" #include "DoricLayouts.h"
#include "shader/DoricScrollerNode.h"
DoricLayouts::DoricLayouts(QObject *parent) : QObject(parent) { DoricLayouts::DoricLayouts(QObject *parent) : QObject(parent) {
this->widthSpec = DoricLayoutSpec::DoricLayoutJust; this->widthSpec = DoricLayoutSpec::DoricLayoutJust;
@ -222,15 +223,27 @@ void DoricLayouts::measureContent(QSizeF targetSize) {
} }
void DoricLayouts::measureUndefinedContent(QSizeF targetSize) { void DoricLayouts::measureUndefinedContent(QSizeF targetSize) {
// begin size that fits
qreal width = this->view->width(); qreal width = this->view->width();
qreal height = this->view->height(); qreal height = this->view->height();
if (width > targetSize.width()) { if (tag == "Scroller") {
width = targetSize.width(); QObject *object =
} (QObject *)(this->view->property("wrapper").toULongLong());
if (height > targetSize.height()) { DoricScrollerNode *viewNode = dynamic_cast<DoricScrollerNode *>(object);
height = targetSize.height(); QSizeF measuredSize = viewNode->sizeThatFits(targetSize);
width = measuredSize.width();
height = measuredSize.height();
} else {
if (width > targetSize.width()) {
width = targetSize.width();
}
if (height > targetSize.height()) {
height = targetSize.height();
}
} }
// end size that fits
if (this->widthSpec == DoricLayoutSpec::DoricLayoutFit) { if (this->widthSpec == DoricLayoutSpec::DoricLayoutFit) {
setMeasuredWidth(width + this->paddingLeft + this->paddingRight); setMeasuredWidth(width + this->paddingLeft + this->paddingRight);
} }