feat: add activate option to webContents.openDevTools (#13852)

This commit is contained in:
Milan Burda 2018-11-27 10:34:44 +01:00 committed by Cheng Zhao
parent aafbd865bf
commit d63a848011
13 changed files with 63 additions and 32 deletions

View file

@ -1307,14 +1307,16 @@ void WebContents::OpenDevTools(mate::Arguments* args) {
if (type_ == WEB_VIEW || !owner_window()) { if (type_ == WEB_VIEW || !owner_window()) {
state = "detach"; state = "detach";
} }
bool activate = true;
if (args && args->Length() == 1) { if (args && args->Length() == 1) {
mate::Dictionary options; mate::Dictionary options;
if (args->GetNext(&options)) { if (args->GetNext(&options)) {
options.Get("mode", &state); options.Get("mode", &state);
options.Get("activate", &activate);
} }
} }
managed_web_contents()->SetDockState(state); managed_web_contents()->SetDockState(state);
managed_web_contents()->ShowDevTools(); managed_web_contents()->ShowDevTools(activate);
} }
void WebContents::CloseDevTools() { void WebContents::CloseDevTools() {

View file

@ -34,10 +34,10 @@ using atom::InspectableWebContentsViewMac;
(InspectableWebContentsViewMac*)view; (InspectableWebContentsViewMac*)view;
- (void)removeObservers; - (void)removeObservers;
- (void)notifyDevToolsFocused; - (void)notifyDevToolsFocused;
- (void)setDevToolsVisible:(BOOL)visible; - (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate;
- (BOOL)isDevToolsVisible; - (BOOL)isDevToolsVisible;
- (BOOL)isDevToolsFocused; - (BOOL)isDevToolsFocused;
- (void)setIsDocked:(BOOL)docked; - (void)setIsDocked:(BOOL)docked activate:(BOOL)activate;
- (void)setContentsResizingStrategy: - (void)setContentsResizingStrategy:
(const DevToolsContentsResizingStrategy&)strategy; (const DevToolsContentsResizingStrategy&)strategy;
- (void)setTitle:(NSString*)title; - (void)setTitle:(NSString*)title;

View file

@ -63,7 +63,7 @@
} }
- (IBAction)showDevTools:(id)sender { - (IBAction)showDevTools:(id)sender {
inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools(); inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools(true);
} }
- (void)notifyDevToolsFocused { - (void)notifyDevToolsFocused {
@ -71,7 +71,7 @@
inspectableWebContentsView_->GetDelegate()->DevToolsFocused(); inspectableWebContentsView_->GetDelegate()->DevToolsFocused();
} }
- (void)setDevToolsVisible:(BOOL)visible { - (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate {
if (visible == devtools_visible_) if (visible == devtools_visible_)
return; return;
@ -106,7 +106,11 @@
} }
} else { } else {
if (visible) { if (visible) {
[devtools_window_ makeKeyAndOrderFront:nil]; if (activate) {
[devtools_window_ makeKeyAndOrderFront:nil];
} else {
[devtools_window_ orderBack:nil];
}
} else { } else {
[devtools_window_ setDelegate:nil]; [devtools_window_ setDelegate:nil];
[devtools_window_ close]; [devtools_window_ close];
@ -127,9 +131,9 @@
} }
} }
- (void)setIsDocked:(BOOL)docked { - (void)setIsDocked:(BOOL)docked activate:(BOOL)activate {
// Revert to no-devtools state. // Revert to no-devtools state.
[self setDevToolsVisible:NO]; [self setDevToolsVisible:NO activate:NO];
// Switch to new state. // Switch to new state.
devtools_docked_ = docked; devtools_docked_ = docked;
@ -163,7 +167,7 @@
[contentView addSubview:devToolsView]; [contentView addSubview:devToolsView];
} }
[self setDevToolsVisible:YES]; [self setDevToolsVisible:YES activate:activate];
} }
- (void)setContentsResizingStrategy: - (void)setContentsResizingStrategy:

View file

@ -47,7 +47,7 @@ class InspectableWebContents {
virtual void ReleaseWebContents() = 0; virtual void ReleaseWebContents() = 0;
virtual void SetDevToolsWebContents(content::WebContents* devtools) = 0; virtual void SetDevToolsWebContents(content::WebContents* devtools) = 0;
virtual void SetDockState(const std::string& state) = 0; virtual void SetDockState(const std::string& state) = 0;
virtual void ShowDevTools() = 0; virtual void ShowDevTools(bool activate) = 0;
virtual void CloseDevTools() = 0; virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0; virtual bool IsDevToolsViewShowing() = 0;
virtual void AttachTo(scoped_refptr<content::DevToolsAgentHost>) = 0; virtual void AttachTo(scoped_refptr<content::DevToolsAgentHost>) = 0;

View file

@ -306,13 +306,15 @@ void InspectableWebContentsImpl::SetDevToolsWebContents(
external_devtools_web_contents_ = devtools; external_devtools_web_contents_ = devtools;
} }
void InspectableWebContentsImpl::ShowDevTools() { void InspectableWebContentsImpl::ShowDevTools(bool activate) {
if (embedder_message_dispatcher_) { if (embedder_message_dispatcher_) {
if (managed_devtools_web_contents_) if (managed_devtools_web_contents_)
view_->ShowDevTools(); view_->ShowDevTools(activate);
return; return;
} }
activate_ = activate;
// Show devtools only after it has done loading, this is to make sure the // Show devtools only after it has done loading, this is to make sure the
// SetIsDocked is called *BEFORE* ShowDevTools. // SetIsDocked is called *BEFORE* ShowDevTools.
embedder_message_dispatcher_.reset( embedder_message_dispatcher_.reset(
@ -430,7 +432,7 @@ void InspectableWebContentsImpl::CloseWindow() {
void InspectableWebContentsImpl::LoadCompleted() { void InspectableWebContentsImpl::LoadCompleted() {
frontend_loaded_ = true; frontend_loaded_ = true;
if (managed_devtools_web_contents_) if (managed_devtools_web_contents_)
view_->ShowDevTools(); view_->ShowDevTools(activate_);
// If the devtools can dock, "SetIsDocked" will be called by devtools itself. // If the devtools can dock, "SetIsDocked" will be called by devtools itself.
if (!can_dock_) { if (!can_dock_) {
@ -501,7 +503,7 @@ void InspectableWebContentsImpl::LoadNetworkResource(
void InspectableWebContentsImpl::SetIsDocked(const DispatchCallback& callback, void InspectableWebContentsImpl::SetIsDocked(const DispatchCallback& callback,
bool docked) { bool docked) {
if (managed_devtools_web_contents_) if (managed_devtools_web_contents_)
view_->SetIsDocked(docked); view_->SetIsDocked(docked, activate_);
if (!callback.is_null()) if (!callback.is_null())
callback.Run(nullptr); callback.Run(nullptr);
} }

View file

@ -55,7 +55,7 @@ class InspectableWebContentsImpl
void ReleaseWebContents() override; void ReleaseWebContents() override;
void SetDevToolsWebContents(content::WebContents* devtools) override; void SetDevToolsWebContents(content::WebContents* devtools) override;
void SetDockState(const std::string& state) override; void SetDockState(const std::string& state) override;
void ShowDevTools() override; void ShowDevTools(bool activate) override;
void CloseDevTools() override; void CloseDevTools() override;
bool IsDevToolsViewShowing() override; bool IsDevToolsViewShowing() override;
void AttachTo(scoped_refptr<content::DevToolsAgentHost>) override; void AttachTo(scoped_refptr<content::DevToolsAgentHost>) override;
@ -211,6 +211,7 @@ class InspectableWebContentsImpl
gfx::Rect devtools_bounds_; gfx::Rect devtools_bounds_;
bool can_dock_; bool can_dock_;
std::string dock_state_; std::string dock_state_;
bool activate_ = true;
using PendingRequestsMap = std::map<const net::URLFetcher*, DispatchCallback>; using PendingRequestsMap = std::map<const net::URLFetcher*, DispatchCallback>;
PendingRequestsMap pending_requests_; PendingRequestsMap pending_requests_;

View file

@ -43,12 +43,12 @@ class InspectableWebContentsView {
virtual gfx::NativeView GetNativeView() const = 0; virtual gfx::NativeView GetNativeView() const = 0;
#endif #endif
virtual void ShowDevTools() = 0; virtual void ShowDevTools(bool activate) = 0;
// Hide the DevTools view. // Hide the DevTools view.
virtual void CloseDevTools() = 0; virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0; virtual bool IsDevToolsViewShowing() = 0;
virtual bool IsDevToolsViewFocused() = 0; virtual bool IsDevToolsViewFocused() = 0;
virtual void SetIsDocked(bool docked) = 0; virtual void SetIsDocked(bool docked, bool activate) = 0;
virtual void SetContentsResizingStrategy( virtual void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) = 0; const DevToolsContentsResizingStrategy& strategy) = 0;
virtual void SetTitle(const base::string16& title) = 0; virtual void SetTitle(const base::string16& title) = 0;

View file

@ -23,11 +23,11 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView {
~InspectableWebContentsViewMac() override; ~InspectableWebContentsViewMac() override;
gfx::NativeView GetNativeView() const override; gfx::NativeView GetNativeView() const override;
void ShowDevTools() override; void ShowDevTools(bool activate) override;
void CloseDevTools() override; void CloseDevTools() override;
bool IsDevToolsViewShowing() override; bool IsDevToolsViewShowing() override;
bool IsDevToolsViewFocused() override; bool IsDevToolsViewFocused() override;
void SetIsDocked(bool docked) override; void SetIsDocked(bool docked, bool activate) override;
void SetContentsResizingStrategy( void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override; const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const base::string16& title) override; void SetTitle(const base::string16& title) override;

View file

@ -34,12 +34,12 @@ gfx::NativeView InspectableWebContentsViewMac::GetNativeView() const {
return view_.get(); return view_.get();
} }
void InspectableWebContentsViewMac::ShowDevTools() { void InspectableWebContentsViewMac::ShowDevTools(bool activate) {
[view_ setDevToolsVisible:YES]; [view_ setDevToolsVisible:YES activate:activate];
} }
void InspectableWebContentsViewMac::CloseDevTools() { void InspectableWebContentsViewMac::CloseDevTools() {
[view_ setDevToolsVisible:NO]; [view_ setDevToolsVisible:NO activate:NO];
} }
bool InspectableWebContentsViewMac::IsDevToolsViewShowing() { bool InspectableWebContentsViewMac::IsDevToolsViewShowing() {
@ -50,8 +50,8 @@ bool InspectableWebContentsViewMac::IsDevToolsViewFocused() {
return [view_ isDevToolsFocused]; return [view_ isDevToolsFocused];
} }
void InspectableWebContentsViewMac::SetIsDocked(bool docked) { void InspectableWebContentsViewMac::SetIsDocked(bool docked, bool activate) {
[view_ setIsDocked:docked]; [view_ setIsDocked:docked activate:activate];
} }
void InspectableWebContentsViewMac::SetContentsResizingStrategy( void InspectableWebContentsViewMac::SetContentsResizingStrategy(

View file

@ -115,7 +115,7 @@ views::View* InspectableWebContentsViewViews::GetWebView() {
return contents_web_view_; return contents_web_view_;
} }
void InspectableWebContentsViewViews::ShowDevTools() { void InspectableWebContentsViewViews::ShowDevTools(bool activate) {
if (devtools_visible_) if (devtools_visible_)
return; return;
@ -125,7 +125,11 @@ void InspectableWebContentsViewViews::ShowDevTools() {
inspectable_web_contents_->GetDevToolsWebContents()); inspectable_web_contents_->GetDevToolsWebContents());
devtools_window_->SetBounds( devtools_window_->SetBounds(
inspectable_web_contents()->GetDevToolsBounds()); inspectable_web_contents()->GetDevToolsBounds());
devtools_window_->Show(); if (activate) {
devtools_window_->Show();
} else {
devtools_window_->ShowInactive();
}
} else { } else {
devtools_web_view_->SetVisible(true); devtools_web_view_->SetVisible(true);
devtools_web_view_->SetWebContents( devtools_web_view_->SetWebContents(
@ -166,7 +170,7 @@ bool InspectableWebContentsViewViews::IsDevToolsViewFocused() {
return false; return false;
} }
void InspectableWebContentsViewViews::SetIsDocked(bool docked) { void InspectableWebContentsViewViews::SetIsDocked(bool docked, bool activate) {
CloseDevTools(); CloseDevTools();
if (!docked) { if (!docked) {
@ -191,7 +195,7 @@ void InspectableWebContentsViewViews::SetIsDocked(bool docked) {
devtools_window_->UpdateWindowIcon(); devtools_window_->UpdateWindowIcon();
} }
ShowDevTools(); ShowDevTools(activate);
} }
void InspectableWebContentsViewViews::SetContentsResizingStrategy( void InspectableWebContentsViewViews::SetContentsResizingStrategy(

View file

@ -32,11 +32,11 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView,
// InspectableWebContentsView: // InspectableWebContentsView:
views::View* GetView() override; views::View* GetView() override;
views::View* GetWebView() override; views::View* GetWebView() override;
void ShowDevTools() override; void ShowDevTools(bool activate) override;
void CloseDevTools() override; void CloseDevTools() override;
bool IsDevToolsViewShowing() override; bool IsDevToolsViewShowing() override;
bool IsDevToolsViewFocused() override; bool IsDevToolsViewFocused() override;
void SetIsDocked(bool docked) override; void SetIsDocked(bool docked, bool activate) override;
void SetContentsResizingStrategy( void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override; const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const base::string16& title) override; void SetTitle(const base::string16& title) override;

View file

@ -1245,8 +1245,10 @@ app.once('ready', () => {
* `options` Object (optional) * `options` Object (optional)
* `mode` String - Opens the devtools with specified dock state, can be * `mode` String - Opens the devtools with specified dock state, can be
`right`, `bottom`, `undocked`, `detach`. Defaults to last used dock state. `right`, `bottom`, `undocked`, `detach`. Defaults to last used dock state.
In `undocked` mode it's possible to dock back. In `detach` mode it's not. In `undocked` mode it's possible to dock back. In `detach` mode it's not.
* `activate` Boolean (optional) - Whether to bring the opened devtools window
to the foreground. The default is `true`.
Opens the devtools. Opens the devtools.

View file

@ -159,6 +159,22 @@ describe('webContents module', () => {
}) })
}) })
describe('openDevTools() API', () => {
it('can show window with activation', async () => {
w.show()
assert.strictEqual(w.isFocused(), true)
w.webContents.openDevTools({ mode: 'detach', activate: true })
await emittedOnce(w.webContents, 'devtools-opened')
assert.strictEqual(w.isFocused(), false)
})
it('can show window without activation', async () => {
w.webContents.openDevTools({ mode: 'detach', activate: false })
await emittedOnce(w.webContents, 'devtools-opened')
assert.strictEqual(w.isDevToolsOpened(), true)
})
})
describe('before-input-event event', () => { describe('before-input-event event', () => {
it('can prevent document keyboard events', (done) => { it('can prevent document keyboard events', (done) => {
ipcMain.once('keydown', (event, key) => { ipcMain.once('keydown', (event, key) => {