From d4afba3a246cf8899008fb2897d5db7bc8275014 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 8 Jan 2022 07:41:44 +0300 Subject: [PATCH] Added ability to copy monospace text via click. --- Telegram/Resources/langs/lang.strings | 2 + .../SourceFiles/core/click_handler_types.cpp | 40 +++++++++++++++++++ .../SourceFiles/core/click_handler_types.h | 17 ++++++++ Telegram/SourceFiles/core/ui_integration.cpp | 5 +++ 4 files changed, 64 insertions(+) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index b01c293ec4..0488dd517e 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1972,6 +1972,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_formatting_link_url" = "URL"; "lng_formatting_link_create" = "Create"; +"lng_text_copied" = "Text copied to clipboard."; + "lng_spellchecker_submenu" = "Spelling"; "lng_spellchecker_add" = "Add to Dictionary"; "lng_spellchecker_remove" = "Remove from Dictionary"; diff --git a/Telegram/SourceFiles/core/click_handler_types.cpp b/Telegram/SourceFiles/core/click_handler_types.cpp index 151b070854..e920e8eb1f 100644 --- a/Telegram/SourceFiles/core/click_handler_types.cpp +++ b/Telegram/SourceFiles/core/click_handler_types.cpp @@ -14,6 +14,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwindow.h" #include "main/main_session.h" #include "ui/boxes/confirm_box.h" +#include "ui/text/text_entity.h" +#include "ui/toast/toast.h" #include "base/qthelp_regex.h" #include "storage/storage_account.h" #include "history/history.h" @@ -266,3 +268,41 @@ void BotCommandClickHandler::onClick(ClickContext context) const { auto BotCommandClickHandler::getTextEntity() const -> TextEntity { return { EntityType::BotCommand }; } + +MonospaceClickHandler::MonospaceClickHandler( + const QString &text, + EntityType type) +: _text(text) +, _entity({ type }) { +} + +void MonospaceClickHandler::onClick(ClickContext context) const { + const auto button = context.button; + if (button != Qt::LeftButton && button != Qt::MiddleButton) { + return; + } + const auto my = context.other.value(); + if (const auto controller = my.sessionWindow.get()) { + auto &data = controller->session().data(); + const auto item = data.message(my.itemId); + const auto hasCopyRestriction = item + && (!item->history()->peer->allowsForwarding() + || item->forbidsForward()); + if (hasCopyRestriction) { + Ui::Toast::Show(item->history()->peer->isBroadcast() + ? tr::lng_error_nocopy_channel(tr::now) + : tr::lng_error_nocopy_group(tr::now)); + return; + } + } + Ui::Toast::Show(tr::lng_text_copied(tr::now)); + TextUtilities::SetClipboardText(TextForMimeData::Simple(_text.trimmed())); +} + +auto MonospaceClickHandler::getTextEntity() const -> TextEntity { + return _entity; +} + +QString MonospaceClickHandler::url() const { + return _text; +} diff --git a/Telegram/SourceFiles/core/click_handler_types.h b/Telegram/SourceFiles/core/click_handler_types.h index 6bdb282f9c..55b99eea19 100644 --- a/Telegram/SourceFiles/core/click_handler_types.h +++ b/Telegram/SourceFiles/core/click_handler_types.h @@ -195,3 +195,20 @@ private: QString _cmd; }; + +class MonospaceClickHandler : public TextClickHandler { +public: + MonospaceClickHandler(const QString &text, EntityType type); + + void onClick(ClickContext context) const override; + + TextEntity getTextEntity() const override; + +protected: + QString url() const override; + +private: + const QString _text; + const TextEntity _entity; + +}; diff --git a/Telegram/SourceFiles/core/ui_integration.cpp b/Telegram/SourceFiles/core/ui_integration.cpp index 96f45d1473..5c40e6a8d6 100644 --- a/Telegram/SourceFiles/core/ui_integration.cpp +++ b/Telegram/SourceFiles/core/ui_integration.cpp @@ -202,6 +202,11 @@ std::shared_ptr UiIntegration::createLinkHandler( LOG(("Bad mention name: %1").arg(data.data)); } } break; + + case EntityType::Code: + return std::make_shared(data.text, data.type); + case EntityType::Pre: + return std::make_shared(data.text, data.type); } return Integration::createLinkHandler(data, context); }