From 0a87dbea6829f36927e9930fd101a3aa3e758714 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 16 Feb 2024 11:25:15 +0400 Subject: [PATCH] Fix focusing IV content. --- Telegram/Resources/iv_html/page.css | 3 +++ Telegram/Resources/iv_html/page.js | 27 ++++++++++++++++++----- Telegram/SourceFiles/iv/iv_controller.cpp | 16 +++++++++++++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Telegram/Resources/iv_html/page.css b/Telegram/Resources/iv_html/page.css index 1da34f68ac..e6bd5af80f 100644 --- a/Telegram/Resources/iv_html/page.css +++ b/Telegram/Resources/iv_html/page.css @@ -135,6 +135,9 @@ html.custom_scroll ::-webkit-scrollbar-thumb:hover { overflow-x: hidden; overflow-y: auto; } +.page-scroll:focus { + outline: none; +} .page-slide { position: relative; width: 100%; diff --git a/Telegram/Resources/iv_html/page.js b/Telegram/Resources/iv_html/page.js index 8628592033..333c127f1e 100644 --- a/Telegram/Resources/iv_html/page.js +++ b/Telegram/Resources/iv_html/page.js @@ -79,10 +79,14 @@ var IV = { }); } else if (e.key === 'Escape' || e.keyCode === 27) { e.preventDefault(); - IV.notify({ - event: 'keydown', - key: 'escape', - }); + if (IV.position) { + window.history.back(); + } else { + IV.notify({ + event: 'keydown', + key: 'escape', + }); + } } }, frameMouseEnter: function (e) { @@ -250,6 +254,8 @@ var IV = { } IV.initMedia(); IV.notify({ event: 'ready' }); + + IV.forceScrollFocus(); }, initMedia: function () { const photos = document.getElementsByClassName('photo'); @@ -356,6 +362,7 @@ var IV = { var data = JSON.parse(IV.cache[index].content); var el = document.createElement('div'); el.className = 'page-scroll'; + el.tabIndex = '-1'; el.innerHTML = '
' + data.html + '
'; @@ -370,6 +377,7 @@ var IV = { IV.pending = null; if (IV.index == index) { IV.jumpToHash(hash, IV.mac); + IV.forceScrollFocus(); return; } window.history.replaceState(IV.computeCurrentState(), ''); @@ -452,6 +460,15 @@ var IV = { } else { IV.jumpToHash(hash, IV.mac); } + + IV.forceScrollFocus(); + }, + forceScrollFocus: function () { + IV.findPageScroll().focus(); + setTimeout(function () { + // Doesn't work on #hash-ed pages in Windows WebView2 otherwise. + IV.findPageScroll().focus(); + }, 100); }, back: function () { window.history.back(); @@ -467,9 +484,9 @@ document.onkeydown = IV.frameKeyDown; document.onmouseenter = IV.frameMouseEnter; document.onmouseup = IV.frameMouseUp; window.onmessage = IV.postMessageHandler; - window.addEventListener('popstate', function (e) { if (e.state) { IV.showDOM(e.state.index, e.state.hash, e.state.scroll); } }); +document.addEventListener("DOMContentLoaded", IV.forceScrollFocus); diff --git a/Telegram/SourceFiles/iv/iv_controller.cpp b/Telegram/SourceFiles/iv/iv_controller.cpp index 98de7e49d6..b26ca82371 100644 --- a/Telegram/SourceFiles/iv/iv_controller.cpp +++ b/Telegram/SourceFiles/iv/iv_controller.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/platform/base_platform_info.h" #include "base/invoke_queued.h" +#include "base/qt_signal_producer.h" #include "iv/iv_data.h" #include "lang/lang_keys.h" #include "ui/platform/ui_platform_window_title.h" @@ -31,6 +32,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include #include #include +#include #include namespace Iv { @@ -182,7 +184,7 @@ namespace { -
+
"_q + page.content + R"(
@@ -286,6 +288,15 @@ void Controller::createWindow() { _window->setTitleStyle(st::ivTitle); const auto window = _window.get(); + base::qt_signal_producer( + window->window()->windowHandle(), + &QWindow::activeChanged + ) | rpl::filter([=] { + return _webview && window->window()->windowHandle()->isActive(); + }) | rpl::start_with_next([=] { + _webview->focus(); + }, window->lifetime()); + _title = std::make_unique(window); _title->setAttribute(Qt::WA_TransparentForMouseEvents); _title->paintRequest() | rpl::start_with_next([=](QRect clip) { @@ -521,6 +532,7 @@ void Controller::showInWindow(const QString &dataPath, Prepared page) { id += '#' + hash; } _webview->navigateToData(id); + _webview->focus(); } else { _events.fire({ Event::Type::Close }); } @@ -529,12 +541,14 @@ void Controller::showInWindow(const QString &dataPath, Prepared page) { _window->raise(); _window->activateWindow(); _window->setFocus(); + _webview->focus(); } else { _navigateToIndexWhenReady = index; _navigateToHashWhenReady = hash; _window->raise(); _window->activateWindow(); _window->setFocus(); + _webview->focus(); } }