Show webview error in Iv::Controller.

This commit is contained in:
John Preston 2024-07-30 15:53:06 +02:00
parent 699a7bdc58
commit a25b2e9700
6 changed files with 91 additions and 51 deletions

View file

@ -11,8 +11,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/invoke_queued.h" #include "base/invoke_queued.h"
#include "base/qt_signal_producer.h" #include "base/qt_signal_producer.h"
#include "base/qthelp_url.h" #include "base/qthelp_url.h"
#include "core/file_utilities.h"
#include "iv/iv_data.h" #include "iv/iv_data.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "ui/chat/attach/attach_bot_webview.h"
#include "ui/platform/ui_platform_window_title.h" #include "ui/platform/ui_platform_window_title.h"
#include "ui/widgets/buttons.h" #include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h" #include "ui/widgets/labels.h"
@ -28,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/palette.h" #include "styles/palette.h"
#include "styles/style_iv.h" #include "styles/style_iv.h"
#include "styles/style_menu_icons.h" #include "styles/style_menu_icons.h"
#include "styles/style_payments.h" // paymentsCriticalError
#include "styles/style_widgets.h" #include "styles/style_widgets.h"
#include "styles/style_window.h" #include "styles/style_window.h"
@ -194,7 +197,7 @@ Controller::~Controller() {
_window->hide(); _window->hide();
} }
_ready = false; _ready = false;
_webview = nullptr; base::take(_webview);
_back.destroy(); _back.destroy();
_forward.destroy(); _forward.destroy();
_menu = nullptr; _menu = nullptr;
@ -335,8 +338,6 @@ void Controller::showTonSite(
if (_webview && _webview->widget()) { if (_webview && _webview->widget()) {
_webview->navigate(url); _webview->navigate(url);
activate(); activate();
} else {
_events.fire({ Event::Type::Close });
} }
_url = url; _url = url;
_subtitleText = _url.value( _subtitleText = _url.value(
@ -435,7 +436,7 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
window->lifetime().add([=] { window->lifetime().add([=] {
_ready = false; _ready = false;
_webview = nullptr; base::take(_webview);
}); });
window->events( window->events(
@ -449,11 +450,32 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
} }
} }
}, window->lifetime()); }, window->lifetime());
raw->widget()->show();
const auto widget = raw->widget();
if (!widget) {
base::take(_webview);
showWebviewError();
return;
}
widget->show();
QObject::connect(widget, &QObject::destroyed, [=] {
if (!_webview) {
// If we destroyed _webview ourselves,
// we don't show any message, nothing crashed.
return;
}
crl::on_main(window, [=] {
showWebviewError({ "Error: WebView has crashed." });
});
base::take(_webview);
});
_container->sizeValue( _container->sizeValue(
) | rpl::start_with_next([=](QSize size) { ) | rpl::start_with_next([=](QSize size) {
raw->widget()->setGeometry(QRect(QPoint(), size)); if (const auto widget = raw->widget()) {
widget->setGeometry(QRect(QPoint(), size));
}
}, _container->lifetime()); }, _container->lifetime());
raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) { raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) {
@ -600,11 +622,45 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
_back->setAttribute( _back->setAttribute(
Qt::WA_TransparentForMouseEvents, Qt::WA_TransparentForMouseEvents,
!state.canGoBack); !state.canGoBack);
_url = QString::fromStdString(state.url);
}, _webview->lifetime()); }, _webview->lifetime());
raw->init(R"()"); raw->init(R"()");
} }
void Controller::showWebviewError() {
const auto available = Webview::Availability();
if (available.error != Webview::Available::Error::None) {
showWebviewError(Ui::BotWebView::ErrorText(available));
} else {
showWebviewError({ "Error: Could not initialize WebView." });
}
}
void Controller::showWebviewError(TextWithEntities text) {
auto error = Ui::CreateChild<Ui::PaddingWrap<Ui::FlatLabel>>(
_container,
object_ptr<Ui::FlatLabel>(
_container,
rpl::single(text),
st::paymentsCriticalError),
st::paymentsCriticalErrorPadding);
error->entity()->setClickHandlerFilter([=](
const ClickHandlerPtr &handler,
Qt::MouseButton) {
const auto entity = handler->getTextEntity();
if (entity.type != EntityType::CustomUrl) {
return true;
}
File::OpenUrl(entity.data);
return false;
});
error->show();
_container->sizeValue() | rpl::start_with_next([=](QSize size) {
error->setGeometry(0, 0, size.width(), size.height() * 2 / 3);
}, error->lifetime());
}
void Controller::showInWindow( void Controller::showInWindow(
const Webview::StorageId &storageId, const Webview::StorageId &storageId,
Prepared page) { Prepared page) {

View file

@ -123,6 +123,9 @@ private:
void showShareMenu(); void showShareMenu();
void destroyShareMenu(); void destroyShareMenu();
void showWebviewError();
void showWebviewError(TextWithEntities text);
const not_null<Delegate*> _delegate; const not_null<Delegate*> _delegate;
std::unique_ptr<Ui::RpWindow> _window; std::unique_ptr<Ui::RpWindow> _window;

View file

@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/checkbox.h" #include "ui/widgets/checkbox.h"
#include "ui/wrap/fade_wrap.h" #include "ui/wrap/fade_wrap.h"
#include "ui/boxes/single_choice_box.h" #include "ui/boxes/single_choice_box.h"
#include "ui/chat/attach/attach_bot_webview.h"
#include "ui/text/format_values.h" #include "ui/text/format_values.h"
#include "ui/text/text_utilities.h" #include "ui/text/text_utilities.h"
#include "ui/effects/radial_animation.h" #include "ui/effects/radial_animation.h"
@ -908,32 +909,9 @@ std::shared_ptr<Show> Panel::uiShow() {
void Panel::showWebviewError( void Panel::showWebviewError(
const QString &text, const QString &text,
const Webview::Available &information) { const Webview::Available &information) {
using Error = Webview::Available::Error; showCriticalError(TextWithEntities{ text }.append(
Expects(information.error != Error::None); "\n\n"
).append(BotWebView::ErrorText(information)));
auto rich = TextWithEntities{ text };
rich.append("\n\n");
switch (information.error) {
case Error::NoWebview2: {
rich.append(tr::lng_payments_webview_install_edge(
tr::now,
lt_link,
Text::Link(
"Microsoft Edge WebView2 Runtime",
"https://go.microsoft.com/fwlink/p/?LinkId=2124703"),
Ui::Text::WithEntities));
} break;
case Error::NoWebKitGTK:
rich.append(tr::lng_payments_webview_install_webkit(tr::now));
break;
case Error::OldWindows:
rich.append(tr::lng_payments_webview_update_windows(tr::now));
break;
default:
rich.append(QString::fromStdString(information.details));
break;
}
showCriticalError(rich);
} }
void Panel::updateThemeParams(const Webview::ThemeParams &params) { void Panel::updateThemeParams(const Webview::ThemeParams &params) {

View file

@ -24,7 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "media/player/media_player_instance.h" #include "media/player/media_player_instance.h"
#include "webview/platform/win/webview_windows_edge_chromium.h"
#include "webview/webview_embed.h" #include "webview/webview_embed.h"
#include "window/main_window.h" #include "window/main_window.h"
#include "window/window_peer_menu.h" #include "window/window_peer_menu.h"
@ -150,7 +149,7 @@ void SetupExperimental(
addToggle(Media::Player::kOptionDisableAutoplayNext); addToggle(Media::Player::kOptionDisableAutoplayNext);
addToggle(kOptionSendLargePhotos); addToggle(kOptionSendLargePhotos);
addToggle(Webview::kOptionWebviewDebugEnabled); addToggle(Webview::kOptionWebviewDebugEnabled);
addToggle(Webview::EdgeChromium::kOptionWebviewLegacyEdge); addToggle(Webview::kOptionWebviewLegacyEdge);
addToggle(kOptionAutoScrollInactiveChat); addToggle(kOptionAutoScrollInactiveChat);
addToggle(Window::Notifications::kOptionGNotification); addToggle(Window::Notifications::kOptionGNotification);
addToggle(Core::kOptionFreeType); addToggle(Core::kOptionFreeType);

View file

@ -1401,32 +1401,34 @@ if (window.TelegramGameProxy) {
)"); )");
} }
void Panel::showWebviewError( TextWithEntities ErrorText(const Webview::Available &info) {
const QString &text, Expects(info.error != Webview::Available::Error::None);
const Webview::Available &information) {
using Error = Webview::Available::Error;
Expects(information.error != Error::None);
auto rich = TextWithEntities{ text }; using Error = Webview::Available::Error;
rich.append("\n\n"); switch (info.error) {
switch (information.error) { case Error::NoWebview2:
case Error::NoWebview2: { return tr::lng_payments_webview_install_edge(
rich.append(tr::lng_payments_webview_install_edge(
tr::now, tr::now,
lt_link, lt_link,
Text::Link( Text::Link(
"Microsoft Edge WebView2 Runtime", "Microsoft Edge WebView2 Runtime",
"https://go.microsoft.com/fwlink/p/?LinkId=2124703"), "https://go.microsoft.com/fwlink/p/?LinkId=2124703"),
Ui::Text::WithEntities)); Ui::Text::WithEntities);
} break;
case Error::NoWebKitGTK: case Error::NoWebKitGTK:
rich.append(tr::lng_payments_webview_install_webkit(tr::now)); return { tr::lng_payments_webview_install_webkit(tr::now) };
break; case Error::OldWindows:
return { tr::lng_payments_webview_update_windows(tr::now) };
default: default:
rich.append(QString::fromStdString(information.details)); return { QString::fromStdString(info.details) };
break;
} }
showCriticalError(rich); }
void Panel::showWebviewError(
const QString &text,
const Webview::Available &information) {
showCriticalError(TextWithEntities{ text }.append(
"\n\n"
).append(ErrorText(information)));
} }
rpl::lifetime &Panel::lifetime() { rpl::lifetime &Panel::lifetime() {

View file

@ -30,6 +30,8 @@ struct Available;
namespace Ui::BotWebView { namespace Ui::BotWebView {
[[nodiscard]] TextWithEntities ErrorText(const Webview::Available &info);
struct MainButtonArgs { struct MainButtonArgs {
bool isActive = false; bool isActive = false;
bool isVisible = false; bool isVisible = false;