forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflexutils.cpp
More file actions
84 lines (73 loc) · 2.52 KB
/
flexutils.cpp
File metadata and controls
84 lines (73 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "core/FlexLayout/flexutils.h"
FlexNodeContext::FlexNodeContext(void* widget) {
this->_widget = widget;
this->_layoutItem = nullptr;
this->isSizeControlled = false;
}
QWidget* FlexNodeContext::widget() {
QWidget* flexNodeWidget = static_cast<QWidget*>(this->_widget);
return flexNodeWidget;
}
QLayoutItem* FlexNodeContext::layoutItem() { return this->_layoutItem; }
void FlexNodeContext::setLayoutItem(QLayoutItem* item) {
this->_layoutItem = item;
}
QRect flexutils::getFlexNodeGeometry(YGNodeRef node) {
int width = static_cast<int>(YGNodeLayoutGetWidth(node));
int height = static_cast<int>(YGNodeLayoutGetHeight(node));
int left = static_cast<int>(YGNodeLayoutGetLeft(node));
int top = static_cast<int>(YGNodeLayoutGetTop(node));
const QRect geometry(left, top, width, height);
return geometry;
}
void flexutils::setFlexNodeGeometry(YGNodeRef node, const QRect& geometry) {
int width = geometry.width();
int height = geometry.height();
int left = geometry.left();
int top = geometry.top();
YGNodeStyleSetWidth(node, width);
YGNodeStyleSetHeight(node, height);
YGNodeStyleSetPosition(node, YGEdgeTop, top);
YGNodeStyleSetPosition(node, YGEdgeLeft, left);
}
FlexNodeContext* flexutils::getFlexNodeContext(YGNodeRef node) {
if (!node) {
return nullptr;
}
void* rawCtx = YGNodeGetContext(node);
FlexNodeContext* ctx = static_cast<FlexNodeContext*>(rawCtx);
return ctx;
}
// if true, it means this node's size can controlled by external things like
// resize handles in case of qmainwindow etc
bool flexutils::isFlexNodeSizeControlled(YGNodeRef node) {
if (!node) {
return false;
}
FlexNodeContext* ctx = getFlexNodeContext(node);
if (ctx->widget()->isWindow() || ctx->isSizeControlled) {
return true;
}
return false;
}
YGSize flexutils::measureQtWidget(YGNodeRef node, float _width,
YGMeasureMode widthMode, float _height,
YGMeasureMode heightMode) {
FlexNodeContext* ctx = getFlexNodeContext(node);
QWidget* widget = ctx->widget();
QSize size = widget->sizeHint();
float width = static_cast<float>(size.width());
float height = static_cast<float>(size.height());
return YGSize{
width,
height,
};
}
void flexutils::configureFlexNode(QWidget* widget, YGNodeRef node,
bool isLeafNode) {
FlexNodeContext* ctx = new FlexNodeContext(widget);
YGNodeSetContext(node, ctx);
if (isLeafNode) {
YGNodeSetMeasureFunc(node, &flexutils::measureQtWidget);
}
}