diff --git a/Telegram/Resources/icons/info/edit/dotsmini.png b/Telegram/Resources/icons/info/edit/dotsmini.png new file mode 100644 index 000000000..94bce645c Binary files /dev/null and b/Telegram/Resources/icons/info/edit/dotsmini.png differ diff --git a/Telegram/Resources/icons/info/edit/dotsmini@2x.png b/Telegram/Resources/icons/info/edit/dotsmini@2x.png new file mode 100644 index 000000000..92a9ae7bc Binary files /dev/null and b/Telegram/Resources/icons/info/edit/dotsmini@2x.png differ diff --git a/Telegram/Resources/icons/info/edit/dotsmini@3x.png b/Telegram/Resources/icons/info/edit/dotsmini@3x.png new file mode 100644 index 000000000..1e4188622 Binary files /dev/null and b/Telegram/Resources/icons/info/edit/dotsmini@3x.png differ diff --git a/Telegram/SourceFiles/api/api_invite_links.cpp b/Telegram/SourceFiles/api/api_invite_links.cpp index 8e05c1b59..3000fb7ef 100644 --- a/Telegram/SourceFiles/api/api_invite_links.cpp +++ b/Telegram/SourceFiles/api/api_invite_links.cpp @@ -254,12 +254,11 @@ void InviteLinks::setPermanent( not_null peer, const MTPExportedChatInvite &invite) { auto link = parse(peer, invite); - link.permanent = true; // #TODO links remove hack - //if (!link.permanent) { - // LOG(("API Error: " - // "InviteLinks::setPermanent called with non-permanent link.")); - // return; - //} + if (!link.permanent) { + LOG(("API Error: " + "InviteLinks::setPermanent called with non-permanent link.")); + return; + } auto i = _firstSlices.find(peer); if (i == end(_firstSlices)) { i = _firstSlices.emplace(peer).first; @@ -294,9 +293,10 @@ void InviteLinks::notify(not_null peer) { Data::PeerUpdate::Flag::InviteLinks); } -auto InviteLinks::links(not_null peer) const -> Links { +auto InviteLinks::links(not_null peer) const -> const Links & { + static const auto kEmpty = Links(); const auto i = _firstSlices.find(peer); - return (i != end(_firstSlices)) ? i->second : Links(); + return (i != end(_firstSlices)) ? i->second : kEmpty; } auto InviteLinks::parseSlice( diff --git a/Telegram/SourceFiles/api/api_invite_links.h b/Telegram/SourceFiles/api/api_invite_links.h index f2534f3d3..6c53c9fbb 100644 --- a/Telegram/SourceFiles/api/api_invite_links.h +++ b/Telegram/SourceFiles/api/api_invite_links.h @@ -57,7 +57,7 @@ public: void clearPermanent(not_null peer); void requestLinks(not_null peer); - [[nodiscard]] Links links(not_null peer) const; + [[nodiscard]] const Links &links(not_null peer) const; void requestMoreLinks( not_null peer, diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp new file mode 100644 index 000000000..03b58acdc --- /dev/null +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.cpp @@ -0,0 +1,91 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "boxes/peers/edit_peer_invite_links.h" + +#include "data/data_changes.h" +#include "data/data_peer.h" +#include "main/main_session.h" +#include "api/api_invite_links.h" +#include "ui/wrap/vertical_layout.h" +#include "ui/widgets/popup_menu.h" +#include "ui/controls/invite_link_label.h" +#include "ui/toast/toast.h" +#include "lang/lang_keys.h" +#include "apiwrap.h" +#include "styles/style_info.h" + +#include + +void AddPermanentLinkBlock( + not_null container, + not_null peer) { + const auto computePermanentLink = [=] { + const auto &links = peer->session().api().inviteLinks().links( + peer).links; + const auto link = links.empty() ? nullptr : &links.front(); + return (link && link->permanent && !link->revoked) ? link : nullptr; + }; + auto value = peer->session().changes().peerFlagsValue( + peer, + Data::PeerUpdate::Flag::InviteLinks + ) | rpl::map([=] { + const auto link = computePermanentLink(); + return link + ? std::make_tuple(link->link, link->usage) + : std::make_tuple(QString(), 0); + }) | rpl::start_spawning(container->lifetime()); + + const auto copyLink = [=] { + if (const auto link = computePermanentLink()) { + QGuiApplication::clipboard()->setText(link->link); + Ui::Toast::Show(tr::lng_group_invite_copied(tr::now)); + } + }; + const auto shareLink = [=] { + if (const auto link = computePermanentLink()) { + QGuiApplication::clipboard()->setText(link->link); + Ui::Toast::Show(tr::lng_group_invite_copied(tr::now)); + } + }; + const auto revokeLink = [=] { + if (const auto link = computePermanentLink()) { + QGuiApplication::clipboard()->setText(link->link); + Ui::Toast::Show(tr::lng_group_invite_copied(tr::now)); + } + }; + + auto link = rpl::duplicate( + value + ) | rpl::map([=](QString link, int usage) { + const auto prefix = qstr("https://"); + return link.startsWith(prefix) ? link.mid(prefix.size()) : link; + }); + const auto createMenu = [=] { + auto result = base::make_unique_q(container); + result->addAction( + tr::lng_group_invite_context_copy(tr::now), + copyLink); + result->addAction( + tr::lng_group_invite_context_share(tr::now), + shareLink); + result->addAction( + tr::lng_group_invite_context_revoke(tr::now), + revokeLink); + return result; + }; + const auto label = container->lifetime().make_state( + container, + std::move(link), + createMenu); + container->add( + label->take(), + st::inviteLinkFieldPadding); + + label->clicks( + ) | rpl::start_with_next(copyLink, label->lifetime()); +} diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.h b/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.h new file mode 100644 index 000000000..358f82f5f --- /dev/null +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_invite_links.h @@ -0,0 +1,18 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +class PeerData; + +namespace Ui { +class VerticalLayout; +} // namespace Ui + +void AddPermanentLinkBlock( + not_null container, + not_null peer); diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp index 170bb5dc9..117419c56 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_type_box.cpp @@ -14,6 +14,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/confirm_box.h" #include "boxes/peer_list_controllers.h" #include "boxes/peers/edit_participants_box.h" +#include "boxes/peers/edit_peer_info_box.h" // CreateButton. +#include "boxes/peers/edit_peer_invite_links.h" #include "chat_helpers/emoji_suggestions_widget.h" #include "core/application.h" #include "data/data_channel.h" @@ -32,15 +34,18 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/checkbox.h" #include "ui/widgets/input_fields.h" #include "ui/widgets/labels.h" +#include "ui/widgets/popup_menu.h" #include "ui/widgets/box_content_divider.h" #include "ui/wrap/padding_wrap.h" #include "ui/wrap/slide_wrap.h" #include "ui/wrap/vertical_layout.h" #include "ui/special_fields.h" #include "window/window_session_controller.h" +#include "settings/settings_common.h" #include "styles/style_layers.h" #include "styles/style_boxes.h" #include "styles/style_info.h" +#include "styles/style_settings.h" #include #include @@ -101,8 +106,6 @@ private: object_ptr createUsernameEdit(); object_ptr createInviteLinkBlock(); - void observeInviteLink(); - void privacyChanged(Privacy value); void checkUsernameAvailability(); @@ -114,8 +117,6 @@ private: rpl::producer &&text, not_null st); - bool canEditInviteLink() const; - void refreshInviteLinkBlock(); void createInviteLink(); void revokeInviteLink(const QString &link); @@ -178,6 +179,18 @@ void Controller::createContent() { _wrap->add(createInviteLinkBlock()); _wrap->add(createUsernameEdit()); + using namespace Settings; + AddSkip(_wrap.get()); + _wrap->add(EditPeerInfoBox::CreateButton( + _wrap.get(), + tr::lng_group_invite_manage(), + rpl::single(QString()), + [=] { /*ShowEditInviteLinks(_navigation, _peer);*/ }, + st::manageGroupButton, + &st::infoIconInviteLinks)); + AddSkip(_wrap.get()); + AddDividerText(_wrap.get(), tr::lng_group_invite_manage_about()); + if (_controls.privacy->value() == Privacy::NoUsername) { checkUsernameAvailability(); } @@ -292,21 +305,23 @@ object_ptr Controller::createUsernameEdit() { auto result = object_ptr>( _wrap, - object_ptr(_wrap), - st::editPeerUsernameMargins); + object_ptr(_wrap)); _controls.usernameWrap = result.data(); const auto container = result->entity(); - container->add(object_ptr>( - container, + + using namespace Settings; + AddSkip(container); + container->add( object_ptr( container, tr::lng_create_group_link(), - st::editPeerSectionLabel), - st::editPeerUsernameTitleLabelMargins)); + st::settingsSubsectionTitle), + st::settingsSubsectionTitlePadding); - const auto placeholder = container->add(object_ptr( - container)); + const auto placeholder = container->add( + object_ptr(container), + st::editPeerUsernameFieldMargins); placeholder->setAttribute(Qt::WA_TransparentForMouseEvents); _controls.usernameInput = Ui::AttachParentChild( container, @@ -328,13 +343,9 @@ object_ptr Controller::createUsernameEdit() { }, placeholder->lifetime()); _controls.usernameInput->move(placeholder->pos()); - container->add(object_ptr>( + AddDividerText( container, - object_ptr( - container, - tr::lng_create_channel_link_about(), - st::editPeerPrivacyLabel), - st::editPeerUsernameAboutLabelMargins)); + tr::lng_create_channel_link_about()); QObject::connect( _controls.usernameInput, @@ -348,6 +359,11 @@ object_ptr Controller::createUsernameEdit() { } void Controller::privacyChanged(Privacy value) { + const auto toggleInviteLink = [&] { + _controls.inviteLinkWrap->toggle( + (value != Privacy::HasUsername), + anim::type::instant); + }; const auto toggleEditUsername = [&] { _controls.usernameWrap->toggle( (value == Privacy::HasUsername), @@ -358,14 +374,14 @@ void Controller::privacyChanged(Privacy value) { // Otherwise box will change own Y position. if (value == Privacy::HasUsername) { - refreshInviteLinkBlock(); + toggleInviteLink(); toggleEditUsername(); _controls.usernameResult = nullptr; checkUsernameAvailability(); } else { toggleEditUsername(); - refreshInviteLinkBlock(); + toggleInviteLink(); } }; if (value == Privacy::HasUsername) { @@ -538,100 +554,38 @@ void Controller::revokeInviteLink(const QString &link) { Ui::show(std::move(box), Ui::LayerOption::KeepOther); } -bool Controller::canEditInviteLink() const { - if (const auto channel = _peer->asChannel()) { - return channel->canHaveInviteLink(); - } else if (const auto chat = _peer->asChat()) { - return chat->canHaveInviteLink(); - } - return false; -} - -void Controller::observeInviteLink() { - if (!_controls.inviteLinkWrap) { - return; - } - _peer->session().changes().peerFlagsValue( - _peer, - Data::PeerUpdate::Flag::InviteLinks - ) | rpl::start_with_next([=] { - refreshInviteLinkBlock(); - }, _controls.inviteLinkWrap->lifetime()); -} - object_ptr Controller::createInviteLinkBlock() { Expects(_wrap != nullptr); - if (!canEditInviteLink()) { - return nullptr; - } - auto result = object_ptr>( _wrap, - object_ptr(_wrap), - st::editPeerInvitesMargins); + object_ptr(_wrap)); _controls.inviteLinkWrap = result.data(); const auto container = result->entity(); - container->add(object_ptr( - container, - tr::lng_profile_invite_link_section(), - st::editPeerSectionLabel)); - container->add(object_ptr( - container, - st::editPeerInviteLinkBoxBottomSkip)); - _controls.inviteLink = container->add(object_ptr( - container, - st::editPeerInviteLink)); - _controls.inviteLink->setSelectable(true); - _controls.inviteLink->setContextCopyText(QString()); - _controls.inviteLink->setBreakEverywhere(true); - _controls.inviteLink->setClickHandlerFilter([=](auto&&...) { - QGuiApplication::clipboard()->setText(inviteLinkText()); - Ui::Toast::Show(tr::lng_group_invite_copied(tr::now)); - return false; - }); + using namespace Settings; + AddSkip(container); + container->add( + object_ptr( + container, + tr::lng_create_permanent_link_title(), + st::settingsSubsectionTitle), + st::settingsSubsectionTitlePadding); - container->add(object_ptr( - container, - st::editPeerInviteLinkSkip)); - container->add(object_ptr( - container, - tr::lng_group_invite_create_new(tr::now), - st::editPeerInviteLinkButton) - )->addClickHandler([=] { revokeInviteLink(inviteLinkText()); }); + AddPermanentLinkBlock(container, _peer); - observeInviteLink(); + AddSkip(container); + + AddDividerText( + container, + ((_peer->isMegagroup() || _peer->asChat()) + ? tr::lng_group_invite_about_permanent_group() + : tr::lng_group_invite_about_permanent_channel())); return result; } -void Controller::refreshInviteLinkBlock() { - const auto link = inviteLinkText(); - auto text = TextWithEntities(); - if (!link.isEmpty()) { - text.text = link; - const auto remove = qstr("https://"); - if (text.text.startsWith(remove)) { - text.text.remove(0, remove.size()); - } - text.entities.push_back({ - EntityType::CustomUrl, - 0, - text.text.size(), - link }); - } - _controls.inviteLink->setMarkedText(text); - - // Hack to expand FlatLabel width to naturalWidth again. - _controls.inviteLinkWrap->resizeToWidth(st::boxWideWidth); - - _controls.inviteLinkWrap->toggle( - inviteLinkShown() && !link.isEmpty(), - anim::type::instant); -} - bool Controller::inviteLinkShown() { return !_controls.privacy || (_controls.privacy->value() == Privacy::NoUsername); diff --git a/Telegram/SourceFiles/info/info.style b/Telegram/SourceFiles/info/info.style index 72601f186..5dce0cf3e 100644 --- a/Telegram/SourceFiles/info/info.style +++ b/Telegram/SourceFiles/info/info.style @@ -693,8 +693,8 @@ editPeerSectionLabel: FlatLabel(boxTitle) { linkFontOver: font(15px semibold underline); } } -editPeerUsernameTitleLabelMargins: margins(0px, 0px, 0px, 10px); -editPeerUsernameAboutLabelMargins: margins(0px, 15px, 34px, 15px); +editPeerUsernameTitleLabelMargins: margins(22px, 17px, 22px, 10px); +editPeerUsernameFieldMargins: margins(22px, 0px, 22px, 20px); editPeerUsername: setupChannelLink; editPeerUsernameSkip: 8px; editPeerInviteLink: FlatLabel(defaultFlatLabel) { @@ -702,7 +702,6 @@ editPeerInviteLink: FlatLabel(defaultFlatLabel) { style: boxTextStyle; } editPeerInviteLinkButton: boxLinkButton; -editPeerUsernameMargins: margins(22px, 17px, 22px, 2px); editPeerUsernameGood: FlatLabel(defaultFlatLabel) { textFg: boxTextFgGood; style: boxTextStyle; @@ -832,3 +831,21 @@ separatePanelBack: IconButton(separatePanelClose) { icon: infoTopBarBackIcon; iconOver: infoTopBarBackIconOver; } + +inviteLinkField: FlatInput(defaultFlatInput) { + font: font(fsize); + + height: 44px; + textMrg: margins(14px, 12px, 36px, 9px); +} +inviteLinkThreeDots: IconButton(defaultIconButton) { + width: 36px; + height: 44px; + + icon: icon {{ "info/edit/dotsmini", dialogsMenuIconFg }}; + iconOver: icon {{ "info/edit/dotsmini", dialogsMenuIconFgOver }}; + iconPosition: point(-1px, -1px); + + rippleAreaSize: 0px; +} +inviteLinkFieldPadding: margins(22px, 7px, 22px, 9px); diff --git a/Telegram/SourceFiles/ui/controls/invite_link_label.cpp b/Telegram/SourceFiles/ui/controls/invite_link_label.cpp new file mode 100644 index 000000000..15fa86d3a --- /dev/null +++ b/Telegram/SourceFiles/ui/controls/invite_link_label.cpp @@ -0,0 +1,102 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "ui/controls/invite_link_label.h" + +#include "ui/rp_widget.h" +#include "ui/widgets/labels.h" +#include "ui/widgets/buttons.h" +#include "ui/widgets/popup_menu.h" +#include "styles/style_info.h" + +namespace Ui { + +InviteLinkLabel::InviteLinkLabel( + not_null parent, + rpl::producer text, + Fn()> createMenu) +: _outer(std::in_place, parent) { + _outer->resize(_outer->width(), st::inviteLinkField.height); + const auto label = CreateChild( + _outer.get(), + std::move(text), + st::defaultFlatLabel); + label->setAttribute(Qt::WA_TransparentForMouseEvents); + + const auto button = CreateChild( + _outer.get(), + st::inviteLinkThreeDots); + + _outer->widthValue( + ) | rpl::start_with_next([=](int width) { + const auto margin = st::inviteLinkField.textMrg; + label->resizeToWidth(width - margin.left() - margin.right()); + label->moveToLeft(margin.left(), margin.top()); + button->moveToRight(0, 0); + }, _outer->lifetime()); + + _outer->paintRequest( + ) | rpl::start_with_next([=] { + auto p = QPainter(_outer.get()); + p.setPen(Qt::NoPen); + p.setBrush(st::inviteLinkField.bgColor); + { + PainterHighQualityEnabler hq(p); + p.drawRoundedRect( + _outer->rect(), + st::roundRadiusSmall, + st::roundRadiusSmall); + } + }, _outer->lifetime()); + + _outer->setCursor(style::cur_pointer); + + rpl::merge( + button->clicks() | rpl::to_empty, + _outer->events( + ) | rpl::filter([=](not_null event) { + return (event->type() == QEvent::MouseButtonPress) + && (static_cast(event.get())->button() + == Qt::RightButton); + }) | rpl::to_empty + ) | rpl::start_with_next([=] { + if (_menu) { + _menu = nullptr; + } else if ((_menu = createMenu())) { + _menu->popup(QCursor::pos()); + } + }, _outer->lifetime()); +} + +object_ptr InviteLinkLabel::take() { + return object_ptr::fromRaw(_outer.get()); +} + +rpl::producer<> InviteLinkLabel::clicks() { + return _outer->events( + ) | rpl::filter([=](not_null event) { + return (event->type() == QEvent::MouseButtonPress) + && (static_cast(event.get())->button() + == Qt::LeftButton); + }) | rpl::map([=](not_null event) { + return _outer->events( + ) | rpl::filter([=](not_null event) { + return (event->type() == QEvent::MouseButtonRelease) + && (static_cast(event.get())->button() + == Qt::LeftButton); + }) | rpl::take(1) | rpl::filter([=](not_null event) { + return (_outer->rect().contains( + static_cast(event.get())->pos())); + }); + }) | rpl::flatten_latest() | rpl::to_empty; +} + +rpl::lifetime &InviteLinkLabel::lifetime() { + return _outer->lifetime(); +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/controls/invite_link_label.h b/Telegram/SourceFiles/ui/controls/invite_link_label.h new file mode 100644 index 000000000..2af7defdb --- /dev/null +++ b/Telegram/SourceFiles/ui/controls/invite_link_label.h @@ -0,0 +1,37 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +#include "base/object_ptr.h" +#include "base/unique_qptr.h" + +namespace Ui { + +class RpWidget; +class PopupMenu; + +class InviteLinkLabel final { +public: + InviteLinkLabel( + not_null parent, + rpl::producer text, + Fn()> createMenu); + + [[nodiscard]] object_ptr take(); + + [[nodiscard]] rpl::producer<> clicks(); + + [[nodiscard]] rpl::lifetime &lifetime(); + +private: + const base::unique_qptr _outer; + base::unique_qptr _menu; + +}; + +} // namespace Ui diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index bcdee7aae..af6fce641 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -88,6 +88,8 @@ PRIVATE ui/chat/pinned_bar.h ui/controls/emoji_button.cpp ui/controls/emoji_button.h + ui/controls/invite_link_label.cpp + ui/controls/invite_link_label.h ui/controls/send_button.cpp ui/controls/send_button.h ui/text/format_values.cpp