[Option][GUI] Show chat ID

This commit is contained in:
Eric Kotato 2022-08-26 21:59:40 +03:00 committed by Eric Kotato
parent 1dc16bd42a
commit 62de69cbbc
7 changed files with 173 additions and 0 deletions

View file

@ -43,10 +43,26 @@
"ktg_settings_network": "Network",
"ktg_settings_system": "System",
"ktg_settings_other": "Other",
"ktg_profile_copy_id": "Copy ID",
"ktg_profile_bot_id": "Bot ID",
"ktg_profile_user_id": "User ID",
"ktg_profile_group_id": "Group ID",
"ktg_profile_supergroup_id": "Supergroup ID",
"ktg_profile_channel_id": "Channel ID",
"ktg_settings_adaptive_bubbles": "Adaptive bubbles",
"ktg_settings_filters": "Folders",
"ktg_settings_messages": "Messages",
"ktg_settings_chat_id": "Chat ID in profile",
"ktg_settings_chat_id_desc": "You can choose desired format here.\n\nTelegram API uses IDs as-is, but Bot API adds minus in the beginning for groups, and -100 for channels and supergroups to fit it in one field.\n\nIf you have profile panel opened, re-open it to see changes.",
"ktg_settings_chat_id_disable": "Hide",
"ktg_settings_chat_id_telegram": "Telegram API",
"ktg_settings_chat_id_bot": "Bot API",
"ktg_settings_monospace_large_bubbles": "Expand bubbles with monospace",
"ktg_bot_id_copied": "Bot ID copied to clipboard.",
"ktg_user_id_copied": "User ID copied to clipboard.",
"ktg_group_id_copied": "Group ID copied to clipboard.",
"ktg_supergroup_id_copied": "Supergroup ID copied to clipboard.",
"ktg_channel_id_copied": "Channel ID copied to clipboard.",
"ktg_settings_forward": "Forward",
"ktg_in_app_update_disabled": "In-app updater is disabled.",
"dummy_last_string": ""

View file

@ -1010,6 +1010,11 @@ infoLabeledOneLine: FlatLabel(defaultFlatLabel) {
}
margin: margins(5px, 5px, 5px, 5px);
}
infoLabeledOneLineInline: FlatLabel(infoLabeledOneLine) {
palette: TextPalette(defaultTextPalette) {
linkFg: windowFg;
}
}
infoLabelSkip: 2px;
infoLabeled: FlatLabel(infoLabeledOneLine) {
minWidth: 180px;

View file

@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "info/profile/info_profile_actions.h"
#include "kotato/kotato_lang.h"
#include "kotato/kotato_settings.h"
#include "api/api_blocked_peers.h"
#include "api/api_chat_participants.h"
#include "apiwrap.h"
@ -1030,8 +1032,44 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
result.text->setContextCopyText(contextCopyText);
return result;
};
auto addInfoOneLineInline = [&](
rpl::producer<QString> &&label,
rpl::producer<TextWithEntities> &&text,
const QString &contextCopyText) {
auto result = addInfoLine(
std::move(label),
std::move(text),
st::infoLabeledOneLineInline);
result->setContextCopyText(contextCopyText);
return result;
};
if (const auto user = _peer->asUser()) {
const auto controller = _controller->parentController();
if (::Kotato::JsonSettings::GetInt("show_chat_id") != 0) {
auto idDrawableText = IDValue(
user
) | rpl::map([](TextWithEntities &&text) {
return Ui::Text::Link(text.text);
});
auto idInfo = addInfoOneLineInline(
(user->isBot()
? rktr("ktg_profile_bot_id")
: rktr("ktg_profile_user_id")),
std::move(idDrawableText),
ktr("ktg_profile_copy_id"));
idInfo->setClickHandlerFilter([user](auto&&...) {
const auto idText = IDString(user);
if (!idText.isEmpty()) {
QGuiApplication::clipboard()->setText(idText);
Ui::Toast::Show(user->isBot()
? ktr("ktg_bot_id_copied")
: ktr("ktg_user_id_copied"));
}
return false;
});
}
if (user->session().supportMode()) {
addInfoLineGeneric(
user->session().supportHelper().infoLabelValue(user),
@ -1151,6 +1189,35 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
[=] { controller->window().show(Box(EditContactBox, controller, user)); },
tracker);
} else {
if (::Kotato::JsonSettings::GetInt("show_chat_id") != 0) {
auto idDrawableText = IDValue(
_peer
) | rpl::map([](TextWithEntities &&text) {
return Ui::Text::Link(text.text);
});
auto idInfo = addInfoOneLineInline(
(_peer->isChat()
? rktr("ktg_profile_group_id")
: _peer->isMegagroup()
? rktr("ktg_profile_supergroup_id")
: rktr("ktg_profile_channel_id")),
std::move(idDrawableText),
ktr("ktg_profile_copy_id"));
idInfo->setClickHandlerFilter([peer = _peer](auto&&...) {
const auto idText = IDString(peer);
if (!idText.isEmpty()) {
QGuiApplication::clipboard()->setText(idText);
Ui::Toast::Show(peer->isChat()
? ktr("ktg_group_id_copied")
: peer->isMegagroup()
? ktr("ktg_supergroup_id_copied")
: ktr("ktg_channel_id_copied"));
}
return false;
});
}
const auto topicRootId = _topic ? _topic->rootId() : 0;
const auto addToLink = topicRootId
? ('/' + QString::number(topicRootId.bare))

View file

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "info/profile/info_profile_values.h"
#include "kotato/kotato_settings.h"
#include "api/api_chat_participants.h"
#include "apiwrap.h"
#include "info/profile/info_profile_phone_menu.h"
@ -40,6 +41,8 @@ namespace Info {
namespace Profile {
namespace {
constexpr auto kMaxChannelId = -1000000000000;
using UpdateFlag = Data::PeerUpdate::Flag;
auto PlainAboutValue(not_null<PeerData*> peer) {
@ -92,6 +95,30 @@ void StripExternalLinks(TextWithEntities &text) {
} // namespace
QString IDString(not_null<PeerData*> peer) {
auto resultId = QString::number(peerIsUser(peer->id)
? peerToUser(peer->id).bare
: peerIsChat(peer->id)
? peerToChat(peer->id).bare
: peerIsChannel(peer->id)
? peerToChannel(peer->id).bare
: peer->id.value);
if (::Kotato::JsonSettings::GetInt("show_chat_id") == 2) {
if (peer->isChannel()) {
resultId = QString::number(peerToChannel(peer->id).bare - kMaxChannelId).prepend("-");
} else if (peer->isChat()) {
resultId = resultId.prepend("-");
}
}
return resultId;
}
rpl::producer<TextWithEntities> IDValue(not_null<PeerData*> peer) {
return rpl::single(IDString(peer)) | Ui::Text::ToWithEntities();
}
rpl::producer<QString> NameValue(not_null<PeerData*> peer) {
return peer->session().changes().peerFlagsValue(
peer,

View file

@ -45,6 +45,10 @@ inline auto ToSingleLine() {
rpl::producer<not_null<PeerData*>> MigratedOrMeValue(
not_null<PeerData*> peer);
QString IDString(not_null<PeerData*> peer);
[[nodiscard]] rpl::producer<TextWithEntities> IDValue(
not_null<PeerData*> peer);
[[nodiscard]] rpl::producer<QString> NameValue(not_null<PeerData*> peer);
[[nodiscard]] rpl::producer<QString> TitleValue(
not_null<Data::ForumTopic*> topic);

View file

@ -245,6 +245,10 @@ const std::map<QString, Definition, std::greater<QString>> DefinitionMap {
{ "always_show_scheduled", {
.type = SettingType::BoolSetting,
.defaultValue = false, }},
{ "show_chat_id", {
.type = SettingType::IntSetting,
.defaultValue = 2,
.limitHandler = IntLimit(0, 2, 2), }},
};
using OldOptionKey = QString;

View file

@ -49,6 +49,23 @@ namespace Settings {
namespace {
QString ChatIdLabel(int option) {
switch (option) {
case 0:
return ktr("ktg_settings_chat_id_disable");
case 1:
return ktr("ktg_settings_chat_id_telegram");
case 2:
return ktr("ktg_settings_chat_id_bot");
default:
Unexpected("Option in Settings::ChatIdLabel.");
}
return QString();
}
} // namespace
#define SettingsMenuJsonSwitch(LangKey, Option) container->add(object_ptr<Button>( \
@ -228,6 +245,39 @@ void SetupKotatoOther(
Ui::AddSkip(container);
Ui::AddSubsectionTitle(container, rktr("ktg_settings_other"));
const auto chatIdButton = container->add(
object_ptr<Button>(
container,
rktr("ktg_settings_chat_id"),
st::settingsButtonNoIcon));
auto chatIdText = rpl::single(
ChatIdLabel(::Kotato::JsonSettings::GetInt("show_chat_id"))
) | rpl::then(
::Kotato::JsonSettings::Events(
"show_chat_id"
) | rpl::map([] {
return ChatIdLabel(::Kotato::JsonSettings::GetInt("show_chat_id"));
})
);
CreateRightLabel(
chatIdButton,
std::move(chatIdText),
st::settingsButtonNoIcon,
rktr("ktg_settings_chat_id"));
chatIdButton->addClickHandler([=] {
Ui::show(Box<::Kotato::RadioBox>(
ktr("ktg_settings_chat_id"),
ktr("ktg_settings_chat_id_desc"),
::Kotato::JsonSettings::GetInt("show_chat_id"),
3,
ChatIdLabel,
[=] (int value) {
::Kotato::JsonSettings::Set("show_chat_id", value);
::Kotato::JsonSettings::Write();
}));
});
Ui::AddSkip(container);
}