tdesktop/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h

147 lines
3.9 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "base/object_ptr.h"
#include "base/weak_ptr.h"
#include "base/flags.h"
namespace Ui {
class BoxContent;
class RpWidget;
class SeparatePanel;
} // namespace Ui
namespace Webview {
struct Available;
struct ThemeParams;
} // namespace Webview
namespace Ui::BotWebView {
2022-04-04 11:28:38 +00:00
struct MainButtonArgs {
bool isActive = false;
2022-04-04 11:28:38 +00:00
bool isVisible = false;
bool isProgressVisible = false;
QString text;
};
enum class MenuButton {
None = 0x00,
Settings = 0x01,
OpenBot = 0x02,
RemoveFromMenu = 0x04,
};
inline constexpr bool is_flag_type(MenuButton) { return true; }
using MenuButtons = base::flags<MenuButton>;
class Panel final : public base::has_weak_ptr {
public:
2022-03-28 14:02:54 +00:00
Panel(
const QString &userDataPath,
2022-03-29 10:34:57 +00:00
rpl::producer<QString> title,
Fn<bool(QString)> handleLocalUri,
Fn<void(QString)> handleInvoice,
2022-03-28 14:02:54 +00:00
Fn<void(QByteArray)> sendData,
Fn<void()> close,
MenuButtons menuButtons,
Fn<void(MenuButton)> handleMenuButton,
Fn<Webview::ThemeParams()> themeParams);
~Panel();
void requestActivate();
void toggleProgress(bool shown);
bool showWebview(
const QString &url,
const Webview::ThemeParams &params,
rpl::producer<QString> bottomText);
void showBox(object_ptr<BoxContent> box);
void showToast(const TextWithEntities &text);
void showCriticalError(const TextWithEntities &text);
2022-03-29 14:14:11 +00:00
void showWebviewError(
const QString &text,
const Webview::Available &information);
void updateThemeParams(const Webview::ThemeParams &params);
void hideForPayment();
void invoiceClosed(const QString &slug, const QString &status);
[[nodiscard]] rpl::lifetime &lifetime();
private:
2022-04-04 11:28:38 +00:00
class Button;
struct Progress;
struct WebviewWithLifetime;
bool createWebview();
void showWebviewProgress();
void hideWebviewProgress();
void setTitle(rpl::producer<QString> title);
2022-07-07 04:39:33 +00:00
void sendDataMessage(const QJsonObject &args);
void processMainButtonMessage(const QJsonObject &args);
void processBackButtonMessage(const QJsonObject &args);
void openTgLink(const QJsonObject &args);
void openExternalLink(const QJsonObject &args);
void openInvoice(const QJsonObject &args);
void openPopup(const QJsonObject &args);
void setupClosingBehaviour(const QJsonObject &args);
2022-04-04 11:28:38 +00:00
void createMainButton();
2022-07-07 04:39:33 +00:00
void scheduleCloseWithConfirmation();
void closeWithConfirmation();
2022-04-04 11:28:38 +00:00
void postEvent(const QString &event, const QString &data = {});
[[nodiscard]] bool progressWithBackground() const;
[[nodiscard]] QRect progressRect() const;
void setupProgressGeometry();
QString _userDataPath;
Fn<bool(QString)> _handleLocalUri;
Fn<void(QString)> _handleInvoice;
2022-03-28 14:02:54 +00:00
Fn<void(QByteArray)> _sendData;
Fn<void()> _close;
2022-07-07 04:39:33 +00:00
bool _closeNeedConfirmation = false;
MenuButtons _menuButtons = {};
Fn<void(MenuButton)> _handleMenuButton;
std::unique_ptr<SeparatePanel> _widget;
std::unique_ptr<WebviewWithLifetime> _webview;
std::unique_ptr<RpWidget> _webviewBottom;
2022-04-04 11:28:38 +00:00
QPointer<QWidget> _webviewParent;
std::unique_ptr<Button> _mainButton;
2022-05-26 09:08:11 +00:00
crl::time _mainButtonLastClick = 0;
std::unique_ptr<Progress> _progress;
rpl::event_stream<> _themeUpdateForced;
2022-04-04 11:28:38 +00:00
rpl::lifetime _fgLifetime;
rpl::lifetime _bgLifetime;
bool _webviewProgress = false;
bool _themeUpdateScheduled = false;
bool _hiddenForPayment = false;
2022-07-07 04:39:33 +00:00
bool _closeWithConfirmationScheduled = false;
};
struct Args {
QString url;
QString userDataPath;
2022-03-29 10:34:57 +00:00
rpl::producer<QString> title;
rpl::producer<QString> bottom;
Fn<bool(QString)> handleLocalUri;
Fn<void(QString)> handleInvoice;
2022-03-28 14:02:54 +00:00
Fn<void(QByteArray)> sendData;
Fn<void()> close;
MenuButtons menuButtons;
Fn<void(MenuButton)> handleMenuButton;
Fn<Webview::ThemeParams()> themeParams;
};
[[nodiscard]] std::unique_ptr<Panel> Show(Args &&args);
} // namespace Ui::BotWebView