From 79f5f290c1eead991e4267a431de9b519abd9834 Mon Sep 17 00:00:00 2001 From: Balazs Egedi Date: Mon, 20 Sep 2021 13:50:50 +0200 Subject: Add onTouchSelectionMenuRequested QML handler Task-number: QTBUG-85043 Change-Id: Iecd2f5ae20fcf031937a1d44cc154f2e2a3ff52f Reviewed-by: Michal Klocek --- .../webenginequick/customdialogs/CMakeLists.txt | 2 + examples/webenginequick/customdialogs/WebView.qml | 9 ++ .../customdialogs/forms/TouchSelectionMenu.qml | 61 +++++++++ .../forms/TouchSelectionMenuForm.ui.qml | 86 +++++++++++++ examples/webenginequick/customdialogs/index.html | 3 + examples/webenginequick/customdialogs/main.qml | 2 +- src/core/touch_selection_menu_controller.cpp | 29 +++-- src/core/touch_selection_menu_controller.h | 13 +- src/webenginequick/CMakeLists.txt | 3 + .../qquickwebenginetouchselectionmenurequest.cpp | 140 +++++++++++++++++++++ .../qquickwebenginetouchselectionmenurequest_p.h | 102 +++++++++++++++ .../qquickwebenginetouchselectionmenurequest_p_p.h | 79 ++++++++++++ src/webenginequick/api/qquickwebengineview.cpp | 11 ++ src/webenginequick/api/qquickwebengineview_p.h | 2 + .../doc/src/touch_selection_menu_request.qdoc | 103 +++++++++++++++ tests/auto/quick/publicapi/tst_publicapi.cpp | 9 ++ 16 files changed, 639 insertions(+), 15 deletions(-) create mode 100644 examples/webenginequick/customdialogs/forms/TouchSelectionMenu.qml create mode 100644 examples/webenginequick/customdialogs/forms/TouchSelectionMenuForm.ui.qml create mode 100644 src/webenginequick/api/qquickwebenginetouchselectionmenurequest.cpp create mode 100644 src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p.h create mode 100644 src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p_p.h create mode 100644 src/webenginequick/doc/src/touch_selection_menu_request.qdoc diff --git a/examples/webenginequick/customdialogs/CMakeLists.txt b/examples/webenginequick/customdialogs/CMakeLists.txt index bd1810931..3e4e457fa 100644 --- a/examples/webenginequick/customdialogs/CMakeLists.txt +++ b/examples/webenginequick/customdialogs/CMakeLists.txt @@ -48,6 +48,8 @@ set(customdialogs_resource_files "forms/JavaScriptForm.ui.qml" "forms/Menu.qml" "forms/MenuForm.ui.qml" + "forms/TouchSelectionMenu.qml" + "forms/TouchSelectionMenuForm.ui.qml" "icon.svg" "index.html" "main.qml" diff --git a/examples/webenginequick/customdialogs/WebView.qml b/examples/webenginequick/customdialogs/WebView.qml index 61bbea87e..fcb10b7da 100644 --- a/examples/webenginequick/customdialogs/WebView.qml +++ b/examples/webenginequick/customdialogs/WebView.qml @@ -152,4 +152,13 @@ WebEngineView { properties: {"request": request}}); } + + onTouchSelectionMenuRequested: function(request) { + if (useDefaultDialogs) + return; + + request.accepted = true; + openForm({item: Qt.resolvedUrl("forms/TouchSelectionMenu.qml"), + properties: {"request": request}}); + } } diff --git a/examples/webenginequick/customdialogs/forms/TouchSelectionMenu.qml b/examples/webenginequick/customdialogs/forms/TouchSelectionMenu.qml new file mode 100644 index 000000000..6d3dec564 --- /dev/null +++ b/examples/webenginequick/customdialogs/forms/TouchSelectionMenu.qml @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick + +TouchSelectionMenuForm { + property QtObject request + signal closeForm() + + cut.onClicked: closeForm() + copy.onClicked: closeForm() + paste.onClicked: closeForm() + contextMenu.onClicked: closeForm() +} diff --git a/examples/webenginequick/customdialogs/forms/TouchSelectionMenuForm.ui.qml b/examples/webenginequick/customdialogs/forms/TouchSelectionMenuForm.ui.qml new file mode 100644 index 000000000..30d9a3179 --- /dev/null +++ b/examples/webenginequick/customdialogs/forms/TouchSelectionMenuForm.ui.qml @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Layouts + +Item { + property alias cut: cut + property alias copy: copy + property alias paste: paste + property alias contextMenu: contextMenu + + ColumnLayout { + id: columnLayout + anchors.verticalCenter: parent.verticalCenter + anchors.horizontalCenter: parent.horizontalCenter + + CustomButton { + id: cut + btnText: qsTr("Cut") + } + + CustomButton { + id: copy + btnText: qsTr("Copy") + } + + CustomButton { + id: paste + btnText: qsTr("Paste") + } + + CustomButton { + id: contextMenu + btnText: qsTr("...") + } + + } +} diff --git a/examples/webenginequick/customdialogs/index.html b/examples/webenginequick/customdialogs/index.html index e9fb56228..d5de2827c 100644 --- a/examples/webenginequick/customdialogs/index.html +++ b/examples/webenginequick/customdialogs/index.html @@ -13,6 +13,9 @@

Hover this text to display a tooltip

+ +

Touch devices only: long press on this text to see the touch selection menu

+ diff --git a/examples/webenginequick/customdialogs/main.qml b/examples/webenginequick/customdialogs/main.qml index 529737e99..6d81c7979 100644 --- a/examples/webenginequick/customdialogs/main.qml +++ b/examples/webenginequick/customdialogs/main.qml @@ -56,7 +56,7 @@ import QtQuick.Window Window { id: mainWindow width: 800 - height: 600 + height: 610 visible: true StackView { diff --git a/src/core/touch_selection_menu_controller.cpp b/src/core/touch_selection_menu_controller.cpp index cdec9a064..3a6d49160 100644 --- a/src/core/touch_selection_menu_controller.cpp +++ b/src/core/touch_selection_menu_controller.cpp @@ -53,17 +53,11 @@ TouchSelectionMenuController::~TouchSelectionMenuController() int TouchSelectionMenuController::buttonCount() { - int buttonCount = 1; - - for (int commandId = 0; commandId <= static_cast(Paste); ++commandId) { - if (m_touchSelectionControllerClient->IsCommandIdEnabled(commandId)) - buttonCount++; - } - - return buttonCount; + // Context menu should be always available + return qPopulationCount(static_cast(availableActions())) + 1; } -bool TouchSelectionMenuController::isCommandEnabled(TouchSelectionCommand command) +bool TouchSelectionMenuController::isCommandEnabled(TouchSelectionMenuController::TouchSelectionCommandFlag command) { return m_touchSelectionControllerClient->IsCommandIdEnabled(static_cast(command)); } @@ -88,4 +82,21 @@ void TouchSelectionMenuController::runContextMenu() return m_touchSelectionControllerClient->RunContextMenu(); } +TouchSelectionMenuController::TouchSelectionCommandFlags TouchSelectionMenuController::availableActions() +{ + TouchSelectionCommandFlags availableActions; + + if (m_touchSelectionControllerClient->IsCommandIdEnabled(Cut)) { + availableActions |= TouchSelectionMenuController::Cut; + } + if (m_touchSelectionControllerClient->IsCommandIdEnabled(Copy)) { + availableActions |= TouchSelectionMenuController::Copy; + } + if (m_touchSelectionControllerClient->IsCommandIdEnabled(Paste)) { + availableActions |= TouchSelectionMenuController::Paste; + } + + return availableActions; +} + } // namespace QtWebEngineCore diff --git a/src/core/touch_selection_menu_controller.h b/src/core/touch_selection_menu_controller.h index 8955c7294..a90695c80 100644 --- a/src/core/touch_selection_menu_controller.h +++ b/src/core/touch_selection_menu_controller.h @@ -50,16 +50,19 @@ class TouchSelectionControllerClientQt; class Q_WEBENGINECORE_PRIVATE_EXPORT TouchSelectionMenuController : public QObject { Q_OBJECT public: - enum TouchSelectionCommand { - Cut, - Copy, - Paste + enum TouchSelectionCommandFlag { + Cut = 0x1, + Copy = 0x2, + Paste = 0x4 }; + Q_DECLARE_FLAGS(TouchSelectionCommandFlags, TouchSelectionCommandFlag); + Q_FLAG(TouchSelectionCommandFlag) TouchSelectionMenuController(TouchSelectionControllerClientQt *touchSelectionControllerClient); ~TouchSelectionMenuController(); int buttonCount(); - bool isCommandEnabled(TouchSelectionCommand); + bool isCommandEnabled(TouchSelectionCommandFlag); + TouchSelectionCommandFlags availableActions(); public Q_SLOTS: void cut(); diff --git a/src/webenginequick/CMakeLists.txt b/src/webenginequick/CMakeLists.txt index 30b0fa2cf..05340a3a7 100644 --- a/src/webenginequick/CMakeLists.txt +++ b/src/webenginequick/CMakeLists.txt @@ -22,6 +22,9 @@ qt_internal_add_qml_module(WebEngineQuick api/qquickwebenginesingleton.cpp api/qquickwebenginesingleton_p.h api/qquickwebenginetouchhandleprovider.cpp api/qquickwebenginetouchhandleprovider_p_p.h + api/qquickwebenginetouchselectionmenurequest.cpp + api/qquickwebenginetouchselectionmenurequest_p.h + api/qquickwebenginetouchselectionmenurequest_p_p.h api/qquickwebengineview.cpp api/qquickwebengineview_p.h api/qquickwebengineview_p_p.h api/qquickwebengineforeigntypes_p.h diff --git a/src/webenginequick/api/qquickwebenginetouchselectionmenurequest.cpp b/src/webenginequick/api/qquickwebenginetouchselectionmenurequest.cpp new file mode 100644 index 000000000..21948727e --- /dev/null +++ b/src/webenginequick/api/qquickwebenginetouchselectionmenurequest.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickwebenginetouchselectionmenurequest_p.h" +#include "qquickwebenginetouchselectionmenurequest_p_p.h" +#include "touch_selection_menu_controller.h" + +QT_BEGIN_NAMESPACE + +ASSERT_ENUMS_MATCH(QQuickWebEngineTouchSelectionMenuRequest::Cut, + QtWebEngineCore::TouchSelectionMenuController::TouchSelectionCommandFlag::Cut) +ASSERT_ENUMS_MATCH(QQuickWebEngineTouchSelectionMenuRequest::Copy, + QtWebEngineCore::TouchSelectionMenuController::TouchSelectionCommandFlag::Copy) +ASSERT_ENUMS_MATCH(QQuickWebEngineTouchSelectionMenuRequest::Paste, + QtWebEngineCore::TouchSelectionMenuController::TouchSelectionCommandFlag::Paste) + +/*! + \class QWebEngineTouchSelectionMenuRequest + \since 6.3 + \brief The QQuickWebEngineTouchSelectionMenuRequest class provides request for a touch selection menu. + \inmodule QtWebEngineQuick + QQuickWebEngineTouchSelectionMenuRequest is returned after a touch selection event, + and contains information about where the bounding box of touch selection is and what + actions are available. +*/ + +/*! + \enum QWebEngineTouchSelectionMenuRequest::TouchSelectionCommandFlag + \readonly + \since 6.3 + + The available operations in the current touch selection menu request. + + \value Cut Cut is available. + \value Copy Copy is available. + \value Paste Paste is available. +*/ +QQuickWebEngineTouchSelectionMenuRequest::QQuickWebEngineTouchSelectionMenuRequest( + QRect bounds, QtWebEngineCore::TouchSelectionMenuController *touchSelectionMenuController) + : d(new QQuickWebEngineTouchSelectionMenuRequestPrivate(bounds, touchSelectionMenuController)) +{ +} + +QQuickWebEngineTouchSelectionMenuRequestPrivate::QQuickWebEngineTouchSelectionMenuRequestPrivate( + QRect bounds, QtWebEngineCore::TouchSelectionMenuController *touchSelectionMenuController) +{ + selectionBounds = bounds; + buttonCount = touchSelectionMenuController->buttonCount(); + touchSelectionCommandFlags = touchSelectionMenuController->availableActions(); +} + +/*! + Destroys the touch selection menu request. +*/ +QQuickWebEngineTouchSelectionMenuRequest::~QQuickWebEngineTouchSelectionMenuRequest() = default; + +/*! + Returns the number of buttons that must be displayed, based on the available actions. +*/ +int QQuickWebEngineTouchSelectionMenuRequest::buttonCount() +{ + return d->buttonCount; +} + +/*! + \qmlproperty bool QQuickWebEngineTouchSelectionMenuRequest::accepted + + Indicates whether the touch selection menu request has been + accepted by the signal handler. + + If the property is \c false after any signal handlers + for WebEngineView::touchSelectionMenuRequested have been executed, + a default touch selection menu will be shown. + To prevent this, set \c{request.accepted} to \c true. + + The default is \c false. +*/ +bool QQuickWebEngineTouchSelectionMenuRequest::isAccepted() const +{ + return d->accepted; +} + +void QQuickWebEngineTouchSelectionMenuRequest::setAccepted(bool accepted) +{ + d->accepted = accepted; +} + +/*! + Returns the bound rectangle of text selection. +*/ +QRect QQuickWebEngineTouchSelectionMenuRequest::selectionBounds() +{ + return d->selectionBounds; +} + +/*! + Returns the available operations in the current context. +*/ +QQuickWebEngineTouchSelectionMenuRequest::TouchSelectionCommandFlags QQuickWebEngineTouchSelectionMenuRequest::touchSelectionCommandFlags() const +{ + return static_cast(d->touchSelectionCommandFlags); +} + +QT_END_NAMESPACE diff --git a/src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p.h b/src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p.h new file mode 100644 index 000000000..c6a8fc8c6 --- /dev/null +++ b/src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKWEBENGINETOUCHSELECTIONMENUREQUEST_P_H +#define QQUICKWEBENGINETOUCHSELECTIONMENUREQUEST_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include + +namespace QtWebEngineCore { +class TouchSelectionMenuController; +} + +QT_BEGIN_NAMESPACE + +class QQuickWebEngineTouchSelectionMenuRequestPrivate; + +class Q_WEBENGINEQUICK_EXPORT QQuickWebEngineTouchSelectionMenuRequest : public QObject +{ + Q_OBJECT +public: + enum TouchSelectionCommandFlag { + Cut = 0x1, + Copy = 0x2, + Paste = 0x4 + }; + + Q_DECLARE_FLAGS(TouchSelectionCommandFlags, TouchSelectionCommandFlag) + Q_FLAG(TouchSelectionCommandFlags) + Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted FINAL) + Q_PROPERTY(QRect selectionBounds READ selectionBounds CONSTANT FINAL REVISION(1)) + Q_PROPERTY(TouchSelectionCommandFlags touchSelectionCommandFlags READ touchSelectionCommandFlags CONSTANT FINAL REVISION(1)) + QML_NAMED_ELEMENT(QQuickWebEngineTouchSelectionMenuRequest) + QML_ADDED_IN_VERSION(6, 3) + QML_UNCREATABLE("") + + QQuickWebEngineTouchSelectionMenuRequest(QRect bounds, + QtWebEngineCore::TouchSelectionMenuController *touchSelectionMenuController); + virtual ~QQuickWebEngineTouchSelectionMenuRequest(); + + int buttonCount(); + bool isAccepted() const; + void setAccepted(bool accepted); + QRect selectionBounds(); + TouchSelectionCommandFlags touchSelectionCommandFlags() const; + +private: + QScopedPointer d; +}; + +QT_END_NAMESPACE + +#endif // QQUICKWEBENGINETOUCHSELECTIONMENUREQUEST_P_H diff --git a/src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p_p.h b/src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p_p.h new file mode 100644 index 000000000..9d72e7fee --- /dev/null +++ b/src/webenginequick/api/qquickwebenginetouchselectionmenurequest_p_p.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKWEBENGINETOUCHSELECTIONMENUREQUEST_P_P_H +#define QQUICKWEBENGINETOUCHSELECTIONMENUREQUEST_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include "qquickwebenginetouchselectionmenurequest_p.h" + +namespace QtWebEngineCore { +class TouchSelectionMenuController; +} + +QT_BEGIN_NAMESPACE + +class QQuickWebEngineTouchSelectionMenuRequest; + +class QQuickWebEngineTouchSelectionMenuRequestPrivate +{ +public: + QQuickWebEngineTouchSelectionMenuRequestPrivate( + QRect bounds, QtWebEngineCore::TouchSelectionMenuController *touchSelectionMenuController); + + bool accepted = false; + QRect selectionBounds; + uint touchSelectionCommandFlags; + int buttonCount; +}; + +QT_END_NAMESPACE + +#endif // QQUICKWEBENGINETOUCHSELECTIONMENUREQUEST_P_P_H diff --git a/src/webenginequick/api/qquickwebengineview.cpp b/src/webenginequick/api/qquickwebengineview.cpp index 8f02648e8..42460ca34 100644 --- a/src/webenginequick/api/qquickwebengineview.cpp +++ b/src/webenginequick/api/qquickwebengineview.cpp @@ -48,6 +48,7 @@ #include "qquickwebenginescriptcollection_p.h" #include "qquickwebenginesettings_p.h" #include "qquickwebenginetouchhandleprovider_p_p.h" +#include "qquickwebenginetouchselectionmenurequest_p.h" #include "qquickwebengineview_p.h" #include "qquickwebengineview_p_p.h" @@ -1241,11 +1242,21 @@ QtWebEngineCore::TouchHandleDrawableClient *QQuickWebEngineViewPrivate::createTo void QQuickWebEngineViewPrivate::showTouchSelectionMenu(QtWebEngineCore::TouchSelectionMenuController *menuController, const QRect &selectionBounds, const QSize &handleSize) { Q_UNUSED(handleSize); + Q_Q(QQuickWebEngineView); const int kSpacingBetweenButtons = 2; const int kMenuButtonMinWidth = 63; const int kMenuButtonMinHeight = 38; + QQuickWebEngineTouchSelectionMenuRequest *request = new QQuickWebEngineTouchSelectionMenuRequest( + selectionBounds, menuController); + qmlEngine(q)->newQObject(request); + Q_EMIT q->touchSelectionMenuRequested(request); + + if (request->isAccepted()) { + return; + } + int buttonCount = menuController->buttonCount(); if (buttonCount == 1) { menuController->runContextMenu(); diff --git a/src/webenginequick/api/qquickwebengineview_p.h b/src/webenginequick/api/qquickwebengineview_p.h index 3d3356a4e..1a42726e4 100644 --- a/src/webenginequick/api/qquickwebengineview_p.h +++ b/src/webenginequick/api/qquickwebengineview_p.h @@ -87,6 +87,7 @@ class QWebEngineNewWindowRequest; class QWebEngineQuotaRequest; class QWebEngineRegisterProtocolHandlerRequest; class QQuickWebEngineScriptCollection; +class QQuickWebEngineTouchSelectionMenuRequest; class Q_WEBENGINEQUICK_PRIVATE_EXPORT QQuickWebEngineView : public QQuickItem { Q_OBJECT @@ -563,6 +564,7 @@ Q_SIGNALS: Q_REVISION(1,11) void canGoBackChanged(); Q_REVISION(1,11) void canGoForwardChanged(); Q_REVISION(1,12) void newWindowRequested(QQuickWebEngineNewWindowRequest *request); + Q_REVISION(6,3) void touchSelectionMenuRequested(QQuickWebEngineTouchSelectionMenuRequest *request); protected: void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; diff --git a/src/webenginequick/doc/src/touch_selection_menu_request.qdoc b/src/webenginequick/doc/src/touch_selection_menu_request.qdoc new file mode 100644 index 000000000..da4c2062a --- /dev/null +++ b/src/webenginequick/doc/src/touch_selection_menu_request.qdoc @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \qmltype TouchSelectionMenuRequest + //! \instantiates QQuickWebEngineTouchSelectionMenuRequest + \inqmlmodule QtWebEngine + \since QtWebEngine 6.3 + + \brief A request for showing a touch selection menu. + + A TouchSelectionMenuRequest is passed as an argument of the + WebEngineView::touchSelectionMenuRequest signal. It provides further + information about the context of the request. The \l selectionBounds + property provides the origin of the request. + + The \l accepted property of the request indicates whether the request + is handled by the user code or the default touch selection menu should + be displayed. + + The following code uses a custom menu to handle the request: + + \code + WebEngineView { + id: view + // ... + onTouchSelectionMenuRequested: function(request) { + request.accepted = true; + myMenu.x = request.selectionBounds.x; + myMenu.y = request.selectionBounds.y; + myMenu.trigger.connect(view.triggerWebAction); + myMenu.popup(); + } + // ... + } + \endcode +*/ + +/*! + \qmlproperty rect TouchSelectionMenuRequest::selectionBounds + \readonly + + The position of the bound rectangle from the touch text + selection. +*/ + +/*! + \qmlproperty bool TouchSelectionMenuRequest::accepted + + Indicates whether the touch selection menu request has + been handled by the signal handler. + + If the property is \c false after any signal handlers + for WebEngineView::touchSelectionMenuRequested have been executed, + a default touch selection menu will be shown. + To prevent this, set \c{request.accepted} to \c true. + + The default is \c false. + + \note The default content of the touch selection menu depends + on the web element for which the request was actually generated. +*/ + + +/*! + \qmlproperty flags QQuickWebEngineTouchSelectionMenuRequest::touchSelectionCommandFlags + \readonly + \since QtWebEngine 6.3 + + \note In the default touch selection menu, showing the context menu + is always available as a separate action. + + \value QQuickWebEngineTouchSelectionMenuRequest.Cut + Cut is available. + \value QQuickWebEngineTouchSelectionMenuRequest.Copy + Copy is available. + \value QQuickWebEngineTouchSelectionMenuRequest.Paste + Paste is available. +*/ diff --git a/tests/auto/quick/publicapi/tst_publicapi.cpp b/tests/auto/quick/publicapi/tst_publicapi.cpp index c8e09f1c2..af22add1f 100644 --- a/tests/auto/quick/publicapi/tst_publicapi.cpp +++ b/tests/auto/quick/publicapi/tst_publicapi.cpp @@ -55,6 +55,7 @@ #include #include #include +#include class tst_publicapi : public QObject { Q_OBJECT @@ -91,6 +92,7 @@ static const QList typesToCheck = QList knownEnumNames = QList(); @@ -447,6 +449,12 @@ static const QStringList expectedAPI = QStringList() << "QQuickWebEngineSingleton.defaultProfile --> QQuickWebEngineProfile*" << "QQuickWebEngineSingleton.settings --> QQuickWebEngineSettings*" << "QQuickWebEngineSingleton.script() --> QWebEngineScript" + << "QQuickWebEngineTouchSelectionMenuRequest.accepted --> bool" + << "QQuickWebEngineTouchSelectionMenuRequest.Cut --> TouchSelectionCommandFlags" + << "QQuickWebEngineTouchSelectionMenuRequest.Copy --> TouchSelectionCommandFlags" + << "QQuickWebEngineTouchSelectionMenuRequest.Paste --> TouchSelectionCommandFlags" + << "QQuickWebEngineTouchSelectionMenuRequest.selectionBounds --> QRect" + << "QQuickWebEngineTouchSelectionMenuRequest.touchSelectionCommandFlags --> QFlags" << "QWebEngineScript.ApplicationWorld --> ScriptWorldId" << "QWebEngineScript.Deferred --> InjectionPoint" << "QWebEngineScript.DocumentCreation --> InjectionPoint" @@ -764,6 +772,7 @@ static const QStringList expectedAPI = QStringList() << "QQuickWebEngineView.title --> QString" << "QQuickWebEngineView.titleChanged() --> void" << "QQuickWebEngineView.tooltipRequested(QQuickWebEngineTooltipRequest*) --> void" + << "QQuickWebEngineView.touchSelectionMenuRequested(QQuickWebEngineTouchSelectionMenuRequest*) --> void" << "QQuickWebEngineView.triggerWebAction(WebAction) --> void" << "QQuickWebEngineView.url --> QUrl" << "QQuickWebEngineView.urlChanged() --> void" -- cgit v1.2.3