Merge pull request #12637 from electron/expose-toplevel-window

Refactor NativeWindow (Part 7): Expose TopLevelWindow in JavaScript
This commit is contained in:
Cheng Zhao 2018-04-19 14:52:04 +09:00 committed by GitHub
commit 6c9f3066fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 113 additions and 66 deletions

View file

@ -29,7 +29,7 @@ namespace api {
BrowserWindow::BrowserWindow(v8::Isolate* isolate, BrowserWindow::BrowserWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper, v8::Local<v8::Object> wrapper,
const mate::Dictionary& options) const mate::Dictionary& options)
: TopLevelWindow(isolate, wrapper, options), weak_factory_(this) { : TopLevelWindow(isolate, options), weak_factory_(this) {
mate::Handle<class WebContents> web_contents; mate::Handle<class WebContents> web_contents;
// Use options.webPreferences in WebContents. // Use options.webPreferences in WebContents.
@ -397,7 +397,6 @@ mate::WrappableBase* BrowserWindow::New(mate::Arguments* args) {
// static // static
void BrowserWindow::BuildPrototype(v8::Isolate* isolate, void BrowserWindow::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) { v8::Local<v8::FunctionTemplate> prototype) {
TopLevelWindow::BuildPrototype(isolate, prototype);
prototype->SetClassName(mate::StringToV8(isolate, "BrowserWindow")); prototype->SetClassName(mate::StringToV8(isolate, "BrowserWindow"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("focusOnWebView", &BrowserWindow::FocusOnWebView) .SetMethod("focusOnWebView", &BrowserWindow::FocusOnWebView)
@ -439,14 +438,8 @@ void Initialize(v8::Local<v8::Object> exports,
templ->InstanceTemplate()->SetInternalFieldCount(1); templ->InstanceTemplate()->SetInternalFieldCount(1);
BrowserWindow::BuildPrototype(isolate, templ); BrowserWindow::BuildPrototype(isolate, templ);
mate::Dictionary browser_window(isolate, templ->GetFunction());
browser_window.SetMethod(
"fromId", &mate::TrackableObject<TopLevelWindow>::FromWeakMapID);
browser_window.SetMethod("getAllWindows",
&mate::TrackableObject<TopLevelWindow>::GetAll);
mate::Dictionary dict(isolate, exports); mate::Dictionary dict(isolate, exports);
dict.Set("BrowserWindow", browser_window); dict.Set("BrowserWindow", templ->GetFunction());
} }
} // namespace } // namespace

View file

@ -118,27 +118,4 @@ class BrowserWindow : public TopLevelWindow,
} // namespace atom } // namespace atom
namespace mate {
template <>
struct Converter<atom::NativeWindow*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
atom::NativeWindow** out) {
// null would be tranfered to NULL.
if (val->IsNull()) {
*out = NULL;
return true;
}
atom::api::BrowserWindow* window;
if (!Converter<atom::api::BrowserWindow*>::FromV8(isolate, val, &window))
return false;
*out = window->window();
return true;
}
};
} // namespace mate
#endif // ATOM_BROWSER_API_ATOM_API_BROWSER_WINDOW_H_ #endif // ATOM_BROWSER_API_ATOM_API_BROWSER_WINDOW_H_

View file

@ -8,7 +8,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "atom/browser/api/atom_api_browser_window.h" #include "atom/browser/api/atom_api_top_level_window.h"
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "atom/browser/ui/atom_menu_model.h" #include "atom/browser/ui/atom_menu_model.h"
#include "base/callback.h" #include "base/callback.h"
@ -54,7 +54,7 @@ class Menu : public mate::TrackableObject<Menu>,
void ExecuteCommand(int command_id, int event_flags) override; void ExecuteCommand(int command_id, int event_flags) override;
void MenuWillShow(ui::SimpleMenuModel* source) override; void MenuWillShow(ui::SimpleMenuModel* source) override;
virtual void PopupAt(BrowserWindow* window, virtual void PopupAt(TopLevelWindow* window,
int x, int x,
int y, int y,
int positioning_item, int positioning_item,

View file

@ -22,7 +22,7 @@ class MenuMac : public Menu {
protected: protected:
MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper); MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
void PopupAt(BrowserWindow* window, void PopupAt(TopLevelWindow* window,
int x, int x,
int y, int y,
int positioning_item, int positioning_item,

View file

@ -9,8 +9,6 @@
#include "base/mac/scoped_sending_event.h" #include "base/mac/scoped_sending_event.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
@ -27,7 +25,7 @@ MenuMac::MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
weak_factory_(this) { weak_factory_(this) {
} }
void MenuMac::PopupAt(BrowserWindow* window, void MenuMac::PopupAt(TopLevelWindow* window,
int x, int y, int positioning_item, int x, int y, int positioning_item,
const base::Closure& callback) { const base::Closure& callback) {
NativeWindow* native_window = window->window(); NativeWindow* native_window = window->window();

View file

@ -6,8 +6,6 @@
#include "atom/browser/native_window_views.h" #include "atom/browser/native_window_views.h"
#include "atom/browser/unresponsive_suppressor.h" #include "atom/browser/unresponsive_suppressor.h"
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "ui/display/screen.h" #include "ui/display/screen.h"
using views::MenuRunner; using views::MenuRunner;
@ -19,7 +17,7 @@ namespace api {
MenuViews::MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) MenuViews::MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
: Menu(isolate, wrapper), weak_factory_(this) {} : Menu(isolate, wrapper), weak_factory_(this) {}
void MenuViews::PopupAt(BrowserWindow* window, void MenuViews::PopupAt(TopLevelWindow* window,
int x, int x,
int y, int y,
int positioning_item, int positioning_item,

View file

@ -21,7 +21,7 @@ class MenuViews : public Menu {
MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper); MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
protected: protected:
void PopupAt(BrowserWindow* window, void PopupAt(TopLevelWindow* window,
int x, int x,
int y, int y,
int positioning_item, int positioning_item,

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_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"
#include "atom/common/native_mate_converters/file_path_converter.h" #include "atom/common/native_mate_converters/file_path_converter.h"
@ -70,7 +71,6 @@ v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
} // namespace } // namespace
TopLevelWindow::TopLevelWindow(v8::Isolate* isolate, TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options) const mate::Dictionary& options)
: weak_factory_(this) { : weak_factory_(this) {
// The parent window. // The parent window.
@ -99,13 +99,15 @@ TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
if (options.Get(options::kIcon, &icon) && !icon.IsEmpty()) if (options.Get(options::kIcon, &icon) && !icon.IsEmpty())
SetIcon(icon); SetIcon(icon);
#endif #endif
}
AttachAsUserData(window_.get()); TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
// We can only append this window to parent window's child windows after this const mate::Dictionary& options)
// window's JS wrapper gets initialized. : TopLevelWindow(isolate, options) {
if (!parent.IsEmpty()) InitWith(isolate, wrapper);
parent->child_windows_.Set(isolate, weak_map_id(), wrapper); // Init window after everything has been setup.
window()->InitFromOptions(options);
} }
TopLevelWindow::~TopLevelWindow() { TopLevelWindow::~TopLevelWindow() {
@ -117,6 +119,21 @@ TopLevelWindow::~TopLevelWindow() {
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release()); base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release());
} }
void TopLevelWindow::InitWith(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper) {
AttachAsUserData(window_.get());
mate::TrackableObject<TopLevelWindow>::InitWith(isolate, wrapper);
// We can only append this window to parent window's child windows after this
// window's JS wrapper gets initialized.
if (!parent_window_.IsEmpty()) {
mate::Handle<TopLevelWindow> parent;
mate::ConvertFromV8(isolate, GetParentWindow(), &parent);
DCHECK(!parent.IsEmpty());
parent->child_windows_.Set(isolate, weak_map_id(), wrapper);
}
}
void TopLevelWindow::WillCloseWindow(bool* prevent_default) { void TopLevelWindow::WillCloseWindow(bool* prevent_default) {
*prevent_default = Emit("close"); *prevent_default = Emit("close");
} }

View file

@ -35,11 +35,17 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
NativeWindow* window() const { return window_.get(); } NativeWindow* window() const { return window_.get(); }
protected: protected:
// Common constructor.
TopLevelWindow(v8::Isolate* isolate, const mate::Dictionary& options);
// Creating independent TopLevelWindow instance.
TopLevelWindow(v8::Isolate* isolate, TopLevelWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper, v8::Local<v8::Object> wrapper,
const mate::Dictionary& options); const mate::Dictionary& options);
~TopLevelWindow() override; ~TopLevelWindow() override;
// TrackableObject:
void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override;
// NativeWindowObserver: // NativeWindowObserver:
void WillCloseWindow(bool* prevent_default) override; void WillCloseWindow(bool* prevent_default) override;
void OnWindowClosed() override; void OnWindowClosed() override;
@ -223,4 +229,27 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
} // namespace atom } // namespace atom
namespace mate {
template<>
struct Converter<atom::NativeWindow*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
atom::NativeWindow** out) {
// null would be tranfered to NULL.
if (val->IsNull()) {
*out = NULL;
return true;
}
atom::api::TopLevelWindow* window;
if (!Converter<atom::api::TopLevelWindow*>::FromV8(isolate, val, &window))
return false;
*out = window->window();
return true;
}
};
} // namespace mate
#endif // ATOM_BROWSER_API_ATOM_API_TOP_LEVEL_WINDOW_H_ #endif // ATOM_BROWSER_API_ATOM_API_TOP_LEVEL_WINDOW_H_

View file

@ -34,6 +34,7 @@
'lib/browser/api/screen.js', 'lib/browser/api/screen.js',
'lib/browser/api/session.js', 'lib/browser/api/session.js',
'lib/browser/api/system-preferences.js', 'lib/browser/api/system-preferences.js',
'lib/browser/api/top-level-window.js',
'lib/browser/api/touch-bar.js', 'lib/browser/api/touch-bar.js',
'lib/browser/api/tray.js', 'lib/browser/api/tray.js',
'lib/browser/api/web-contents.js', 'lib/browser/api/web-contents.js',

View file

@ -1,22 +1,18 @@
'use strict' 'use strict'
const electron = require('electron') const electron = require('electron')
const {ipcMain} = electron const {ipcMain, TopLevelWindow} = electron
const {EventEmitter} = require('events')
const {BrowserWindow} = process.atomBinding('window') const {BrowserWindow} = process.atomBinding('window')
const v8Util = process.atomBinding('v8_util') const v8Util = process.atomBinding('v8_util')
Object.setPrototypeOf(BrowserWindow.prototype, EventEmitter.prototype) Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)
BrowserWindow.prototype._init = function () { BrowserWindow.prototype._init = function () {
// Avoid recursive require. // Call parent class's _init.
const {app} = require('electron') TopLevelWindow.prototype._init.call(this)
// Simulate the application menu on platforms other than macOS. // Avoid recursive require.
if (process.platform !== 'darwin') { const {app} = electron
const menu = app.getApplicationMenu()
if (menu) this.setMenu(menu)
}
// 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,
@ -132,6 +128,19 @@ BrowserWindow.prototype._init = function () {
}) })
} }
const isBrowserWindow = (win) => {
return win && win.constructor.name === 'BrowserWindow'
}
BrowserWindow.fromId = (id) => {
const win = TopLevelWindow.fromId(id)
return isBrowserWindow(win) ? win : null
}
BrowserWindow.getAllWindows = () => {
return TopLevelWindow.getAllWindows().filter(isBrowserWindow)
}
BrowserWindow.getFocusedWindow = () => { BrowserWindow.getFocusedWindow = () => {
for (let window of BrowserWindow.getAllWindows()) { for (let window of BrowserWindow.getAllWindows()) {
if (window.isFocused() || window.isDevToolsFocused()) return window if (window.isFocused() || window.isDevToolsFocused()) return window

View file

@ -1,6 +1,6 @@
'use strict' 'use strict'
const {BrowserWindow, MenuItem, webContents} = require('electron') const {TopLevelWindow, MenuItem, webContents} = require('electron')
const EventEmitter = require('events').EventEmitter const EventEmitter = require('events').EventEmitter
const v8Util = process.atomBinding('v8_util') const v8Util = process.atomBinding('v8_util')
const bindings = process.atomBinding('menu') const bindings = process.atomBinding('menu')
@ -26,7 +26,7 @@ const delegate = {
executeCommand: (menu, event, id) => { executeCommand: (menu, event, id) => {
const command = menu.commandsMap[id] const command = menu.commandsMap[id]
if (!command) return if (!command) return
command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents()) command.click(event, TopLevelWindow.getFocusedWindow(), webContents.getFocusedWebContents())
}, },
menuWillShow: (menu) => { menuWillShow: (menu) => {
// Ensure radio groups have at least one menu item seleted // Ensure radio groups have at least one menu item seleted
@ -61,14 +61,14 @@ Menu.prototype.popup = function (options) {
if (typeof positioningItem !== 'number') positioningItem = -1 if (typeof positioningItem !== 'number') positioningItem = -1
// find which window to use // find which window to use
const wins = BrowserWindow.getAllWindows() const wins = TopLevelWindow.getAllWindows()
if (!wins || wins.indexOf(window) === -1) { if (!wins || wins.indexOf(window) === -1) {
window = BrowserWindow.getFocusedWindow() window = TopLevelWindow.getFocusedWindow()
if (!window && wins && wins.length > 0) { if (!window && wins && wins.length > 0) {
window = wins[0] window = wins[0]
} }
if (!window) { if (!window) {
throw new Error(`Cannot open Menu without a BrowserWindow present`) throw new Error(`Cannot open Menu without a TopLevelWindow present`)
} }
} }
@ -77,7 +77,7 @@ Menu.prototype.popup = function (options) {
} }
Menu.prototype.closePopup = function (window) { Menu.prototype.closePopup = function (window) {
if (window && window.constructor !== BrowserWindow) { if (window instanceof TopLevelWindow) {
this.closePopupAt(window.id) this.closePopupAt(window.id)
} else { } else {
// Passing -1 (invalid) would make closePopupAt close the all menu runners // Passing -1 (invalid) would make closePopupAt close the all menu runners
@ -148,7 +148,7 @@ Menu.setApplicationMenu = function (menu) {
menu._callMenuWillShow() menu._callMenuWillShow()
bindings.setApplicationMenu(menu) bindings.setApplicationMenu(menu)
} else { } else {
const windows = BrowserWindow.getAllWindows() const windows = TopLevelWindow.getAllWindows()
return windows.map(w => w.setMenu(menu)) return windows.map(w => w.setMenu(menu))
} }
} }

View file

@ -19,6 +19,7 @@ module.exports = [
{name: 'screen', file: 'screen'}, {name: 'screen', file: 'screen'},
{name: 'session', file: 'session'}, {name: 'session', file: 'session'},
{name: 'systemPreferences', file: 'system-preferences'}, {name: 'systemPreferences', file: 'system-preferences'},
{name: 'TopLevelWindow', file: 'top-level-window'},
{name: 'TouchBar', file: 'touch-bar'}, {name: 'TouchBar', file: 'touch-bar'},
{name: 'Tray', file: 'tray'}, {name: 'Tray', file: 'tray'},
{name: 'webContents', file: 'web-contents'}, {name: 'webContents', file: 'web-contents'},

View file

@ -0,0 +1,24 @@
'use strict'
const electron = require('electron')
const {EventEmitter} = require('events')
const {TopLevelWindow} = process.atomBinding('top_level_window')
Object.setPrototypeOf(TopLevelWindow.prototype, EventEmitter.prototype)
TopLevelWindow.prototype._init = function () {
// Avoid recursive require.
const {app} = electron
// Simulate the application menu on platforms other than macOS.
if (process.platform !== 'darwin') {
const menu = app.getApplicationMenu()
if (menu) this.setMenu(menu)
}
}
TopLevelWindow.getFocusedWindow = () => {
return TopLevelWindow.getAllWindows().find((win) => win.isFocused())
}
module.exports = TopLevelWindow