tdesktop/Telegram/SourceFiles/dialogs/dialogs_row.cpp

252 lines
6.1 KiB
C++
Raw Normal View History

/*
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/dialogs_row.h"
#include "ui/effects/ripple_animation.h"
#include "ui/text_options.h"
#include "dialogs/dialogs_entry.h"
#include "data/data_folder.h"
2019-06-17 14:37:29 +00:00
#include "data/data_peer_values.h"
#include "history/history.h"
#include "lang/lang_keys.h"
#include "mainwidget.h"
#include "styles/style_dialogs.h"
namespace Dialogs {
namespace {
QString ComposeFolderListEntryText(not_null<Data::Folder*> folder) {
2019-05-08 08:50:39 +00:00
const auto &list = folder->lastHistories();
if (list.empty()) {
2019-05-08 08:50:39 +00:00
return QString();
}
2019-05-08 08:50:39 +00:00
const auto count = std::max(
int(list.size()),
folder->chatsList()->fullSize().current());
2019-05-08 08:50:39 +00:00
const auto throwAwayLastName = (list.size() > 1)
&& (count == list.size() + 1);
auto &&peers = ranges::view::all(
list
) | ranges::view::take(
list.size() - (throwAwayLastName ? 1 : 0)
);
const auto wrapName = [](not_null<History*> history) {
const auto name = TextUtilities::Clean(history->peer->name);
return (history->unreadCount() > 0)
? (textcmdStartSemibold()
+ textcmdLink(1, name)
+ textcmdStopSemibold())
: name;
};
2019-05-08 08:50:39 +00:00
const auto shown = int(peers.size());
const auto accumulated = [&] {
Expects(shown > 0);
auto i = peers.begin();
auto result = wrapName(*i);
2019-05-08 08:50:39 +00:00
for (++i; i != peers.end(); ++i) {
result = tr::lng_archived_last_list(
tr::now,
2019-05-08 08:50:39 +00:00
lt_accumulated,
result,
lt_chat,
wrapName(*i));
2019-05-08 08:50:39 +00:00
}
return result;
}();
return (shown < count)
? tr::lng_archived_last(tr::now, lt_count, (count - shown), lt_chats, accumulated)
2019-05-08 08:50:39 +00:00
: accumulated;
}
} // namespace
2019-06-17 14:37:29 +00:00
BasicRow::BasicRow() = default;
BasicRow::~BasicRow() = default;
2019-06-17 14:37:29 +00:00
void BasicRow::setOnline(bool online, Fn<void()> updateCallback) const {
if (_online == online) {
return;
}
_online = online;
if (_onlineUserpic && _onlineUserpic->animation.animating()) {
_onlineUserpic->animation.change(
_online ? 1. : 0.,
st::dialogsOnlineBadgeDuration);
} else if (updateCallback) {
ensureOnlineUserpic();
_onlineUserpic->animation.start(
std::move(updateCallback),
_online ? 0. : 1.,
_online ? 1. : 0.,
st::dialogsOnlineBadgeDuration);
}
if (!_online
&& _onlineUserpic
&& !_onlineUserpic->animation.animating()) {
_onlineUserpic = nullptr;
}
}
void BasicRow::addRipple(
QPoint origin,
QSize size,
Fn<void()> updateCallback) {
if (!_ripple) {
auto mask = Ui::RippleAnimation::rectMask(size);
2019-06-17 14:37:29 +00:00
_ripple = std::make_unique<Ui::RippleAnimation>(
st::dialogsRipple,
std::move(mask),
std::move(updateCallback));
}
_ripple->add(origin);
}
2019-06-17 14:37:29 +00:00
void BasicRow::stopLastRipple() {
if (_ripple) {
_ripple->lastStop();
}
}
2019-06-17 14:37:29 +00:00
void BasicRow::paintRipple(
Painter &p,
int x,
int y,
int outerWidth,
const QColor *colorOverride) const {
if (_ripple) {
2019-04-02 09:13:30 +00:00
_ripple->paint(p, x, y, outerWidth, colorOverride);
if (_ripple->empty()) {
_ripple.reset();
}
}
}
2019-06-17 14:37:29 +00:00
void BasicRow::ensureOnlineUserpic() const {
if (_onlineUserpic) {
return;
}
_onlineUserpic = std::make_unique<OnlineUserpic>();
}
void BasicRow::PaintOnlineFrame(
not_null<OnlineUserpic*> data,
2020-05-28 14:32:10 +00:00
not_null<PeerData*> peer,
std::shared_ptr<Data::CloudImageView> &view) {
2019-06-17 14:37:29 +00:00
data->frame.fill(Qt::transparent);
Painter q(&data->frame);
peer->paintUserpic(
q,
2020-05-28 14:32:10 +00:00
view,
2019-06-17 14:37:29 +00:00
0,
0,
st::dialogsPhotoSize);
PainterHighQualityEnabler hq(q);
q.setCompositionMode(QPainter::CompositionMode_Source);
const auto size = st::dialogsOnlineBadgeSize;
const auto stroke = st::dialogsOnlineBadgeStroke;
const auto skip = st::dialogsOnlineBadgeSkip;
const auto edge = st::dialogsPadding.x() + st::dialogsPhotoSize;
const auto shrink = (size / 2) * (1. - data->online);
auto pen = QPen(Qt::transparent);
pen.setWidthF(stroke * data->online);
q.setPen(pen);
q.setBrush(data->active
? st::dialogsOnlineBadgeFgActive
: st::dialogsOnlineBadgeFg);
q.drawEllipse(QRectF(
edge - skip.x() - size,
edge - skip.y() - size,
size,
size
).marginsRemoved({ shrink, shrink, shrink, shrink }));
}
void BasicRow::paintUserpic(
Painter &p,
not_null<PeerData*> peer,
bool allowOnline,
bool active,
int fullWidth) const {
setOnline(Data::IsPeerAnOnlineUser(peer));
const auto online = _onlineUserpic
? _onlineUserpic->animation.value(_online ? 1. : 0.)
: (_online ? 1. : 0.);
if (!allowOnline || online == 0.) {
peer->paintUserpicLeft(
p,
2020-05-28 14:32:10 +00:00
_userpic,
2019-06-17 14:37:29 +00:00
st::dialogsPadding.x(),
st::dialogsPadding.y(),
fullWidth,
st::dialogsPhotoSize);
if (!allowOnline || !_online) {
_onlineUserpic = nullptr;
}
return;
}
ensureOnlineUserpic();
if (_onlineUserpic->frame.isNull()) {
_onlineUserpic->frame = QImage(
st::dialogsPhotoSize * cRetinaFactor(),
st::dialogsPhotoSize * cRetinaFactor(),
QImage::Format_ARGB32_Premultiplied);
_onlineUserpic->frame.setDevicePixelRatio(cRetinaFactor());
}
2020-05-28 14:32:10 +00:00
const auto key = peer->userpicUniqueKey(_userpic);
2019-06-17 14:37:29 +00:00
if (_onlineUserpic->online != online
|| _onlineUserpic->key != key
|| _onlineUserpic->active != active) {
_onlineUserpic->online = online;
_onlineUserpic->key = key;
_onlineUserpic->active = active;
2020-05-28 14:32:10 +00:00
PaintOnlineFrame(_onlineUserpic.get(), peer, _userpic);
2019-06-17 14:37:29 +00:00
}
p.drawImage(st::dialogsPadding, _onlineUserpic->frame);
}
Row::Row(Key key, int pos) : _id(key), _pos(pos) {
if (const auto history = key.history()) {
setOnline(Data::IsPeerAnOnlineUser(history->peer));
}
}
2020-03-17 13:04:30 +00:00
uint64 Row::sortKey(FilterId filterId) const {
return _id.entry()->sortKeyInChatList(filterId);
}
void Row::validateListEntryCache() const {
const auto folder = _id.folder();
if (!folder) {
return;
}
const auto version = folder->chatListViewVersion();
if (_listEntryCacheVersion == version) {
return;
}
_listEntryCacheVersion = version;
_listEntryCache.setText(
st::dialogsTextStyle,
ComposeFolderListEntryText(folder),
Ui::DialogTextOptions());
}
FakeRow::FakeRow(Key searchInChat, not_null<HistoryItem*> item)
: _searchInChat(searchInChat)
, _item(item)
, _cache(st::dialogsTextWidthMin) {
}
} // namespace Dialogs