2016-07-13 15:54:40 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2018-04-12 07:16:00 +00:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
#include "shell/browser/ui/cocoa/event_dispatching_window.h"
|
|
|
|
#include "shell/browser/web_contents_preferences.h"
|
2021-02-18 01:31:53 +00:00
|
|
|
#include "ui/base/cocoa/command_dispatcher.h"
|
2020-11-10 17:06:56 +00:00
|
|
|
#include "ui/events/keycodes/keyboard_codes.h"
|
2016-07-13 15:54:40 +00:00
|
|
|
|
2016-08-23 16:51:54 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2016-07-13 15:54:40 +00:00
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
@interface NSWindow (EventDispatchingWindow)
|
|
|
|
- (void)redispatchKeyEvent:(NSEvent*)event;
|
|
|
|
@end
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2016-07-13 15:54:40 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
bool WebContents::IsFocused() const {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* view = web_contents()->GetRenderWidgetHostView();
|
2018-04-20 18:47:04 +00:00
|
|
|
if (!view)
|
|
|
|
return false;
|
2016-07-28 22:47:52 +00:00
|
|
|
|
2020-10-27 17:51:45 +00:00
|
|
|
if (GetType() != Type::kBackgroundPage) {
|
2019-01-09 20:25:19 +00:00
|
|
|
auto window = [web_contents()->GetNativeView().GetNativeNSView() window];
|
2016-07-13 15:54:40 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:47:52 +00:00
|
|
|
return view->HasFocus();
|
2016-07-13 15:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 17:06:56 +00:00
|
|
|
bool WebContents::PlatformHandleKeyboardEvent(
|
|
|
|
content::WebContents* source,
|
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
if (event.skip_in_browser ||
|
|
|
|
event.GetType() == content::NativeWebKeyboardEvent::Type::kChar)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Escape exits tabbed fullscreen mode.
|
|
|
|
if (event.windows_key_code == ui::VKEY_ESCAPE && is_html_fullscreen()) {
|
|
|
|
ExitFullscreenModeForTab(source);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the webContents has preferences and to ignore shortcuts
|
|
|
|
auto* web_preferences = WebContentsPreferences::From(source);
|
2021-07-26 16:04:09 +00:00
|
|
|
if (web_preferences && web_preferences->ShouldIgnoreMenuShortcuts())
|
2020-11-10 17:06:56 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the event to the menu before sending it to the window
|
|
|
|
if (event.os_event.type == NSKeyDown &&
|
|
|
|
[[NSApp mainMenu] performKeyEquivalent:event.os_event])
|
|
|
|
return true;
|
|
|
|
|
2021-02-18 01:31:53 +00:00
|
|
|
// Let the window redispatch the OS event
|
2020-11-10 17:06:56 +00:00
|
|
|
if (event.os_event.window &&
|
|
|
|
[event.os_event.window isKindOfClass:[EventDispatchingWindow class]]) {
|
|
|
|
[event.os_event.window redispatchKeyEvent:event.os_event];
|
|
|
|
// FIXME(nornagon): this isn't the right return value; we should implement
|
|
|
|
// devtools windows as Widgets in order to take advantage of the
|
|
|
|
// pre-existing redispatch code in bridged_native_widget.
|
|
|
|
return false;
|
2021-02-18 01:31:53 +00:00
|
|
|
} else if (event.os_event.window &&
|
|
|
|
[event.os_event.window
|
|
|
|
conformsToProtocol:@protocol(CommandDispatchingWindow)]) {
|
|
|
|
NSObject<CommandDispatchingWindow>* window =
|
|
|
|
static_cast<NSObject<CommandDispatchingWindow>*>(event.os_event.window);
|
|
|
|
[[window commandDispatcher] redispatchKeyEvent:event.os_event];
|
|
|
|
// FIXME(clavin): Not exactly sure what to return here, likely the same
|
|
|
|
// situation as the branch above. If a future refactor removes
|
|
|
|
// |EventDispatchingWindow| then only this branch will need to remain.
|
|
|
|
return false;
|
2020-11-10 17:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-13 15:54:40 +00:00
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|