Better support for web_app_open_link.

This commit is contained in:
John Preston 2022-05-26 13:08:11 +04:00
parent be16a7725c
commit b1dd3b2a19
2 changed files with 36 additions and 1 deletions

View File

@ -33,6 +33,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Ui::BotWebView {
namespace {
constexpr auto kProcessClickTimeout = crl::time(1000);
constexpr auto kProgressDuration = crl::time(200);
constexpr auto kProgressOpacity = 0.3;
constexpr auto kLightnessThreshold = 128;
@ -86,6 +87,30 @@ constexpr auto kLightnessDelta = 32;
alpha);
}
[[nodiscard]] QString EncodeForJs(const QString &text) {
auto result = QString();
for (auto ch = text.data(), e = ch + text.size(); ch != e; ++ch) {
const auto code = ch->unicode();
const auto hex = [&](int value) {
const auto v = value & 0x0F;
return QChar((v < 10) ? ('0' + v) : 'A' + (v - 10));
};
if (code >= 32 && code < 128) {
if (code == '\\' || code == '"' || code == '\'') {
result += QChar('\\');
}
result += *ch;
} else {
result += u"\\u"_q
+ hex(code >> 12)
+ hex(code >> 8)
+ hex(code >> 4)
+ hex(code);
}
}
return result;
}
} // namespace
class Panel::Button final : public RippleButton {
@ -667,7 +692,15 @@ void Panel::openExternalLink(const QJsonValue &value) {
_close();
return;
}
const auto now = crl::now();
if (_mainButtonLastClick
&& _mainButtonLastClick + kProcessClickTimeout >= now) {
_mainButtonLastClick = 0;
QDesktopServices::openUrl(url);
} else {
const auto string = EncodeForJs(url);
_webview->window.eval(("window.open(\"" + string + "\");").toUtf8());
}
}
void Panel::openInvoice(const QJsonValue &value) {
@ -747,6 +780,7 @@ void Panel::createMainButton() {
button->setClickedCallback([=] {
if (!button->isDisabled()) {
postEvent("main_button_pressed");
_mainButtonLastClick = crl::now();
}
});
button->hide();

View File

@ -97,6 +97,7 @@ private:
std::unique_ptr<RpWidget> _webviewBottom;
QPointer<QWidget> _webviewParent;
std::unique_ptr<Button> _mainButton;
crl::time _mainButtonLastClick = 0;
std::unique_ptr<Progress> _progress;
rpl::event_stream<> _themeUpdateForced;
rpl::lifetime _fgLifetime;