From c30f458d4fb17655a86a8177d78b84c33dc6aa32 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 15 Sep 2015 11:04:46 +0800 Subject: [PATCH 1/2] Add IsDevToolsViewFocused method --- .../browser/inspectable_web_contents_view.h | 1 + .../inspectable_web_contents_view_mac.h | 1 + .../inspectable_web_contents_view_mac.mm | 4 ++ .../mac/bry_inspectable_web_contents_view.h | 2 + .../mac/bry_inspectable_web_contents_view.mm | 38 +++++++++++++++++++ .../inspectable_web_contents_view_views.cc | 7 ++++ .../inspectable_web_contents_view_views.h | 1 + 7 files changed, 54 insertions(+) diff --git a/brightray/browser/inspectable_web_contents_view.h b/brightray/browser/inspectable_web_contents_view.h index 469677a1ae..2fabf5d2ba 100644 --- a/brightray/browser/inspectable_web_contents_view.h +++ b/brightray/browser/inspectable_web_contents_view.h @@ -44,6 +44,7 @@ class InspectableWebContentsView { // Hide the DevTools view. virtual void CloseDevTools() = 0; virtual bool IsDevToolsViewShowing() = 0; + virtual bool IsDevToolsViewFocused() = 0; virtual void SetIsDocked(bool docked) = 0; virtual void SetContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy) = 0; diff --git a/brightray/browser/inspectable_web_contents_view_mac.h b/brightray/browser/inspectable_web_contents_view_mac.h index 0dbb7beb82..e7ad57fbee 100644 --- a/brightray/browser/inspectable_web_contents_view_mac.h +++ b/brightray/browser/inspectable_web_contents_view_mac.h @@ -21,6 +21,7 @@ class InspectableWebContentsViewMac : public InspectableWebContentsView { void ShowDevTools() override; void CloseDevTools() override; bool IsDevToolsViewShowing() override; + bool IsDevToolsViewFocused() override; void SetIsDocked(bool docked) override; void SetContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy) override; diff --git a/brightray/browser/inspectable_web_contents_view_mac.mm b/brightray/browser/inspectable_web_contents_view_mac.mm index 42ca3c6f3a..7d61fb0b7a 100644 --- a/brightray/browser/inspectable_web_contents_view_mac.mm +++ b/brightray/browser/inspectable_web_contents_view_mac.mm @@ -42,6 +42,10 @@ bool InspectableWebContentsViewMac::IsDevToolsViewShowing() { return [view_ isDevToolsVisible]; } +bool InspectableWebContentsViewMac::IsDevToolsViewFocused() { + return [view_ isDevToolsFocused]; +} + void InspectableWebContentsViewMac::SetIsDocked(bool docked) { [view_ setIsDocked:docked]; } diff --git a/brightray/browser/mac/bry_inspectable_web_contents_view.h b/brightray/browser/mac/bry_inspectable_web_contents_view.h index 3b72aba08c..7d8556973a 100644 --- a/brightray/browser/mac/bry_inspectable_web_contents_view.h +++ b/brightray/browser/mac/bry_inspectable_web_contents_view.h @@ -18,6 +18,7 @@ using brightray::InspectableWebContentsViewMac; base::scoped_nsobject devtools_window_; BOOL devtools_visible_; BOOL devtools_docked_; + BOOL devtools_is_first_responder_; DevToolsContentsResizingStrategy strategy_; } @@ -25,6 +26,7 @@ using brightray::InspectableWebContentsViewMac; - (instancetype)initWithInspectableWebContentsViewMac:(InspectableWebContentsViewMac*)view; - (void)setDevToolsVisible:(BOOL)visible; - (BOOL)isDevToolsVisible; +- (BOOL)isDevToolsFocused; - (void)setIsDocked:(BOOL)docked; - (void)setContentsResizingStrategy:(const DevToolsContentsResizingStrategy&)strategy; - (void)setTitle:(NSString*)title; diff --git a/brightray/browser/mac/bry_inspectable_web_contents_view.mm b/brightray/browser/mac/bry_inspectable_web_contents_view.mm index 1bb84e5abb..dd40f1e259 100644 --- a/brightray/browser/mac/bry_inspectable_web_contents_view.mm +++ b/brightray/browser/mac/bry_inspectable_web_contents_view.mm @@ -19,6 +19,13 @@ using namespace brightray; inspectableWebContentsView_ = view; devtools_visible_ = NO; devtools_docked_ = NO; + devtools_is_first_responder_ = NO; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(viewDidBecomeFirstResponder:) + name:kViewDidBecomeFirstResponder + object:nil]; auto contents = inspectableWebContentsView_->inspectable_web_contents()->GetWebContents(); auto contentsView = contents->GetNativeView(); @@ -86,6 +93,14 @@ using namespace brightray; return devtools_visible_; } +- (BOOL)isDevToolsFocused { + if (devtools_docked_) { + return [[self window] isKeyWindow] && devtools_is_first_responder_; + } else { + return [devtools_window_ isKeyWindow]; + } +} + - (void)setIsDocked:(BOOL)docked { // Revert to no-devtools state. [self setDevToolsVisible:NO]; @@ -156,6 +171,29 @@ using namespace brightray; [devtools_window_ setTitle:title]; } +- (void)viewDidBecomeFirstResponder:(NSNotification*)notification { + auto inspectable_web_contents = inspectableWebContentsView_->inspectable_web_contents(); + if (!inspectable_web_contents) + return; + auto webContents = inspectable_web_contents->GetWebContents(); + auto webContentsView = webContents->GetNativeView(); + + NSView* view = [notification object]; + if ([[webContentsView subviews] containsObject:view]) { + devtools_is_first_responder_ = NO; + return; + } + + auto devToolsWebContents = inspectable_web_contents->GetDevToolsWebContents(); + if (!devToolsWebContents) + return; + auto devToolsView = devToolsWebContents->GetNativeView(); + + if ([[devToolsView subviews] containsObject:view]) { + devtools_is_first_responder_ = YES; + } +} + #pragma mark - NSWindowDelegate - (void)windowWillClose:(NSNotification*)notification { diff --git a/brightray/browser/views/inspectable_web_contents_view_views.cc b/brightray/browser/views/inspectable_web_contents_view_views.cc index 6a7dd75aa6..79fcc10379 100644 --- a/brightray/browser/views/inspectable_web_contents_view_views.cc +++ b/brightray/browser/views/inspectable_web_contents_view_views.cc @@ -145,6 +145,13 @@ bool InspectableWebContentsViewViews::IsDevToolsViewShowing() { return devtools_visible_; } +bool InspectableWebContentsViewViews::IsDevToolsViewFocused() { + if (devtools_web_view_) + return devtools_web_view_->HasFocus(); + else + return false; +} + void InspectableWebContentsViewViews::SetIsDocked(bool docked) { CloseDevTools(); diff --git a/brightray/browser/views/inspectable_web_contents_view_views.h b/brightray/browser/views/inspectable_web_contents_view_views.h index a6c3ae62f8..bcdbdb9518 100644 --- a/brightray/browser/views/inspectable_web_contents_view_views.h +++ b/brightray/browser/views/inspectable_web_contents_view_views.h @@ -37,6 +37,7 @@ class InspectableWebContentsViewViews : public InspectableWebContentsView, void ShowDevTools() override; void CloseDevTools() override; bool IsDevToolsViewShowing() override; + bool IsDevToolsViewFocused() override; void SetIsDocked(bool docked) override; void SetContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy) override; From 352f758cd1ceeddb50298930a378b3092a154bc8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 15 Sep 2015 11:24:35 +0800 Subject: [PATCH 2/2] Make DevToolsFocused in OS X --- .../browser/inspectable_web_contents_impl.cc | 2 ++ .../mac/bry_inspectable_web_contents_view.h | 1 + .../mac/bry_inspectable_web_contents_view.mm | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index c7d93a7aca..f6532e4ed0 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -608,8 +608,10 @@ void InspectableWebContentsImpl::CloseContents(content::WebContents* source) { } void InspectableWebContentsImpl::OnWebContentsFocused() { +#if defined(TOOLKIT_VIEWS) if (view_->GetDelegate()) view_->GetDelegate()->DevToolsFocused(); +#endif } void InspectableWebContentsImpl::OnURLFetchComplete(const net::URLFetcher* source) { diff --git a/brightray/browser/mac/bry_inspectable_web_contents_view.h b/brightray/browser/mac/bry_inspectable_web_contents_view.h index 7d8556973a..b67baa6385 100644 --- a/brightray/browser/mac/bry_inspectable_web_contents_view.h +++ b/brightray/browser/mac/bry_inspectable_web_contents_view.h @@ -24,6 +24,7 @@ using brightray::InspectableWebContentsViewMac; } - (instancetype)initWithInspectableWebContentsViewMac:(InspectableWebContentsViewMac*)view; +- (void)notifyDevToolsFocused; - (void)setDevToolsVisible:(BOOL)visible; - (BOOL)isDevToolsVisible; - (BOOL)isDevToolsFocused; diff --git a/brightray/browser/mac/bry_inspectable_web_contents_view.mm b/brightray/browser/mac/bry_inspectable_web_contents_view.mm index dd40f1e259..1123f7e0cd 100644 --- a/brightray/browser/mac/bry_inspectable_web_contents_view.mm +++ b/brightray/browser/mac/bry_inspectable_web_contents_view.mm @@ -1,6 +1,7 @@ #include "browser/mac/bry_inspectable_web_contents_view.h" #include "browser/inspectable_web_contents_impl.h" +#include "browser/inspectable_web_contents_view_delegate.h" #include "browser/inspectable_web_contents_view_mac.h" #include "content/public/browser/render_widget_host_view.h" @@ -27,6 +28,12 @@ using namespace brightray; name:kViewDidBecomeFirstResponder object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(parentWindowBecameMain:) + name:NSWindowDidBecomeMainNotification + object:nil]; + auto contents = inspectableWebContentsView_->inspectable_web_contents()->GetWebContents(); auto contentsView = contents->GetNativeView(); [contentsView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; @@ -38,6 +45,11 @@ using namespace brightray; return self; } +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize { [self adjustSubviews]; } @@ -46,6 +58,11 @@ using namespace brightray; inspectableWebContentsView_->inspectable_web_contents()->ShowDevTools(); } +- (void)notifyDevToolsFocused { + if (inspectableWebContentsView_->GetDelegate()) + inspectableWebContentsView_->GetDelegate()->DevToolsFocused(); +} + - (void)setDevToolsVisible:(BOOL)visible { if (visible == devtools_visible_) return; @@ -191,9 +208,16 @@ using namespace brightray; if ([[devToolsView subviews] containsObject:view]) { devtools_is_first_responder_ = YES; + [self notifyDevToolsFocused]; } } +- (void)parentWindowBecameMain:(NSNotification*)notification { + NSWindow* parentWindow = [notification object]; + if ([self window] == parentWindow && devtools_docked_ && devtools_is_first_responder_) + [self notifyDevToolsFocused]; +} + #pragma mark - NSWindowDelegate - (void)windowWillClose:(NSNotification*)notification { @@ -211,6 +235,8 @@ using namespace brightray; content::RenderWidgetHostView* rwhv = web_contents->GetRenderWidgetHostView(); if (rwhv) rwhv->SetActive(true); + + [self notifyDevToolsFocused]; } - (void)windowDidResignMain:(NSNotification*)notification {