Merge pull request #88 from nodegui/fix/layout-issues

Fixes Flexbox layout issues
This commit is contained in:
Atul R 2019-09-07 19:22:08 +02:00 committed by GitHub
commit 69e5af7a31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 26 deletions

View File

@ -9,25 +9,6 @@ class NMainWindow: public QMainWindow, public NodeWidget
NODEWIDGET_IMPLEMENTATIONS(QMainWindow)
public:
using QMainWindow::QMainWindow; //inherit all constructors of QMainWindow
private:
void calculateLayout(){
YGDirection direction = YGNodeStyleGetDirection(this->getFlexNode());
YGNodeCalculateLayout(this->getFlexNode(),width(),height(),direction);
}
bool eventFilter(QObject *object, QEvent *event) { // This will be installed on mainwidgetwrap
switch(event->type()) {
case QEvent::LayoutRequest:
case QEvent::ChildRemoved: {
calculateLayout(); break;
}
default: ; // do nothing
}
return QMainWindow::eventFilter(object, event);
}
void resizeEvent(QResizeEvent * event){
calculateLayout();
QMainWindow::resizeEvent(event);
}
};

View File

@ -4,7 +4,7 @@
#include "spdlog/spdlog.h"
#include "src/cpp/core/YogaWidget/yogawidget.h"
FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node)
FlexLayout::NodeContext* FlexLayout::getNodeContext(YGNodeRef node)
{
if(!node){
return nullptr;
@ -16,7 +16,6 @@ FlexLayout::NodeContext *FlexLayout::getNodeContext(YGNodeRef node)
FlexLayout::FlexLayout(QWidget *parentWidget, YGNodeRef parentNode): QLayout(parentWidget)
{
// spdlog::set_level(spdlog::level::off);
this->node = parentNode;
}
@ -58,7 +57,6 @@ QLayoutItem *FlexLayout::itemAt(int index) const
YGNodeRef childNode = YGNodeGetChild(this->node, static_cast<uint>(index));
NodeContext *ctx = getNodeContext(childNode);
if(!ctx){
// spdlog::info("flexlayout: itemAt null context {}",index);
return nullptr;
}
return ctx->item;
@ -91,14 +89,13 @@ void FlexLayout::addWidget(QWidget* childWidget, YGNodeRef childNode)
spdlog::warn("Flex layout's parent yoga node not set yet. Set it using setFlexNode. Child widget will not be added to Flex Layout");
return;
}
// spdlog::info("flexlayout: addWidget Object: {}",childWidget->metaObject()->className());
uint count = YGNodeGetChildCount(this->node);
YGNodeInsertChild(this->node,childNode, count);
QLayoutItem* layoutItem = new QWidgetItem(childWidget);
NodeContext* childContext = new NodeContext(layoutItem);
YGNodeSetContext(childNode, static_cast<void *>(childContext));
QLayout::addWidget(childWidget);
this->invalidate();
}
void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
@ -114,6 +111,7 @@ void FlexLayout::removeWidget(QWidget* childWidget, YGNodeRef childNode)
}
YGNodeRemoveChild(this->node, childNode);
QLayout::removeWidget(childWidget);
this->invalidate();
}
void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNode, YGNodeRef childNode)
@ -135,14 +133,30 @@ void FlexLayout::insertChildBefore(QWidget* childWidget, YGNodeRef beforeChildNo
NodeContext* childContext = new NodeContext(layoutItem);
YGNodeSetContext(childNode, static_cast<void *>(childContext));
QLayout::addWidget(childWidget);
this->invalidate();
}
YGNodeRef FlexLayout::getRootNode(YGNodeRef node){
YGNodeRef parent = node->getOwner();
if(!parent){
return node;
}else {
return getRootNode(parent);
}
}
void FlexLayout::setGeometry(const QRect &rect)
{
if(!this->node){
return;
}
YGNodeRef rootNode = getRootNode(this->node);
QWidget* parentWidget = this->parentWidget();
QWidget* window = parentWidget->window();
YGDirection direction = YGNodeStyleGetDirection(rootNode);
YGNodeCalculateLayout(rootNode,window->width(),window->height(),direction);
uint count = YGNodeGetChildCount(this->node);
for (uint i = 0; i < count; ++i) {

View File

@ -24,6 +24,7 @@ class FlexLayout: public QLayout
{
private:
YGNodeRef node;
YGNodeRef getRootNode(YGNodeRef node);
public:
struct NodeContext
{

View File

@ -49,6 +49,9 @@ button.addEventListener("clicked", () => {
const clipboard = QApplication.clipboard();
console.log("clipboard: ", clipboard.text(QClipboardMode.Clipboard));
clipboard.setText("yooooo", QClipboardMode.Clipboard);
if (rootView.layout) {
(rootView.layout as FlexLayout).removeWidget(dial);
}
});
const nodeguiLogo = new QIcon(
@ -111,7 +114,7 @@ win.setStyleSheet(`
win.setWindowIcon(nodeguiLogo);
win.setWindowTitle("NodeGUI Demo");
win.resize(400, 400);
win.resize(400, 700);
win.show();
win.setWindowState(WindowState.WindowActive);