Skip to content

Commit

Permalink
feat: add activate option to webContents.openDevTools
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jul 30, 2018
1 parent b22ac5f commit 3916d59
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 30 deletions.
4 changes: 3 additions & 1 deletion atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -1279,14 +1279,16 @@ void WebContents::OpenDevTools(mate::Arguments* args) {
if (type_ == WEB_VIEW || !owner_window()) {
state = "detach";
}
bool activate = true;
if (args && args->Length() == 1) {
mate::Dictionary options;
if (args->GetNext(&options)) {
options.Get("mode", &state);
options.Get("activate", &activate);
}
}
managed_web_contents()->SetDockState(state);
managed_web_contents()->ShowDevTools();
managed_web_contents()->ShowDevTools(activate);
}

void WebContents::CloseDevTools() {
Expand Down
2 changes: 1 addition & 1 deletion brightray/browser/inspectable_web_contents.h
Expand Up @@ -39,7 +39,7 @@ class InspectableWebContents {

virtual void SetDevToolsWebContents(content::WebContents* devtools) = 0;
virtual void SetDockState(const std::string& state) = 0;
virtual void ShowDevTools() = 0;
virtual void ShowDevTools(bool activate) = 0;
virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0;
virtual void AttachTo(scoped_refptr<content::DevToolsAgentHost>) = 0;
Expand Down
10 changes: 6 additions & 4 deletions brightray/browser/inspectable_web_contents_impl.cc
Expand Up @@ -297,13 +297,15 @@ void InspectableWebContentsImpl::SetDevToolsWebContents(
external_devtools_web_contents_ = devtools;
}

void InspectableWebContentsImpl::ShowDevTools() {
void InspectableWebContentsImpl::ShowDevTools(bool activate) {
if (embedder_message_dispatcher_) {
if (managed_devtools_web_contents_)
view_->ShowDevTools();
view_->ShowDevTools(activate);
return;
}

activate_ = activate;

// Show devtools only after it has done loading, this is to make sure the
// SetIsDocked is called *BEFORE* ShowDevTools.
embedder_message_dispatcher_.reset(
Expand Down Expand Up @@ -413,7 +415,7 @@ void InspectableWebContentsImpl::CloseWindow() {
void InspectableWebContentsImpl::LoadCompleted() {
frontend_loaded_ = true;
if (managed_devtools_web_contents_)
view_->ShowDevTools();
view_->ShowDevTools(activate_);

// If the devtools can dock, "SetIsDocked" will be called by devtools itself.
if (!can_dock_) {
Expand Down Expand Up @@ -483,7 +485,7 @@ void InspectableWebContentsImpl::LoadNetworkResource(
void InspectableWebContentsImpl::SetIsDocked(const DispatchCallback& callback,
bool docked) {
if (managed_devtools_web_contents_)
view_->SetIsDocked(docked);
view_->SetIsDocked(docked, activate_);
if (!callback.is_null())
callback.Run(nullptr);
}
Expand Down
3 changes: 2 additions & 1 deletion brightray/browser/inspectable_web_contents_impl.h
Expand Up @@ -50,7 +50,7 @@ class InspectableWebContentsImpl
InspectableWebContentsDelegate* GetDelegate() const override;
void SetDevToolsWebContents(content::WebContents* devtools) override;
void SetDockState(const std::string& state) override;
void ShowDevTools() override;
void ShowDevTools(bool activate) override;
void CloseDevTools() override;
bool IsDevToolsViewShowing() override;
void AttachTo(scoped_refptr<content::DevToolsAgentHost>) override;
Expand Down Expand Up @@ -199,6 +199,7 @@ class InspectableWebContentsImpl
gfx::Rect devtools_bounds_;
bool can_dock_;
std::string dock_state_;
bool activate_ = true;

using PendingRequestsMap = std::map<const net::URLFetcher*, DispatchCallback>;
PendingRequestsMap pending_requests_;
Expand Down
4 changes: 2 additions & 2 deletions brightray/browser/inspectable_web_contents_view.h
Expand Up @@ -38,12 +38,12 @@ class InspectableWebContentsView {
virtual gfx::NativeView GetNativeView() const = 0;
#endif

virtual void ShowDevTools() = 0;
virtual void ShowDevTools(bool activate) = 0;
// Hide the DevTools view.
virtual void CloseDevTools() = 0;
virtual bool IsDevToolsViewShowing() = 0;
virtual bool IsDevToolsViewFocused() = 0;
virtual void SetIsDocked(bool docked) = 0;
virtual void SetIsDocked(bool docked, bool activate) = 0;
virtual void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) = 0;
virtual void SetTitle(const base::string16& title) = 0;
Expand Down
4 changes: 2 additions & 2 deletions brightray/browser/inspectable_web_contents_view_mac.h
Expand Up @@ -18,11 +18,11 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView {
~InspectableWebContentsViewMac() override;

gfx::NativeView GetNativeView() const override;
void ShowDevTools() override;
void ShowDevTools(bool activate) override;
void CloseDevTools() override;
bool IsDevToolsViewShowing() override;
bool IsDevToolsViewFocused() override;
void SetIsDocked(bool docked) override;
void SetIsDocked(bool docked, bool activate) override;
void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const base::string16& title) override;
Expand Down
10 changes: 5 additions & 5 deletions brightray/browser/inspectable_web_contents_view_mac.mm
Expand Up @@ -29,12 +29,12 @@
return view_.get();
}

void InspectableWebContentsViewMac::ShowDevTools() {
[view_ setDevToolsVisible:YES];
void InspectableWebContentsViewMac::ShowDevTools(bool activate) {
[view_ setDevToolsVisible:YES activate:activate];
}

void InspectableWebContentsViewMac::CloseDevTools() {
[view_ setDevToolsVisible:NO];
[view_ setDevToolsVisible:NO activate:NO];
}

bool InspectableWebContentsViewMac::IsDevToolsViewShowing() {
Expand All @@ -45,8 +45,8 @@
return [view_ isDevToolsFocused];
}

void InspectableWebContentsViewMac::SetIsDocked(bool docked) {
[view_ setIsDocked:docked];
void InspectableWebContentsViewMac::SetIsDocked(bool docked, bool activate) {
[view_ setIsDocked:docked activate:activate];
}

void InspectableWebContentsViewMac::SetContentsResizingStrategy(
Expand Down
4 changes: 2 additions & 2 deletions brightray/browser/mac/bry_inspectable_web_contents_view.h
Expand Up @@ -26,10 +26,10 @@ using brightray::InspectableWebContentsViewMac;
(InspectableWebContentsViewMac*)view;
- (void)removeObservers;
- (void)notifyDevToolsFocused;
- (void)setDevToolsVisible:(BOOL)visible;
- (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate;
- (BOOL)isDevToolsVisible;
- (BOOL)isDevToolsFocused;
- (void)setIsDocked:(BOOL)docked;
- (void)setIsDocked:(BOOL)docked activate:(BOOL)activate;
- (void)setContentsResizingStrategy:
(const DevToolsContentsResizingStrategy&)strategy;
- (void)setTitle:(NSString*)title;
Expand Down
16 changes: 10 additions & 6 deletions brightray/browser/mac/bry_inspectable_web_contents_view.mm
Expand Up @@ -53,15 +53,15 @@ - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
}

- (IBAction)showDevTools:(id)sender {
inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools();
inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools(true);
}

- (void)notifyDevToolsFocused {
if (inspectableWebContentsView_->GetDelegate())
inspectableWebContentsView_->GetDelegate()->DevToolsFocused();
}

- (void)setDevToolsVisible:(BOOL)visible {
- (void)setDevToolsVisible:(BOOL)visible activate:(BOOL)activate {
if (visible == devtools_visible_)
return;

Expand Down Expand Up @@ -96,7 +96,11 @@ - (void)setDevToolsVisible:(BOOL)visible {
}
} else {
if (visible) {
[devtools_window_ makeKeyAndOrderFront:nil];
if (activate) {
[devtools_window_ makeKeyAndOrderFront:nil];
} else {
[devtools_window_ orderBack:nil];
}
} else {
[devtools_window_ setDelegate:nil];
[devtools_window_ close];
Expand All @@ -117,9 +121,9 @@ - (BOOL)isDevToolsFocused {
}
}

- (void)setIsDocked:(BOOL)docked {
- (void)setIsDocked:(BOOL)docked activate:(BOOL)activate {
// Revert to no-devtools state.
[self setDevToolsVisible:NO];
[self setDevToolsVisible:NO activate:NO];

// Switch to new state.
devtools_docked_ = docked;
Expand Down Expand Up @@ -153,7 +157,7 @@ - (void)setIsDocked:(BOOL)docked {

[contentView addSubview:devToolsView];
}
[self setDevToolsVisible:YES];
[self setDevToolsVisible:YES activate:activate];
}

- (void)setContentsResizingStrategy:
Expand Down
12 changes: 8 additions & 4 deletions brightray/browser/views/inspectable_web_contents_view_views.cc
Expand Up @@ -110,7 +110,7 @@ views::View* InspectableWebContentsViewViews::GetWebView() {
return contents_web_view_;
}

void InspectableWebContentsViewViews::ShowDevTools() {
void InspectableWebContentsViewViews::ShowDevTools(bool activate) {
if (devtools_visible_)
return;

Expand All @@ -120,7 +120,11 @@ void InspectableWebContentsViewViews::ShowDevTools() {
inspectable_web_contents_->GetDevToolsWebContents());
devtools_window_->SetBounds(
inspectable_web_contents()->GetDevToolsBounds());
devtools_window_->Show();
if (activate) {
devtools_window_->Show();
} else {
devtools_window_->ShowInactive();
}
} else {
devtools_web_view_->SetVisible(true);
devtools_web_view_->SetWebContents(
Expand Down Expand Up @@ -161,7 +165,7 @@ bool InspectableWebContentsViewViews::IsDevToolsViewFocused() {
return false;
}

void InspectableWebContentsViewViews::SetIsDocked(bool docked) {
void InspectableWebContentsViewViews::SetIsDocked(bool docked, bool activate) {
CloseDevTools();

if (!docked) {
Expand All @@ -186,7 +190,7 @@ void InspectableWebContentsViewViews::SetIsDocked(bool docked) {
devtools_window_->UpdateWindowIcon();
}

ShowDevTools();
ShowDevTools(activate);
}

void InspectableWebContentsViewViews::SetContentsResizingStrategy(
Expand Down
4 changes: 2 additions & 2 deletions brightray/browser/views/inspectable_web_contents_view_views.h
Expand Up @@ -26,11 +26,11 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView,
// InspectableWebContentsView:
views::View* GetView() override;
views::View* GetWebView() override;
void ShowDevTools() override;
void ShowDevTools(bool activate) override;
void CloseDevTools() override;
bool IsDevToolsViewShowing() override;
bool IsDevToolsViewFocused() override;
void SetIsDocked(bool docked) override;
void SetIsDocked(bool docked, bool activate) override;
void SetContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy) override;
void SetTitle(const base::string16& title) override;
Expand Down
2 changes: 2 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -1182,6 +1182,8 @@ app.once('ready', () => {
* `options` Object (optional)
* `mode` String - Opens the devtools with specified dock state, can be
`right`, `bottom`, `undocked`, `detach`. Defaults to last used dock state.
* `activate` Boolean (optional) - Whether to bring the opened devtools window to the
foreground. The default is `true`.
In `undocked` mode it's possible to dock back. In `detach` mode it's not.

Opens the devtools.
Expand Down
22 changes: 22 additions & 0 deletions spec/api-web-contents-spec.js
Expand Up @@ -147,6 +147,28 @@ describe('webContents module', () => {
})
})

describe('openDevTools() API', () => {
it('can show window with activation', (done) => {
w.show()
assert.equal(w.isFocused(), true)
w.webContents.openDevTools({ mode: 'detach', activate: true })
w.webContents.once('devtools-opened', () => {
assert.equal(w.isFocused(), false)
done()
})
})

it('can show window without activation', (done) => {
w.show()
assert.equal(w.isFocused(), true)
w.webContents.openDevTools({ mode: 'detach', activate: false })
w.webContents.once('devtools-opened', () => {
assert.equal(w.isFocused(), true)
done()
})
})
})

describe('before-input-event event', () => {
it('can prevent document keyboard events', (done) => {
w.loadURL(`file://${path.join(__dirname, 'fixtures', 'pages', 'key-events.html')}`)
Expand Down

0 comments on commit 3916d59

Please sign in to comment.