diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 8622942668b0..43e078d6f163 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -789,6 +789,13 @@ WebContents::Type WebContents::GetType() const { return type_; } +#if !defined(OS_MACOSX) +bool WebContents::IsFocused() const { + auto view = web_contents()->GetRenderWidgetHostView(); + return view && view->HasFocus(); +} +#endif + bool WebContents::Equal(const WebContents* web_contents) const { return GetID() == web_contents->GetID(); } @@ -1418,6 +1425,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("showDefinitionForSelection", &WebContents::ShowDefinitionForSelection) .SetMethod("capturePage", &WebContents::CapturePage) + .SetMethod("isFocused", &WebContents::IsFocused) .SetProperty("id", &WebContents::ID) .SetProperty("session", &WebContents::Session) .SetProperty("hostWebContents", &WebContents::HostWebContents) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 389550390beb..5e1a48783d88 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -67,6 +67,7 @@ class WebContents : public mate::TrackableObject, int GetID() const; Type GetType() const; + bool IsFocused() const; bool Equal(const WebContents* web_contents) const; void LoadURL(const GURL& url, const mate::Dictionary& options); void DownloadURL(const GURL& url); diff --git a/atom/browser/api/atom_api_web_contents_mac.mm b/atom/browser/api/atom_api_web_contents_mac.mm new file mode 100644 index 000000000000..19246e82ac7f --- /dev/null +++ b/atom/browser/api/atom_api_web_contents_mac.mm @@ -0,0 +1,30 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/api/atom_api_web_contents.h" + +@interface NSWindow +- (BOOL)isKeyWindow; +@end + +namespace atom { + +namespace api { + +bool WebContents::IsFocused() const { + if (GetType() != BACKGROUND_PAGE) { + auto window = web_contents()->GetTopLevelNativeWindow(); + // On Mac the render widget host view does not lose focus when the window + // loses focus so check if the top level window is the key window. + if (window && ![window isKeyWindow]) + return false; + } + + auto view = web_contents()->GetRenderWidgetHostView(); + return view && view->HasFocus(); +} + +} // namespace api + +} // namespace atom diff --git a/filenames.gypi b/filenames.gypi index c809a2e05a8f..0ca3859b0311 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -129,6 +129,7 @@ 'atom/browser/api/atom_api_system_preferences_mac.mm', 'atom/browser/api/atom_api_tray.cc', 'atom/browser/api/atom_api_tray.h', + 'atom/browser/api/atom_api_web_contents_mac.mm', 'atom/browser/api/atom_api_web_contents.cc', 'atom/browser/api/atom_api_web_contents.h', 'atom/browser/api/atom_api_web_request.cc', diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index dcd14a40fe30..e418077316ff 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -244,5 +244,8 @@ module.exports = { fromId (id) { return binding.fromId(id) + }, + getAllWebContents () { + return binding.getAllWebContents() } }