Add View.addChildView API
This commit is contained in:
parent
322bde526c
commit
2c8dc9e0bd
5 changed files with 36 additions and 3 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "atom/browser/api/atom_api_view.h"
|
||||||
#include "atom/common/api/constructor.h"
|
#include "atom/common/api/constructor.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
|
||||||
|
@ -42,6 +43,11 @@ BoxLayout::BoxLayout(views::BoxLayout::Orientation orientation)
|
||||||
|
|
||||||
BoxLayout::~BoxLayout() {}
|
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
|
// static
|
||||||
mate::WrappableBase* BoxLayout::New(mate::Arguments* args,
|
mate::WrappableBase* BoxLayout::New(mate::Arguments* args,
|
||||||
views::BoxLayout::Orientation orientation) {
|
views::BoxLayout::Orientation orientation) {
|
||||||
|
@ -52,7 +58,11 @@ mate::WrappableBase* BoxLayout::New(mate::Arguments* args,
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void BoxLayout::BuildPrototype(v8::Isolate* isolate,
|
void BoxLayout::BuildPrototype(v8::Isolate* isolate,
|
||||||
v8::Local<v8::FunctionTemplate> prototype) {}
|
v8::Local<v8::FunctionTemplate> prototype) {
|
||||||
|
prototype->SetClassName(mate::StringToV8(isolate, "BoxLayout"));
|
||||||
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||||
|
.SetMethod("setFlexForView", &BoxLayout::SetFlexForView);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
||||||
|
|
|
@ -6,12 +6,15 @@
|
||||||
#define 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 "atom/browser/api/atom_api_layout_manager.h"
|
||||||
|
#include "native_mate/handle.h"
|
||||||
#include "ui/views/layout/box_layout.h"
|
#include "ui/views/layout/box_layout.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
|
class View;
|
||||||
|
|
||||||
class BoxLayout : public LayoutManager {
|
class BoxLayout : public LayoutManager {
|
||||||
public:
|
public:
|
||||||
static mate::WrappableBase* New(mate::Arguments* args,
|
static mate::WrappableBase* New(mate::Arguments* args,
|
||||||
|
@ -20,6 +23,8 @@ class BoxLayout : public LayoutManager {
|
||||||
static void BuildPrototype(v8::Isolate* isolate,
|
static void BuildPrototype(v8::Isolate* isolate,
|
||||||
v8::Local<v8::FunctionTemplate> prototype);
|
v8::Local<v8::FunctionTemplate> prototype);
|
||||||
|
|
||||||
|
void SetFlexForView(mate::Handle<View> view, int flex);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit BoxLayout(views::BoxLayout::Orientation orientation);
|
explicit BoxLayout(views::BoxLayout::Orientation orientation);
|
||||||
~BoxLayout() override;
|
~BoxLayout() override;
|
||||||
|
|
|
@ -30,6 +30,18 @@ void View::SetLayoutManager(mate::Handle<LayoutManager> layout_manager) {
|
||||||
view()->SetLayoutManager(layout_manager->TakeOver().release());
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::WrappableBase* View::New(mate::Arguments* args) {
|
mate::WrappableBase* View::New(mate::Arguments* args) {
|
||||||
auto* view = new View();
|
auto* view = new View();
|
||||||
|
@ -42,7 +54,9 @@ void View::BuildPrototype(v8::Isolate* isolate,
|
||||||
v8::Local<v8::FunctionTemplate> prototype) {
|
v8::Local<v8::FunctionTemplate> prototype) {
|
||||||
prototype->SetClassName(mate::StringToV8(isolate, "View"));
|
prototype->SetClassName(mate::StringToV8(isolate, "View"));
|
||||||
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||||
.SetMethod("setLayoutManager", &View::SetLayoutManager);
|
.SetMethod("setLayoutManager", &View::SetLayoutManager)
|
||||||
|
.SetMethod("addChildView", &View::AddChildView)
|
||||||
|
.SetMethod("addChildViewAt", &View::AddChildViewAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define ATOM_BROWSER_API_ATOM_API_VIEW_H_
|
#define ATOM_BROWSER_API_ATOM_API_VIEW_H_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_layout_manager.h"
|
#include "atom/browser/api/atom_api_layout_manager.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
|
@ -23,6 +24,8 @@ class View : public mate::TrackableObject<View> {
|
||||||
v8::Local<v8::FunctionTemplate> prototype);
|
v8::Local<v8::FunctionTemplate> prototype);
|
||||||
|
|
||||||
void SetLayoutManager(mate::Handle<LayoutManager> layout_manager);
|
void SetLayoutManager(mate::Handle<LayoutManager> layout_manager);
|
||||||
|
void AddChildView(mate::Handle<View> view);
|
||||||
|
void AddChildViewAt(mate::Handle<View> view, size_t index);
|
||||||
|
|
||||||
views::View* view() const { return view_; }
|
views::View* view() const { return view_; }
|
||||||
|
|
||||||
|
@ -36,6 +39,7 @@ class View : public mate::TrackableObject<View> {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
v8::Global<v8::Object> layout_manager_;
|
v8::Global<v8::Object> layout_manager_;
|
||||||
|
std::vector<v8::Global<v8::Object>> child_views_;
|
||||||
|
|
||||||
bool delete_view_ = true;
|
bool delete_view_ = true;
|
||||||
views::View* view_ = nullptr;
|
views::View* view_ = nullptr;
|
||||||
|
|
|
@ -14,7 +14,7 @@ DelayedNativeViewHost::~DelayedNativeViewHost() {}
|
||||||
void DelayedNativeViewHost::ViewHierarchyChanged(
|
void DelayedNativeViewHost::ViewHierarchyChanged(
|
||||||
const ViewHierarchyChangedDetails& details) {
|
const ViewHierarchyChangedDetails& details) {
|
||||||
NativeViewHost::ViewHierarchyChanged(details);
|
NativeViewHost::ViewHierarchyChanged(details);
|
||||||
if (details.is_add)
|
if (details.is_add && GetWidget())
|
||||||
Attach(native_view_);
|
Attach(native_view_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue