feat: rework copyUsernameAsLink

This commit is contained in:
AlexeyZavar 2024-04-18 22:08:42 +03:00
parent daec2545b4
commit a48534b51a
2 changed files with 27 additions and 4 deletions

View file

@ -127,6 +127,8 @@ base::options::toggle ShowPeerIdBelowAbout({
const QString &addToLink) { const QString &addToLink) {
const auto weak = base::make_weak(controller); const auto weak = base::make_weak(controller);
return [=](QString link) { return [=](QString link) {
auto settings = &AyuSettings::getInstance();
if (link.startsWith(u"internal:"_q)) { if (link.startsWith(u"internal:"_q)) {
Core::App().openInternalUrl(link, Core::App().openInternalUrl(link,
QVariant::fromValue(ClickHandlerContext{ QVariant::fromValue(ClickHandlerContext{
@ -138,9 +140,15 @@ base::options::toggle ShowPeerIdBelowAbout({
+ addToLink; + addToLink;
} }
if (!link.isEmpty()) { if (!link.isEmpty()) {
if (!settings->copyUsernameAsLink && addToLink.isEmpty()) {
link = '@' + link.replace("https://t.me/", "");
}
QGuiApplication::clipboard()->setText(link); QGuiApplication::clipboard()->setText(link);
if (const auto window = weak.get()) { if (const auto window = weak.get()) {
window->showToast(tr::lng_username_copied(tr::now)); window->showToast(settings->copyUsernameAsLink
? tr::lng_username_copied(tr::now) // "Link copied to clipboard."
: tr::lng_text_copied(tr::now)); // "Text copied to clipboard."
} }
} }
}; };

View file

@ -26,6 +26,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <QtCore/QRegularExpression> #include <QtCore/QRegularExpression>
#include <QtGui/QGuiApplication> #include <QtGui/QGuiApplication>
// AyuGram includes
#include "ayu/ayu_settings.h"
namespace Ui { namespace Ui {
namespace { namespace {
@ -188,11 +192,22 @@ void CollectibleInfoBox(
Ui::Text::Link(formatted), Ui::Text::Link(formatted),
Ui::Text::WithEntities); Ui::Text::WithEntities);
const auto copyCallback = [box, type, formatted, text = info.copyText] { const auto copyCallback = [box, type, formatted, text = info.copyText] {
QGuiApplication::clipboard()->setText( auto toCopy = text.isEmpty() ? formatted : text;
text.isEmpty() ? formatted : text); const auto settings = &AyuSettings::getInstance();
if (type == CollectibleType::Username) {
if (settings->copyUsernameAsLink && !text.startsWith("https://")) {
toCopy = "https://t.me/" + toCopy.replace("@", "");
} else if (!settings->copyUsernameAsLink && !text.startsWith("@")) {
toCopy = "@" + toCopy.replace("https://t.me/", "");
}
}
QGuiApplication::clipboard()->setText(toCopy);
box->uiShow()->showToast((type == CollectibleType::Phone) box->uiShow()->showToast((type == CollectibleType::Phone)
? tr::lng_text_copied(tr::now) ? tr::lng_text_copied(tr::now)
: tr::lng_username_copied(tr::now)); : settings->copyUsernameAsLink
? tr::lng_username_copied(tr::now) // "Link copied to clipboard."
: tr::lng_text_copied(tr::now)); // "Text copied to clipboard."
}; };
box->addRow( box->addRow(
object_ptr<Ui::FlatLabel>( object_ptr<Ui::FlatLabel>(