diff --git a/atom/browser/api/atom_api_box_layout.cc b/atom/browser/api/atom_api_box_layout.cc index b37430b6eb0..99e2efc55b7 100644 --- a/atom/browser/api/atom_api_box_layout.cc +++ b/atom/browser/api/atom_api_box_layout.cc @@ -6,6 +6,7 @@ #include +#include "atom/browser/api/atom_api_view.h" #include "atom/common/api/constructor.h" #include "native_mate/dictionary.h" @@ -42,6 +43,11 @@ BoxLayout::BoxLayout(views::BoxLayout::Orientation orientation) BoxLayout::~BoxLayout() {} +void BoxLayout::SetFlexForView(mate::Handle view, int flex) { + auto* box_layout = static_cast(layout_manager()); + box_layout->SetFlexForView(view->view(), flex); +} + // static mate::WrappableBase* BoxLayout::New(mate::Arguments* args, views::BoxLayout::Orientation orientation) { @@ -52,7 +58,11 @@ mate::WrappableBase* BoxLayout::New(mate::Arguments* args, // static void BoxLayout::BuildPrototype(v8::Isolate* isolate, - v8::Local prototype) {} + v8::Local prototype) { + prototype->SetClassName(mate::StringToV8(isolate, "BoxLayout")); + mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) + .SetMethod("setFlexForView", &BoxLayout::SetFlexForView); +} } // namespace api diff --git a/atom/browser/api/atom_api_box_layout.h b/atom/browser/api/atom_api_box_layout.h index 804cc244c1d..b29e2eda6ab 100644 --- a/atom/browser/api/atom_api_box_layout.h +++ b/atom/browser/api/atom_api_box_layout.h @@ -6,12 +6,15 @@ #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, @@ -20,6 +23,8 @@ class BoxLayout : public LayoutManager { static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); + void SetFlexForView(mate::Handle view, int flex); + protected: explicit BoxLayout(views::BoxLayout::Orientation orientation); ~BoxLayout() override; diff --git a/atom/browser/api/atom_api_view.cc b/atom/browser/api/atom_api_view.cc index 650ad18cdec..a79e6fa689d 100644 --- a/atom/browser/api/atom_api_view.cc +++ b/atom/browser/api/atom_api_view.cc @@ -30,6 +30,18 @@ void View::SetLayoutManager(mate::Handle layout_manager) { view()->SetLayoutManager(layout_manager->TakeOver().release()); } +void View::AddChildView(mate::Handle child) { + AddChildViewAt(child, child_views_.size()); +} + +void View::AddChildViewAt(mate::Handle 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 mate::WrappableBase* View::New(mate::Arguments* args) { auto* view = new View(); @@ -42,7 +54,9 @@ void View::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { prototype->SetClassName(mate::StringToV8(isolate, "View")); mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) - .SetMethod("setLayoutManager", &View::SetLayoutManager); + .SetMethod("setLayoutManager", &View::SetLayoutManager) + .SetMethod("addChildView", &View::AddChildView) + .SetMethod("addChildViewAt", &View::AddChildViewAt); } } // namespace api diff --git a/atom/browser/api/atom_api_view.h b/atom/browser/api/atom_api_view.h index 37f7ae4e1c7..80f17e61416 100644 --- a/atom/browser/api/atom_api_view.h +++ b/atom/browser/api/atom_api_view.h @@ -6,6 +6,7 @@ #define ATOM_BROWSER_API_ATOM_API_VIEW_H_ #include +#include #include "atom/browser/api/atom_api_layout_manager.h" #include "native_mate/handle.h" @@ -23,6 +24,8 @@ class View : public mate::TrackableObject { v8::Local prototype); void SetLayoutManager(mate::Handle layout_manager); + void AddChildView(mate::Handle view); + void AddChildViewAt(mate::Handle view, size_t index); views::View* view() const { return view_; } @@ -36,6 +39,7 @@ class View : public mate::TrackableObject { private: v8::Global layout_manager_; + std::vector> child_views_; bool delete_view_ = true; views::View* view_ = nullptr; diff --git a/atom/browser/ui/cocoa/delayed_native_view_host.cc b/atom/browser/ui/cocoa/delayed_native_view_host.cc index 0d48668895d..95d369bfd82 100644 --- a/atom/browser/ui/cocoa/delayed_native_view_host.cc +++ b/atom/browser/ui/cocoa/delayed_native_view_host.cc @@ -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_); }