diff options
author | Peter Varga <pvarga@inf.u-szeged.hu> | 2023-10-27 11:03:15 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2023-12-09 19:53:11 +0100 |
commit | 5b7348aacee8688dda4d99011130754a9bf4678c (patch) | |
tree | 8469fe12125757a7ee3ceb4ae8fb2b55a1a543a6 | |
parent | 66804e89073700b3f05e42515d738a5906a30ae9 (diff) |
Add conversion functions for base::apple::OwnedNSEvent
It is necessary after Chromium 116 adaptations.
Change-Id: I00a3aa532cc60414f9dd124b0afabc66901e6ae8
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r-- | src/core/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/core/native_web_keyboard_event_qt.cpp | 48 | ||||
-rw-r--r-- | src/core/native_web_keyboard_event_qt.h | 20 | ||||
-rw-r--r-- | src/core/native_web_keyboard_event_qt_mac.mm | 155 | ||||
-rw-r--r-- | src/core/web_contents_delegate_qt.cpp | 3 | ||||
-rw-r--r-- | src/core/web_event_factory.cpp | 3 |
6 files changed, 212 insertions, 20 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8e54fcef0..025a47441 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -128,7 +128,7 @@ foreach(arch ${archs}) javascript_dialog_manager_qt.cpp javascript_dialog_manager_qt.h login_delegate_qt.cpp login_delegate_qt.h media_capture_devices_dispatcher.cpp media_capture_devices_dispatcher.h - native_web_keyboard_event_qt.cpp + native_web_keyboard_event_qt.cpp native_web_keyboard_event_qt.h net/client_cert_qt.cpp net/client_cert_qt.h net/client_cert_store_data.cpp net/client_cert_store_data.h net/cookie_monster_delegate_qt.cpp net/cookie_monster_delegate_qt.h @@ -225,6 +225,7 @@ foreach(arch ${archs}) extend_gn_target(${buildGn} CONDITION MACOS SOURCES + native_web_keyboard_event_qt_mac.mm compositor/native_skia_output_device_mac.mm compositor/native_skia_output_device_mac2.mm ) diff --git a/src/core/native_web_keyboard_event_qt.cpp b/src/core/native_web_keyboard_event_qt.cpp index 4755c60bf..e2026a46a 100644 --- a/src/core/native_web_keyboard_event_qt.cpp +++ b/src/core/native_web_keyboard_event_qt.cpp @@ -1,41 +1,54 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only // Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE.Chromium file. -#include "content/public/browser/native_web_keyboard_event.h" -#include <QKeyEvent> +#include <QtCore/qglobal.h> -namespace { +#if !defined(Q_OS_MACOS) +#include "native_web_keyboard_event_qt.h" + +#include <QtGui/QKeyEvent> + +namespace QtWebEngineCore { +gfx::NativeEvent ToNativeEvent(QKeyEvent *keyEvent) +{ + return reinterpret_cast<gfx::NativeEvent>(keyEvent); +} +QKeyEvent *ToKeyEvent(gfx::NativeEvent nativeEvent) +{ + return reinterpret_cast<QKeyEvent *>(nativeEvent); +} +} // namespace QtWebEngineCore + +namespace { // We need to copy |os_event| in NativeWebKeyboardEvent because it is // queued in RenderWidgetHost and may be passed and used // RenderViewHostDelegate::HandledKeybardEvent after the original aura // event is destroyed. -gfx::NativeEvent CopyEvent(gfx::NativeEvent event) +gfx::NativeEvent CopyEvent(gfx::NativeEvent nativeEvent) { - if (!event) + if (!nativeEvent) return nullptr; - QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event); - return reinterpret_cast<gfx::NativeEvent>(keyEvent->clone()); + QKeyEvent *keyEvent = QtWebEngineCore::ToKeyEvent(nativeEvent); + return QtWebEngineCore::ToNativeEvent(keyEvent->clone()); } -void DestroyEvent(gfx::NativeEvent event) +void DestroyEvent(gfx::NativeEvent nativeEvent) { - delete reinterpret_cast<QKeyEvent*>(event); + delete QtWebEngineCore::ToKeyEvent(nativeEvent); } +} // namespace -} // namespace - -using blink::WebKeyboardEvent; namespace content { NativeWebKeyboardEvent::NativeWebKeyboardEvent(const blink::WebKeyboardEvent &web_event, gfx::NativeView) - : WebKeyboardEvent(web_event) + : blink::WebKeyboardEvent(web_event) , os_event(nullptr) , skip_in_browser(false) { @@ -43,7 +56,7 @@ NativeWebKeyboardEvent::NativeWebKeyboardEvent(const blink::WebKeyboardEvent &we NativeWebKeyboardEvent::NativeWebKeyboardEvent(blink::WebInputEvent::Type type, int modifiers, base::TimeTicks timestamp) - : WebKeyboardEvent(type, modifiers, timestamp) + : blink::WebKeyboardEvent(type, modifiers, timestamp) , os_event(nullptr) , skip_in_browser(false) { @@ -56,7 +69,7 @@ NativeWebKeyboardEvent::NativeWebKeyboardEvent(gfx::NativeEvent native_event) } NativeWebKeyboardEvent::NativeWebKeyboardEvent(const NativeWebKeyboardEvent& other) - : WebKeyboardEvent(other) + : blink::WebKeyboardEvent(other) , os_event(CopyEvent(other.os_event)) , skip_in_browser(other.skip_in_browser) { @@ -66,7 +79,7 @@ NativeWebKeyboardEvent &NativeWebKeyboardEvent::operator=(const NativeWebKeyboar { if (this == &other) return *this; - WebKeyboardEvent::operator=(other); + blink::WebKeyboardEvent::operator=(other); DestroyEvent(os_event); os_event = CopyEvent(other.os_event); skip_in_browser = other.skip_in_browser; @@ -78,3 +91,4 @@ NativeWebKeyboardEvent::~NativeWebKeyboardEvent() { } } // namespace content +#endif // !defined(Q_OS_MACOS) diff --git a/src/core/native_web_keyboard_event_qt.h b/src/core/native_web_keyboard_event_qt.h new file mode 100644 index 000000000..c98ea263e --- /dev/null +++ b/src/core/native_web_keyboard_event_qt.h @@ -0,0 +1,20 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef NATIVE_WEB_KEYBOARD_EVENT_QT_H +#define NATIVE_WEB_KEYBOARD_EVENT_QT_H + +#include <QtCore/qglobal.h> + +#include "content/public/browser/native_web_keyboard_event.h" + +QT_FORWARD_DECLARE_CLASS(QKeyEvent) + +namespace QtWebEngineCore { + +gfx::NativeEvent ToNativeEvent(QKeyEvent *keyEvent); +QKeyEvent *ToKeyEvent(gfx::NativeEvent event); + +} // namespace QtWebEngineCore + +#endif // NATIVE_WEB_KEYBOARD_EVENT_QT_H diff --git a/src/core/native_web_keyboard_event_qt_mac.mm b/src/core/native_web_keyboard_event_qt_mac.mm new file mode 100644 index 000000000..696d0b55b --- /dev/null +++ b/src/core/native_web_keyboard_event_qt_mac.mm @@ -0,0 +1,155 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// Copyright 2011 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE.Chromium file. + +// This is a workaround to be able to include Qt headers without +// "redefinition of 'NSString' as different kind of symbol" errors. +// TODO: Remove this when namespace ambiguity issues are fixed properly, +// see get_forward_declaration_macro() in cmake/Functions.cmake +#undef Q_FORWARD_DECLARE_OBJC_CLASS + +#include "native_web_keyboard_event_qt.h" + +#include <AppKit/AppKit.h> + +#include "base/apple/owned_objc.h" + +#include <QtGui/private/qapplekeymapper_p.h> +#include <QtGui/QKeyEvent> + +namespace QtWebEngineCore { + +base::apple::OwnedNSEvent ToNativeEvent(QKeyEvent *keyEvent) +{ + NSEventType type; + switch (keyEvent->type()) { + case QEvent::KeyPress: + type = NSEventTypeKeyDown; + break; + case QEvent::KeyRelease: + type = NSEventTypeKeyUp; + break; + default: + Q_UNREACHABLE(); + return base::apple::OwnedNSEvent(); + } + + NSString *text = keyEvent->text().toNSString(); + if (text.length == 0) { + Qt::Key key = static_cast<Qt::Key>(keyEvent->key()); + QChar cocoaKey = QAppleKeyMapper::toCocoaKey(key); + text = QStringView(&cocoaKey, 1).toNSString(); + } + + return base::apple::OwnedNSEvent([NSEvent + keyEventWithType:type + location:NSZeroPoint + modifierFlags:keyEvent->nativeModifiers() + timestamp:keyEvent->timestamp() / 1000 + windowNumber:0 + context:nil + characters:text + charactersIgnoringModifiers:text + isARepeat:keyEvent->isAutoRepeat() + keyCode:keyEvent->nativeVirtualKey() + ]); +} + +// Based on qtbase/src/plugins/platforms/cocoa/qnsview_keys.mm (KeyEvent::KeyEvent()) +QKeyEvent *ToKeyEvent(base::apple::OwnedNSEvent event) +{ + NSEvent *nsevent = event.Get(); + + QEvent::Type type = QEvent::None; + switch (nsevent.type) { + case NSEventTypeKeyDown: + type = QEvent::KeyPress; + break; + case NSEventTypeKeyUp: + type = QEvent::KeyRelease; + break; + default: + Q_UNREACHABLE(); + return nullptr; + } + + // Scan codes are hardware dependent codes for each key. There is no way to get these + // from Carbon or Cocoa, so leave it 0, as documented in QKeyEvent::nativeScanCode(). + quint32 nativeScanCode = 0; + quint32 nativeVirtualKey = nsevent.keyCode; + + NSEventModifierFlags nativeModifiers = nsevent.modifierFlags; + Qt::KeyboardModifiers modifiers = QAppleKeyMapper::fromCocoaModifiers(nativeModifiers); + + NSString *charactersIgnoringModifiers = nsevent.charactersIgnoringModifiers; + NSString *characters = nsevent.characters; + + // If a dead key occurs as a result of pressing a key combination then + // characters will have 0 length, but charactersIgnoringModifiers will + // have a valid character in it. This enables key combinations such as + // ALT+E to be used as a shortcut with an English keyboard even though + // pressing ALT+E will give a dead key while doing normal text input. + Qt::Key key = Qt::Key_unknown; + if (characters.length || charactersIgnoringModifiers.length) { + QChar character = QChar::ReplacementCharacter; + if (nativeModifiers & (NSEventModifierFlagControl | NSEventModifierFlagOption) + && charactersIgnoringModifiers.length) + character = QChar([charactersIgnoringModifiers characterAtIndex:0]); + else if (characters.length) + character = QChar([characters characterAtIndex:0]); + key = QAppleKeyMapper::fromCocoaKey(character); + } + + QString text = QString::fromNSString(characters); + bool autorep = nsevent.ARepeat; + + return new QKeyEvent(type, key, modifiers, nativeScanCode, nativeVirtualKey, nativeModifiers, + text, autorep); +} + +} // namespace QtWebEngineCore + +namespace content { + +NativeWebKeyboardEvent::NativeWebKeyboardEvent(const blink::WebKeyboardEvent &web_event, gfx::NativeView) + : blink::WebKeyboardEvent(web_event) + , skip_in_browser(false) +{ +} + +NativeWebKeyboardEvent::NativeWebKeyboardEvent(blink::WebInputEvent::Type type, int modifiers, + base::TimeTicks timestamp) + : blink::WebKeyboardEvent(type, modifiers, timestamp) + , skip_in_browser(false) +{ +} + +NativeWebKeyboardEvent::NativeWebKeyboardEvent(gfx::NativeEvent native_event) + : os_event(native_event) // FIXME: Copy? + , skip_in_browser(false) +{ +} + +NativeWebKeyboardEvent::NativeWebKeyboardEvent(const NativeWebKeyboardEvent& other) + : blink::WebKeyboardEvent(other) + , os_event(other.os_event) // FIXME: Copy? + , skip_in_browser(other.skip_in_browser) +{ +} + +NativeWebKeyboardEvent &NativeWebKeyboardEvent::operator=(const NativeWebKeyboardEvent &other) +{ + blink::WebKeyboardEvent::operator=(other); + + os_event = other.os_event; // FIXME: Copy? + skip_in_browser = other.skip_in_browser; + + return *this; +} + +NativeWebKeyboardEvent::~NativeWebKeyboardEvent() = default; + +} // namespace content diff --git a/src/core/web_contents_delegate_qt.cpp b/src/core/web_contents_delegate_qt.cpp index 64ccd529d..e6e5c0d46 100644 --- a/src/core/web_contents_delegate_qt.cpp +++ b/src/core/web_contents_delegate_qt.cpp @@ -16,6 +16,7 @@ #include "find_text_helper.h" #include "javascript_dialog_manager_qt.h" #include "media_capture_devices_dispatcher.h" +#include "native_web_keyboard_event_qt.h" #include "profile_adapter.h" #include "profile_qt.h" #include "qwebengineloadinginfo.h" @@ -246,7 +247,7 @@ bool WebContentsDelegateQt::HandleKeyboardEvent(content::WebContents *, const co { Q_ASSERT(!event.skip_in_browser); if (event.os_event) - m_viewClient->unhandledKeyEvent(reinterpret_cast<QKeyEvent *>(event.os_event)); + m_viewClient->unhandledKeyEvent(ToKeyEvent(event.os_event)); // FIXME: ? return true; } diff --git a/src/core/web_event_factory.cpp b/src/core/web_event_factory.cpp index f4b37e838..d64c844ca 100644 --- a/src/core/web_event_factory.cpp +++ b/src/core/web_event_factory.cpp @@ -35,6 +35,7 @@ #include "ui/events/keycodes/dom/keycode_converter.h" #include "ui/events/keycodes/keyboard_code_conversion.h" +#include "native_web_keyboard_event_qt.h" #include "render_widget_host_view_qt_delegate.h" #include <QtGui/private/qtgui-config_p.h> @@ -1659,7 +1660,7 @@ void WebEventFactory::sendUnhandledWheelEvent(const blink::WebGestureEvent &even content::NativeWebKeyboardEvent WebEventFactory::toWebKeyboardEvent(QKeyEvent *ev) { - content::NativeWebKeyboardEvent webKitEvent(reinterpret_cast<gfx::NativeEvent>(ev)); + content::NativeWebKeyboardEvent webKitEvent(ToNativeEvent(ev)); webKitEvent.SetTimeStamp(base::TimeTicks::Now()); webKitEvent.SetModifiers(modifiersForEvent(ev)); webKitEvent.SetType(webEventTypeForEvent(ev)); |