From 35610da7508cb51e060cef764fabb0c88a85f1ce Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 2 Mar 2021 15:47:08 +0400 Subject: [PATCH] Use lib_webview implementation on Windows. --- Telegram/SourceFiles/facades.cpp | 68 ++++++++++++++++++++------------ docs/building-msvc-x64.md | 2 + docs/building-msvc.md | 2 + 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index c51e358857..bb19841714 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -35,6 +35,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_chat.h" #include "webview/webview_embed.h" +#include "webview/webview_interface.h" +#include "core/local_url_handlers.h" #include "ui/widgets/window.h" #include "ui/toast/toast.h" #include @@ -66,38 +68,42 @@ void GetPaymentForm(not_null msg) { }, window->lifetime()); const auto body = window->body(); + body->paintRequest( + ) | rpl::start_with_next([=](QRect clip) { + QPainter(body).fillRect(clip, st::windowBg); + }, body->lifetime()); + const auto webview = Ui::CreateChild( window, window); + if (!webview->widget()) { + delete window; + Ui::show(Box( + tr::lng_payments_not_supported(tr::now))); + return; + } + body->geometryValue( ) | rpl::start_with_next([=](QRect geometry) { webview->widget()->setGeometry(geometry); }, body->lifetime()); - webview->bind("buy_callback", [=](const QByteArray &result) { - auto error = QJsonParseError{ 0, QJsonParseError::NoError }; - const auto typeAndArguments = QJsonDocument::fromJson( - result, - &error); - if (error.error != QJsonParseError::NoError) { + webview->setMessageHandler([=](const QJsonDocument &message) { + if (!message.isArray()) { LOG(("Payments Error: " - "Failed to parse buy_callback result, error: %1." - ).arg(error.errorString())); - return; - } else if (!typeAndArguments.isArray()) { - LOG(("API Error: " "Not an array received in buy_callback arguments.")); return; } - const auto list = typeAndArguments.array(); + const auto list = message.array(); if (list.at(0).toString() != "payment_form_submit") { return; } else if (!list.at(1).isString()) { - LOG(("API Error: " + LOG(("Payments Error: " "Not a string received in buy_callback result.")); return; } + auto error = QJsonParseError(); const auto document = QJsonDocument::fromJson( list.at(1).toString().toUtf8(), &error); @@ -107,7 +113,7 @@ void GetPaymentForm(not_null msg) { ).arg(error.errorString())); return; } else if (!document.isObject()) { - LOG(("API Error: " + LOG(("Payments Error: " "Not an object decoded in buy_callback result.")); return; } @@ -115,7 +121,7 @@ void GetPaymentForm(not_null msg) { const auto title = root.value("title").toString(); const auto credentials = root.value("credentials"); if (!credentials.isObject()) { - LOG(("API Error: " + LOG(("Payments Error: " "Not an object received in payment credentials.")); return; } @@ -145,15 +151,21 @@ void GetPaymentForm(not_null msg) { }).send(); }); - webview->init("(function(){" - "window.TelegramWebviewProxy = {" - "postEvent: function(eventType, eventData) {" - "if (window.buy_callback) {" - "window.buy_callback(eventType, eventData);" - "}" - "}" - "};" - "}());"); + webview->setNavigationHandler([=](const QString &uri) { + if (Core::TryConvertUrlToLocal(uri) != uri) { + window->deleteLater(); + App::wnd()->activate(); + } + }); + + webview->init(R"( +window.TelegramWebviewProxy = { + postEvent: function(eventType, eventData) { + if (window.external && window.external.invoke) { + window.external.invoke(JSON.stringify([eventType, eventData])); + } + } +};)"); const auto &data = result.c_payments_paymentForm(); webview->navigate(qs(data.vurl())); @@ -252,8 +264,12 @@ void activateBotCommand( } break; case ButtonType::Buy: { - Api::GetPaymentForm(msg); - //Ui::show(Box(tr::lng_payments_not_supported(tr::now))); + if (Webview::Supported()) { + Api::GetPaymentForm(msg); + } else { + Ui::show(Box( + tr::lng_payments_not_supported(tr::now))); + } } break; case ButtonType::Url: { diff --git a/docs/building-msvc-x64.md b/docs/building-msvc-x64.md index 5750c29fa6..f5e73e1a60 100644 --- a/docs/building-msvc-x64.md +++ b/docs/building-msvc-x64.md @@ -27,6 +27,7 @@ You will require **api_id** and **api_hash** to access the Telegram API servers. * Download **CMake** installer from [https://cmake.org/download/](https://cmake.org/download/) and install to ***BuildPath*\\ThirdParty\\cmake** * Download **Ninja** executable from [https://github.com/ninja-build/ninja/releases/download/v1.7.2/ninja-win.zip](https://github.com/ninja-build/ninja/releases/download/v1.7.2/ninja-win.zip) and unpack to ***BuildPath*\\ThirdParty\\Ninja** * Download **Git** installer from [https://git-scm.com/download/win](https://git-scm.com/download/win) and install it. +* Download **NuGet** executable from [https://dist.nuget.org/win-x86-commandline/latest/nuget.exe](https://www.nuget.org/downloads) and put to ***BuildPath*\\ThirdParty\\NuGet** Open **x64 Native Tools Command Prompt for VS 2019.bat**, go to ***BuildPath*** and run @@ -49,6 +50,7 @@ Add **GYP** and **Ninja** to your PATH: * Press **Edit** * Add ***BuildPath*\\ThirdParty\\gyp** value * Add ***BuildPath*\\ThirdParty\\Ninja** value +* Add ***BuildPath*\\ThirdParty\\NuGet** value ## Clone source code and prepare libraries diff --git a/docs/building-msvc.md b/docs/building-msvc.md index 243a385fb6..9499a5a4ea 100644 --- a/docs/building-msvc.md +++ b/docs/building-msvc.md @@ -27,6 +27,7 @@ You will require **api_id** and **api_hash** to access the Telegram API servers. * Download **CMake** installer from [https://cmake.org/download/](https://cmake.org/download/) and install to ***BuildPath*\\ThirdParty\\cmake** * Download **Ninja** executable from [https://github.com/ninja-build/ninja/releases/download/v1.7.2/ninja-win.zip](https://github.com/ninja-build/ninja/releases/download/v1.7.2/ninja-win.zip) and unpack to ***BuildPath*\\ThirdParty\\Ninja** * Download **Git** installer from [https://git-scm.com/download/win](https://git-scm.com/download/win) and install it. +* Download **NuGet** executable from [https://dist.nuget.org/win-x86-commandline/latest/nuget.exe](https://www.nuget.org/downloads) and put to ***BuildPath*\\ThirdParty\\NuGet** Open **x86 Native Tools Command Prompt for VS 2019.bat**, go to ***BuildPath*** and run @@ -49,6 +50,7 @@ Add **GYP** and **Ninja** to your PATH: * Press **Edit** * Add ***BuildPath*\\ThirdParty\\gyp** value * Add ***BuildPath*\\ThirdParty\\Ninja** value +* Add ***BuildPath*\\ThirdParty\\NuGet** value ## Clone source code and prepare libraries