Skip to content

Commit 9b61c3a

Browse files
committed
Add TextView.set/getText
1 parent baeeea9 commit 9b61c3a

6 files changed

Lines changed: 142 additions & 48 deletions

File tree

patches/yoga/build.patch

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Add GN build files for Yoga.
77

88
diff --git a/BUILD.gn b/BUILD.gn
99
new file mode 100644
10-
index 0000000000000000000000000000000000000000..3d322b2de0717cef005493c79144b751c79d81f6
10+
index 00000000..3d322b2d
1111
--- /dev/null
1212
+++ b/BUILD.gn
1313
@@ -0,0 +1,48 @@
@@ -60,7 +60,7 @@ index 0000000000000000000000000000000000000000..3d322b2de0717cef005493c79144b751
6060
+ public_configs = [ ":yoga_config" ]
6161
+}
6262
diff --git a/yoga/Utils.cpp b/yoga/Utils.cpp
63-
index c4281b60fc6e164b219d85296e1eb60e3ef1e0e4..4ec7c0c085a6d31b8b2ea5879305ed19aeb04337 100644
63+
index c4281b60..4ec7c0c0 100644
6464
--- a/yoga/Utils.cpp
6565
+++ b/yoga/Utils.cpp
6666
@@ -74,5 +74,5 @@ YGFloatOptional YGFloatOptionalMax(YGFloatOptional op1, YGFloatOptional op2) {
@@ -70,3 +70,37 @@ index c4281b60fc6e164b219d85296e1eb60e3ef1e0e4..4ec7c0c085a6d31b8b2ea5879305ed19
7070
- throw std::logic_error(message);
7171
+ assert(message);
7272
}
73+
diff --git a/yoga/YGNodePrint.cpp b/yoga/YGNodePrint.cpp
74+
index 26efa485..9126de65 100644
75+
--- a/yoga/YGNodePrint.cpp
76+
+++ b/yoga/YGNodePrint.cpp
77+
@@ -5,7 +5,6 @@
78+
* LICENSE file in the root directory of this source tree.
79+
*/
80+
81+
-#ifdef DEBUG
82+
#include "YGNodePrint.h"
83+
#include <stdarg.h>
84+
#include "YGEnums.h"
85+
@@ -222,4 +221,3 @@ void YGNodeToString(
86+
}
87+
} // namespace yoga
88+
} // namespace facebook
89+
-#endif
90+
diff --git a/yoga/YGNodePrint.h b/yoga/YGNodePrint.h
91+
index 3db504b4..f1b8659d 100644
92+
--- a/yoga/YGNodePrint.h
93+
+++ b/yoga/YGNodePrint.h
94+
@@ -5,7 +5,6 @@
95+
* LICENSE file in the root directory of this source tree.
96+
*/
97+
98+
-#ifdef DEBUG
99+
#pragma once
100+
#include <string>
101+
102+
@@ -22,4 +21,3 @@ void YGNodeToString(
103+
104+
} // namespace yoga
105+
} // namespace facebook
106+
-#endif

shell/browser/api/electron_api_view.cc

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
#if BUILDFLAG(ENABLE_VIEWS_API)
1212
#include "shell/browser/api/views/yoga_layout_manager.h"
1313
#include "shell/browser/api/views/yoga_util.h"
14+
#include "shell/common/color_util.h"
1415
#include "shell/common/gin_converters/value_converter.h"
16+
#include "third_party/yoga/yoga/YGNodePrint.h"
1517
#include "third_party/yoga/yoga/Yoga.h"
1618
#include "ui/base/layout.h"
19+
#include "ui/views/background.h"
1720
#include "ui/views/widget/widget.h"
1821
#endif
1922

@@ -43,6 +46,7 @@ View::View(views::View* view) : view_(view) {
4346
#if BUILDFLAG(ENABLE_VIEWS_API)
4447
yoga_config_ = YGConfigNew();
4548
yoga_node_ = YGNodeNewWithConfig(yoga_config_);
49+
YGNodeSetContext(yoga_node_, this);
4650
AttachYogaNode(view, yoga_node_);
4751

4852
view_->SetLayoutManager(std::make_unique<YogaLayoutManager>());
@@ -81,17 +85,34 @@ void View::AddChildViewAt(gin::Handle<View> child, size_t index) {
8185
view()->Layout();
8286
}
8387

88+
void View::SetBackgroundColor(const std::string& color_name) {
89+
SkColor color = ParseHexColor(color_name);
90+
view()->SetBackground(views::CreateSolidBackground(color));
91+
}
92+
8493
void View::SetStyle(std::map<std::string, base::Value> dict) {
8594
for (const auto& it : dict) {
86-
if (it.second.is_double())
95+
if (it.first == "backgroundColor") {
96+
if (it.second.is_string())
97+
SetBackgroundColor(it.second.GetString());
98+
} else if (it.second.is_double()) {
8799
SetYogaProperty(yoga_node_, it.first, it.second.GetDouble());
88-
else if (it.second.is_int())
100+
} else if (it.second.is_int()) {
89101
SetYogaProperty(yoga_node_, it.first, it.second.GetInt());
90-
else if (it.second.is_string())
102+
} else if (it.second.is_string()) {
91103
SetYogaProperty(yoga_node_, it.first, it.second.GetString());
104+
}
92105
}
93106
}
94107

108+
std::string View::GetComputedLayout() const {
109+
std::string result;
110+
auto options = static_cast<YGPrintOptions>(
111+
YGPrintOptionsLayout | YGPrintOptionsStyle | YGPrintOptionsChildren);
112+
facebook::yoga::YGNodeToString(result, yoga_node_, options, 0);
113+
return result;
114+
}
115+
95116
void View::OnViewVisibilityChanged(views::View* observed_view,
96117
views::View* starting_view) {
97118
YGNodeStyleSetDisplay(yoga_node_,
@@ -118,7 +139,9 @@ void View::BuildPrototype(v8::Isolate* isolate,
118139
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
119140
.SetMethod("addChildView", &View::AddChildView)
120141
.SetMethod("addChildViewAt", &View::AddChildViewAt)
121-
.SetMethod("setStyle", &View::SetStyle);
142+
.SetMethod("setBackgroundColor", &View::SetBackgroundColor)
143+
.SetMethod("setStyle", &View::SetStyle)
144+
.SetMethod("getComputedLayout", &View::GetComputedLayout);
122145
#endif
123146
}
124147

shell/browser/api/electron_api_view.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ class View : public gin_helper::Wrappable<View>, public views::ViewObserver {
3535
void AddChildView(gin::Handle<View> view);
3636
void AddChildViewAt(gin::Handle<View> view, size_t index);
3737

38+
void SetBackgroundColor(const std::string& color_name);
3839
void SetStyle(std::map<std::string, base::Value> dict);
40+
std::string GetComputedLayout() const;
3941
#endif
4042

43+
#if BUILDFLAG(ENABLE_VIEWS_API)
44+
YGNodeRef yoga_node() const { return yoga_node_; }
45+
#endif
4146
views::View* view() const { return view_; }
4247

4348
protected:

shell/browser/api/views/electron_api_text_view.cc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,43 @@
88
#include "shell/common/gin_helper/dictionary.h"
99
#include "shell/common/gin_helper/object_template_builder.h"
1010
#include "shell/common/node_includes.h"
11+
#include "third_party/yoga/yoga/Yoga.h"
1112

1213
namespace electron {
1314

1415
namespace api {
1516

17+
namespace {
18+
19+
YGSize MeasureText(YGNodeRef node,
20+
float width,
21+
YGMeasureMode mode,
22+
float height,
23+
YGMeasureMode height_mode) {
24+
auto* text_view = static_cast<TextView*>(YGNodeGetContext(node));
25+
gfx::Size size = text_view->view()->GetPreferredSize();
26+
return {size.width(), size.height()};
27+
}
28+
29+
} // namespace
30+
1631
TextView::TextView(const base::string16& text)
17-
: View(new views::StyledLabel(text, this)) {}
32+
: View(new views::StyledLabel(text, this)) {
33+
YGNodeSetMeasureFunc(yoga_node(), MeasureText);
34+
YGNodeMarkDirty(yoga_node());
35+
}
1836

1937
TextView::~TextView() {}
2038

39+
void TextView::SetText(const base::string16& text) {
40+
label()->SetText(text);
41+
YGNodeMarkDirty(yoga_node());
42+
}
43+
44+
const base::string16& TextView::GetText() const {
45+
return label()->GetText();
46+
}
47+
2148
// static
2249
gin_helper::WrappableBase* TextView::New(const base::string16& text,
2350
gin_helper::Arguments* args) {
@@ -31,7 +58,9 @@ gin_helper::WrappableBase* TextView::New(const base::string16& text,
3158
void TextView::BuildPrototype(v8::Isolate* isolate,
3259
v8::Local<v8::FunctionTemplate> prototype) {
3360
prototype->SetClassName(gin::StringToV8(isolate, "TextView"));
34-
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate());
61+
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
62+
.SetMethod("setText", &TextView::SetText)
63+
.SetMethod("getText", &TextView::GetText);
3564
}
3665

3766
} // namespace api

shell/browser/api/views/electron_api_text_view.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class TextView : public View, public views::StyledLabelListener {
2121
static void BuildPrototype(v8::Isolate* isolate,
2222
v8::Local<v8::FunctionTemplate> prototype);
2323

24+
void SetText(const base::string16& text);
25+
const base::string16& GetText() const;
26+
2427
protected:
2528
explicit TextView(const base::string16& text);
2629
~TextView() override;

shell/browser/api/views/yoga_util.cc

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -158,88 +158,88 @@ using EdgeSetter = void (*)(const YGNodeRef, const YGEdge, float);
158158

159159
// Sorted list of CSS node properties.
160160
const std::tuple<const char*, IntConverter, IntSetter> int_setters[] = {
161-
{"aligncontent", AlignValue,
161+
{"alignContent", AlignValue,
162162
reinterpret_cast<IntSetter>(YGNodeStyleSetAlignContent)},
163-
{"alignitems", AlignValue,
163+
{"alignItems", AlignValue,
164164
reinterpret_cast<IntSetter>(YGNodeStyleSetAlignItems)},
165-
{"alignself", AlignValue,
165+
{"alignSelf", AlignValue,
166166
reinterpret_cast<IntSetter>(YGNodeStyleSetAlignSelf)},
167167
{"direction", DirectionValue,
168168
reinterpret_cast<IntSetter>(YGNodeStyleSetDirection)},
169169
{"display", DisplayValue,
170170
reinterpret_cast<IntSetter>(YGNodeStyleSetDisplay)},
171-
{"flexdirection", FlexDirectionValue,
171+
{"flexDirection", FlexDirectionValue,
172172
reinterpret_cast<IntSetter>(YGNodeStyleSetFlexDirection)},
173-
{"flexwrap", WrapValue,
173+
{"flexWrap", WrapValue,
174174
reinterpret_cast<IntSetter>(YGNodeStyleSetFlexWrap)},
175-
{"justifycontent", JustifyValue,
175+
{"justifyContent", JustifyValue,
176176
reinterpret_cast<IntSetter>(YGNodeStyleSetJustifyContent)},
177177
{"overflow", OverflowValue,
178178
reinterpret_cast<IntSetter>(YGNodeStyleSetOverflow)},
179179
{"position", PositionValue,
180180
reinterpret_cast<IntSetter>(YGNodeStyleSetPositionType)},
181181
};
182182
const std::pair<const char*, FloatSetter> float_setters[] = {
183-
{"aspectratio", YGNodeStyleSetAspectRatio},
183+
{"aspectRatio", YGNodeStyleSetAspectRatio},
184184
{"flex", YGNodeStyleSetFlex},
185-
{"flexbasis", YGNodeStyleSetFlexBasis},
186-
{"flexgrow", YGNodeStyleSetFlexGrow},
187-
{"flexshrink", YGNodeStyleSetFlexShrink},
185+
{"flexBasis", YGNodeStyleSetFlexBasis},
186+
{"flexGrow", YGNodeStyleSetFlexGrow},
187+
{"flexShrink", YGNodeStyleSetFlexShrink},
188188
{"height", YGNodeStyleSetHeight},
189-
{"maxheight", YGNodeStyleSetMaxHeight},
190-
{"maxwidth", YGNodeStyleSetMaxWidth},
191-
{"minheight", YGNodeStyleSetMinHeight},
192-
{"minwidth", YGNodeStyleSetMinWidth},
189+
{"maxHeight", YGNodeStyleSetMaxHeight},
190+
{"maxWidth", YGNodeStyleSetMaxWidth},
191+
{"minHeight", YGNodeStyleSetMinHeight},
192+
{"minWidth", YGNodeStyleSetMinWidth},
193193
{"width", YGNodeStyleSetWidth},
194194
};
195195
const std::pair<const char*, AutoSetter> auto_setters[] = {
196-
{"flexbasis", YGNodeStyleSetFlexBasisAuto},
196+
{"flexBasis", YGNodeStyleSetFlexBasisAuto},
197197
{"height", YGNodeStyleSetHeightAuto},
198198
{"width", YGNodeStyleSetWidthAuto},
199199
};
200200
const std::pair<const char*, FloatSetter> percent_setters[] = {
201-
{"flexbasis", YGNodeStyleSetFlexBasisPercent},
201+
{"flexBasis", YGNodeStyleSetFlexBasisPercent},
202202
{"height", YGNodeStyleSetHeightPercent},
203-
{"maxheight", YGNodeStyleSetMaxHeightPercent},
204-
{"maxwidth", YGNodeStyleSetMaxWidthPercent},
205-
{"minheight", YGNodeStyleSetMinHeightPercent},
206-
{"minwidth", YGNodeStyleSetMinWidthPercent},
203+
{"maxHeight", YGNodeStyleSetMaxHeightPercent},
204+
{"maxWidth", YGNodeStyleSetMaxWidthPercent},
205+
{"minHeight", YGNodeStyleSetMinHeightPercent},
206+
{"minWidth", YGNodeStyleSetMinWidthPercent},
207207
{"width", YGNodeStyleSetWidthPercent},
208208
};
209209
const std::tuple<const char*, YGEdge, EdgeSetter> edge_setters[] = {
210210
{"border", YGEdgeAll, YGNodeStyleSetBorder},
211-
{"borderbottom", YGEdgeBottom, YGNodeStyleSetBorder},
212-
{"borderleft", YGEdgeLeft, YGNodeStyleSetBorder},
213-
{"borderright", YGEdgeRight, YGNodeStyleSetBorder},
214-
{"bordertop", YGEdgeTop, YGNodeStyleSetBorder},
211+
{"borderBottom", YGEdgeBottom, YGNodeStyleSetBorder},
212+
{"borderLeft", YGEdgeLeft, YGNodeStyleSetBorder},
213+
{"borderRight", YGEdgeRight, YGNodeStyleSetBorder},
214+
{"borderTop", YGEdgeTop, YGNodeStyleSetBorder},
215215
{"bottom", YGEdgeBottom, YGNodeStyleSetPosition},
216216
{"left", YGEdgeLeft, YGNodeStyleSetPosition},
217217
{"margin", YGEdgeAll, YGNodeStyleSetMargin},
218-
{"marginbottom", YGEdgeBottom, YGNodeStyleSetMargin},
219-
{"marginleft", YGEdgeLeft, YGNodeStyleSetMargin},
220-
{"marginright", YGEdgeRight, YGNodeStyleSetMargin},
221-
{"margintop", YGEdgeTop, YGNodeStyleSetMargin},
218+
{"marginBottom", YGEdgeBottom, YGNodeStyleSetMargin},
219+
{"marginLeft", YGEdgeLeft, YGNodeStyleSetMargin},
220+
{"marginRight", YGEdgeRight, YGNodeStyleSetMargin},
221+
{"marginTop", YGEdgeTop, YGNodeStyleSetMargin},
222222
{"padding", YGEdgeAll, YGNodeStyleSetPadding},
223-
{"paddingbottom", YGEdgeBottom, YGNodeStyleSetPadding},
224-
{"paddingleft", YGEdgeLeft, YGNodeStyleSetPadding},
225-
{"paddingright", YGEdgeRight, YGNodeStyleSetPadding},
226-
{"paddingtop", YGEdgeTop, YGNodeStyleSetPadding},
223+
{"paddingBottom", YGEdgeBottom, YGNodeStyleSetPadding},
224+
{"paddingLeft", YGEdgeLeft, YGNodeStyleSetPadding},
225+
{"paddingRight", YGEdgeRight, YGNodeStyleSetPadding},
226+
{"paddingTop", YGEdgeTop, YGNodeStyleSetPadding},
227227
{"right", YGEdgeRight, YGNodeStyleSetPosition},
228228
{"top", YGEdgeTop, YGNodeStyleSetPosition},
229229
};
230230
const std::tuple<const char*, YGEdge, EdgeSetter> edge_percent_setters[] = {
231231
{"bottom", YGEdgeBottom, YGNodeStyleSetPositionPercent},
232232
{"left", YGEdgeLeft, YGNodeStyleSetPositionPercent},
233233
{"margin", YGEdgeAll, YGNodeStyleSetMarginPercent},
234-
{"marginbottom", YGEdgeBottom, YGNodeStyleSetMarginPercent},
235-
{"marginleft", YGEdgeLeft, YGNodeStyleSetMarginPercent},
236-
{"marginright", YGEdgeRight, YGNodeStyleSetMarginPercent},
237-
{"margintop", YGEdgeTop, YGNodeStyleSetMarginPercent},
234+
{"marginBottom", YGEdgeBottom, YGNodeStyleSetMarginPercent},
235+
{"marginLeft", YGEdgeLeft, YGNodeStyleSetMarginPercent},
236+
{"marginRight", YGEdgeRight, YGNodeStyleSetMarginPercent},
237+
{"marginTop", YGEdgeTop, YGNodeStyleSetMarginPercent},
238238
{"padding", YGEdgeAll, YGNodeStyleSetPaddingPercent},
239-
{"paddingbottom", YGEdgeBottom, YGNodeStyleSetPaddingPercent},
240-
{"paddingleft", YGEdgeLeft, YGNodeStyleSetPaddingPercent},
241-
{"paddingright", YGEdgeRight, YGNodeStyleSetPaddingPercent},
242-
{"paddingtop", YGEdgeTop, YGNodeStyleSetPaddingPercent},
239+
{"paddingBottom", YGEdgeBottom, YGNodeStyleSetPaddingPercent},
240+
{"paddingLeft", YGEdgeLeft, YGNodeStyleSetPaddingPercent},
241+
{"paddingRight", YGEdgeRight, YGNodeStyleSetPaddingPercent},
242+
{"paddingTop", YGEdgeTop, YGNodeStyleSetPaddingPercent},
243243
{"right", YGEdgeRight, YGNodeStyleSetPositionPercent},
244244
{"top", YGEdgeTop, YGNodeStyleSetPositionPercent},
245245
};

0 commit comments

Comments
 (0)