Merge pull request #13044 from electron/more-view-apis

Add layout support for View API
This commit is contained in:
Cheng Zhao 2018-05-24 16:34:03 +09:00 коммит произвёл GitHub
Родитель b3edfd7d89 0f7c25fc63
Коммит a798a40026
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
17 изменённых файлов: 371 добавлений и 19 удалений

Просмотреть файл

@ -0,0 +1,87 @@
// 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 "atom/browser/api/atom_api_box_layout.h"
#include <string>
#include "atom/browser/api/atom_api_view.h"
#include "atom/common/api/constructor.h"
#include "native_mate/dictionary.h"
#include "atom/common/node_includes.h"
namespace mate {
template <>
struct Converter<views::BoxLayout::Orientation> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
views::BoxLayout::Orientation* out) {
std::string orientation;
if (!ConvertFromV8(isolate, val, &orientation))
return false;
if (orientation == "horizontal")
*out = views::BoxLayout::kHorizontal;
else if (orientation == "vertical")
*out = views::BoxLayout::kVertical;
else
return false;
return true;
}
};
} // namespace mate
namespace atom {
namespace api {
BoxLayout::BoxLayout(views::BoxLayout::Orientation orientation)
: LayoutManager(new views::BoxLayout(orientation)) {}
BoxLayout::~BoxLayout() {}
void BoxLayout::SetFlexForView(mate::Handle<View> view, int flex) {
auto* box_layout = static_cast<views::BoxLayout*>(layout_manager());
box_layout->SetFlexForView(view->view(), flex);
}
// static
mate::WrappableBase* BoxLayout::New(mate::Arguments* args,
views::BoxLayout::Orientation orientation) {
auto* layout = new BoxLayout(orientation);
layout->InitWith(args->isolate(), args->GetThis());
return layout;
}
// static
void BoxLayout::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "BoxLayout"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("setFlexForView", &BoxLayout::SetFlexForView);
}
} // namespace api
} // namespace atom
namespace {
using atom::api::BoxLayout;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("BoxLayout", mate::CreateConstructor<BoxLayout>(
isolate, base::Bind(&BoxLayout::New)));
}
} // namespace
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_box_layout, Initialize)

Просмотреть файл

@ -0,0 +1,40 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_ATOM_API_BOX_LAYOUT_H_
#define ATOM_BROWSER_API_ATOM_API_BOX_LAYOUT_H_
#include "atom/browser/api/atom_api_layout_manager.h"
#include "native_mate/handle.h"
#include "ui/views/layout/box_layout.h"
namespace atom {
namespace api {
class View;
class BoxLayout : public LayoutManager {
public:
static mate::WrappableBase* New(mate::Arguments* args,
views::BoxLayout::Orientation orientation);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
void SetFlexForView(mate::Handle<View> view, int flex);
protected:
explicit BoxLayout(views::BoxLayout::Orientation orientation);
~BoxLayout() override;
private:
DISALLOW_COPY_AND_ASSIGN(BoxLayout);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_BOX_LAYOUT_H_

Просмотреть файл

@ -9,6 +9,7 @@
#include "atom/browser/web_contents_preferences.h"
#include "atom/browser/window_list.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/api/constructor.h"
#include "atom/common/color_util.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/value_converter.h"
@ -428,17 +429,9 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
// Calling SetConstructor would only use TopLevelWindow's prototype.
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
isolate,
base::Bind(
&mate::internal::InvokeNew<mate::WrappableBase*(mate::Arguments*)>,
base::Bind(&BrowserWindow::New)));
templ->InstanceTemplate()->SetInternalFieldCount(1);
BrowserWindow::BuildPrototype(isolate, templ);
mate::Dictionary dict(isolate, exports);
dict.Set("BrowserWindow", templ->GetFunction());
dict.Set("BrowserWindow", mate::CreateConstructor<BrowserWindow>(
isolate, base::Bind(&BrowserWindow::New)));
}
} // namespace

Просмотреть файл

@ -0,0 +1,63 @@
// 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 "atom/browser/api/atom_api_layout_manager.h"
#include "atom/common/api/constructor.h"
#include "native_mate/dictionary.h"
#include "atom/common/node_includes.h"
namespace atom {
namespace api {
LayoutManager::LayoutManager(views::LayoutManager* layout_manager)
: layout_manager_(layout_manager) {
DCHECK(layout_manager_);
}
LayoutManager::~LayoutManager() {
if (managed_by_us_)
delete layout_manager_;
}
std::unique_ptr<views::LayoutManager> LayoutManager::TakeOver() {
if (!managed_by_us_) // already taken over.
return nullptr;
managed_by_us_ = false;
return std::unique_ptr<views::LayoutManager>(layout_manager_);
}
// static
mate::WrappableBase* LayoutManager::New(mate::Arguments* args) {
args->ThrowError("LayoutManager can not be created directly");
return nullptr;
}
// static
void LayoutManager::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {}
} // namespace api
} // namespace atom
namespace {
using atom::api::LayoutManager;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("LayoutManager", mate::CreateConstructor<LayoutManager>(
isolate, base::Bind(&LayoutManager::New)));
}
} // namespace
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_layout_manager, Initialize)

Просмотреть файл

@ -0,0 +1,44 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_ATOM_API_LAYOUT_MANAGER_H_
#define ATOM_BROWSER_API_ATOM_API_LAYOUT_MANAGER_H_
#include <memory>
#include "atom/browser/api/trackable_object.h"
#include "ui/views/layout/layout_manager.h"
namespace atom {
namespace api {
class LayoutManager : public mate::TrackableObject<LayoutManager> {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
// Take over the ownership of the LayoutManager, and leave weak ref here.
std::unique_ptr<views::LayoutManager> TakeOver();
views::LayoutManager* layout_manager() const { return layout_manager_; }
protected:
explicit LayoutManager(views::LayoutManager* layout_manager);
~LayoutManager() override;
private:
bool managed_by_us_ = true;
views::LayoutManager* layout_manager_;
DISALLOW_COPY_AND_ASSIGN(LayoutManager);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_LAYOUT_MANAGER_H_

Просмотреть файл

@ -23,6 +23,27 @@ View::~View() {
delete view_;
}
#if defined(ENABLE_VIEW_API)
void View::SetLayoutManager(mate::Handle<LayoutManager> layout_manager) {
layout_manager_.Reset(isolate(), layout_manager->GetWrapper());
// TODO(zcbenz): New versions of Chrome takes std::unique_ptr instead of raw
// pointer, remove the "release()" call when we upgraded to it.
view()->SetLayoutManager(layout_manager->TakeOver().release());
}
void View::AddChildView(mate::Handle<View> child) {
AddChildViewAt(child, child_views_.size());
}
void View::AddChildViewAt(mate::Handle<View> child, size_t index) {
if (index > child_views_.size())
return;
child_views_.emplace(child_views_.begin() + index, // index
isolate(), child->GetWrapper()); // v8::Global(args...)
view()->AddChildViewAt(child->view(), index);
}
#endif
// static
mate::WrappableBase* View::New(mate::Arguments* args) {
auto* view = new View();
@ -32,7 +53,15 @@ mate::WrappableBase* View::New(mate::Arguments* args) {
// static
void View::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {}
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "View"));
#if defined(ENABLE_VIEW_API)
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("setLayoutManager", &View::SetLayoutManager)
.SetMethod("addChildView", &View::AddChildView)
.SetMethod("addChildViewAt", &View::AddChildViewAt);
#endif
}
} // namespace api

Просмотреть файл

@ -6,8 +6,10 @@
#define ATOM_BROWSER_API_ATOM_API_VIEW_H_
#include <memory>
#include <vector>
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/api/atom_api_layout_manager.h"
#include "native_mate/handle.h"
#include "ui/views/view.h"
namespace atom {
@ -21,6 +23,12 @@ class View : public mate::TrackableObject<View> {
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
#if defined(ENABLE_VIEW_API)
void SetLayoutManager(mate::Handle<LayoutManager> layout_manager);
void AddChildView(mate::Handle<View> view);
void AddChildViewAt(mate::Handle<View> view, size_t index);
#endif
views::View* view() const { return view_; }
protected:
@ -32,6 +40,9 @@ class View : public mate::TrackableObject<View> {
void set_delete_view(bool should) { delete_view_ = should; }
private:
v8::Global<v8::Object> layout_manager_;
std::vector<v8::Global<v8::Object>> child_views_;
bool delete_view_ = true;
views::View* view_ = nullptr;

Просмотреть файл

@ -5,6 +5,7 @@
#include "atom/browser/api/atom_api_web_contents_view.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/api/constructor.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "content/public/browser/web_contents_user_data.h"
#include "native_mate/dictionary.h"
@ -117,13 +118,9 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
WebContentsView::SetConstructor(isolate, base::Bind(&WebContentsView::New));
mate::Dictionary constructor(
isolate, WebContentsView::GetConstructor(isolate)->GetFunction());
mate::Dictionary dict(isolate, exports);
dict.Set("WebContentsView", constructor);
dict.Set("WebContentsView", mate::CreateConstructor<WebContentsView>(
isolate, base::Bind(&WebContentsView::New)));
}
} // namespace

Просмотреть файл

@ -14,7 +14,7 @@ DelayedNativeViewHost::~DelayedNativeViewHost() {}
void DelayedNativeViewHost::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
NativeViewHost::ViewHierarchyChanged(details);
if (details.is_add)
if (details.is_add && GetWidget())
Attach(native_view_);
}

Просмотреть файл

@ -0,0 +1,33 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_API_CONSTRUCTOR_H_
#define ATOM_COMMON_API_CONSTRUCTOR_H_
#include "native_mate/constructor.h"
namespace mate {
// Create a FunctionTemplate that can be "new"ed in JavaScript.
// It is user's responsibility to ensure this function is called for one type
// only ONCE in the program's whole lifetime, otherwise we would have memory
// leak.
template <typename T, typename Sig>
v8::Local<v8::Function> CreateConstructor(v8::Isolate* isolate,
const base::Callback<Sig>& func) {
#ifndef NDEBUG
static bool called = false;
CHECK(!called) << "CreateConstructor can only be called for one type once";
called = true;
#endif
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
isolate, base::Bind(&mate::internal::InvokeNew<Sig>, func));
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
return templ->GetFunction();
}
} // namespace mate
#endif // ATOM_COMMON_API_CONSTRUCTOR_H_

Просмотреть файл

@ -65,6 +65,10 @@
V(atom_renderer_ipc) \
V(atom_renderer_web_frame)
#define ELECTRON_VIEW_MODULES(V) \
V(atom_browser_box_layout) \
V(atom_browser_layout_manager)
// This is used to load built-in modules. Instead of using
// __attribute__((constructor)), we call the _register_<modname>
// function for each built-in modules explicitly. This is only
@ -72,6 +76,9 @@
// implementation when calling the NODE_BUILTIN_MODULE_CONTEXT_AWARE.
#define V(modname) void _register_##modname();
ELECTRON_BUILTIN_MODULES(V)
#if defined(ENABLE_VIEW_API)
ELECTRON_VIEW_MODULES(V)
#endif
#undef V
namespace {
@ -165,6 +172,9 @@ NodeBindings::~NodeBindings() {
void NodeBindings::RegisterBuiltinModules() {
#define V(modname) _register_##modname();
ELECTRON_BUILTIN_MODULES(V)
#if defined(ENABLE_VIEW_API)
ELECTRON_VIEW_MODULES(V)
#endif
#undef V
}

Просмотреть файл

@ -38,6 +38,11 @@
'ENABLE_RUN_AS_NODE',
],
}], # enable_run_as_node
['enable_view_api==1', {
'defines': [
'ENABLE_VIEW_API',
],
}], # enable_view_api
],
},
'targets': [

Просмотреть файл

@ -5,9 +5,11 @@
'enable_osr%': 1, # FIXME(alexeykuzmin)
'enable_pdf_viewer%': 0, # FIXME(deepak1556)
'enable_run_as_node%': 1,
'enable_view_api%': 0,
},
'enable_osr%': '<(enable_osr)',
'enable_pdf_viewer%': '<(enable_pdf_viewer)',
'enable_run_as_node%': '<(enable_run_as_node)',
'enable_view_api%': '<(enable_view_api)',
},
}

Просмотреть файл

@ -436,6 +436,7 @@
'atom/common/api/atom_api_v8_util.cc',
'atom/common/api/atom_bindings.cc',
'atom/common/api/atom_bindings.h',
'atom/common/api/constructor.h',
'atom/common/api/event_emitter_caller.cc',
'atom/common/api/event_emitter_caller.h',
'atom/common/api/features.cc',
@ -774,6 +775,18 @@
'atom/app/node_main.h',
],
}], # enable_run_as_node
['enable_view_api==1', {
'js_sources': [
'lib/browser/api/box-layout.js',
'lib/browser/api/layout-manager.js',
],
'lib_sources': [
'atom/browser/api/atom_api_box_layout.cc',
'atom/browser/api/atom_api_box_layout.h',
'atom/browser/api/atom_api_layout_manager.cc',
'atom/browser/api/atom_api_layout_manager.h',
],
}], # enable_view_api
['mas_build==1', {
'lib_sources': [
'atom/browser/api/atom_api_app_mas.mm',

Просмотреть файл

@ -0,0 +1,15 @@
'use strict'
const electron = require('electron')
const {LayoutManager} = electron
const {BoxLayout} = process.atomBinding('box_layout')
Object.setPrototypeOf(BoxLayout.prototype, LayoutManager.prototype)
BoxLayout.prototype._init = function () {
// Call parent class's _init.
LayoutManager.prototype._init.call(this)
}
module.exports = BoxLayout

Просмотреть файл

@ -0,0 +1,8 @@
'use strict'
const {LayoutManager} = process.atomBinding('layout_manager')
LayoutManager.prototype._init = function () {
}
module.exports = LayoutManager

Просмотреть файл

@ -2,6 +2,7 @@
module.exports = [
{name: 'app', file: 'app'},
{name: 'autoUpdater', file: 'auto-updater'},
{name: 'BoxLayout', file: 'box-layout'},
{name: 'BrowserView', file: 'browser-view'},
{name: 'BrowserWindow', file: 'browser-window'},
{name: 'contentTracing', file: 'content-tracing'},
@ -9,6 +10,7 @@ module.exports = [
{name: 'globalShortcut', file: 'global-shortcut'},
{name: 'ipcMain', file: 'ipc-main'},
{name: 'inAppPurchase', file: 'in-app-purchase'},
{name: 'LayoutManager', file: 'layout-manager'},
{name: 'Menu', file: 'menu'},
{name: 'MenuItem', file: 'menu-item'},
{name: 'net', file: 'net'},