From b2e9c4ab757f2735c61fec78694ec7f52e7fe844 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 3 Dec 2022 20:36:08 +0300 Subject: [PATCH] Moved Ui::EmptyUserpic to td_ui. --- Telegram/CMakeLists.txt | 2 - Telegram/SourceFiles/api/api_chat_invite.cpp | 2 +- Telegram/SourceFiles/calls/calls_userpic.cpp | 3 +- Telegram/SourceFiles/data/data_peer.cpp | 10 +- Telegram/SourceFiles/data/data_peer.h | 1 - .../history/history_item_components.cpp | 2 +- .../view/media/history_view_contact.cpp | 4 +- .../inline_bots/inline_bot_layout_item.cpp | 2 +- Telegram/SourceFiles/ui/empty_userpic.cpp | 100 ++++++++++++------ Telegram/SourceFiles/ui/empty_userpic.h | 20 ++-- Telegram/cmake/td_ui.cmake | 2 + 11 files changed, 86 insertions(+), 62 deletions(-) diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index da0d43f652..b6ff95b00f 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1273,8 +1273,6 @@ PRIVATE ui/widgets/level_meter.h ui/countryinput.cpp ui/countryinput.h - ui/empty_userpic.cpp - ui/empty_userpic.h ui/filter_icons.cpp ui/filter_icons.h ui/filter_icon_panel.cpp diff --git a/Telegram/SourceFiles/api/api_chat_invite.cpp b/Telegram/SourceFiles/api/api_chat_invite.cpp index ea5f2771c5..e0afbabf44 100644 --- a/Telegram/SourceFiles/api/api_chat_invite.cpp +++ b/Telegram/SourceFiles/api/api_chat_invite.cpp @@ -241,7 +241,7 @@ ConfirmInviteBox::ConfirmInviteBox( } } else { _photoEmpty = std::make_unique( - Ui::PeerUserpicColor(0), + Ui::EmptyUserpic::UserpicColor(0), invite.title); } } diff --git a/Telegram/SourceFiles/calls/calls_userpic.cpp b/Telegram/SourceFiles/calls/calls_userpic.cpp index 4cd10d1c8d..400299f29f 100644 --- a/Telegram/SourceFiles/calls/calls_userpic.cpp +++ b/Telegram/SourceFiles/calls/calls_userpic.cpp @@ -197,7 +197,8 @@ void Userpic::createCache(Image *image) { { auto p = QPainter(&filled); Ui::EmptyUserpic( - Ui::PeerUserpicColor(_peer->id), + Ui::EmptyUserpic::UserpicColor( + Data::PeerColorIndex(_peer->id)), _peer->name() ).paint(p, 0, 0, size, size); } diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 5f20faf690..a296bc72ad 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -59,14 +59,8 @@ using UpdateFlag = Data::PeerUpdate::Flag; namespace Data { -int PeerColorIndex(BareId bareId) { - const auto index = bareId % 7; - const int map[] = { 0, 7, 4, 1, 6, 3, 5 }; - return map[index]; -} - int PeerColorIndex(PeerId peerId) { - return PeerColorIndex(peerId.value & PeerId::kChatTypeMask); + return Ui::EmptyUserpic::ColorIndex(peerId.value & PeerId::kChatTypeMask); } PeerId FakePeerIdForJustName(const QString &name) { @@ -236,7 +230,7 @@ not_null PeerData::ensureEmptyUserpic() const { if (!_userpicEmpty) { const auto user = asUser(); _userpicEmpty = std::make_unique( - Ui::PeerUserpicColor(id), + Ui::EmptyUserpic::UserpicColor(Data::PeerColorIndex(id)), user && user->isInaccessible() ? Ui::EmptyUserpic::InaccessibleName() : name()); diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index 7ffc167108..fe925ad826 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -40,7 +40,6 @@ class CloudImageView; struct ReactionId; int PeerColorIndex(PeerId peerId); -int PeerColorIndex(BareId bareId); // Must be used only for PeerColor-s. PeerId FakePeerIdForJustName(const QString &name); diff --git a/Telegram/SourceFiles/history/history_item_components.cpp b/Telegram/SourceFiles/history/history_item_components.cpp index d903f47891..160b056095 100644 --- a/Telegram/SourceFiles/history/history_item_components.cpp +++ b/Telegram/SourceFiles/history/history_item_components.cpp @@ -105,7 +105,7 @@ HiddenSenderInfo::HiddenSenderInfo(const QString &name, bool external) : name(name) , colorPeerId(Data::FakePeerIdForJustName(name)) , emptyUserpic( - Ui::PeerUserpicColor(colorPeerId), + Ui::EmptyUserpic::UserpicColor(Data::PeerColorIndex(colorPeerId)), (external ? Ui::EmptyUserpic::ExternalName() : name)) { diff --git a/Telegram/SourceFiles/history/view/media/history_view_contact.cpp b/Telegram/SourceFiles/history/view/media/history_view_contact.cpp index 46bb6b37a9..c522272876 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_contact.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_contact.cpp @@ -122,9 +122,9 @@ QSize Contact::countOptimalSize() { } else { const auto full = _name.toString(); _photoEmpty = std::make_unique( - Ui::PeerUserpicColor(_userId + Ui::EmptyUserpic::UserpicColor(Data::PeerColorIndex(_userId ? peerFromUser(_userId) - : Data::FakePeerIdForJustName(full)), + : Data::FakePeerIdForJustName(full))), full); } if (_contact && _contact->isContact()) { diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp index f763bf30c7..2d5ba3c263 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_item.cpp @@ -158,7 +158,7 @@ Image *ItemBase::getResultThumb(Data::FileOrigin origin) const { QPixmap ItemBase::getResultContactAvatar(int width, int height) const { if (_result->_type == Result::Type::Contact) { auto result = Ui::EmptyUserpic( - Ui::PeerUserpicColor(FakeChatId(BareId(qHash(_result->_id)))), + Ui::EmptyUserpic::UserpicColor(BareId(qHash(_result->_id))), _result->getLayoutTitle() ).generate(width); if (result.height() != height * cIntRetinaFactor()) { diff --git a/Telegram/SourceFiles/ui/empty_userpic.cpp b/Telegram/SourceFiles/ui/empty_userpic.cpp index a74293760d..143f11e7b9 100644 --- a/Telegram/SourceFiles/ui/empty_userpic.cpp +++ b/Telegram/SourceFiles/ui/empty_userpic.cpp @@ -7,7 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "ui/empty_userpic.h" -#include "data/data_peer.h" #include "ui/emoji_config.h" #include "ui/effects/animation_value.h" #include "ui/painter.h" @@ -194,12 +193,11 @@ void PaintInaccessibleAccountInner( } } -template -[[nodiscard]] QPixmap Generate(int size, Callback callback) { +[[nodiscard]] QPixmap Generate(int size, Fn callback) { auto result = QImage( - QSize(size, size) * cIntRetinaFactor(), + QSize(size, size) * style::DevicePixelRatio(), QImage::Format_ARGB32_Premultiplied); - result.setDevicePixelRatio(cRetinaFactor()); + result.setDevicePixelRatio(style::DevicePixelRatio()); result.fill(Qt::transparent); { Painter p(&result); @@ -223,15 +221,34 @@ QString EmptyUserpic::InaccessibleName() { return QChar(0) + u"inaccessible"_q; } -template +int EmptyUserpic::ColorIndex(uint64 id) { + const auto index = id % 7; + const int map[] = { 0, 7, 4, 1, 6, 3, 5 }; + return map[index]; +} + +EmptyUserpic::BgColors EmptyUserpic::UserpicColor(int id) { + const EmptyUserpic::BgColors colors[] = { + { st::historyPeer1UserpicBg, st::historyPeer1UserpicBg2 }, + { st::historyPeer2UserpicBg, st::historyPeer2UserpicBg2 }, + { st::historyPeer3UserpicBg, st::historyPeer3UserpicBg2 }, + { st::historyPeer4UserpicBg, st::historyPeer4UserpicBg2 }, + { st::historyPeer5UserpicBg, st::historyPeer5UserpicBg2 }, + { st::historyPeer6UserpicBg, st::historyPeer6UserpicBg2 }, + { st::historyPeer7UserpicBg, st::historyPeer7UserpicBg2 }, + { st::historyPeer8UserpicBg, st::historyPeer8UserpicBg2 }, + }; + return colors[id]; +} + void EmptyUserpic::paint( QPainter &p, int x, int y, int outerWidth, int size, - Callback paintBackground) const { - x = rtl() ? (outerWidth - x - size) : x; + Fn paintBackground) const { + x = style::RightToLeft() ? (outerWidth - x - size) : x; const auto fontsize = (size * 13) / 33; auto font = st::historyPeerUserpicFont->f; @@ -252,7 +269,12 @@ void EmptyUserpic::paint( if (IsExternal(_string)) { PaintExternalMessagesInner(p, x, y, size, st::historyPeerUserpicFg); } else if (IsInaccessible(_string)) { - PaintInaccessibleAccountInner(p, x, y, size, st::historyPeerUserpicFg); + PaintInaccessibleAccountInner( + p, + x, + y, + size, + st::historyPeerUserpicFg); } else { p.setFont(font); p.setBrush(Qt::NoBrush); @@ -290,7 +312,12 @@ void EmptyUserpic::paintRounded( }); } -void EmptyUserpic::paintSquare(QPainter &p, int x, int y, int outerWidth, int size) const { +void EmptyUserpic::paintSquare( + QPainter &p, + int x, + int y, + int outerWidth, + int size) const { paint(p, x, y, outerWidth, size, [&] { p.fillRect(x, y, size, size, p.brush()); }); @@ -334,7 +361,7 @@ void EmptyUserpic::PaintSavedMessages( int size, QBrush bg, const style::color &fg) { - x = rtl() ? (outerWidth - x - size) : x; + x = style::RightToLeft() ? (outerWidth - x - size) : x; PainterHighQualityEnabler hq(p); p.setBrush(std::move(bg)); @@ -352,12 +379,18 @@ void EmptyUserpic::PaintSavedMessagesRounded( int size, QBrush bg, const style::color &fg) { - x = rtl() ? (outerWidth - x - size) : x; + x = style::RightToLeft() ? (outerWidth - x - size) : x; PainterHighQualityEnabler hq(p); p.setBrush(std::move(bg)); p.setPen(Qt::NoPen); - p.drawRoundedRect(x, y, size, size, st::roundRadiusSmall, st::roundRadiusSmall); + p.drawRoundedRect( + x, + y, + size, + size, + st::roundRadiusSmall, + st::roundRadiusSmall); PaintSavedMessagesInner(p, x, y, size, fg); } @@ -412,7 +445,7 @@ void EmptyUserpic::PaintRepliesMessages( int size, QBrush bg, const style::color &fg) { - x = rtl() ? (outerWidth - x - size) : x; + x = style::RightToLeft() ? (outerWidth - x - size) : x; PainterHighQualityEnabler hq(p); p.setBrush(bg); @@ -430,12 +463,18 @@ void EmptyUserpic::PaintRepliesMessagesRounded( int size, QBrush bg, const style::color &fg) { - x = rtl() ? (outerWidth - x - size) : x; + x = style::RightToLeft() ? (outerWidth - x - size) : x; PainterHighQualityEnabler hq(p); p.setBrush(bg); p.setPen(Qt::NoPen); - p.drawRoundedRect(x, y, size, size, st::roundRadiusSmall, st::roundRadiusSmall); + p.drawRoundedRect( + x, + y, + size, + size, + st::roundRadiusSmall, + st::roundRadiusSmall); PaintRepliesMessagesInner(p, x, y, size, fg); } @@ -452,17 +491,22 @@ QPixmap EmptyUserpic::GenerateRepliesMessagesRounded(int size) { }); } -InMemoryKey EmptyUserpic::uniqueKey() const { +std::pair EmptyUserpic::uniqueKey() const { const auto first = (uint64(0xFFFFFFFFU) << 32) | anim::getPremultiplied(_colors.color1->c); auto second = uint64(0); - memcpy(&second, _string.constData(), qMin(sizeof(second), _string.size() * sizeof(QChar))); - return InMemoryKey(first, second); + memcpy( + &second, + _string.constData(), + std::min(sizeof(second), size_t(_string.size()) * sizeof(QChar))); + return { first, second }; } QPixmap EmptyUserpic::generate(int size) { - auto result = QImage(QSize(size, size) * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied); - result.setDevicePixelRatio(cRetinaFactor()); + auto result = QImage( + QSize(size, size) * style::DevicePixelRatio(), + QImage::Format_ARGB32_Premultiplied); + result.setDevicePixelRatio(style::DevicePixelRatio()); result.fill(Qt::transparent); { auto p = QPainter(&result); @@ -535,18 +579,4 @@ void EmptyUserpic::fillString(const QString &name) { EmptyUserpic::~EmptyUserpic() = default; -EmptyUserpic::BgColors PeerUserpicColor(PeerId peerId) { - const EmptyUserpic::BgColors colors[] = { - { st::historyPeer1UserpicBg, st::historyPeer1UserpicBg2 }, - { st::historyPeer2UserpicBg, st::historyPeer2UserpicBg2 }, - { st::historyPeer3UserpicBg, st::historyPeer3UserpicBg2 }, - { st::historyPeer4UserpicBg, st::historyPeer4UserpicBg2 }, - { st::historyPeer5UserpicBg, st::historyPeer5UserpicBg2 }, - { st::historyPeer6UserpicBg, st::historyPeer6UserpicBg2 }, - { st::historyPeer7UserpicBg, st::historyPeer7UserpicBg2 }, - { st::historyPeer8UserpicBg, st::historyPeer8UserpicBg2 }, - }; - return colors[Data::PeerColorIndex(peerId)]; -} - } // namespace Ui diff --git a/Telegram/SourceFiles/ui/empty_userpic.h b/Telegram/SourceFiles/ui/empty_userpic.h index d20e6ef26d..01539329c4 100644 --- a/Telegram/SourceFiles/ui/empty_userpic.h +++ b/Telegram/SourceFiles/ui/empty_userpic.h @@ -16,6 +16,9 @@ public: const style::color color2; }; + [[nodiscard]] static int ColorIndex(uint64 id); + [[nodiscard]] static EmptyUserpic::BgColors UserpicColor(int id); + [[nodiscard]] static QString ExternalName(); [[nodiscard]] static QString InaccessibleName(); @@ -40,8 +43,8 @@ public: int y, int outerWidth, int size) const; - QPixmap generate(int size); - InMemoryKey uniqueKey() const; + [[nodiscard]] QPixmap generate(int size); + [[nodiscard]] std::pair uniqueKey() const; static void PaintSavedMessages( QPainter &p, @@ -71,8 +74,8 @@ public: int size, QBrush bg, const style::color &fg); - static QPixmap GenerateSavedMessages(int size); - static QPixmap GenerateSavedMessagesRounded(int size); + [[nodiscard]] static QPixmap GenerateSavedMessages(int size); + [[nodiscard]] static QPixmap GenerateSavedMessagesRounded(int size); static void PaintRepliesMessages( QPainter &p, @@ -102,20 +105,19 @@ public: int size, QBrush bg, const style::color &fg); - static QPixmap GenerateRepliesMessages(int size); - static QPixmap GenerateRepliesMessagesRounded(int size); + [[nodiscard]] static QPixmap GenerateRepliesMessages(int size); + [[nodiscard]] static QPixmap GenerateRepliesMessagesRounded(int size); ~EmptyUserpic(); private: - template void paint( QPainter &p, int x, int y, int outerWidth, int size, - Callback paintBackground) const; + Fn paintBackground) const; void fillString(const QString &name); @@ -124,6 +126,4 @@ private: }; -[[nodiscard]] EmptyUserpic::BgColors PeerUserpicColor(PeerId peerId); - } // namespace Ui diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index 7fb2e8defd..1d3717bf49 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -274,6 +274,8 @@ PRIVATE ui/color_contrast.h ui/color_int_conversion.cpp ui/color_int_conversion.h + ui/empty_userpic.cpp + ui/empty_userpic.h ui/grouped_layout.cpp ui/grouped_layout.h ui/widgets/fields/special_fields.cpp