Initial version of top peers.

This commit is contained in:
John Preston
2024-04-09 19:04:18 +04:00
parent 11e4c45969
commit 18598f8dca
21 changed files with 908 additions and 27 deletions
@@ -591,6 +591,11 @@ dialogsStoriesFull: DialogsStories {
font: font(11px);
}
}
topPeers: DialogsStories(dialogsStoriesFull) {
photo: 46px;
photoLeft: 10px;
photoTop: 8px;
}
dialogsStoriesList: DialogsStoriesList {
small: dialogsStories;
@@ -633,3 +638,4 @@ dialogsSearchTagArrowPadding: margins(-6px, 3px, 0px, 0px);
dialogsSearchTagPromoLeft: 6px;
dialogsSearchTagPromoRight: 1px;
dialogsSearchTagPromoSkip: 6px;
@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/options.h"
#include "dialogs/ui/dialogs_stories_content.h"
#include "dialogs/ui/dialogs_stories_list.h"
#include "dialogs/ui/dialogs_suggestions.h"
#include "dialogs/dialogs_inner_widget.h"
#include "dialogs/dialogs_search_from_controllers.h"
#include "dialogs/dialogs_key.h"
@@ -1021,7 +1022,10 @@ void Widget::fullSearchRefreshOn(rpl::producer<> events) {
void Widget::updateControlsVisibility(bool fast) {
updateLoadMoreChatsVisibility();
_scroll->show();
_scroll->setVisible(!_suggestions);
if (_suggestions) {
_suggestions->show();
}
updateStoriesVisibility();
if ((_openedFolder || _openedForum) && _searchHasFocus.current()) {
setInnerFocus();
@@ -1085,8 +1089,36 @@ void Widget::updateLockUnlockPosition() {
}
void Widget::updateHasFocus(not_null<QWidget*> focused) {
_searchHasFocus = (focused == _search.data());
updateForceDisplayWide();
const auto has = (focused == _search.data());
if (_searchHasFocus.current() != has) {
_searchHasFocus = (focused == _search.data());
updateStoriesVisibility();
updateForceDisplayWide();
updateSuggestions(anim::type::normal);
}
}
void Widget::updateSuggestions(anim::type animated) {
const auto suggest = _searchHasFocus.current()
&& !_searchInChat
&& (_inner->state() == WidgetState::Default);
if (!suggest && _suggestions) {
_suggestions = nullptr;
_scroll->show();
} else if (suggest && !_suggestions) {
_suggestions = std::make_unique<Suggestions>(
this,
rpl::single(TopPeersContent(&session())));
_suggestions->topPeerChosen(
) | rpl::start_with_next([=](PeerId id) {
controller()->showPeerHistory(id);
}, _suggestions->lifetime());
_suggestions->show();
_scroll->hide();
updateControlsGeometry();
}
}
void Widget::changeOpenedSubsection(
@@ -1513,7 +1545,10 @@ void Widget::startWidthAnimation() {
void Widget::stopWidthAnimation() {
_widthAnimationCache = QPixmap();
if (!_showAnimation) {
_scroll->show();
_scroll->setVisible(!_suggestions);
if (_suggestions) {
_suggestions->show();
}
}
updateStoriesVisibility();
update();
@@ -1528,6 +1563,7 @@ void Widget::updateStoriesVisibility() {
|| _openedForum
|| !_widthAnimationCache.isNull()
|| _childList
|| _searchHasFocus.current()
|| !_search->getLastText().isEmpty()
|| _searchInChat
|| _stories->empty();
@@ -2460,6 +2496,7 @@ void Widget::applySearchUpdate(bool force) {
clearSearchCache();
}
_cancelSearch->toggle(!filterText.isEmpty(), anim::type::normal);
updateSuggestions(anim::type::instant);
updateLoadMoreChatsVisibility();
updateJumpToDateVisibility();
updateLockUnlockPosition();
@@ -2675,6 +2712,7 @@ bool Widget::setSearchInChat(
if (searchInPeerUpdated) {
_searchInChat = chat;
controller()->setSearchInChat(_searchInChat);
updateSuggestions(anim::type::instant);
updateJumpToDateVisibility();
updateStoriesVisibility();
}
@@ -3041,6 +3079,14 @@ void Widget::updateControlsGeometry() {
};
_updateScrollGeometryCached();
if (_suggestions) {
_suggestions->setGeometry(
0,
expandedStoriesTop,
scrollWidth,
height() - expandedStoriesTop - bottomSkip);
}
_inner->resize(scrollWidth, _inner->height());
_inner->setNarrowRatio(narrowRatio);
if (newScrollTop != wasScrollTop) {
@@ -3097,6 +3143,7 @@ void Widget::keyPressEvent(QKeyEvent *e) {
&& !_openedFolder
&& !_openedForum
&& _search->isVisible()
&& !_search->hasFocus()
&& !e->text().isEmpty()) {
_search->setFocusFast();
QCoreApplication::sendEvent(_search->rawTextEdit(), e);
@@ -75,6 +75,7 @@ class Key;
struct ChosenRow;
class InnerWidget;
enum class SearchRequestType;
class Suggestions;
class Widget final : public Window::AbstractSectionWidget {
public:
@@ -242,6 +243,7 @@ private:
void startScrollUpButtonAnimation(bool shown);
void updateScrollUpPosition();
void updateLockUnlockPosition();
void updateSuggestions(anim::type animated);
MTP::Sender _api;
@@ -273,6 +275,7 @@ private:
object_ptr<Ui::ElasticScroll> _scroll;
QPointer<InnerWidget> _inner;
std::unique_ptr<Suggestions> _suggestions;
class BottomButton;
object_ptr<BottomButton> _updateTelegram = { nullptr };
object_ptr<BottomButton> _loadMoreChats = { nullptr };
@@ -291,7 +294,7 @@ private:
Data::Folder *_openedFolder = nullptr;
Data::Forum *_openedForum = nullptr;
Dialogs::Key _searchInChat;
Key _searchInChat;
History *_searchInMigrated = nullptr;
PeerData *_searchFromAuthor = nullptr;
std::vector<Data::ReactionId> _searchTags;
@@ -184,9 +184,9 @@ rpl::producer<bool> List::toggleExpandedRequests() const {
return _toggleExpandedRequests.events();
}
rpl::producer<> List::entered() const {
return _entered.events();
}
//rpl::producer<> List::entered() const {
// return _entered.events();
//}
rpl::producer<> List::loadMoreRequests() const {
return _loadMoreRequests.events();
@@ -217,9 +217,9 @@ void List::requestExpanded(bool expanded) {
_toggleExpandedRequests.fire_copy(_expanded);
}
void List::enterEventHook(QEnterEvent *e) {
_entered.fire({});
}
//void List::enterEventHook(QEnterEvent *e) {
//_entered.fire({});
//}
void List::resizeEvent(QResizeEvent *e) {
updateScrollMax();
@@ -94,7 +94,7 @@ public:
[[nodiscard]] rpl::producer<uint64> clicks() const;
[[nodiscard]] rpl::producer<ShowMenuRequest> showMenuRequests() const;
[[nodiscard]] rpl::producer<bool> toggleExpandedRequests() const;
[[nodiscard]] rpl::producer<> entered() const;
//[[nodiscard]] rpl::producer<> entered() const;
[[nodiscard]] rpl::producer<> loadMoreRequests() const;
[[nodiscard]] auto verticalScrollEvents() const
@@ -123,7 +123,7 @@ private:
};
void showContent(Content &&content);
void enterEventHook(QEnterEvent *e) override;
//void enterEventHook(QEnterEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
void paintEvent(QPaintEvent *e) override;
void wheelEvent(QWheelEvent *e) override;
@@ -173,7 +173,7 @@ private:
rpl::event_stream<uint64> _clicks;
rpl::event_stream<ShowMenuRequest> _showMenuRequests;
rpl::event_stream<bool> _toggleExpandedRequests;
rpl::event_stream<> _entered;
//rpl::event_stream<> _entered;
rpl::event_stream<> _loadMoreRequests;
rpl::event_stream<> _collapsedGeometryChanged;
@@ -0,0 +1,77 @@
/*
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 "dialogs/ui/dialogs_suggestions.h"
#include "data/components/top_peers.h"
#include "data/data_user.h"
#include "main/main_session.h"
#include "ui/widgets/elastic_scroll.h"
#include "ui/widgets/labels.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/slide_wrap.h"
#include "ui/dynamic_thumbnails.h"
#include "styles/style_layers.h"
namespace Dialogs {
Suggestions::Suggestions(
not_null<QWidget*> parent,
rpl::producer<TopPeersList> topPeers)
: RpWidget(parent)
, _scroll(std::make_unique<Ui::ElasticScroll>(this))
, _content(_scroll->setOwnedWidget(object_ptr<Ui::VerticalLayout>(this)))
, _topPeersWrap(_content->add(object_ptr<Ui::SlideWrap<TopPeersStrip>>(
this,
object_ptr<TopPeersStrip>(this, std::move(topPeers)))))
, _topPeers(_topPeersWrap->entity())
, _divider(_content->add(setupDivider())) {
_topPeers->emptyValue() | rpl::start_with_next([=](bool empty) {
_topPeersWrap->toggle(!empty, anim::type::instant);
}, _topPeers->lifetime());
_topPeers->clicks() | rpl::start_with_next([=](uint64 peerIdRaw) {
_topPeerChosen.fire(PeerId(peerIdRaw));
}, _topPeers->lifetime());
}
Suggestions::~Suggestions() = default;
void Suggestions::paintEvent(QPaintEvent *e) {
QPainter(this).fillRect(e->rect(), st::windowBg);
}
void Suggestions::resizeEvent(QResizeEvent *e) {
_scroll->setGeometry(rect());
_content->resizeToWidth(width());
}
object_ptr<Ui::RpWidget> Suggestions::setupDivider() {
auto result = object_ptr<Ui::DividerLabel>(
this,
object_ptr<Ui::FlatLabel>(
this,
rpl::single(u"Recent"_q),
st::boxDividerLabel),
st::defaultBoxDividerLabelPadding);
return result;
}
TopPeersList TopPeersContent(not_null<Main::Session*> session) {
auto result = TopPeersList();
for (const auto &peer : session->topPeers().list()) {
result.entries.push_back(TopPeersEntry{
.id = peer->id.value,
.name = peer->shortName(),
.userpic = Ui::MakeUserpicThumbnail(peer),
});
}
return result;
}
} // namespace Dialogs
@@ -0,0 +1,59 @@
/*
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 "dialogs/ui/top_peers_strip.h"
#include "ui/rp_widget.h"
namespace Main {
class Session;
} // namespace Main
namespace Ui {
class ElasticScroll;
class VerticalLayout;
template <typename Widget>
class SlideWrap;
} // namespace Ui
namespace Dialogs {
class Suggestions final : public Ui::RpWidget {
public:
Suggestions(
not_null<QWidget*> parent,
rpl::producer<TopPeersList> topPeers);
~Suggestions();
[[nodiscard]] rpl::producer<PeerId> topPeerChosen() const {
return _topPeerChosen.events();
}
private:
void paintEvent(QPaintEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
[[nodiscard]] object_ptr<Ui::RpWidget> setupDivider();
void updateControlsGeometry();
const std::unique_ptr<Ui::ElasticScroll> _scroll;
const not_null<Ui::VerticalLayout*> _content;
const not_null<Ui::SlideWrap<TopPeersStrip>*> _topPeersWrap;
const not_null<TopPeersStrip*> _topPeers;
const not_null<Ui::RpWidget*> _divider;
rpl::event_stream<PeerId> _topPeerChosen;
};
[[nodiscard]] TopPeersList TopPeersContent(
not_null<Main::Session*> session);
} // namespace Dialogs
@@ -0,0 +1,302 @@
/*
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 "dialogs/ui/top_peers_strip.h"
#include "ui/text/text.h"
#include "ui/widgets/menu/menu_add_action_callback_factory.h"
#include "ui/widgets/popup_menu.h"
#include "ui/dynamic_image.h"
#include "ui/painter.h"
#include "styles/style_dialogs.h"
#include "styles/style_widgets.h"
#include <QtWidgets/QApplication>
namespace Dialogs {
struct TopPeersStrip::Entry {
uint64 id = 0;
Ui::Text::String name;
std::shared_ptr<Ui::DynamicImage> userpic;
bool subscribed = false;
};
TopPeersStrip::TopPeersStrip(
not_null<QWidget*> parent,
rpl::producer<TopPeersList> content)
: RpWidget(parent) {
resize(0, st::topPeers.height);
std::move(content) | rpl::start_with_next([=](const TopPeersList &list) {
apply(list);
}, lifetime());
setMouseTracking(true);
}
TopPeersStrip::~TopPeersStrip() = default;
void TopPeersStrip::resizeEvent(QResizeEvent *e) {
updateScrollMax();
}
void TopPeersStrip::wheelEvent(QWheelEvent *e) {
const auto phase = e->phase();
const auto fullDelta = e->pixelDelta().isNull()
? e->angleDelta()
: e->pixelDelta();
if (phase == Qt::ScrollBegin || phase == Qt::ScrollEnd) {
_scrollingLock = Qt::Orientation();
if (fullDelta.isNull()) {
return;
}
}
const auto vertical = qAbs(fullDelta.x()) < qAbs(fullDelta.y());
if (_scrollingLock == Qt::Orientation() && phase != Qt::NoScrollPhase) {
_scrollingLock = vertical ? Qt::Vertical : Qt::Horizontal;
}
if (_scrollingLock == Qt::Vertical || (vertical && !_scrollLeftMax)) {
_verticalScrollEvents.fire(e);
return;
}
const auto delta = vertical
? fullDelta.y()
: ((style::RightToLeft() ? -1 : 1) * fullDelta.x());
const auto now = _scrollLeft;
const auto used = now - delta;
const auto next = std::clamp(used, 0, _scrollLeftMax);
if (next != now) {
_scrollLeft = next;
updateSelected();
update();
}
e->accept();
}
void TopPeersStrip::mousePressEvent(QMouseEvent *e) {
if (e->button() != Qt::LeftButton) {
return;
}
_lastMousePosition = e->globalPos();
updateSelected();
_mouseDownPosition = _lastMousePosition;
_pressed = _selected;
}
void TopPeersStrip::mouseMoveEvent(QMouseEvent *e) {
_lastMousePosition = e->globalPos();
updateSelected();
if (!_dragging && _mouseDownPosition) {
if ((_lastMousePosition - *_mouseDownPosition).manhattanLength()
>= QApplication::startDragDistance()) {
_dragging = true;
_startDraggingLeft = _scrollLeft;
}
}
checkDragging();
}
void TopPeersStrip::checkDragging() {
if (_dragging) {
const auto sign = (style::RightToLeft() ? -1 : 1);
const auto newLeft = std::clamp(
(sign * (_mouseDownPosition->x() - _lastMousePosition.x())
+ _startDraggingLeft),
0,
_scrollLeftMax);
if (newLeft != _scrollLeft) {
_scrollLeft = newLeft;
update();
}
}
}
void TopPeersStrip::mouseReleaseEvent(QMouseEvent *e) {
_lastMousePosition = e->globalPos();
const auto guard = gsl::finally([&] {
_mouseDownPosition = std::nullopt;
});
const auto pressed = std::exchange(_pressed, -1);
if (finishDragging()) {
return;
}
updateSelected();
if (_selected == pressed) {
if (_selected < _entries.size()) {
_clicks.fire_copy(_entries[_selected].id);
}
}
}
void TopPeersStrip::updateScrollMax() {
const auto &st = st::topPeers;
const auto single = st.photoLeft * 2 + st.photo;
const auto widthFull = int(_entries.size()) * single;
_scrollLeftMax = std::max(widthFull - width(), 0);
_scrollLeft = std::clamp(_scrollLeft, 0, _scrollLeftMax);
update();
}
bool TopPeersStrip::empty() const {
return _empty.current();
}
rpl::producer<bool> TopPeersStrip::emptyValue() const {
return _empty.value();
}
rpl::producer<uint64> TopPeersStrip::clicks() const {
return _clicks.events();
}
auto TopPeersStrip::showMenuRequests() const
-> rpl::producer<ShowTopPeerMenuRequest> {
return _showMenuRequests.events();
}
void TopPeersStrip::apply(const TopPeersList &list) {
auto now = std::vector<Entry>();
if (list.entries.empty()) {
_empty = true;
}
for (const auto &entry : list.entries) {
const auto i = ranges::find(_entries, entry.id, &Entry::id);
if (i != end(_entries)) {
now.push_back(base::take(*i));
} else {
now.push_back({ .id = entry.id });
}
apply(now.back(), entry);
}
for (auto &entry : _entries) {
if (entry.subscribed) {
entry.userpic->subscribeToUpdates(nullptr);
entry.subscribed = 0;
}
}
_entries = std::move(now);
if (!_entries.empty()) {
_empty = false;
}
update();
}
void TopPeersStrip::apply(Entry &entry, const TopPeersEntry &data) {
Expects(entry.id == data.id);
Expects(data.userpic != nullptr);
if (entry.name.toString() != data.name) {
entry.name.setText(st::topPeers.nameStyle, data.name);
}
if (entry.userpic.get() != data.userpic.get()) {
if (entry.subscribed) {
entry.userpic->subscribeToUpdates(nullptr);
entry.subscribed = 0;
}
entry.userpic = data.userpic;
}
}
void TopPeersStrip::paintEvent(QPaintEvent *e) {
auto p = Painter(this);
auto x = -_scrollLeft;
const auto &st = st::topPeers;
const auto line = st.lineTwice / 2;
const auto single = st.photoLeft * 2 + st.photo;
for (auto &entry : _entries) {
if (!entry.subscribed) {
entry.userpic->subscribeToUpdates([=] {
update();
});
entry.subscribed = 1;
}
const auto image = entry.userpic->image(st.photo);
p.drawImage(
QRect(x + st.photoLeft, st.photoTop, st.photo, st.photo),
image);
const auto nameLeft = x + st.nameLeft;
entry.name.drawElided(p, nameLeft, st.nameTop, single, 1, style::al_top);
x += single;
}
}
void TopPeersStrip::contextMenuEvent(QContextMenuEvent *e) {
_menu = nullptr;
if (e->reason() == QContextMenuEvent::Mouse) {
_lastMousePosition = e->globalPos();
updateSelected();
}
if (_selected < 0 || _entries.empty()) {
return;
}
_menu = base::make_unique_q<Ui::PopupMenu>(
this,
st::popupMenuWithIcons);
_showMenuRequests.fire({
_entries[_selected].id,
Ui::Menu::CreateAddActionCallback(_menu),
});
if (_menu->empty()) {
_menu = nullptr;
return;
}
const auto updateAfterMenuDestroyed = [=] {
const auto globalPosition = QCursor::pos();
if (rect().contains(mapFromGlobal(globalPosition))) {
_lastMousePosition = globalPosition;
updateSelected();
}
};
QObject::connect(
_menu.get(),
&QObject::destroyed,
crl::guard(&_menuGuard, updateAfterMenuDestroyed));
_menu->popup(e->globalPos());
e->accept();
}
bool TopPeersStrip::finishDragging() {
if (!_dragging) {
return false;
}
checkDragging();
_dragging = false;
updateSelected();
return true;
}
void TopPeersStrip::updateSelected() {
if (_pressed >= 0) {
return;
}
const auto &st = st::topPeers;
const auto p = mapFromGlobal(_lastMousePosition);
const auto x = p.x();
const auto single = st.photoLeft * 2 + st.photo;
const auto index = (x - _scrollLeft) / single;
const auto selected = (index < 0 || index >= _entries.size())
? -1
: index;
if (_selected != selected) {
const auto over = (selected >= 0);
if (over != (_selected >= 0)) {
setCursor(over ? style::cur_pointer : style::cur_default);
}
_selected = selected;
}
}
} // namespace Dialogs
@@ -0,0 +1,93 @@
/*
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/weak_ptr.h"
#include "ui/widgets/menu/menu_add_action_callback.h"
#include "ui/rp_widget.h"
namespace Ui {
class DynamicImage;
} // namespace Ui
namespace Dialogs {
struct TopPeersEntry {
uint64 id = 0;
QString name;
std::shared_ptr<Ui::DynamicImage> userpic;
uint32 badge : 30 = 0;
uint32 muted : 1 = 0;
uint32 online : 1 = 0;
};
struct TopPeersList {
std::vector<TopPeersEntry> entries;
};
struct ShowTopPeerMenuRequest {
uint64 id = 0;
Ui::Menu::MenuCallback callback;
};
class TopPeersStrip final : public Ui::RpWidget {
public:
TopPeersStrip(
not_null<QWidget*> parent,
rpl::producer<TopPeersList> content);
~TopPeersStrip();
[[nodiscard]] bool empty() const;
[[nodiscard]] rpl::producer<bool> emptyValue() const;
[[nodiscard]] rpl::producer<uint64> clicks() const;
[[nodiscard]] auto showMenuRequests() const
-> rpl::producer<ShowTopPeerMenuRequest>;
private:
struct Entry;
void resizeEvent(QResizeEvent *e) override;
void paintEvent(QPaintEvent *e) override;
void wheelEvent(QWheelEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;
void updateScrollMax();
void updateSelected();
void checkDragging();
bool finishDragging();
void apply(const TopPeersList &list);
void apply(Entry &entry, const TopPeersEntry &data);
std::vector<Entry> _entries;
rpl::variable<bool> _empty = true;
rpl::event_stream<uint64> _clicks;
rpl::event_stream<ShowTopPeerMenuRequest> _showMenuRequests;
rpl::event_stream<not_null<QWheelEvent*>> _verticalScrollEvents;
QPoint _lastMousePosition;
std::optional<QPoint> _mouseDownPosition;
int _startDraggingLeft = 0;
int _scrollLeft = 0;
int _scrollLeftMax = 0;
bool _dragging = false;
Qt::Orientation _scrollingLock = {};
int _selected = -1;
int _pressed = -1;
base::unique_qptr<Ui::PopupMenu> _menu;
base::has_weak_ptr _menuGuard;
};
} // namespace Dialogs