diff options
author | Szabolcs David <davidsz@inf.u-szeged.hu> | 2023-01-30 18:21:48 +0100 |
---|---|---|
committer | Szabolcs David <davidsz@inf.u-szeged.hu> | 2023-02-15 13:19:29 +0100 |
commit | 47564e329a71fed8b73be403bbdc30fbf684f7a4 (patch) | |
tree | 16ad3d22d7e301ed2458732eaafbdc9e2c5542ea /src/core | |
parent | 552f941ec35987b643dac0fdb8a323366bf4e3d0 (diff) |
Support LTR and RTL text directions in input fields
Implement WebActions to temporarily change text direction directly from
context menu - like Chrome does. They only work for the selected input
field. Omit the "default" option since it is always grayed out in Chrome.
Task-number: QTWB-67
Change-Id: I4bacbd82c6bd7261ac465eec9da3776613f98074
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/api/qwebenginepage.cpp | 12 | ||||
-rw-r--r-- | src/core/api/qwebenginepage.h | 3 | ||||
-rw-r--r-- | src/core/doc/src/qwebenginepage_lgpl.qdoc | 4 | ||||
-rw-r--r-- | src/core/web_contents_adapter.cpp | 14 | ||||
-rw-r--r-- | src/core/web_contents_adapter.h | 1 |
5 files changed, 34 insertions, 0 deletions
diff --git a/src/core/api/qwebenginepage.cpp b/src/core/api/qwebenginepage.cpp index ac44fad10..db795f846 100644 --- a/src/core/api/qwebenginepage.cpp +++ b/src/core/api/qwebenginepage.cpp @@ -1179,6 +1179,12 @@ QAction *QWebEnginePage::action(WebAction action) const case InsertUnorderedList: text = tr("Insert &Unordered List"); break; + case ChangeTextDirectionLTR: + text = tr("Change text direction left to right"); + break; + case ChangeTextDirectionRTL: + text = tr("Change text direction right to left"); + break; case NoWebAction: case WebActionCount: Q_UNREACHABLE(); @@ -1452,6 +1458,12 @@ void QWebEnginePage::triggerAction(WebAction action, bool) case InsertUnorderedList: runJavaScript(QStringLiteral("document.execCommand('insertUnorderedList');"), QWebEngineScript::ApplicationWorld); break; + case ChangeTextDirectionLTR: + d->adapter->changeTextDirection(true /*left to right*/); + break; + case ChangeTextDirectionRTL: + d->adapter->changeTextDirection(false /*left to right*/); + break; case NoWebAction: break; case WebActionCount: diff --git a/src/core/api/qwebenginepage.h b/src/core/api/qwebenginepage.h index c60feb3ab..e58cb3a84 100644 --- a/src/core/api/qwebenginepage.h +++ b/src/core/api/qwebenginepage.h @@ -122,6 +122,9 @@ public: InsertOrderedList, InsertUnorderedList, + ChangeTextDirectionLTR, + ChangeTextDirectionRTL, + WebActionCount }; Q_ENUM(WebAction) diff --git a/src/core/doc/src/qwebenginepage_lgpl.qdoc b/src/core/doc/src/qwebenginepage_lgpl.qdoc index 19efa2903..fe84ff76b 100644 --- a/src/core/doc/src/qwebenginepage_lgpl.qdoc +++ b/src/core/doc/src/qwebenginepage_lgpl.qdoc @@ -194,6 +194,10 @@ Inserts an unordered list at the current cursor position, deleting the current selection. Requires \c contenteditable="true". (Added in Qt 5.10) + \value ChangeTextDirectionLTR + Changes text direction to left-to-right in the focused input element. (Added in Qt 6.6) + \value ChangeTextDirectionRTL + Changes text direction to right-to-left in the focused input element. (Added in Qt 6.6) \omitvalue WebActionCount */ diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index 081505d7f..5b880d31a 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -1772,6 +1772,20 @@ void WebContentsAdapter::resetTouchSelectionController() rwhv->resetTouchSelectionController(); } +void WebContentsAdapter::changeTextDirection(bool leftToRight) +{ + CHECK_INITIALIZED(); + if (auto rwhv = static_cast<RenderWidgetHostViewQt *>(m_webContents->GetRenderWidgetHostView())) { + auto textInputManager = rwhv->GetTextInputManager(); + if (!textInputManager) + return; + if (auto activeWidget = textInputManager->GetActiveWidget()) { + activeWidget->UpdateTextDirection(leftToRight ? base::i18n::TextDirection::LEFT_TO_RIGHT : base::i18n::TextDirection::RIGHT_TO_LEFT); + activeWidget->NotifyTextDirection(); + } + } +} + WebContentsAdapterClient::RenderProcessTerminationStatus WebContentsAdapterClient::renderProcessExitStatus(int terminationStatus) { auto status = WebContentsAdapterClient::RenderProcessTerminationStatus(-1); diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index 1f2f08d73..0a97fde56 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -201,6 +201,7 @@ public: bool hasFocusedFrame() const; void resetSelection(); void resetTouchSelectionController(); + void changeTextDirection(bool leftToRight); // meant to be used within WebEngineCore only void initialize(content::SiteInstance *site); |