Added ability to copy monospace text via click.

This commit is contained in:
23rd 2022-01-08 07:41:44 +03:00 committed by John Preston
parent 4ee9751feb
commit d4afba3a24
4 changed files with 64 additions and 0 deletions

View File

@ -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";

View File

@ -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<ClickHandlerContext>();
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;
}

View File

@ -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;
};

View File

@ -202,6 +202,11 @@ std::shared_ptr<ClickHandler> UiIntegration::createLinkHandler(
LOG(("Bad mention name: %1").arg(data.data));
}
} break;
case EntityType::Code:
return std::make_shared<MonospaceClickHandler>(data.text, data.type);
case EntityType::Pre:
return std::make_shared<MonospaceClickHandler>(data.text, data.type);
}
return Integration::createLinkHandler(data, context);
}