From 92333e982ce60eecf8e3915fd2b172a660a00c04 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 18 Dec 2017 19:44:50 +0400 Subject: [PATCH] Move message components to history_item_components. Also fix channel signatures rendering. --- Telegram/SourceFiles/apiwrap.cpp | 1 + Telegram/SourceFiles/app.cpp | 1 + Telegram/SourceFiles/auth_session.cpp | 1 + .../SourceFiles/chat_helpers/bot_keyboard.cpp | 118 ++- .../SourceFiles/chat_helpers/bot_keyboard.h | 30 +- Telegram/SourceFiles/facades.cpp | 16 +- Telegram/SourceFiles/facades.h | 16 +- Telegram/SourceFiles/history/history.cpp | 3 +- .../history/history_admin_log_inner.cpp | 11 +- .../history/history_inner_widget.cpp | 18 +- Telegram/SourceFiles/history/history_item.cpp | 683 +++----------- Telegram/SourceFiles/history/history_item.h | 497 +--------- .../history/history_item_components.cpp | 879 ++++++++++++++++++ .../history/history_item_components.h | 474 ++++++++++ Telegram/SourceFiles/history/history_media.h | 2 + .../history/history_media_grouped.cpp | 1 + .../history/history_media_types.cpp | 100 +- .../SourceFiles/history/history_media_types.h | 112 +-- .../SourceFiles/history/history_message.cpp | 489 ++++------ .../SourceFiles/history/history_message.h | 66 +- .../SourceFiles/history/history_service.cpp | 1 + .../SourceFiles/history/history_widget.cpp | 17 +- Telegram/SourceFiles/history/history_widget.h | 7 +- Telegram/SourceFiles/mainwidget.cpp | 8 +- Telegram/SourceFiles/mainwidget.h | 22 +- .../SourceFiles/overview/overview_layout.cpp | 3 +- .../window/notifications_manager.cpp | 1 + Telegram/gyp/telegram_sources.txt | 2 + 28 files changed, 1941 insertions(+), 1638 deletions(-) create mode 100644 Telegram/SourceFiles/history/history_item_components.cpp create mode 100644 Telegram/SourceFiles/history/history_item_components.h diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index ff3c6205ea..aa7e148f47 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -33,6 +33,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "mainwidget.h" #include "boxes/add_contact_box.h" #include "history/history_message.h" +#include "history/history_item_components.h" #include "storage/localstorage.h" #include "auth_session.h" #include "boxes/confirm_box.h" diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index eb940db99a..24dd7a3891 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -34,6 +34,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "history/history_service_layout.h" #include "history/history_location_manager.h" #include "history/history_media_types.h" +#include "history/history_item_components.h" #include "media/media_audio.h" #include "inline_bots/inline_bot_layout_item.h" #include "messenger.h" diff --git a/Telegram/SourceFiles/auth_session.cpp b/Telegram/SourceFiles/auth_session.cpp index 643a64d8d6..da9eeb9578 100644 --- a/Telegram/SourceFiles/auth_session.cpp +++ b/Telegram/SourceFiles/auth_session.cpp @@ -27,6 +27,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "storage/localstorage.h" #include "storage/storage_facade.h" #include "storage/serialize_common.h" +#include "history/history_item_components.h" #include "window/notifications_manager.h" #include "platform/platform_specific.h" #include "calls/calls_instance.h" diff --git a/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp b/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp index c5a468f8ad..c7c0e6540f 100644 --- a/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp +++ b/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp @@ -20,9 +20,91 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ #include "chat_helpers/bot_keyboard.h" +#include "history/history_item_components.h" #include "styles/style_widgets.h" #include "styles/style_history.h" +namespace { + +class Style : public ReplyKeyboard::Style { +public: + Style( + not_null parent, + const style::BotKeyboardButton &st); + + int buttonRadius() const override; + + void startPaint(Painter &p) const override; + const style::TextStyle &textStyle() const override; + void repaint(not_null item) const override; + +protected: + void paintButtonBg( + Painter &p, + const QRect &rect, + float64 howMuchOver) const override; + void paintButtonIcon( + Painter &p, + const QRect &rect, + int outerWidth, + HistoryMessageMarkupButton::Type type) const override; + void paintButtonLoading(Painter &p, const QRect &rect) const override; + int minButtonWidth(HistoryMessageMarkupButton::Type type) const override; + +private: + not_null _parent; + +}; + +Style::Style( + not_null parent, + const style::BotKeyboardButton &st) +: ReplyKeyboard::Style(st), _parent(parent) { +} + +void Style::startPaint(Painter &p) const { + p.setPen(st::botKbColor); + p.setFont(st::botKbStyle.font); +} + +const style::TextStyle &Style::textStyle() const { + return st::botKbStyle; +} + +void Style::repaint(not_null item) const { + _parent->update(); +} + +int Style::buttonRadius() const { + return st::buttonRadius; +} + +void Style::paintButtonBg( + Painter &p, + const QRect &rect, + float64 howMuchOver) const { + App::roundRect(p, rect, st::botKbBg, BotKeyboardCorners); +} + +void Style::paintButtonIcon( + Painter &p, + const QRect &rect, + int outerWidth, + HistoryMessageMarkupButton::Type type) const { + // Buttons with icons should not appear here. +} + +void Style::paintButtonLoading(Painter &p, const QRect &rect) const { + // Buttons with loading progress should not appear here. +} + +int Style::minButtonWidth(HistoryMessageMarkupButton::Type type) const { + int result = 2 * buttonPadding(); + return result; +} + +} // namespace + BotKeyboard::BotKeyboard(QWidget *parent) : TWidget(parent) , _st(&st::botKbButton) { setGeometry(0, 0, _st->margin, st::botKbScroll.deltat); @@ -43,40 +125,6 @@ void BotKeyboard::paintEvent(QPaintEvent *e) { } } -void BotKeyboard::Style::startPaint(Painter &p) const { - p.setPen(st::botKbColor); - p.setFont(st::botKbStyle.font); -} - -const style::TextStyle &BotKeyboard::Style::textStyle() const { - return st::botKbStyle; -} - -void BotKeyboard::Style::repaint(not_null item) const { - _parent->update(); -} - -int BotKeyboard::Style::buttonRadius() const { - return st::buttonRadius; -} - -void BotKeyboard::Style::paintButtonBg(Painter &p, const QRect &rect, float64 howMuchOver) const { - App::roundRect(p, rect, st::botKbBg, BotKeyboardCorners); -} - -void BotKeyboard::Style::paintButtonIcon(Painter &p, const QRect &rect, int outerWidth, HistoryMessageReplyMarkup::Button::Type type) const { - // Buttons with icons should not appear here. -} - -void BotKeyboard::Style::paintButtonLoading(Painter &p, const QRect &rect) const { - // Buttons with loading progress should not appear here. -} - -int BotKeyboard::Style::minButtonWidth(HistoryMessageReplyMarkup::Button::Type type) const { - int result = 2 * buttonPadding(); - return result; -} - void BotKeyboard::mousePressEvent(QMouseEvent *e) { _lastMousePos = e->globalPos(); updateSelected(); @@ -250,3 +298,5 @@ void BotKeyboard::updateSelected() { setCursor(link ? style::cur_pointer : style::cur_default); } } + +BotKeyboard::~BotKeyboard() = default; diff --git a/Telegram/SourceFiles/chat_helpers/bot_keyboard.h b/Telegram/SourceFiles/chat_helpers/bot_keyboard.h index a307318251..863ec9c0ea 100644 --- a/Telegram/SourceFiles/chat_helpers/bot_keyboard.h +++ b/Telegram/SourceFiles/chat_helpers/bot_keyboard.h @@ -22,7 +22,12 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "ui/widgets/tooltip.h" -class BotKeyboard : public TWidget, public Ui::AbstractTooltipShower, public ClickHandlerHost { +class ReplyKeyboard; + +class BotKeyboard + : public TWidget + , public Ui::AbstractTooltipShower + , public ClickHandlerHost { Q_OBJECT public: @@ -57,6 +62,8 @@ public: void clickHandlerActiveChanged(const ClickHandlerPtr &p, bool active) override; void clickHandlerPressedChanged(const ClickHandlerPtr &p, bool pressed) override; + ~BotKeyboard(); + protected: int resizeGetHeight(int newWidth) override; @@ -83,27 +90,6 @@ private: QPoint _lastMousePos; std::unique_ptr _impl; - class Style : public ReplyKeyboard::Style { - public: - Style(BotKeyboard *parent, const style::BotKeyboardButton &st) : ReplyKeyboard::Style(st), _parent(parent) { - } - - int buttonRadius() const override; - - void startPaint(Painter &p) const override; - const style::TextStyle &textStyle() const override; - void repaint(not_null item) const override; - - protected: - void paintButtonBg(Painter &p, const QRect &rect, float64 howMuchOver) const override; - void paintButtonIcon(Painter &p, const QRect &rect, int outerWidth, HistoryMessageReplyMarkup::Button::Type type) const override; - void paintButtonLoading(Painter &p, const QRect &rect) const override; - int minButtonWidth(HistoryMessageReplyMarkup::Button::Type type) const override; - - private: - BotKeyboard *_parent; - - }; const style::BotKeyboardButton *_st = nullptr; }; diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index e1ca65e657..fd457e6880 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -24,6 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "core/click_handler_types.h" #include "media/media_clip_reader.h" #include "window/window_controller.h" +#include "history/history_item_components.h" #include "observer_peer.h" #include "mainwindow.h" #include "mainwidget.h" @@ -69,19 +70,22 @@ bool insertBotCommand(const QString &cmd) { return false; } -void activateBotCommand(const HistoryItem *msg, int row, int col) { - const HistoryMessageReplyMarkup::Button *button = nullptr; +void activateBotCommand( + not_null msg, + int row, + int column) { + const HistoryMessageMarkupButton *button = nullptr; if (auto markup = msg->Get()) { if (row < markup->rows.size()) { auto &buttonRow = markup->rows[row]; - if (col < buttonRow.size()) { - button = &buttonRow.at(col); + if (column < buttonRow.size()) { + button = &buttonRow[column]; } } } if (!button) return; - using ButtonType = HistoryMessageReplyMarkup::Button::Type; + using ButtonType = HistoryMessageMarkupButton::Type; switch (button->type) { case ButtonType::Default: { // Copy string before passing it to the sending method @@ -93,7 +97,7 @@ void activateBotCommand(const HistoryItem *msg, int row, int col) { case ButtonType::Callback: case ButtonType::Game: { if (auto m = main()) { - m->app_sendBotCallback(button, msg, row, col); + m->app_sendBotCallback(button, msg, row, column); } } break; diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index bd9569313c..5c20815785 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -130,11 +130,21 @@ inline auto LambdaDelayedOnce( }; } -void sendBotCommand(PeerData *peer, UserData *bot, const QString &cmd, MsgId replyTo = 0); +void sendBotCommand( + PeerData *peer, + UserData *bot, + const QString &cmd, + MsgId replyTo = 0); bool insertBotCommand(const QString &cmd); -void activateBotCommand(const HistoryItem *msg, int row, int col); +void activateBotCommand( + not_null msg, + int row, + int column); void searchByHashtag(const QString &tag, PeerData *inPeer); -void openPeerByName(const QString &username, MsgId msgId = ShowAtUnreadMsgId, const QString &startToken = QString()); +void openPeerByName( + const QString &username, + MsgId msgId = ShowAtUnreadMsgId, + const QString &startToken = QString()); void joinGroupByHash(const QString &hash); void removeDialog(History *history); void showSettings(); diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 8527b05bf6..79b4c891d3 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -23,6 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "history/history_message.h" #include "history/history_media_types.h" #include "history/history_service.h" +#include "history/history_item_components.h" #include "dialogs/dialogs_indexed_list.h" #include "styles/style_dialogs.h" #include "data/data_drafts.h" @@ -679,7 +680,7 @@ void checkForSwitchInlineButton(HistoryItem *item) { if (auto markup = item->Get()) { for_const (auto &row, markup->rows) { for_const (auto &button, row) { - if (button.type == HistoryMessageReplyMarkup::Button::Type::SwitchInline) { + if (button.type == HistoryMessageMarkupButton::Type::SwitchInline) { Notify::switchInlineBotButtonReceived(QString::fromUtf8(button.data)); return; } diff --git a/Telegram/SourceFiles/history/history_admin_log_inner.cpp b/Telegram/SourceFiles/history/history_admin_log_inner.cpp index 7f9b250e5e..9bd9b59747 100644 --- a/Telegram/SourceFiles/history/history_admin_log_inner.cpp +++ b/Telegram/SourceFiles/history/history_admin_log_inner.cpp @@ -26,6 +26,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "history/history_service_layout.h" #include "history/history_admin_log_section.h" #include "history/history_admin_log_filter.h" +#include "history/history_item_components.h" #include "chat_helpers/message_field.h" #include "mainwindow.h" #include "mainwidget.h" @@ -424,17 +425,17 @@ void InnerWidget::updateEmptyText() { QString InnerWidget::tooltipText() const { if (_mouseCursorState == HistoryInDateCursorState && _mouseAction == MouseAction::None) { - if (auto item = App::hoveredItem()) { + if (const auto item = App::hoveredItem()) { auto dateText = item->date.toString(QLocale::system().dateTimeFormat(QLocale::LongFormat)); return dateText; } } else if (_mouseCursorState == HistoryInForwardedCursorState && _mouseAction == MouseAction::None) { - if (auto item = App::hoveredItem()) { - if (auto forwarded = item->Get()) { - return forwarded->_text.originalText(AllTextSelection, ExpandLinksNone); + if (const auto item = App::hoveredItem()) { + if (const auto forwarded = item->Get()) { + return forwarded->text.originalText(AllTextSelection, ExpandLinksNone); } } - } else if (auto lnk = ClickHandler::getActive()) { + } else if (const auto lnk = ClickHandler::getActive()) { return lnk->tooltip(); } return QString(); diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index b98f47e6f3..fd41716ca9 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -26,6 +26,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "history/history_message.h" #include "history/history_service_layout.h" #include "history/history_media_types.h" +#include "history/history_item_components.h" #include "ui/widgets/popup_menu.h" #include "window/window_controller.h" #include "window/window_peer_menu.h" @@ -2774,21 +2775,22 @@ void HistoryInner::applyDragSelection( QString HistoryInner::tooltipText() const { if (_mouseCursorState == HistoryInDateCursorState && _mouseAction == MouseAction::None) { - if (App::hoveredItem()) { - auto dateText = App::hoveredItem()->date.toString(QLocale::system().dateTimeFormat(QLocale::LongFormat)); - auto editedDate = App::hoveredItem()->displayedEditDate(); + if (const auto item = App::hoveredItem()) { + auto dateText = item->date.toString( + QLocale::system().dateTimeFormat(QLocale::LongFormat)); + auto editedDate = item->displayedEditDate(); if (!editedDate.isNull()) { dateText += '\n' + lng_edited_date(lt_date, editedDate.toString(QLocale::system().dateTimeFormat(QLocale::LongFormat))); } - if (auto forwarded = App::hoveredItem()->Get()) { - dateText += '\n' + lng_forwarded_date(lt_date, forwarded->_originalDate.toString(QLocale::system().dateTimeFormat(QLocale::LongFormat))); + if (const auto forwarded = item->Get()) { + dateText += '\n' + lng_forwarded_date(lt_date, forwarded->originalDate.toString(QLocale::system().dateTimeFormat(QLocale::LongFormat))); } return dateText; } } else if (_mouseCursorState == HistoryInForwardedCursorState && _mouseAction == MouseAction::None) { - if (App::hoveredItem()) { - if (auto forwarded = App::hoveredItem()->Get()) { - return forwarded->_text.originalText(AllTextSelection, ExpandLinksNone); + if (const auto item = App::hoveredItem()) { + if (const auto forwarded = item->Get()) { + return forwarded->text.originalText(AllTextSelection, ExpandLinksNone); } } } else if (auto lnk = ClickHandler::getActive()) { diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 0098d9b343..ff357b21cc 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -22,6 +22,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #include "lang/lang_keys.h" #include "mainwidget.h" +#include "history/history_item_components.h" #include "history/history_service_layout.h" #include "history/history_media_types.h" #include "history/history_media_grouped.h" @@ -69,566 +70,6 @@ HistoryTextState::HistoryTextState( , link(link) { } -ReplyMarkupClickHandler::ReplyMarkupClickHandler( - int row, - int column, - FullMsgId context) -: _itemId(context) -, _row(row) -, _column(column) { -} - -// Copy to clipboard support. -void ReplyMarkupClickHandler::copyToClipboard() const { - if (auto button = getButton()) { - if (button->type == HistoryMessageReplyMarkup::Button::Type::Url) { - auto url = QString::fromUtf8(button->data); - if (!url.isEmpty()) { - QApplication::clipboard()->setText(url); - } - } - } -} - -QString ReplyMarkupClickHandler::copyToClipboardContextItemText() const { - if (auto button = getButton()) { - if (button->type == HistoryMessageReplyMarkup::Button::Type::Url) { - return lang(lng_context_copy_link); - } - } - return QString(); -} - -// Finds the corresponding button in the items markup struct. -// If the button is not found it returns nullptr. -// Note: it is possible that we will point to the different button -// than the one was used when constructing the handler, but not a big deal. -const HistoryMessageReplyMarkup::Button *ReplyMarkupClickHandler::getButton() const { - if (auto item = App::histItemById(_itemId)) { - if (auto markup = item->Get()) { - if (_row < markup->rows.size()) { - auto &row = markup->rows[_row]; - if (_column < row.size()) { - return &row[_column]; - } - } - } - } - return nullptr; -} - -void ReplyMarkupClickHandler::onClickImpl() const { - if (auto item = App::histItemById(_itemId)) { - App::activateBotCommand(item, _row, _column); - } -} - -// Returns the full text of the corresponding button. -QString ReplyMarkupClickHandler::buttonText() const { - if (auto button = getButton()) { - return button->text; - } - return QString(); -} - -ReplyKeyboard::Button::Button() = default; -ReplyKeyboard::Button::Button(Button &&other) = default; -ReplyKeyboard::Button &ReplyKeyboard::Button::operator=( - Button &&other) = default; -ReplyKeyboard::Button::~Button() = default; - -ReplyKeyboard::ReplyKeyboard( - not_null item, - std::unique_ptr