From 91c2f8fc0e267b53f24ace0d76227bb881efde93 Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Sun, 11 Sep 2022 05:32:38 +0300 Subject: [PATCH] [Improvement] Restore discussion group button --- Telegram/Resources/langs/lang.strings | 1 + .../SourceFiles/history/history_widget.cpp | 179 +++++++++++++++++- Telegram/SourceFiles/history/history_widget.h | 4 + 3 files changed, 178 insertions(+), 6 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 570b7b3ed..761d24fac 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2049,6 +2049,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_restart_button" = "Restart"; "lng_channel_mute" = "Mute"; "lng_channel_unmute" = "Unmute"; +"lng_channel_discuss" = "Discuss"; "lng_saved_messages" = "Saved Messages"; "lng_saved_short" = "Save"; "lng_saved_forward_here" = "Forward messages here for quick access"; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index eaf432bf5..9c0dc336a 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -195,6 +195,105 @@ constexpr auto kCommonModifiers = 0 | Qt::ControlModifier; const auto kPsaAboutPrefix = "cloud_lng_about_psa_"; +object_ptr SetupDiscussButton( + not_null parent, + not_null controller) { + auto result = object_ptr( + parent, + QString(), + st::historyComposeButton); + const auto button = result.data(); + const auto label = Ui::CreateChild( + button, + tr::lng_channel_discuss() | Ui::Text::ToUpper(), + st::historyComposeButtonLabel); + const auto badge = Ui::CreateChild(button); + label->show(); + + controller->activeChatValue( + ) | rpl::map([=](Dialogs::Key chat) { + return chat.history(); + }) | rpl::map([=](History *history) { + return history ? history->peer->asChannel() : nullptr; + }) | rpl::map([=](ChannelData *channel) -> rpl::producer { + if (channel && channel->isBroadcast()) { + return channel->session().changes().peerFlagsValue( + channel, + Data::PeerUpdate::Flag::ChannelLinkedChat + ) | rpl::map([=] { + return channel->linkedChat(); + }); + } + return rpl::single(nullptr); + }) | rpl::flatten_latest( + ) | rpl::distinct_until_changed( + ) | rpl::map([=](ChannelData *chat) + -> rpl::producer> { + if (chat) { + using UpdateFlag = Data::PeerUpdate::Flag; + return rpl::merge( + chat->session().changes().historyUpdates( + Data::HistoryUpdate::Flag::UnreadView + ) | rpl::filter([=](const Data::HistoryUpdate &update) { + return (update.history->peer == chat); + }) | rpl::to_empty, + + chat->session().changes().peerFlagsValue( + chat, + UpdateFlag::Notifications | UpdateFlag::ChannelAmIn + ) | rpl::to_empty + ) | rpl::map([=] { + const auto history = chat->amIn() + ? chat->owner().historyLoaded(chat) + : nullptr; + return history + ? std::make_tuple( + history->chatListBadgesState().unreadCounter, + !history->chatListBadgesState().unreadMuted) + : std::make_tuple(0, false); + }); + } else { + return rpl::single(std::make_tuple(0, false)); + } + }) | rpl::flatten_latest( + ) | rpl::distinct_until_changed( + ) | rpl::start_with_next([=](int count, bool active) { + badge->setText(QString::number(count), active); + badge->setVisible(count > 0); + }, badge->lifetime()); + + rpl::combine( + badge->shownValue(), + badge->widthValue(), + label->widthValue(), + button->widthValue() + ) | rpl::start_with_next([=]( + bool badgeShown, + int badgeWidth, + int labelWidth, + int width) { + const auto textTop = st::historyComposeButton.textTop; + const auto add = badgeShown + ? (textTop + badgeWidth) + : 0; + const auto total = labelWidth + add; + label->moveToLeft((width - total) / 2, textTop, width); + badge->moveToRight((width - total) / 2, textTop, width); + }, button->lifetime()); + + label->setAttribute(Qt::WA_TransparentForMouseEvents); + badge->setAttribute(Qt::WA_TransparentForMouseEvents); + + return result; +} + +[[nodiscard]] crl::time CountToastDuration(const TextWithEntities &text) { + return std::clamp( + crl::time(1000) * int(text.text.size()) / 14, + crl::time(1000) * 5, + crl::time(1000) * 8); +} + [[nodiscard]] rpl::producer ActivePeerValue( not_null controller) { return controller->activeChatValue( @@ -243,6 +342,7 @@ HistoryWidget::HistoryWidget( this, tr::lng_channel_mute(tr::now).toUpper(), st::historyComposeButton) +, _discuss(SetupDiscussButton(this, controller)) , _reportMessages(this, QString(), st::historyComposeButton) , _attachToggle(this, st::historyAttach) , _tabbedSelectorToggle(this, st::historyAttachEmoji) @@ -315,6 +415,7 @@ HistoryWidget::HistoryWidget( _botStart->addClickHandler([=] { sendBotStartCommand(); }); _joinChannel->addClickHandler([=] { joinChannel(); }); _muteUnmute->addClickHandler([=] { toggleMuteUnmute(); }); + _discuss->addClickHandler([=] { goToDiscussionGroup(); }); _reportMessages->addClickHandler([=] { reportSelectedMessages(); }); connect( _field, @@ -492,6 +593,7 @@ HistoryWidget::HistoryWidget( _botStart->hide(); _joinChannel->hide(); _muteUnmute->hide(); + _discuss->hide(); _reportMessages->hide(); initVoiceRecordBar(); @@ -2868,12 +2970,30 @@ void HistoryWidget::updateControlsVisibility() { toggle(_reportMessages); } else if (isBlocked()) { toggle(_unblock); + _discuss->hide(); } else if (isJoinChannel()) { toggle(_joinChannel); + if (hasDiscussionGroup()) { + if (_discuss->isHidden()) { + _discuss->clearState(); + _discuss->show(); + } + } else { + _discuss->hide(); + } } else if (isMuteUnmute()) { toggle(_muteUnmute); + if (hasDiscussionGroup()) { + if (_discuss->isHidden()) { + _discuss->clearState(); + _discuss->show(); + } + } else { + _discuss->hide(); + } } else if (isBotStart()) { toggle(_botStart); + _discuss->hide(); } _kbShown = false; _fieldAutocomplete->hide(); @@ -2922,6 +3042,7 @@ void HistoryWidget::updateControlsVisibility() { _botStart->hide(); _joinChannel->hide(); _muteUnmute->hide(); + _discuss->hide(); _reportMessages->hide(); _send->show(); updateSendButtonType(); @@ -3005,6 +3126,7 @@ void HistoryWidget::updateControlsVisibility() { _botStart->hide(); _joinChannel->hide(); _muteUnmute->hide(); + _discuss->hide(); _reportMessages->hide(); _attachToggle->hide(); if (_silent) { @@ -4134,6 +4256,22 @@ void HistoryWidget::toggleMuteUnmute() { session().data().notifySettings().update(_peer, muteForSeconds); } +void HistoryWidget::goToDiscussionGroup() { + const auto channel = _peer ? _peer->asChannel() : nullptr; + const auto chat = channel ? channel->linkedChat() : nullptr; + if (!chat) { + return; + } + controller()->showPeerHistory(chat, Window::SectionShow::Way::Forward); +} + +bool HistoryWidget::hasDiscussionGroup() const { + const auto channel = _peer ? _peer->asChannel() : nullptr; + return channel + && channel->isBroadcast() + && (channel->flags() & ChannelDataFlag::HasLink); +} + void HistoryWidget::reportSelectedMessages() { if (!_list || !_chooseForReport || !_list->getSelectionState().count) { return; @@ -5066,7 +5204,7 @@ void HistoryWidget::moveFieldControls() { // (_botMenuButton) (_attachToggle|_replaceMedia) (_sendAs) ---- _inlineResults ------------------------------ _tabbedPanel ------ _fieldBarCancel // (_attachDocument|_attachPhoto) _field (_ttlInfo) (_scheduled) (_silent|_cmdStart|_kbShow) (_kbHide|_tabbedSelectorToggle) _send -// (_botStart|_unblock|_joinChannel|_muteUnmute|_reportMessages) +// (_botStart|_unblock|_joinChannel|{_muteUnmute&_discuss}|_reportMessages) auto buttonsBottom = bottom - _attachToggle->height(); auto left = st::historySendRight; @@ -5129,9 +5267,33 @@ void HistoryWidget::moveFieldControls() { _botStart->height()); _botStart->setGeometry(fullWidthButtonRect); _unblock->setGeometry(fullWidthButtonRect); - _joinChannel->setGeometry(fullWidthButtonRect); - _muteUnmute->setGeometry(fullWidthButtonRect); - _reportMessages->setGeometry(fullWidthButtonRect); + + if (hasDiscussionGroup()) { + _joinChannel->setGeometry(myrtlrect( + 0, + fullWidthButtonRect.y(), + width() / 2, + fullWidthButtonRect.height())); + _reportMessages->setGeometry(myrtlrect( + 0, + fullWidthButtonRect.y(), + width() / 2, + fullWidthButtonRect.height())); + _muteUnmute->setGeometry(myrtlrect( + 0, + fullWidthButtonRect.y(), + width() / 2, + fullWidthButtonRect.height())); + _discuss->setGeometry(myrtlrect( + width() / 2, + fullWidthButtonRect.y(), + width() - (width() / 2), + fullWidthButtonRect.height())); + } else { + _joinChannel->setGeometry(fullWidthButtonRect); + _muteUnmute->setGeometry(fullWidthButtonRect); + _reportMessages->setGeometry(fullWidthButtonRect); + } } void HistoryWidget::updateFieldSize() { @@ -5512,6 +5674,7 @@ void HistoryWidget::handleHistoryChange(not_null history) { const auto botStart = isBotStart(); const auto joinChannel = isJoinChannel(); const auto muteUnmute = isMuteUnmute(); + const auto discuss = (muteUnmute || joinChannel) && hasDiscussionGroup(); const auto reportMessages = isReportMessages(); const auto update = false || (_reportMessages->isHidden() == reportMessages) @@ -5522,7 +5685,8 @@ void HistoryWidget::handleHistoryChange(not_null history) { || (!reportMessages && !unblock && !botStart - && _joinChannel->isHidden() == joinChannel) + && (_joinChannel->isHidden() == joinChannel + || _discuss->isHidden() == discuss)) || (!reportMessages && !unblock && !botStart @@ -7734,7 +7898,10 @@ void HistoryWidget::handlePeerUpdate() { } if (!_showAnimation) { if (_unblock->isHidden() == isBlocked() - || (!isBlocked() && _joinChannel->isHidden() == isJoinChannel())) { + || (!isBlocked() + && _joinChannel->isHidden() == isJoinChannel() + && _discuss->isHidden() == hasDiscussionGroup()) + || (isMuteUnmute() && _discuss->isHidden() == hasDiscussionGroup())) { resize = true; } if (updateCanSendMessage()) { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 34c4023bf..e18a299a5 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -410,6 +410,9 @@ private: void unblockUser(); void sendBotStartCommand(); void joinChannel(); + void goToDiscussionGroup(); + + [[nodiscard]] bool hasDiscussionGroup() const; void supportInitAutocomplete(); void supportInsertText(const QString &text); @@ -740,6 +743,7 @@ private: object_ptr _botStart; object_ptr _joinChannel; object_ptr _muteUnmute; + object_ptr _discuss; object_ptr _reportMessages; object_ptr _botMenuButton = { nullptr }; QString _botMenuButtonText;