2015-11-17 19:20:13 +01:00
|
|
|
// Copyright 2015 The Chromium Embedded Framework Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/native/browser_platform_delegate_native_mac.h"
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import <CoreServices/CoreServices.h>
|
|
|
|
|
2023-05-05 13:17:14 +02:00
|
|
|
#include "include/internal/cef_types_mac.h"
|
2020-09-22 21:54:02 +02:00
|
|
|
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "libcef/browser/context.h"
|
|
|
|
#include "libcef/browser/native/javascript_dialog_runner_mac.h"
|
|
|
|
#include "libcef/browser/native/menu_runner_mac.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
|
2023-06-26 12:13:38 +02:00
|
|
|
#include "base/apple/owned_objc.h"
|
2023-09-15 21:51:43 +02:00
|
|
|
#include "base/apple/scoped_nsautorelease_pool.h"
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "base/memory/ptr_util.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2020-01-23 22:58:01 +01:00
|
|
|
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "content/public/browser/render_widget_host_view.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2023-09-15 21:51:43 +02:00
|
|
|
#include "content/public/common/input/native_web_keyboard_event.h"
|
2020-03-04 01:29:39 +01:00
|
|
|
#include "third_party/blink/public/common/input/web_input_event.h"
|
|
|
|
#include "third_party/blink/public/common/input/web_mouse_event.h"
|
|
|
|
#include "third_party/blink/public/common/input/web_mouse_wheel_event.h"
|
2022-11-08 22:27:22 +01:00
|
|
|
#include "ui/display/screen.h"
|
2020-01-23 22:58:01 +01:00
|
|
|
#include "ui/events/base_event_utils.h"
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "ui/events/keycodes/keyboard_codes_posix.h"
|
|
|
|
#include "ui/gfx/geometry/rect.h"
|
|
|
|
|
|
|
|
// Wrapper NSView for the native view. Necessary to destroy the browser when
|
|
|
|
// the view is deleted.
|
|
|
|
@interface CefBrowserHostView : NSView {
|
|
|
|
@private
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserHostBase* browser_; // weak
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
@property(nonatomic, assign) CefBrowserHostBase* browser;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CefBrowserHostView
|
|
|
|
|
|
|
|
@synthesize browser = browser_;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
- (void)dealloc {
|
2015-11-17 19:20:13 +01:00
|
|
|
if (browser_) {
|
|
|
|
// Force the browser to be destroyed and release the reference added in
|
|
|
|
// PlatformCreateWindow().
|
2020-09-25 03:40:47 +02:00
|
|
|
static_cast<AlloyBrowserHostImpl*>(browser_)->WindowDestroyed();
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// Receives notifications from the browser window. Will delete itself when done.
|
2019-04-23 19:17:56 +02:00
|
|
|
@interface CefWindowDelegate : NSObject <NSWindowDelegate> {
|
2015-11-17 19:20:13 +01:00
|
|
|
@private
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserHostBase* browser_; // weak
|
2023-05-05 13:17:14 +02:00
|
|
|
NSWindow* __strong window_;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
- (id)initWithWindow:(NSWindow*)window andBrowser:(CefBrowserHostBase*)browser;
|
2015-11-17 19:20:13 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation CefWindowDelegate
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
- (id)initWithWindow:(NSWindow*)window andBrowser:(CefBrowserHostBase*)browser {
|
2015-11-17 19:20:13 +01:00
|
|
|
if (self = [super init]) {
|
|
|
|
window_ = window;
|
|
|
|
browser_ = browser;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(id)window {
|
2016-01-19 21:09:01 +01:00
|
|
|
if (browser_ && !browser_->TryCloseBrowser()) {
|
2015-11-17 19:20:13 +01:00
|
|
|
// Cancel the close.
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:17:14 +02:00
|
|
|
// For an NSWindow object, the default is to be released on |close|. We
|
|
|
|
// instead want it to remain valid until all strong references are released
|
|
|
|
// via |cleanup:| and |BrowserDestroyed|.
|
|
|
|
((NSWindow*)window).releasedWhenClosed = NO;
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
// Clean ourselves up after clearing the stack of anything that might have the
|
|
|
|
// window on it.
|
|
|
|
[self performSelectorOnMainThread:@selector(cleanup:)
|
|
|
|
withObject:window
|
|
|
|
waitUntilDone:NO];
|
|
|
|
|
|
|
|
// Allow the close.
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cleanup:(id)window {
|
2018-09-06 13:46:32 +02:00
|
|
|
[window_ setDelegate:nil];
|
2023-05-05 13:17:14 +02:00
|
|
|
window_ = nil;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
NSTimeInterval currentEventTimestamp() {
|
|
|
|
NSEvent* currentEvent = [NSApp currentEvent];
|
2023-01-02 23:59:03 +01:00
|
|
|
if (currentEvent) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return [currentEvent timestamp];
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2015-11-17 19:20:13 +01:00
|
|
|
// FIXME(API): In case there is no current event, the timestamp could be
|
|
|
|
// obtained by getting the time since the application started. This involves
|
|
|
|
// taking some more static functions from Chromium code.
|
|
|
|
// Another option is to have the timestamp as a field in CefEvent structures
|
|
|
|
// and let the client provide it.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUInteger NativeModifiers(int cef_modifiers) {
|
|
|
|
NSUInteger native_modifiers = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_modifiers & EVENTFLAG_SHIFT_DOWN) {
|
2022-06-17 15:28:55 +02:00
|
|
|
native_modifiers |= NSEventModifierFlagShift;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (cef_modifiers & EVENTFLAG_CONTROL_DOWN) {
|
2022-06-17 15:28:55 +02:00
|
|
|
native_modifiers |= NSEventModifierFlagControl;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (cef_modifiers & EVENTFLAG_ALT_DOWN) {
|
2022-06-17 15:28:55 +02:00
|
|
|
native_modifiers |= NSEventModifierFlagOption;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (cef_modifiers & EVENTFLAG_COMMAND_DOWN) {
|
2022-06-17 15:28:55 +02:00
|
|
|
native_modifiers |= NSEventModifierFlagCommand;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (cef_modifiers & EVENTFLAG_CAPS_LOCK_ON) {
|
2022-06-17 15:28:55 +02:00
|
|
|
native_modifiers |= NSEventModifierFlagCapsLock;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (cef_modifiers & EVENTFLAG_NUM_LOCK_ON) {
|
2022-06-17 15:28:55 +02:00
|
|
|
native_modifiers |= NSEventModifierFlagNumericPad;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
return native_modifiers;
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:29:16 +02:00
|
|
|
constexpr int kDefaultHeight = 750;
|
|
|
|
constexpr int kDefaultWidth = 750;
|
|
|
|
constexpr NSWindowStyleMask kDefaultStyleMask =
|
|
|
|
NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
|
2022-11-08 22:27:22 +01:00
|
|
|
NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
|
|
|
|
|
|
|
|
// Keep the frame bounds inside the display work area.
|
|
|
|
NSRect ClampNSBoundsToWorkArea(const NSRect& frame_bounds,
|
|
|
|
const gfx::Rect& display_bounds,
|
|
|
|
const gfx::Rect& work_area) {
|
|
|
|
NSRect bounds = frame_bounds;
|
|
|
|
|
|
|
|
// Convert from DIP coordinates (top-left origin) to macOS coordinates
|
|
|
|
// (bottom-left origin).
|
|
|
|
const int work_area_y =
|
|
|
|
display_bounds.height() - work_area.height() - work_area.y();
|
|
|
|
|
|
|
|
if (bounds.size.width > work_area.width()) {
|
|
|
|
bounds.size.width = work_area.width();
|
|
|
|
}
|
|
|
|
if (bounds.size.height > work_area.height()) {
|
|
|
|
bounds.size.height = work_area.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bounds.origin.x < work_area.x()) {
|
|
|
|
bounds.origin.x = work_area.x();
|
|
|
|
} else if (bounds.origin.x + bounds.size.width >=
|
|
|
|
work_area.x() + work_area.width()) {
|
|
|
|
bounds.origin.x = work_area.x() + work_area.width() - bounds.size.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bounds.origin.y < work_area_y) {
|
|
|
|
bounds.origin.y = work_area_y;
|
|
|
|
} else if (bounds.origin.y + bounds.size.height >=
|
|
|
|
work_area_y + work_area.height()) {
|
|
|
|
bounds.origin.y = work_area_y + work_area.height() - bounds.size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get frame and content area rects matching the input DIP screen bounds. The
|
|
|
|
// resulting window frame will be kept inside the closest display work area. If
|
|
|
|
// |input_content_bounds| is true the input size is used for the content area
|
|
|
|
// and the input origin is used for the frame. Otherwise, both input size and
|
|
|
|
// origin are used for the frame.
|
|
|
|
void GetNSBoundsInDisplay(const gfx::Rect& dip_bounds,
|
|
|
|
bool input_content_bounds,
|
|
|
|
NSWindowStyleMask style_mask,
|
|
|
|
NSRect& frame_rect,
|
|
|
|
NSRect& content_rect) {
|
|
|
|
// Identify the closest display.
|
|
|
|
const auto display =
|
|
|
|
display::Screen::GetScreen()->GetDisplayMatching(dip_bounds);
|
|
|
|
const auto& display_bounds = display.bounds();
|
|
|
|
const auto& display_work_area = display.work_area();
|
|
|
|
|
|
|
|
// Convert from DIP coordinates (top-left origin) to macOS coordinates
|
|
|
|
// (bottom-left origin).
|
|
|
|
NSRect requested_rect = NSMakeRect(dip_bounds.x(), dip_bounds.y(),
|
|
|
|
dip_bounds.width(), dip_bounds.height());
|
|
|
|
requested_rect.origin.y = display_bounds.height() -
|
|
|
|
requested_rect.size.height -
|
|
|
|
requested_rect.origin.y;
|
|
|
|
|
|
|
|
// Calculate the equivalent frame and content bounds.
|
|
|
|
if (input_content_bounds) {
|
|
|
|
// Compute frame rect from content rect. Keep the requested origin.
|
|
|
|
content_rect = requested_rect;
|
2022-11-16 18:15:37 +01:00
|
|
|
frame_rect = [NSWindow frameRectForContentRect:content_rect
|
2022-11-08 22:27:22 +01:00
|
|
|
styleMask:style_mask];
|
|
|
|
frame_rect.origin = requested_rect.origin;
|
|
|
|
} else {
|
|
|
|
// Compute content rect from frame rect.
|
|
|
|
frame_rect = requested_rect;
|
|
|
|
content_rect = [NSWindow contentRectForFrameRect:frame_rect
|
|
|
|
styleMask:style_mask];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the frame inside the display work area.
|
|
|
|
const NSRect new_frame_rect =
|
|
|
|
ClampNSBoundsToWorkArea(frame_rect, display_bounds, display_work_area);
|
|
|
|
if (!NSEqualRects(frame_rect, new_frame_rect)) {
|
|
|
|
frame_rect = new_frame_rect;
|
|
|
|
content_rect = [NSWindow contentRectForFrameRect:frame_rect
|
|
|
|
styleMask:style_mask];
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 12:29:16 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefBrowserPlatformDelegateNativeMac::CefBrowserPlatformDelegateNativeMac(
|
2017-04-20 21:28:17 +02:00
|
|
|
const CefWindowInfo& window_info,
|
|
|
|
SkColor background_color)
|
2023-05-05 13:17:14 +02:00
|
|
|
: CefBrowserPlatformDelegateNative(window_info, background_color) {}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::BrowserDestroyed(
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserHostBase* browser) {
|
2020-07-04 20:21:34 +02:00
|
|
|
CefBrowserPlatformDelegateNative::BrowserDestroyed(browser);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
if (host_window_created_) {
|
2023-05-05 13:17:14 +02:00
|
|
|
// Release the references added in CreateHostWindow().
|
2015-11-17 19:20:13 +01:00
|
|
|
browser->Release();
|
2023-05-05 13:17:14 +02:00
|
|
|
window_delegate_ = nil;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefBrowserPlatformDelegateNativeMac::CreateHostWindow() {
|
2023-09-15 21:51:43 +02:00
|
|
|
base::apple::ScopedNSAutoreleasePool autorelease_pool;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-11-08 22:27:22 +01:00
|
|
|
NSWindow* new_window = nil;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-11-08 22:27:22 +01:00
|
|
|
NSView* parent_view =
|
2019-04-23 19:17:56 +02:00
|
|
|
CAST_CEF_WINDOW_HANDLE_TO_NSVIEW(window_info_.parent_view);
|
2022-11-08 22:27:22 +01:00
|
|
|
NSRect browser_view_rect =
|
|
|
|
NSMakeRect(window_info_.bounds.x, window_info_.bounds.y,
|
|
|
|
window_info_.bounds.width, window_info_.bounds.height);
|
|
|
|
|
|
|
|
if (parent_view == nil) {
|
|
|
|
// TODO(port): If no x,y position is specified the window will always appear
|
|
|
|
// in the upper-left corner. Maybe there's a better default place to put it?
|
|
|
|
const gfx::Rect dip_bounds(
|
|
|
|
window_info_.bounds.x, window_info_.bounds.y,
|
|
|
|
window_info_.bounds.width <= 0 ? kDefaultWidth
|
|
|
|
: window_info_.bounds.width,
|
|
|
|
window_info_.bounds.height <= 0 ? kDefaultHeight
|
|
|
|
: window_info_.bounds.height);
|
|
|
|
|
|
|
|
// Calculate the equivalent frame and content area bounds.
|
|
|
|
NSRect frame_rect, content_rect;
|
|
|
|
GetNSBoundsInDisplay(dip_bounds, /*input_content_bounds=*/true,
|
|
|
|
kDefaultStyleMask, frame_rect, content_rect);
|
2022-04-12 19:12:16 +02:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
// Create a new window.
|
2023-04-26 21:55:59 +02:00
|
|
|
new_window = [[NSWindow alloc] initWithContentRect:content_rect
|
|
|
|
styleMask:kDefaultStyleMask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO];
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// Create the delegate for control and browser window events.
|
2023-05-05 13:17:14 +02:00
|
|
|
// Add a reference that will be released in BrowserDestroyed().
|
|
|
|
window_delegate_ = [[CefWindowDelegate alloc] initWithWindow:new_window
|
|
|
|
andBrowser:browser_];
|
|
|
|
[new_window setDelegate:window_delegate_];
|
2022-11-08 22:27:22 +01:00
|
|
|
|
|
|
|
parent_view = [new_window contentView];
|
|
|
|
browser_view_rect = [parent_view bounds];
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2023-05-05 13:17:14 +02:00
|
|
|
window_info_.parent_view = CAST_NSVIEW_TO_CEF_WINDOW_HANDLE(parent_view);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2016-06-09 20:48:38 +02:00
|
|
|
// Make the content view for the window have a layer. This will make all
|
|
|
|
// sub-views have layers. This is necessary to ensure correct layer
|
|
|
|
// ordering of all child views and their layers.
|
2022-11-08 22:27:22 +01:00
|
|
|
[parent_view setWantsLayer:YES];
|
2022-04-12 19:12:16 +02:00
|
|
|
|
2022-11-08 22:27:22 +01:00
|
|
|
// Place the window at the target point. This is required for proper
|
|
|
|
// placement if the point is on a secondary display.
|
|
|
|
[new_window setFrameOrigin:frame_rect.origin];
|
2016-06-09 20:48:38 +02:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
host_window_created_ = true;
|
|
|
|
|
|
|
|
// Add a reference that will be released in BrowserDestroyed().
|
|
|
|
browser_->AddRef();
|
|
|
|
|
|
|
|
// Create the browser view.
|
|
|
|
CefBrowserHostView* browser_view =
|
2022-11-08 22:27:22 +01:00
|
|
|
[[CefBrowserHostView alloc] initWithFrame:browser_view_rect];
|
2015-11-17 19:20:13 +01:00
|
|
|
browser_view.browser = browser_;
|
2022-11-08 22:27:22 +01:00
|
|
|
[parent_view addSubview:browser_view];
|
2015-11-17 19:20:13 +01:00
|
|
|
[browser_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
|
|
|
|
[browser_view setNeedsDisplay:YES];
|
|
|
|
|
2022-11-08 22:27:22 +01:00
|
|
|
// Parent the WebContents to the browser view.
|
2015-11-17 19:20:13 +01:00
|
|
|
const NSRect bounds = [browser_view bounds];
|
2020-07-08 19:23:29 +02:00
|
|
|
NSView* native_view = web_contents_->GetNativeView().GetNativeNSView();
|
2015-11-17 19:20:13 +01:00
|
|
|
[browser_view addSubview:native_view];
|
|
|
|
[native_view setFrame:bounds];
|
|
|
|
[native_view setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
|
|
|
|
[native_view setNeedsDisplay:YES];
|
|
|
|
|
2023-05-05 13:17:14 +02:00
|
|
|
window_info_.view = CAST_NSVIEW_TO_CEF_WINDOW_HANDLE(browser_view);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-11-08 22:27:22 +01:00
|
|
|
if (new_window != nil && !window_info_.hidden) {
|
2015-11-17 19:20:13 +01:00
|
|
|
// Show the window.
|
2022-11-08 22:27:22 +01:00
|
|
|
[new_window makeKeyAndOrderFront:nil];
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::CloseHostWindow() {
|
2019-04-23 19:17:56 +02:00
|
|
|
NSView* nsview = CAST_CEF_WINDOW_HANDLE_TO_NSVIEW(window_info_.view);
|
|
|
|
if (nsview != nil) {
|
|
|
|
[[nsview window] performSelectorOnMainThread:@selector(performClose:)
|
|
|
|
withObject:nil
|
|
|
|
waitUntilDone:NO];
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefWindowHandle CefBrowserPlatformDelegateNativeMac::GetHostWindowHandle()
|
|
|
|
const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (windowless_handler_) {
|
2015-11-17 19:20:13 +01:00
|
|
|
return windowless_handler_->GetParentWindowHandle();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
return window_info_.view;
|
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
void CefBrowserPlatformDelegateNativeMac::SendKeyEvent(
|
|
|
|
const CefKeyEvent& event) {
|
|
|
|
auto view = GetHostView();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2020-01-23 22:58:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
content::NativeWebKeyboardEvent web_event = TranslateWebKeyEvent(event);
|
|
|
|
view->ForwardKeyboardEvent(web_event, ui::LatencyInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::SendMouseClickEvent(
|
|
|
|
const CefMouseEvent& event,
|
|
|
|
CefBrowserHost::MouseButtonType type,
|
|
|
|
bool mouseUp,
|
|
|
|
int clickCount) {
|
|
|
|
auto view = GetHostView();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2020-01-23 22:58:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
blink::WebMouseEvent web_event =
|
|
|
|
TranslateWebClickEvent(event, type, mouseUp, clickCount);
|
|
|
|
view->RouteOrProcessMouseEvent(web_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::SendMouseMoveEvent(
|
|
|
|
const CefMouseEvent& event,
|
|
|
|
bool mouseLeave) {
|
|
|
|
auto view = GetHostView();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2020-01-23 22:58:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
blink::WebMouseEvent web_event = TranslateWebMoveEvent(event, mouseLeave);
|
|
|
|
view->RouteOrProcessMouseEvent(web_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::SendMouseWheelEvent(
|
|
|
|
const CefMouseEvent& event,
|
|
|
|
int deltaX,
|
|
|
|
int deltaY) {
|
|
|
|
auto view = GetHostView();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2020-01-23 22:58:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
blink::WebMouseWheelEvent web_event =
|
|
|
|
TranslateWebWheelEvent(event, deltaX, deltaY);
|
|
|
|
view->RouteOrProcessMouseEvent(web_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::SendTouchEvent(
|
|
|
|
const CefTouchEvent& event) {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2021-09-27 09:50:07 +02:00
|
|
|
void CefBrowserPlatformDelegateNativeMac::SetFocus(bool setFocus) {
|
2020-01-23 22:58:01 +01:00
|
|
|
auto view = GetHostView();
|
2015-11-17 19:20:13 +01:00
|
|
|
if (view) {
|
|
|
|
view->SetActive(setFocus);
|
|
|
|
|
|
|
|
if (setFocus) {
|
|
|
|
// Give keyboard focus to the native view.
|
2022-09-26 21:30:45 +02:00
|
|
|
NSView* nsview = web_contents_->GetContentNativeView().GetNativeNSView();
|
|
|
|
DCHECK([nsview canBecomeKeyView]);
|
|
|
|
[[nsview window] makeFirstResponder:nsview];
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Point CefBrowserPlatformDelegateNativeMac::GetScreenPoint(
|
2022-05-13 13:38:41 +02:00
|
|
|
const gfx::Point& view,
|
|
|
|
bool want_dip_coords) const {
|
|
|
|
// Mac always operates in DIP coordinates so |want_dip_coords| is ignored.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (windowless_handler_) {
|
2022-05-13 13:38:41 +02:00
|
|
|
return windowless_handler_->GetParentScreenPoint(view, want_dip_coords);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2019-04-23 19:17:56 +02:00
|
|
|
NSView* nsview = CAST_CEF_WINDOW_HANDLE_TO_NSVIEW(window_info_.parent_view);
|
2015-11-17 19:20:13 +01:00
|
|
|
if (nsview) {
|
|
|
|
NSRect bounds = [nsview bounds];
|
2021-07-23 18:40:13 +02:00
|
|
|
NSPoint view_pt = {static_cast<CGFloat>(view.x()),
|
|
|
|
bounds.size.height - static_cast<CGFloat>(view.y())};
|
2015-11-17 19:20:13 +01:00
|
|
|
NSPoint window_pt = [nsview convertPoint:view_pt toView:nil];
|
2023-04-04 20:00:13 +02:00
|
|
|
NSPoint screen_pt = [[nsview window] convertPointToScreen:window_pt];
|
2015-11-17 19:20:13 +01:00
|
|
|
return gfx::Point(screen_pt.x, screen_pt.y);
|
|
|
|
}
|
|
|
|
return gfx::Point();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBrowserPlatformDelegateNativeMac::ViewText(const std::string& text) {
|
|
|
|
// TODO(cef): Implement this functionality.
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2018-11-03 02:15:09 +01:00
|
|
|
bool CefBrowserPlatformDelegateNativeMac::HandleKeyboardEvent(
|
2015-11-17 19:20:13 +01:00
|
|
|
const content::NativeWebKeyboardEvent& event) {
|
|
|
|
// Give the top level menu equivalents a chance to handle the event.
|
2023-06-26 12:13:38 +02:00
|
|
|
NSEvent* ns_event = event.os_event.Get();
|
|
|
|
if (ns_event.type == NSEventTypeKeyDown) {
|
|
|
|
return [[NSApp mainMenu] performKeyEquivalent:ns_event];
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-11-03 02:15:09 +01:00
|
|
|
return false;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
CefEventHandle CefBrowserPlatformDelegateNativeMac::GetEventHandle(
|
|
|
|
const content::NativeWebKeyboardEvent& event) const {
|
2023-06-26 12:13:38 +02:00
|
|
|
return CAST_NSEVENT_TO_CEF_EVENT_HANDLE(event.os_event.Get());
|
2020-01-23 22:58:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CefJavaScriptDialogRunner>
|
|
|
|
CefBrowserPlatformDelegateNativeMac::CreateJavaScriptDialogRunner() {
|
|
|
|
return base::WrapUnique(new CefJavaScriptDialogRunnerMac);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CefMenuRunner>
|
|
|
|
CefBrowserPlatformDelegateNativeMac::CreateMenuRunner() {
|
|
|
|
return base::WrapUnique(new CefMenuRunnerMac);
|
|
|
|
}
|
|
|
|
|
|
|
|
content::NativeWebKeyboardEvent
|
|
|
|
CefBrowserPlatformDelegateNativeMac::TranslateWebKeyEvent(
|
2015-11-17 19:20:13 +01:00
|
|
|
const CefKeyEvent& key_event) const {
|
2020-06-09 19:48:00 +02:00
|
|
|
content::NativeWebKeyboardEvent result(
|
|
|
|
blink::WebInputEvent::Type::kUndefined,
|
|
|
|
blink::WebInputEvent::Modifiers::kNoModifiers, ui::EventTimeForNow());
|
2020-01-23 22:58:01 +01:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
// Use a synthetic NSEvent in order to obtain the windowsKeyCode member from
|
|
|
|
// the NativeWebKeyboardEvent constructor. This is the only member which can
|
|
|
|
// not be easily translated (without hardcoding keyCodes)
|
|
|
|
// Determining whether a modifier key is left or right seems to be done
|
|
|
|
// through the key code as well.
|
|
|
|
NSEventType event_type;
|
|
|
|
if (key_event.character == 0 && key_event.unmodified_character == 0) {
|
|
|
|
// Check if both character and unmodified_characther are empty to determine
|
2022-06-17 15:28:55 +02:00
|
|
|
// if this was a NSEventTypeFlagsChanged event.
|
2015-11-17 19:20:13 +01:00
|
|
|
// A dead key will have an empty character, but a non-empty unmodified
|
|
|
|
// character
|
2022-06-17 15:28:55 +02:00
|
|
|
event_type = NSEventTypeFlagsChanged;
|
2015-11-17 19:20:13 +01:00
|
|
|
} else {
|
|
|
|
switch (key_event.type) {
|
|
|
|
case KEYEVENT_RAWKEYDOWN:
|
|
|
|
case KEYEVENT_KEYDOWN:
|
|
|
|
case KEYEVENT_CHAR:
|
2022-06-17 15:28:55 +02:00
|
|
|
event_type = NSEventTypeKeyDown;
|
2015-11-17 19:20:13 +01:00
|
|
|
break;
|
|
|
|
case KEYEVENT_KEYUP:
|
2022-06-17 15:28:55 +02:00
|
|
|
event_type = NSEventTypeKeyUp;
|
2015-11-17 19:20:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
NSString* charactersIgnoringModifiers =
|
2023-06-01 16:06:15 +02:00
|
|
|
[[NSString alloc] initWithCharacters:reinterpret_cast<const unichar*>(
|
|
|
|
&key_event.unmodified_character)
|
2023-05-05 13:17:14 +02:00
|
|
|
length:1];
|
2023-06-01 16:06:15 +02:00
|
|
|
NSString* characters = [[NSString alloc]
|
|
|
|
initWithCharacters:reinterpret_cast<const unichar*>(&key_event.character)
|
|
|
|
length:1];
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
NSEvent* synthetic_event =
|
2017-05-17 11:29:28 +02:00
|
|
|
[NSEvent keyEventWithType:event_type
|
|
|
|
location:NSMakePoint(0, 0)
|
|
|
|
modifierFlags:NativeModifiers(key_event.modifiers)
|
|
|
|
timestamp:currentEventTimestamp()
|
|
|
|
windowNumber:0
|
|
|
|
context:nil
|
|
|
|
characters:characters
|
|
|
|
charactersIgnoringModifiers:charactersIgnoringModifiers
|
|
|
|
isARepeat:NO
|
|
|
|
keyCode:key_event.native_key_code];
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2023-06-26 12:13:38 +02:00
|
|
|
result = content::NativeWebKeyboardEvent(
|
|
|
|
base::apple::OwnedNSEvent(synthetic_event));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (key_event.type == KEYEVENT_CHAR) {
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(blink::WebInputEvent::Type::kChar);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
result.is_system_key = key_event.is_system_key;
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
return result;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
blink::WebMouseEvent
|
|
|
|
CefBrowserPlatformDelegateNativeMac::TranslateWebClickEvent(
|
2015-11-17 19:20:13 +01:00
|
|
|
const CefMouseEvent& mouse_event,
|
|
|
|
CefBrowserHost::MouseButtonType type,
|
2017-05-17 11:29:28 +02:00
|
|
|
bool mouseUp,
|
|
|
|
int clickCount) const {
|
2020-01-23 22:58:01 +01:00
|
|
|
blink::WebMouseEvent result;
|
|
|
|
TranslateWebMouseEvent(result, mouse_event);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
switch (type) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case MBT_LEFT:
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(mouseUp ? blink::WebInputEvent::Type::kMouseUp
|
|
|
|
: blink::WebInputEvent::Type::kMouseDown);
|
2017-05-17 11:29:28 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kLeft;
|
|
|
|
break;
|
|
|
|
case MBT_MIDDLE:
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(mouseUp ? blink::WebInputEvent::Type::kMouseUp
|
|
|
|
: blink::WebInputEvent::Type::kMouseDown);
|
2017-05-17 11:29:28 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kMiddle;
|
|
|
|
break;
|
|
|
|
case MBT_RIGHT:
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(mouseUp ? blink::WebInputEvent::Type::kMouseUp
|
|
|
|
: blink::WebInputEvent::Type::kMouseDown);
|
2017-05-17 11:29:28 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kRight;
|
|
|
|
break;
|
|
|
|
default:
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
result.click_count = clickCount;
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
return result;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
blink::WebMouseEvent CefBrowserPlatformDelegateNativeMac::TranslateWebMoveEvent(
|
2015-11-17 19:20:13 +01:00
|
|
|
const CefMouseEvent& mouse_event,
|
|
|
|
bool mouseLeave) const {
|
2020-01-23 22:58:01 +01:00
|
|
|
blink::WebMouseEvent result;
|
|
|
|
TranslateWebMouseEvent(result, mouse_event);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
if (!mouseLeave) {
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(blink::WebInputEvent::Type::kMouseMove);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (mouse_event.modifiers & EVENTFLAG_LEFT_MOUSE_BUTTON) {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kLeft;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (mouse_event.modifiers & EVENTFLAG_MIDDLE_MOUSE_BUTTON) {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kMiddle;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (mouse_event.modifiers & EVENTFLAG_RIGHT_MOUSE_BUTTON) {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kRight;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kNoButton;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
} else {
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(blink::WebInputEvent::Type::kMouseLeave);
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kNoButton;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
result.click_count = 0;
|
2020-01-23 22:58:01 +01:00
|
|
|
|
|
|
|
return result;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
blink::WebMouseWheelEvent
|
|
|
|
CefBrowserPlatformDelegateNativeMac::TranslateWebWheelEvent(
|
2015-11-17 19:20:13 +01:00
|
|
|
const CefMouseEvent& mouse_event,
|
2017-05-17 11:29:28 +02:00
|
|
|
int deltaX,
|
|
|
|
int deltaY) const {
|
2020-01-23 22:58:01 +01:00
|
|
|
blink::WebMouseWheelEvent result;
|
|
|
|
TranslateWebMouseEvent(result, mouse_event);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2020-06-09 19:48:00 +02:00
|
|
|
result.SetType(blink::WebInputEvent::Type::kMouseWheel);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
static const double scrollbarPixelsPerCocoaTick = 40.0;
|
2017-09-13 18:47:43 +02:00
|
|
|
result.delta_x = deltaX;
|
2017-04-20 21:28:17 +02:00
|
|
|
result.delta_y = deltaY;
|
|
|
|
result.wheel_ticks_x = deltaX / scrollbarPixelsPerCocoaTick;
|
|
|
|
result.wheel_ticks_y = deltaY / scrollbarPixelsPerCocoaTick;
|
2020-03-30 22:13:42 +02:00
|
|
|
result.delta_units = ui::ScrollGranularity::kScrollByPrecisePixel;
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (mouse_event.modifiers & EVENTFLAG_LEFT_MOUSE_BUTTON) {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kLeft;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (mouse_event.modifiers & EVENTFLAG_MIDDLE_MOUSE_BUTTON) {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kMiddle;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (mouse_event.modifiers & EVENTFLAG_RIGHT_MOUSE_BUTTON) {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kRight;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2017-04-20 21:28:17 +02:00
|
|
|
result.button = blink::WebMouseEvent::Button::kNoButton;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
return result;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
void CefBrowserPlatformDelegateNativeMac::TranslateWebMouseEvent(
|
2015-11-17 19:20:13 +01:00
|
|
|
blink::WebMouseEvent& result,
|
|
|
|
const CefMouseEvent& mouse_event) const {
|
|
|
|
// position
|
2017-04-20 21:28:17 +02:00
|
|
|
result.SetPositionInWidget(mouse_event.x, mouse_event.y);
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2022-05-13 13:38:41 +02:00
|
|
|
const gfx::Point& screen_pt = GetScreenPoint(
|
|
|
|
gfx::Point(mouse_event.x, mouse_event.y), /*want_dip_coords=*/true);
|
2017-04-20 21:28:17 +02:00
|
|
|
result.SetPositionInScreen(screen_pt.x(), screen_pt.y());
|
2015-11-17 19:20:13 +01:00
|
|
|
|
|
|
|
// modifiers
|
2017-05-17 11:29:28 +02:00
|
|
|
result.SetModifiers(result.GetModifiers() |
|
2020-01-23 22:58:01 +01:00
|
|
|
TranslateWebEventModifiers(mouse_event.modifiers));
|
2015-11-17 19:20:13 +01:00
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
// timestamp
|
2018-05-17 10:58:21 +02:00
|
|
|
result.SetTimeStamp(base::TimeTicks() +
|
2021-10-19 00:17:16 +02:00
|
|
|
base::Seconds(currentEventTimestamp()));
|
2018-02-22 18:09:28 +01:00
|
|
|
|
|
|
|
result.pointer_type = blink::WebPointerProperties::PointerType::kMouse;
|
2015-11-17 19:20:13 +01:00
|
|
|
}
|
2019-07-17 20:47:27 +02:00
|
|
|
|
2020-01-23 22:58:01 +01:00
|
|
|
content::RenderWidgetHostViewMac*
|
|
|
|
CefBrowserPlatformDelegateNativeMac::GetHostView() const {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!web_contents_) {
|
2020-07-08 19:23:29 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-23 22:58:01 +01:00
|
|
|
return static_cast<content::RenderWidgetHostViewMac*>(
|
2020-07-08 19:23:29 +02:00
|
|
|
web_contents_->GetRenderWidgetHostView());
|
2019-07-17 20:47:27 +02:00
|
|
|
}
|