From ada490e74e06be003ddeb78bc3586921ea906899 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 + .../chat_helpers/chat_helpers.style | 4 + .../SourceFiles/history/history_widget.cpp | 183 +++++++++++++++++- Telegram/SourceFiles/history/history_widget.h | 4 + 4 files changed, 184 insertions(+), 8 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 87df781f8..aaf76cac0 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2952,6 +2952,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/chat_helpers/chat_helpers.style b/Telegram/SourceFiles/chat_helpers/chat_helpers.style index de976a68b..8eac7989a 100644 --- a/Telegram/SourceFiles/chat_helpers/chat_helpers.style +++ b/Telegram/SourceFiles/chat_helpers/chat_helpers.style @@ -838,6 +838,10 @@ historyComposeButton: FlatButton { color: historyComposeButtonBgRipple; } } +historyComposeButtonLabel: FlatLabel(defaultFlatLabel) { + textFg: windowActiveTextFg; + style: semiboldTextStyle; +} historyUnblock: FlatButton(historyComposeButton) { color: attentionButtonFg; overColor: attentionButtonFgOver; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 0bd2f6d05..e4af29dd5 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -198,6 +198,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( @@ -256,6 +355,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) @@ -334,6 +434,7 @@ HistoryWidget::HistoryWidget( _botStart->addClickHandler([=] { sendBotStartCommand(); }); _joinChannel->addClickHandler([=] { joinChannel(); }); _muteUnmute->addClickHandler([=] { toggleMuteUnmute(); }); + _discuss->addClickHandler([=] { goToDiscussionGroup(); }); _reportMessages->addClickHandler([=] { reportSelectedMessages(); }); _field->submits( ) | rpl::start_with_next([=](Qt::KeyboardModifiers modifiers) { @@ -556,6 +657,7 @@ HistoryWidget::HistoryWidget( _botStart->hide(); _joinChannel->hide(); _muteUnmute->hide(); + _discuss->hide(); _reportMessages->hide(); initVoiceRecordBar(); @@ -3059,12 +3161,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(); @@ -3116,6 +3236,7 @@ void HistoryWidget::updateControlsVisibility() { _botStart->hide(); _joinChannel->hide(); _muteUnmute->hide(); + _discuss->hide(); _reportMessages->hide(); _send->show(); updateSendButtonType(); @@ -3227,6 +3348,7 @@ void HistoryWidget::updateControlsVisibility() { _botStart->hide(); _joinChannel->hide(); _muteUnmute->hide(); + _discuss->hide(); _reportMessages->hide(); _attachToggle->hide(); if (_silent) { @@ -4392,6 +4514,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; @@ -5374,7 +5512,7 @@ void HistoryWidget::moveFieldControls() { // (_botMenu.button) (_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; @@ -5437,11 +5575,35 @@ void HistoryWidget::moveFieldControls() { _botStart->height()); _botStart->setGeometry(fullWidthButtonRect); _unblock->setGeometry(fullWidthButtonRect); - _joinChannel->setGeometry(fullWidthButtonRect); - _muteUnmute->setGeometry(fullWidthButtonRect); - _reportMessages->setGeometry(fullWidthButtonRect); - if (_sendRestriction) { - _sendRestriction->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); + if (_sendRestriction) { + _sendRestriction->setGeometry(fullWidthButtonRect); + } } } @@ -5887,6 +6049,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) @@ -5897,7 +6060,8 @@ void HistoryWidget::handleHistoryChange(not_null history) { || (!reportMessages && !unblock && !botStart - && _joinChannel->isHidden() == joinChannel) + && (_joinChannel->isHidden() == joinChannel + || _discuss->isHidden() == discuss)) || (!reportMessages && !unblock && !botStart @@ -8175,7 +8339,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 e446b819e..e9961b671 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -428,6 +428,9 @@ private: void unblockUser(); void sendBotStartCommand(); void joinChannel(); + void goToDiscussionGroup(); + + [[nodiscard]] bool hasDiscussionGroup() const; void supportInitAutocomplete(); void supportInsertText(const QString &text); @@ -759,6 +762,7 @@ private: object_ptr _botStart; object_ptr _joinChannel; object_ptr _muteUnmute; + object_ptr _discuss; object_ptr _reportMessages; struct { object_ptr button = { nullptr };