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/qt_signal_producer.h"
#include "base/qthelp_url.h"
#include "core/file_utilities.h"
#include "iv/iv_data.h"
#include "lang/lang_keys.h"
#include "ui/chat/attach/attach_bot_webview.h"
#include "ui/platform/ui_platform_window_title.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h"
@ -28,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/palette.h"
#include "styles/style_iv.h"
#include "styles/style_menu_icons.h"
#include "styles/style_payments.h" // paymentsCriticalError
#include "styles/style_widgets.h"
#include "styles/style_window.h"
@ -194,7 +197,7 @@ Controller::~Controller() {
_window->hide();
}
_ready = false;
_webview = nullptr;
base::take(_webview);
_back.destroy();
_forward.destroy();
_menu = nullptr;
@ -335,8 +338,6 @@ void Controller::showTonSite(
if (_webview && _webview->widget()) {
_webview->navigate(url);
activate();
} else {
_events.fire({ Event::Type::Close });
}
_url = url;
_subtitleText = _url.value(
@ -435,7 +436,7 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
window->lifetime().add([=] {
_ready = false;
_webview = nullptr;
base::take(_webview);
});
window->events(
@ -449,11 +450,32 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
}
}
}, 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(
) | 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());
raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) {
@ -600,11 +622,45 @@ void Controller::createWebview(const Webview::StorageId &storageId) {
_back->setAttribute(
Qt::WA_TransparentForMouseEvents,
!state.canGoBack);
_url = QString::fromStdString(state.url);
}, _webview->lifetime());
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(
const Webview::StorageId &storageId,
Prepared page) {

View file

@ -123,6 +123,9 @@ private:
void showShareMenu();
void destroyShareMenu();
void showWebviewError();
void showWebviewError(TextWithEntities text);
const not_null<Delegate*> _delegate;
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/wrap/fade_wrap.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/text_utilities.h"
#include "ui/effects/radial_animation.h"
@ -908,32 +909,9 @@ std::shared_ptr<Show> Panel::uiShow() {
void Panel::showWebviewError(
const QString &text,
const Webview::Available &information) {
using Error = Webview::Available::Error;
Expects(information.error != Error::None);
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);
showCriticalError(TextWithEntities{ text }.append(
"\n\n"
).append(BotWebView::ErrorText(information)));
}
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 "mainwindow.h"
#include "media/player/media_player_instance.h"
#include "webview/platform/win/webview_windows_edge_chromium.h"
#include "webview/webview_embed.h"
#include "window/main_window.h"
#include "window/window_peer_menu.h"
@ -150,7 +149,7 @@ void SetupExperimental(
addToggle(Media::Player::kOptionDisableAutoplayNext);
addToggle(kOptionSendLargePhotos);
addToggle(Webview::kOptionWebviewDebugEnabled);
addToggle(Webview::EdgeChromium::kOptionWebviewLegacyEdge);
addToggle(Webview::kOptionWebviewLegacyEdge);
addToggle(kOptionAutoScrollInactiveChat);
addToggle(Window::Notifications::kOptionGNotification);
addToggle(Core::kOptionFreeType);

View file

@ -1401,32 +1401,34 @@ if (window.TelegramGameProxy) {
)");
}
void Panel::showWebviewError(
const QString &text,
const Webview::Available &information) {
using Error = Webview::Available::Error;
Expects(information.error != Error::None);
TextWithEntities ErrorText(const Webview::Available &info) {
Expects(info.error != Webview::Available::Error::None);
auto rich = TextWithEntities{ text };
rich.append("\n\n");
switch (information.error) {
case Error::NoWebview2: {
rich.append(tr::lng_payments_webview_install_edge(
using Error = Webview::Available::Error;
switch (info.error) {
case Error::NoWebview2:
return 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;
Ui::Text::WithEntities);
case Error::NoWebKitGTK:
rich.append(tr::lng_payments_webview_install_webkit(tr::now));
break;
return { tr::lng_payments_webview_install_webkit(tr::now) };
case Error::OldWindows:
return { tr::lng_payments_webview_update_windows(tr::now) };
default:
rich.append(QString::fromStdString(information.details));
break;
return { QString::fromStdString(info.details) };
}
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() {

View file

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