Add win.getChildWindows() API

This commit is contained in:
Cheng Zhao 2016-06-17 16:57:03 +09:00
parent 22513efd55
commit 6cef29e4ee
3 changed files with 34 additions and 12 deletions

View file

@ -136,6 +136,8 @@ void Window::OnWindowClosed() {
Emit("closed"); Emit("closed");
RemoveFromParentChildWindows();
// Destroy the native class when window is closed. // Destroy the native class when window is closed.
base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure()); base::MessageLoop::current()->PostTask(FROM_HERE, GetDestroyClosure());
} }
@ -650,13 +652,17 @@ void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) {
window_->SetAspectRatio(aspect_ratio, extra_size); window_->SetAspectRatio(aspect_ratio, extra_size);
} }
void Window::SetParentWindow(mate::Arguments* args) { void Window::SetParentWindow(v8::Local<v8::Value> value,
v8::Local<v8::Value> value; mate::Arguments* args) {
NativeWindow* parent; mate::Handle<Window> parent;
if (args->GetNext(&value) && if (value->IsNull()) {
mate::ConvertFromV8(isolate(), value, &parent)) { RemoveFromParentChildWindows();
parent_window_.Reset();
window_->SetParentWindow(nullptr);
} else if (mate::ConvertFromV8(isolate(), value, &parent)) {
parent_window_.Reset(isolate(), value); parent_window_.Reset(isolate(), value);
window_->SetParentWindow(parent); window_->SetParentWindow(parent->window_.get());
parent->child_windows_.Set(isolate(), ID(), GetWrapper());
} else { } else {
args->ThrowError("Must pass BrowserWindow instance or null"); args->ThrowError("Must pass BrowserWindow instance or null");
} }
@ -669,6 +675,10 @@ v8::Local<v8::Value> Window::GetParentWindow() {
return v8::Local<v8::Value>::New(isolate(), parent_window_); return v8::Local<v8::Value>::New(isolate(), parent_window_);
} }
std::vector<v8::Local<v8::Object>> Window::GetChildWindows() {
return child_windows_.Values(isolate());
}
v8::Local<v8::Value> Window::GetNativeWindowHandle() { v8::Local<v8::Value> Window::GetNativeWindowHandle() {
gfx::AcceleratedWidget handle = window_->GetAcceleratedWidget(); gfx::AcceleratedWidget handle = window_->GetAcceleratedWidget();
return ToBuffer( return ToBuffer(
@ -694,6 +704,15 @@ v8::Local<v8::Value> Window::WebContents(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, web_contents_); return v8::Local<v8::Value>::New(isolate, web_contents_);
} }
void Window::RemoveFromParentChildWindows() {
if (parent_window_.IsEmpty())
return;
mate::Handle<Window> parent;
if (mate::ConvertFromV8(isolate(), GetParentWindow(), &parent))
parent->child_windows_.Remove(ID());
}
// static // static
void Window::BuildPrototype(v8::Isolate* isolate, void Window::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype) { v8::Local<v8::ObjectTemplate> prototype) {
@ -718,6 +737,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setAspectRatio", &Window::SetAspectRatio) .SetMethod("setAspectRatio", &Window::SetAspectRatio)
.SetMethod("setParentWindow", &Window::SetParentWindow) .SetMethod("setParentWindow", &Window::SetParentWindow)
.SetMethod("getParentWindow", &Window::GetParentWindow) .SetMethod("getParentWindow", &Window::GetParentWindow)
.SetMethod("getChildWindows", &Window::GetChildWindows)
.SetMethod("getNativeWindowHandle", &Window::GetNativeWindowHandle) .SetMethod("getNativeWindowHandle", &Window::GetNativeWindowHandle)
.SetMethod("getBounds", &Window::GetBounds) .SetMethod("getBounds", &Window::GetBounds)
.SetMethod("setBounds", &Window::SetBounds) .SetMethod("setBounds", &Window::SetBounds)

View file

@ -6,15 +6,16 @@
#define ATOM_BROWSER_API_ATOM_API_WINDOW_H_ #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/memory/scoped_ptr.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/native_window_observer.h" #include "atom/browser/native_window_observer.h"
#include "atom/common/api/atom_api_native_image.h" #include "atom/common/api/atom_api_native_image.h"
#include "atom/common/key_weak_map.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
class GURL; class GURL;
@ -159,8 +160,9 @@ class Window : public mate::TrackableObject<Window>,
void SetMenuBarVisibility(bool visible); void SetMenuBarVisibility(bool visible);
bool IsMenuBarVisible(); bool IsMenuBarVisible();
void SetAspectRatio(double aspect_ratio, mate::Arguments* args); void SetAspectRatio(double aspect_ratio, mate::Arguments* args);
void SetParentWindow(mate::Arguments* args); void SetParentWindow(v8::Local<v8::Value> value, mate::Arguments* args);
v8::Local<v8::Value> GetParentWindow(); v8::Local<v8::Value> GetParentWindow();
std::vector<v8::Local<v8::Object>> GetChildWindows();
v8::Local<v8::Value> GetNativeWindowHandle(); v8::Local<v8::Value> GetNativeWindowHandle();
#if defined(OS_WIN) #if defined(OS_WIN)
@ -183,6 +185,9 @@ class Window : public mate::TrackableObject<Window>,
int32_t ID() const; int32_t ID() const;
v8::Local<v8::Value> WebContents(v8::Isolate* isolate); v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
// Helpers.
void RemoveFromParentChildWindows();
#if defined(OS_WIN) #if defined(OS_WIN)
typedef std::map<UINT, MessageCallback> MessageCallbackMap; typedef std::map<UINT, MessageCallback> MessageCallbackMap;
MessageCallbackMap messages_callback_map_; MessageCallbackMap messages_callback_map_;
@ -191,6 +196,7 @@ class Window : public mate::TrackableObject<Window>,
v8::Global<v8::Value> web_contents_; v8::Global<v8::Value> web_contents_;
v8::Global<v8::Value> menu_; v8::Global<v8::Value> menu_;
v8::Global<v8::Value> parent_window_; v8::Global<v8::Value> parent_window_;
KeyWeakMap<int> child_windows_;
api::WebContents* api_web_contents_; api::WebContents* api_web_contents_;

View file

@ -14,10 +14,6 @@
namespace atom { namespace atom {
namespace internal {
} // namespace internal
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer. // Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
template<typename K> template<typename K>
class KeyWeakMap { class KeyWeakMap {