diff --git a/examples/quick/quicknanobrowser/quickwindow.qml b/examples/quick/quicknanobrowser/quickwindow.qml index 63f86458e88addcdeac5c2067a8ca73c8c10a23d..88fc9f8a0dbe5ffecb82fd2041623a55b56a76ba 100644 --- a/examples/quick/quicknanobrowser/quickwindow.qml +++ b/examples/quick/quicknanobrowser/quickwindow.qml @@ -44,6 +44,7 @@ import QtWebEngine.experimental 1.0 import QtQuick.Controls 1.0 import QtQuick.Controls.Styles 1.0 import QtQuick.Layouts 1.0 +import QtQuick.Controls.Private 1.0 ApplicationWindow { id: browserWindow @@ -55,6 +56,12 @@ ApplicationWindow { visible: true title: tabs.currentView && tabs.currentView.title + // Create a styleItem to determine the platform. + // When using style "mac", ToolButtons are not supposed to accept focus. + StyleItem { id: styleItem } + property bool platformIsMac: styleItem.style == "mac" + + Action { id: focus shortcut: "Ctrl+L" @@ -94,17 +101,20 @@ ApplicationWindow { iconSource: "icons/go-previous.png" onClicked: tabs.currentView.goBack() enabled: tabs.currentView && tabs.currentView.canGoBack + activeFocusOnTab: !browserWindow.platformIsMac } ToolButton { id: forwardButton iconSource: "icons/go-next.png" onClicked: tabs.currentView.goForward() enabled: tabs.currentView && tabs.currentView.canGoForward + activeFocusOnTab: !browserWindow.platformIsMac } ToolButton { id: reloadButton iconSource: tabs.currentView && tabs.currentView.loading ? "icons/process-stop.png" : "icons/view-refresh.png" onClicked: tabs.currentView && tabs.currentView.loading ? tabs.currentView.stop() : tabs.currentView.reload() + activeFocusOnTab: !browserWindow.platformIsMac } TextField { id: addressBar diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h index 598840f1daff5513e89c2e8504d0a7dd489e2d6f..077e21500ad452f4be00689107f274336573daba 100644 --- a/src/core/web_contents_adapter_client.h +++ b/src/core/web_contents_adapter_client.h @@ -132,6 +132,7 @@ public: virtual void didRunJavaScript(const QVariant& result, quint64 requestId) = 0; virtual void didFetchDocumentMarkup(const QString& result, quint64 requestId) = 0; virtual void didFetchDocumentInnerText(const QString& result, quint64 requestId) = 0; + virtual void passOnFocus(bool reverse) = 0; virtual void javaScriptConsoleMessage(int level, const QString& message, int lineNumber, const QString& sourceID) = 0; }; diff --git a/src/core/web_contents_view_qt.cpp b/src/core/web_contents_view_qt.cpp index 65b8e5f3a155df0e8fdc8cef90daad764ae4d0c8..a4cb3556e0745c713b6556bd4836a7eb0d7ad243 100644 --- a/src/core/web_contents_view_qt.cpp +++ b/src/core/web_contents_view_qt.cpp @@ -130,3 +130,8 @@ void WebContentsViewQt::ShowContextMenu(const content::ContextMenuParams ¶ms WebEngineContextMenuData contextMenuData(fromParams(params)); m_client->contextMenuRequested(contextMenuData); } + +void WebContentsViewQt::TakeFocus(bool reverse) +{ + m_client->passOnFocus(reverse); +} diff --git a/src/core/web_contents_view_qt.h b/src/core/web_contents_view_qt.h index ee81ccc813f1d4096b1ea965e6a77481d025e01e..1c9da487ba6204f311546122c60f938f55f939ca 100644 --- a/src/core/web_contents_view_qt.h +++ b/src/core/web_contents_view_qt.h @@ -112,6 +112,8 @@ public: virtual void ShowContextMenu(const content::ContextMenuParams ¶ms) Q_DECL_OVERRIDE; + virtual void TakeFocus(bool reverse) Q_DECL_OVERRIDE; + #if defined(OS_MACOSX) virtual void SetAllowOverlappingViews(bool overlapping) Q_DECL_OVERRIDE { QT_NOT_YET_IMPLEMENTED } virtual void CloseTabAfterEventTracking() Q_DECL_OVERRIDE { QT_NOT_YET_IMPLEMENTED } diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp index 049e353b613d82e7f9c353996512f8167dac395d..2f5d868ea19c99de99574937c22aa89669dc6668 100644 --- a/src/webengine/api/qquickwebengineview.cpp +++ b/src/webengine/api/qquickwebengineview.cpp @@ -180,6 +180,18 @@ void QQuickWebEngineViewPrivate::runFileChooser(FileChooserMode mode, const QStr ui()->showFilePicker(mode, defaultFileName, acceptedMimeTypes, adapter); } +void QQuickWebEngineViewPrivate::passOnFocus(bool reverse) +{ + Q_Q(QQuickWebEngineView); + // In one direction we would pass forward the focus to RenderWidgetHostViewQtDelegateQuick(Painted), + // which in return would forward the tab key event and therefore the focus back to the QQuickWebEngineView. + // This is why we skip RenderWidgetHostViewQtDelegateQuick in the focus chain. + QQuickItem* current = QQuickItemPrivate::nextPrevItemInTabFocusChain(q, !reverse); + if (!qobject_cast<RenderWidgetHostViewQtDelegateQuick*>(current) && !qobject_cast<RenderWidgetHostViewQtDelegateQuickPainted*>(current)) + current = q; + focusNextPrev(current, !reverse); +} + void QQuickWebEngineViewPrivate::titleChanged(const QString &title) { Q_Q(QQuickWebEngineView); diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h index 37ac9edf05fea5c934e70d72869773729968faee..3de5974690ad1104512fb7d4459932e74e92561c 100644 --- a/src/webengine/api/qquickwebengineview_p_p.h +++ b/src/webengine/api/qquickwebengineview_p_p.h @@ -141,6 +141,7 @@ public: virtual void didRunJavaScript(const QVariant&, quint64) Q_DECL_OVERRIDE { } virtual void didFetchDocumentMarkup(const QString&, quint64) Q_DECL_OVERRIDE { } virtual void didFetchDocumentInnerText(const QString&, quint64) Q_DECL_OVERRIDE { } + virtual void passOnFocus(bool reverse) Q_DECL_OVERRIDE; virtual void javaScriptConsoleMessage(int level, const QString& message, int lineNumber, const QString& sourceID) Q_DECL_OVERRIDE; void setDevicePixelRatio(qreal); diff --git a/src/webengine/render_widget_host_view_qt_delegate_quick.h b/src/webengine/render_widget_host_view_qt_delegate_quick.h index 9dbe7994150a80053c7999d626258e4954070629..8f32689d01150f091f5e1e28ddd7698d8c347539 100644 --- a/src/webengine/render_widget_host_view_qt_delegate_quick.h +++ b/src/webengine/render_widget_host_view_qt_delegate_quick.h @@ -60,6 +60,10 @@ public: : ItemBaseT(parent) , m_client(client) { + this->setFocus(true); + this->setActiveFocusOnTab(true); + this->setFlag(QQuickItem::ItemIsFocusScope); + this->setAcceptedMouseButtons(Qt::AllButtons); this->setAcceptHoverEvents(true); } @@ -143,7 +147,7 @@ public: void mousePressEvent(QMouseEvent *event) { - this->setFocus(true); + this->forceActiveFocus(); m_client->forwardEvent(event); } diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h index 595318ffcc757e9b9278735221b858209af26a61..0a90eda0787d1f376cbc0fdeb9f043eb01d15179 100644 --- a/src/webenginewidgets/api/qwebenginepage_p.h +++ b/src/webenginewidgets/api/qwebenginepage_p.h @@ -83,6 +83,8 @@ public: virtual void didRunJavaScript(const QVariant& result, quint64 requestId) Q_DECL_OVERRIDE; virtual void didFetchDocumentMarkup(const QString& result, quint64 requestId) Q_DECL_OVERRIDE; virtual void didFetchDocumentInnerText(const QString& result, quint64 requestId) Q_DECL_OVERRIDE; + virtual void passOnFocus(bool reverse) Q_DECL_OVERRIDE { }; + virtual void javaScriptConsoleMessage(int level, const QString& message, int lineNumber, const QString& sourceID) Q_DECL_OVERRIDE; void updateAction(QWebEnginePage::WebAction) const;