feat: add TopLevelWindow.setContentView API

This commit is contained in:
Cheng Zhao 2018-05-08 14:47:26 +09:00
parent 2b24b26e59
commit bb2715e7a5
10 changed files with 56 additions and 18 deletions

View file

@ -4,7 +4,6 @@
#include "atom/browser/api/atom_api_browser_window.h" #include "atom/browser/api/atom_api_browser_window.h"
#include "atom/browser/api/atom_api_web_contents_view.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/unresponsive_suppressor.h" #include "atom/browser/unresponsive_suppressor.h"
#include "atom/browser/web_contents_preferences.h" #include "atom/browser/web_contents_preferences.h"
@ -68,11 +67,6 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
api_web_contents_->AddObserver(this); api_web_contents_->AddObserver(this);
Observe(api_web_contents_->web_contents()); Observe(api_web_contents_->web_contents());
// Create a WebContentsView for the WebContents.
auto* web_contents_view = static_cast<WebContentsView*>(
WebContentsView::New(isolate, web_contents));
web_contents_view_.Reset(isolate, web_contents_view->GetWrapper());
// Keep a copy of the options for later use. // Keep a copy of the options for later use.
mate::Dictionary(isolate, web_contents->GetWrapper()) mate::Dictionary(isolate, web_contents->GetWrapper())
.Set("browserWindowOptions", options); .Set("browserWindowOptions", options);
@ -83,7 +77,6 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
// Associate with BrowserWindow. // Associate with BrowserWindow.
web_contents->SetOwnerWindow(window()); web_contents->SetOwnerWindow(window());
window()->SetContentView(web_contents_view->view());
auto* host = web_contents->web_contents()->GetRenderViewHost(); auto* host = web_contents->web_contents()->GetRenderViewHost();
if (host) if (host)

View file

@ -109,8 +109,6 @@ class BrowserWindow : public TopLevelWindow,
v8::Global<v8::Value> web_contents_; v8::Global<v8::Value> web_contents_;
api::WebContents* api_web_contents_; api::WebContents* api_web_contents_;
v8::Global<v8::Value> web_contents_view_;
base::WeakPtrFactory<BrowserWindow> weak_factory_; base::WeakPtrFactory<BrowserWindow> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BrowserWindow); DISALLOW_COPY_AND_ASSIGN(BrowserWindow);

View file

@ -9,6 +9,7 @@
#include "atom/browser/api/atom_api_browser_view.h" #include "atom/browser/api/atom_api_browser_view.h"
#include "atom/browser/api/atom_api_menu.h" #include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/api/atom_api_view.h"
#include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/color_util.h" #include "atom/common/color_util.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
@ -267,6 +268,11 @@ void TopLevelWindow::OnWindowMessage(UINT message,
} }
#endif #endif
void TopLevelWindow::SetContentView(mate::Handle<View> view) {
content_view_.Reset(isolate(), view.ToV8());
window_->SetContentView(view->view());
}
void TopLevelWindow::Close() { void TopLevelWindow::Close() {
window_->Close(); window_->Close();
} }
@ -767,6 +773,13 @@ void TopLevelWindow::CloseFilePreview() {
window_->CloseFilePreview(); window_->CloseFilePreview();
} }
v8::Local<v8::Value> TopLevelWindow::GetContentView() const {
if (content_view_.IsEmpty())
return v8::Null(isolate());
else
return v8::Local<v8::Value>::New(isolate(), content_view_);
}
v8::Local<v8::Value> TopLevelWindow::GetParentWindow() const { v8::Local<v8::Value> TopLevelWindow::GetParentWindow() const {
if (parent_window_.IsEmpty()) if (parent_window_.IsEmpty())
return v8::Null(isolate()); return v8::Null(isolate());
@ -915,6 +928,7 @@ void TopLevelWindow::BuildPrototype(v8::Isolate* isolate,
prototype->SetClassName(mate::StringToV8(isolate, "TopLevelWindow")); prototype->SetClassName(mate::StringToV8(isolate, "TopLevelWindow"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable() .MakeDestroyable()
.SetMethod("setContentView", &TopLevelWindow::SetContentView)
.SetMethod("close", &TopLevelWindow::Close) .SetMethod("close", &TopLevelWindow::Close)
.SetMethod("focus", &TopLevelWindow::Focus) .SetMethod("focus", &TopLevelWindow::Focus)
.SetMethod("blur", &TopLevelWindow::Blur) .SetMethod("blur", &TopLevelWindow::Blur)
@ -1024,6 +1038,7 @@ void TopLevelWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setAspectRatio", &TopLevelWindow::SetAspectRatio) .SetMethod("setAspectRatio", &TopLevelWindow::SetAspectRatio)
.SetMethod("previewFile", &TopLevelWindow::PreviewFile) .SetMethod("previewFile", &TopLevelWindow::PreviewFile)
.SetMethod("closeFilePreview", &TopLevelWindow::CloseFilePreview) .SetMethod("closeFilePreview", &TopLevelWindow::CloseFilePreview)
.SetMethod("getContentView", &TopLevelWindow::GetContentView)
.SetMethod("getParentWindow", &TopLevelWindow::GetParentWindow) .SetMethod("getParentWindow", &TopLevelWindow::GetParentWindow)
.SetMethod("getChildWindows", &TopLevelWindow::GetChildWindows) .SetMethod("getChildWindows", &TopLevelWindow::GetChildWindows)
.SetMethod("getBrowserView", &TopLevelWindow::GetBrowserView) .SetMethod("getBrowserView", &TopLevelWindow::GetBrowserView)

View file

@ -20,6 +20,8 @@ namespace atom {
namespace api { namespace api {
class View;
class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>, class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
public NativeWindowObserver { public NativeWindowObserver {
public: public:
@ -79,6 +81,7 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
#endif #endif
// Public APIs of NativeWindow. // Public APIs of NativeWindow.
void SetContentView(mate::Handle<View> view);
void Close(); void Close();
virtual void Focus(); virtual void Focus();
virtual void Blur(); virtual void Blur();
@ -179,6 +182,7 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
void CloseFilePreview(); void CloseFilePreview();
// Public getters of NativeWindow. // Public getters of NativeWindow.
v8::Local<v8::Value> GetContentView() const;
v8::Local<v8::Value> GetParentWindow() const; v8::Local<v8::Value> GetParentWindow() const;
std::vector<v8::Local<v8::Object>> GetChildWindows() const; std::vector<v8::Local<v8::Object>> GetChildWindows() const;
v8::Local<v8::Value> GetBrowserView() const; v8::Local<v8::Value> GetBrowserView() const;
@ -215,6 +219,7 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
MessageCallbackMap messages_callback_map_; MessageCallbackMap messages_callback_map_;
#endif #endif
v8::Global<v8::Value> content_view_;
v8::Global<v8::Value> browser_view_; v8::Global<v8::Value> browser_view_;
v8::Global<v8::Value> menu_; v8::Global<v8::Value> menu_;
v8::Global<v8::Value> parent_window_; v8::Global<v8::Value> parent_window_;
@ -231,7 +236,7 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
namespace mate { namespace mate {
template<> template <>
struct Converter<atom::NativeWindow*> { struct Converter<atom::NativeWindow*> {
static bool FromV8(v8::Isolate* isolate, static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val, v8::Local<v8::Value> val,

View file

@ -25,7 +25,9 @@ View::~View() {
// static // static
mate::WrappableBase* View::New(mate::Arguments* args) { mate::WrappableBase* View::New(mate::Arguments* args) {
return new View(); auto* view = new View();
view->InitWith(args->isolate(), args->GetThis());
return view;
} }
// static // static

View file

@ -42,4 +42,21 @@ class View : public mate::EventEmitter<View> {
} // namespace atom } // namespace atom
namespace mate {
template <>
struct Converter<views::View*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
views::View** out) {
atom::api::View* view;
if (!Converter<atom::api::View*>::FromV8(isolate, val, &view))
return false;
*out = view->view();
return true;
}
};
} // namespace mate
#endif // ATOM_BROWSER_API_ATOM_API_VIEW_H_ #endif // ATOM_BROWSER_API_ATOM_API_VIEW_H_

View file

@ -42,16 +42,18 @@ WebContentsView::~WebContentsView() {}
// static // static
mate::WrappableBase* WebContentsView::New( mate::WrappableBase* WebContentsView::New(
v8::Isolate* isolate, mate::Arguments* args,
mate::Handle<WebContents> web_contents) { mate::Handle<WebContents> web_contents) {
if (!web_contents->managed_web_contents()) { if (!web_contents->managed_web_contents()) {
const char* error = "The WebContents must be created by user"; const char* error = "The WebContents must be created by user";
isolate->ThrowException( args->isolate()->ThrowException(
v8::Exception::Error(mate::StringToV8(isolate, error))); v8::Exception::Error(mate::StringToV8(args->isolate(), error)));
return nullptr; return nullptr;
} }
return new WebContentsView(isolate, web_contents->GetWrapper(), auto* view = new WebContentsView(args->isolate(), web_contents->GetWrapper(),
web_contents->managed_web_contents()); web_contents->managed_web_contents());
view->InitWith(args->isolate(), args->GetThis());
return view;
} }
// static // static

View file

@ -20,7 +20,7 @@ class WebContents;
class WebContentsView : public View { class WebContentsView : public View {
public: public:
static mate::WrappableBase* New(v8::Isolate* isolate, static mate::WrappableBase* New(mate::Arguments* args,
mate::Handle<WebContents> web_contents); mate::Handle<WebContents> web_contents);
static void BuildPrototype(v8::Isolate* isolate, static void BuildPrototype(v8::Isolate* isolate,

View file

@ -1,7 +1,7 @@
'use strict' 'use strict'
const electron = require('electron') const electron = require('electron')
const {ipcMain, TopLevelWindow} = electron const {ipcMain, WebContentsView, TopLevelWindow} = electron
const {BrowserWindow} = process.atomBinding('window') const {BrowserWindow} = process.atomBinding('window')
const v8Util = process.atomBinding('v8_util') const v8Util = process.atomBinding('v8_util')
@ -14,6 +14,9 @@ BrowserWindow.prototype._init = function () {
// Avoid recursive require. // Avoid recursive require.
const {app} = electron const {app} = electron
// Create WebContentsView.
this.setContentView(new WebContentsView(this.webContents))
// Make new windows requested by links behave like "window.open" // Make new windows requested by links behave like "window.open"
this.webContents.on('-new-window', (event, url, frameName, disposition, this.webContents.on('-new-window', (event, url, frameName, disposition,
additionalFeatures, postData, additionalFeatures, postData,

View file

@ -5,4 +5,7 @@ const {View} = process.atomBinding('view')
Object.setPrototypeOf(View.prototype, EventEmitter.prototype) Object.setPrototypeOf(View.prototype, EventEmitter.prototype)
View.prototype._init = function () {
}
module.exports = View module.exports = View