-
Notifications
You must be signed in to change notification settings - Fork 15k
/
electron_api_view.cc
415 lines (363 loc) · 13.1 KB
/
electron_api_view.cc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/api/electron_api_view.h"
#include <algorithm>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include "gin/data_object_builder.h"
#include "gin/wrappable.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
#include "ui/views/background.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/layout_manager_base.h"
#if BUILDFLAG(IS_MAC)
#include "shell/browser/animation_util.h"
#endif
namespace gin {
template <>
struct Converter<views::ChildLayout> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
views::ChildLayout* out) {
gin_helper::Dictionary dict;
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
gin::Handle<electron::api::View> view;
if (!dict.Get("view", &view))
return false;
out->child_view = view->view();
if (dict.Has("bounds"))
dict.Get("bounds", &out->bounds);
out->visible = true;
if (dict.Has("visible"))
dict.Get("visible", &out->visible);
return true;
}
};
template <>
struct Converter<views::ProposedLayout> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
views::ProposedLayout* out) {
gin_helper::Dictionary dict;
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
if (!dict.Get("size", &out->host_size))
return false;
if (!dict.Get("layouts", &out->child_layouts))
return false;
return true;
}
};
template <>
struct Converter<views::LayoutOrientation> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
views::LayoutOrientation* out) {
std::string orientation = base::ToLowerASCII(gin::V8ToString(isolate, val));
if (orientation == "horizontal") {
*out = views::LayoutOrientation::kHorizontal;
} else if (orientation == "vertical") {
*out = views::LayoutOrientation::kVertical;
} else {
return false;
}
return true;
}
};
template <>
struct Converter<views::LayoutAlignment> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
views::LayoutAlignment* out) {
std::string orientation = base::ToLowerASCII(gin::V8ToString(isolate, val));
if (orientation == "start") {
*out = views::LayoutAlignment::kStart;
} else if (orientation == "center") {
*out = views::LayoutAlignment::kCenter;
} else if (orientation == "end") {
*out = views::LayoutAlignment::kEnd;
} else if (orientation == "stretch") {
*out = views::LayoutAlignment::kStretch;
} else if (orientation == "baseline") {
*out = views::LayoutAlignment::kBaseline;
} else {
return false;
}
return true;
}
};
template <>
struct Converter<views::FlexAllocationOrder> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
views::FlexAllocationOrder* out) {
std::string orientation = base::ToLowerASCII(gin::V8ToString(isolate, val));
if (orientation == "normal") {
*out = views::FlexAllocationOrder::kNormal;
} else if (orientation == "reverse") {
*out = views::FlexAllocationOrder::kReverse;
} else {
return false;
}
return true;
}
};
template <>
struct Converter<views::SizeBound> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const views::SizeBound& in) {
if (in.is_bounded())
return v8::Integer::New(isolate, in.value());
return v8::Number::New(isolate, std::numeric_limits<double>::infinity());
}
};
template <>
struct Converter<views::SizeBounds> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const views::SizeBounds& in) {
return gin::DataObjectBuilder(isolate)
.Set("width", in.width())
.Set("height", in.height())
.Build();
}
};
} // namespace gin
namespace electron::api {
using LayoutCallback = base::RepeatingCallback<views::ProposedLayout(
const views::SizeBounds& size_bounds)>;
class JSLayoutManager : public views::LayoutManagerBase {
public:
explicit JSLayoutManager(LayoutCallback layout_callback)
: layout_callback_(std::move(layout_callback)) {}
~JSLayoutManager() override {}
views::ProposedLayout CalculateProposedLayout(
const views::SizeBounds& size_bounds) const override {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
return layout_callback_.Run(size_bounds);
}
private:
LayoutCallback layout_callback_;
};
View::View(views::View* view) : view_(view) {
view_->set_owned_by_client();
view_->AddObserver(this);
}
View::View() : View(new views::View()) {}
View::~View() {
if (!view_)
return;
view_->RemoveObserver(this);
if (delete_view_)
delete view_;
}
void View::ReorderChildView(gin::Handle<View> child, size_t index) {
view_->ReorderChildView(child->view(), index);
const auto i = base::ranges::find(child_views_, child.ToV8());
DCHECK(i != child_views_.end());
// If |view| is already at the desired position, there's nothing to do.
const auto pos = std::next(
child_views_.begin(),
static_cast<ptrdiff_t>(std::min(index, child_views_.size() - 1)));
if (i == pos)
return;
if (pos < i) {
std::rotate(pos, i, std::next(i));
} else {
std::rotate(i, std::next(i), std::next(pos));
}
}
void View::AddChildViewAt(gin::Handle<View> child,
std::optional<size_t> maybe_index) {
// TODO(nornagon): !view_ is only for supporting the weird case of
// WebContentsView's view being deleted when the underlying WebContents is
// destroyed (on non-Mac). We should fix that so that WebContentsView always
// has a View, possibly a wrapper view around the underlying platform View.
if (!view_)
return;
// This will CHECK and crash in View::AddChildViewAtImpl if not handled here.
if (view_ == child->view()) {
gin_helper::ErrorThrower(isolate()).ThrowError(
"A view cannot be added as its own child");
return;
}
size_t index =
std::min(child_views_.size(), maybe_index.value_or(child_views_.size()));
// If the child is already a child of this view, just reorder it.
// This matches the behavior of View::AddChildViewAtImpl and
// otherwise will CHECK if the same view is added multiple times.
if (child->view()->parent() == view_) {
ReorderChildView(child, index);
return;
}
child_views_.emplace(child_views_.begin() + index, // index
isolate(), child->GetWrapper()); // v8::Global(args...)
#if BUILDFLAG(IS_MAC)
// Disable the implicit CALayer animations that happen by default when adding
// or removing sublayers.
// See
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/ReactingtoLayerChanges/ReactingtoLayerChanges.html
// and https://github.com/electron/electron/pull/14911
// TODO(nornagon): Disabling these CALayer animations (which are specific to
// WebContentsView, I think) seems like this is something that remote_cocoa
// or views should be taking care of, but isn't. This should be pushed
// upstream.
ScopedCAActionDisabler disable_animations;
#endif
view_->AddChildViewAt(child->view(), index);
}
void View::RemoveChildView(gin::Handle<View> child) {
if (!view_)
return;
if (!child->view())
return;
const auto it = base::ranges::find(child_views_, child.ToV8());
if (it != child_views_.end()) {
#if BUILDFLAG(IS_MAC)
ScopedCAActionDisabler disable_animations;
#endif
view_->RemoveChildView(child->view());
child_views_.erase(it);
}
}
void View::SetBounds(const gfx::Rect& bounds) {
if (!view_)
return;
view_->SetBoundsRect(bounds);
}
gfx::Rect View::GetBounds() {
if (!view_)
return gfx::Rect();
return view_->bounds();
}
void View::SetLayout(v8::Isolate* isolate, v8::Local<v8::Object> value) {
if (!view_)
return;
gin_helper::Dictionary dict(isolate, value);
LayoutCallback calculate_proposed_layout;
if (dict.Get("calculateProposedLayout", &calculate_proposed_layout)) {
view_->SetLayoutManager(std::make_unique<JSLayoutManager>(
std::move(calculate_proposed_layout)));
} else {
auto* layout =
view_->SetLayoutManager(std::make_unique<views::FlexLayout>());
views::LayoutOrientation orientation;
if (dict.Get("orientation", &orientation))
layout->SetOrientation(orientation);
views::LayoutAlignment main_axis_alignment;
if (dict.Get("mainAxisAlignment", &main_axis_alignment))
layout->SetMainAxisAlignment(main_axis_alignment);
views::LayoutAlignment cross_axis_alignment;
if (dict.Get("crossAxisAlignment", &cross_axis_alignment))
layout->SetCrossAxisAlignment(cross_axis_alignment);
gfx::Insets interior_margin;
if (dict.Get("interiorMargin", &interior_margin))
layout->SetInteriorMargin(interior_margin);
int minimum_cross_axis_size;
if (dict.Get("minimumCrossAxisSize", &minimum_cross_axis_size))
layout->SetMinimumCrossAxisSize(minimum_cross_axis_size);
bool collapse_margins;
if (dict.Has("collapseMargins") &&
dict.Get("collapseMargins", &collapse_margins))
layout->SetCollapseMargins(collapse_margins);
bool include_host_insets_in_layout;
if (dict.Has("includeHostInsetsInLayout") &&
dict.Get("includeHostInsetsInLayout", &include_host_insets_in_layout))
layout->SetIncludeHostInsetsInLayout(include_host_insets_in_layout);
bool ignore_default_main_axis_margins;
if (dict.Has("ignoreDefaultMainAxisMargins") &&
dict.Get("ignoreDefaultMainAxisMargins",
&ignore_default_main_axis_margins))
layout->SetIgnoreDefaultMainAxisMargins(ignore_default_main_axis_margins);
views::FlexAllocationOrder flex_allocation_order;
if (dict.Get("flexAllocationOrder", &flex_allocation_order))
layout->SetFlexAllocationOrder(flex_allocation_order);
}
}
std::vector<v8::Local<v8::Value>> View::GetChildren() {
std::vector<v8::Local<v8::Value>> ret;
ret.reserve(child_views_.size());
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
for (auto& child_view : child_views_)
ret.push_back(child_view.Get(isolate));
return ret;
}
void View::SetBackgroundColor(std::optional<WrappedSkColor> color) {
if (!view_)
return;
view_->SetBackground(color ? views::CreateSolidBackground(*color) : nullptr);
}
void View::SetVisible(bool visible) {
if (!view_)
return;
view_->SetVisible(visible);
}
void View::OnViewBoundsChanged(views::View* observed_view) {
Emit("bounds-changed");
}
void View::OnViewIsDeleting(views::View* observed_view) {
DCHECK_EQ(observed_view, view_);
view_ = nullptr;
}
// static
gin_helper::WrappableBase* View::New(gin::Arguments* args) {
View* view = new View();
view->InitWithArgs(args);
return view;
}
// static
v8::Local<v8::Function> View::GetConstructor(v8::Isolate* isolate) {
static base::NoDestructor<v8::Global<v8::Function>> constructor;
if (constructor.get()->IsEmpty()) {
constructor->Reset(isolate, gin_helper::CreateConstructor<View>(
isolate, base::BindRepeating(&View::New)));
}
return v8::Local<v8::Function>::New(isolate, *constructor.get());
}
// static
gin::Handle<View> View::Create(v8::Isolate* isolate) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Object> obj;
if (GetConstructor(isolate)->NewInstance(context, 0, nullptr).ToLocal(&obj)) {
gin::Handle<View> view;
if (gin::ConvertFromV8(isolate, obj, &view))
return view;
}
return gin::Handle<View>();
}
// static
void View::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "View"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("addChildView", &View::AddChildViewAt)
.SetMethod("removeChildView", &View::RemoveChildView)
.SetProperty("children", &View::GetChildren)
.SetMethod("setBounds", &View::SetBounds)
.SetMethod("getBounds", &View::GetBounds)
.SetMethod("setBackgroundColor", &View::SetBackgroundColor)
.SetMethod("setLayout", &View::SetLayout)
.SetMethod("setVisible", &View::SetVisible);
}
} // namespace electron::api
namespace {
using electron::api::View;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("View", View::GetConstructor(isolate));
}
} // namespace
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_view, Initialize)