/* 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 "chat_helpers/stickers_list_widget.h" #include "data/data_document.h" #include "data/data_session.h" #include "data/data_channel.h" #include "ui/widgets/buttons.h" #include "ui/effects/ripple_animation.h" #include "ui/image/image.h" #include "boxes/stickers_box.h" #include "inline_bots/inline_bot_result.h" #include "chat_helpers/stickers.h" #include "storage/localstorage.h" #include "lang/lang_keys.h" #include "mainwindow.h" #include "dialogs/dialogs_layout.h" #include "boxes/sticker_set_box.h" #include "boxes/stickers_box.h" #include "boxes/confirm_box.h" #include "auth_session.h" #include "observer_peer.h" #include "apiwrap.h" #include "styles/style_chat_helpers.h" #include "styles/style_window.h" namespace ChatHelpers { namespace { constexpr auto kInlineItemsMaxPerRow = 5; constexpr auto kSearchRequestDelay = 400; constexpr auto kRecentDisplayLimit = 20; bool SetInMyList(MTPDstickerSet::Flags flags) { return (flags & MTPDstickerSet::Flag::f_installed_date) && !(flags & MTPDstickerSet::Flag::f_archived); } } // namespace struct StickerIcon { StickerIcon(uint64 setId) : setId(setId) { } StickerIcon(uint64 setId, DocumentData *sticker, int pixw, int pixh) : setId(setId) , sticker(sticker) , pixw(pixw) , pixh(pixh) { } uint64 setId = 0; DocumentData *sticker = nullptr; ChannelData *megagroup = nullptr; int pixw = 0; int pixh = 0; }; class StickersListWidget::Footer : public TabbedSelector::InnerFooter, private base::Subscriber { public: Footer(not_null parent); void preloadImages(); void validateSelectedIcon( uint64 setId, ValidateIconAnimations animations); void refreshIcons(ValidateIconAnimations animations); bool hasOnlyFeaturedSets() const; void leaveToChildEvent(QEvent *e, QWidget *child) override; void stealFocus(); void returnFocus(); void setLoading(bool loading); protected: void paintEvent(QPaintEvent *e) override; void resizeEvent(QResizeEvent *e) override; void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; bool event(QEvent *e) override; void processHideFinished() override; private: enum class SpecialOver { None, Search, Settings, }; using OverState = base::variant; template void enumerateVisibleIcons(Callback callback); void step_icons(TimeMs ms, bool timer); void setSelectedIcon( int newSelected, ValidateIconAnimations animations); void refreshIconsGeometry(ValidateIconAnimations animations); void updateSelected(); void finishDragging(); void paintStickerSettingsIcon(Painter &p) const; void paintSearchIcon(Painter &p) const; void paintFeaturedStickerSetsBadge(Painter &p, int iconLeft) const; void paintSetIcon(Painter &p, const StickerIcon &icon, int x) const; void paintSelectionBar(Painter &p) const; void paintLeftRightFading(Painter &p) const; void initSearch(); void toggleSearch(bool visible); void resizeSearchControls(); void scrollByWheelEvent(not_null e); not_null _pan; static constexpr auto kVisibleIconsCount = 8; QList _icons; OverState _iconOver = SpecialOver::None; int _iconSel = 0; OverState _iconDown = SpecialOver::None; bool _iconsDragging = false; BasicAnimation _a_icons; QPoint _iconsMousePos, _iconsMouseDown; int _iconsLeft = 0; int _iconsRight = 0; int _iconsTop = 0; int _iconsStartX = 0; int _iconsMax = 0; anim::value _iconsX; anim::value _iconSelX; TimeMs _iconsStartAnim = 0; bool _horizontal = false; bool _searchShown = false; object_ptr _searchField = { nullptr }; object_ptr _searchCancel = { nullptr }; QPointer _focusTakenFrom; }; StickersListWidget::Set::Set( uint64 id, MTPDstickerSet::Flags flags, const QString &title, const QString &shortName, bool externalLayout, int count, const Stickers::Pack &pack) : id(id) , flags(flags) , title(title) , shortName(shortName) , pack(pack) , externalLayout(externalLayout) , count(count) { } StickersListWidget::Set::Set(Set &&other) = default; StickersListWidget::Set &StickersListWidget::Set::operator=( Set &&other) = default; StickersListWidget::Set::~Set() = default; StickersListWidget::Footer::Footer(not_null parent) : InnerFooter(parent) , _pan(parent) , _a_icons(animation(this, &Footer::step_icons)) { setMouseTracking(true); _iconsLeft = _iconsRight = st::emojiCategorySkip + st::stickerIconWidth; subscribe(Auth().downloaderTaskFinished(), [this] { update(); }); } void StickersListWidget::Footer::initSearch() { _searchField.create( this, st::gifsSearchField, langFactory(lng_stickers_search_sets)); _searchCancel.create(this, st::gifsSearchCancel); _searchField->show(); _searchCancel->show(anim::type::instant); const auto cancelSearch = [=] { if (_searchField->getLastText().isEmpty()) { toggleSearch(false); } else { _searchField->setText(QString()); } }; connect(_searchField, &Ui::InputField::submitted, [=] { _pan->sendSearchRequest(); }); connect(_searchField, &Ui::InputField::cancelled, cancelSearch); connect(_searchField, &Ui::InputField::changed, [=] { _pan->searchForSets(_searchField->getLastText()); }); _searchCancel->setClickedCallback(cancelSearch); resizeSearchControls(); } void StickersListWidget::Footer::toggleSearch(bool visible) { if (_searchShown == visible) { return; } _searchShown = visible; if (_searchShown) { initSearch(); stealFocus(); } else if (_searchField) { returnFocus(); _searchField.destroy(); _searchCancel.destroy(); _focusTakenFrom = nullptr; } update(); } void StickersListWidget::Footer::stealFocus() { if (_searchField) { if (!_focusTakenFrom) { _focusTakenFrom = QApplication::focusWidget(); } _searchField->setFocus(); } } void StickersListWidget::Footer::returnFocus() { if (_searchField && _focusTakenFrom) { if (_searchField->hasFocus()) { _focusTakenFrom->setFocus(); } _focusTakenFrom = nullptr; } } template void StickersListWidget::Footer::enumerateVisibleIcons(Callback callback) { auto iconsX = qRound(_iconsX.current()); auto index = iconsX / st::stickerIconWidth; auto x = _iconsLeft - (iconsX % st::stickerIconWidth); auto first = floorclamp(iconsX, st::stickerIconWidth, 0, _icons.size()); auto last = ceilclamp(iconsX + width(), st::stickerIconWidth, 0, _icons.size()); for (auto index = first; index != last; ++index) { callback(_icons[index], x); x += st::stickerIconWidth; } } void StickersListWidget::Footer::preloadImages() { enumerateVisibleIcons([](const StickerIcon &icon, int x) { if (const auto sticker = icon.sticker) { sticker->loadThumbnail(sticker->stickerSetOrigin()); } }); } void StickersListWidget::Footer::validateSelectedIcon( uint64 setId, ValidateIconAnimations animations) { auto favedIconIndex = -1; auto newSelected = -1; for (auto i = 0, l = _icons.size(); i != l; ++i) { if (_icons[i].setId == setId || (_icons[i].setId == Stickers::FavedSetId && setId == Stickers::RecentSetId)) { newSelected = i; break; } else if (_icons[i].setId == Stickers::FavedSetId) { favedIconIndex = i; } } setSelectedIcon( (newSelected >= 0 ? newSelected : (favedIconIndex >= 0) ? favedIconIndex : 0), animations); } void StickersListWidget::Footer::setSelectedIcon( int newSelected, ValidateIconAnimations animations) { if (_iconSel == newSelected) { return; } _iconSel = newSelected; auto iconSelXFinal = _iconSel * st::stickerIconWidth; if (animations == ValidateIconAnimations::Full) { _iconSelX.start(iconSelXFinal); } else { _iconSelX = anim::value(iconSelXFinal, iconSelXFinal); } auto iconsCountForCentering = (2 * _iconSel + 1); auto iconsWidthForCentering = iconsCountForCentering * st::stickerIconWidth; auto iconsXFinal = snap( (_iconsLeft + iconsWidthForCentering + _iconsRight - width()) / 2, 0, _iconsMax); if (animations == ValidateIconAnimations::None) { _iconsX = anim::value(iconsXFinal, iconsXFinal); _a_icons.stop(); } else { _iconsX.start(iconsXFinal); _iconsStartAnim = getms(); _a_icons.start(); } updateSelected(); update(); } void StickersListWidget::Footer::processHideFinished() { _iconOver = _iconDown = SpecialOver::None; _iconsStartAnim = 0; _a_icons.stop(); _iconsX.finish(); _iconSelX.finish(); _horizontal = false; } void StickersListWidget::Footer::leaveToChildEvent(QEvent *e, QWidget *child) { _iconsMousePos = QCursor::pos(); updateSelected(); } void StickersListWidget::Footer::setLoading(bool loading) { if (_searchCancel) { _searchCancel->setLoadingAnimation(loading); } } void StickersListWidget::Footer::paintEvent(QPaintEvent *e) { Painter p(this); paintSearchIcon(p); if (_icons.isEmpty() || _searchShown) { return; } if (!hasOnlyFeaturedSets()) { paintStickerSettingsIcon(p); } auto clip = QRect( _iconsLeft, _iconsTop, width() - _iconsLeft - _iconsRight, st::emojiFooterHeight); if (rtl()) { clip.moveLeft(width() - _iconsLeft - clip.width()); } p.setClipRect(clip); enumerateVisibleIcons([&](const StickerIcon &icon, int x) { paintSetIcon(p, icon, x); }); paintSelectionBar(p); paintLeftRightFading(p); } void StickersListWidget::Footer::paintSelectionBar(Painter &p) const { auto selxrel = _iconsLeft + qRound(_iconSelX.current()); auto selx = selxrel - qRound(_iconsX.current()); if (rtl()) { selx = width() - selx - st::stickerIconWidth; } p.fillRect( selx, _iconsTop + st::emojiFooterHeight - st::stickerIconPadding, st::stickerIconWidth, st::stickerIconSel, st::stickerIconSelColor); } void StickersListWidget::Footer::paintLeftRightFading(Painter &p) const { auto o_left = snap(_iconsX.current() / st::stickerIconLeft.width(), 0., 1.); if (o_left > 0) { p.setOpacity(o_left); st::stickerIconLeft.fill(p, rtlrect(_iconsLeft, _iconsTop, st::stickerIconLeft.width(), st::emojiFooterHeight, width())); p.setOpacity(1.); } auto o_right = snap((_iconsMax - _iconsX.current()) / st::stickerIconRight.width(), 0., 1.); if (o_right > 0) { p.setOpacity(o_right); st::stickerIconRight.fill(p, rtlrect(width() - _iconsRight - st::stickerIconRight.width(), _iconsTop, st::stickerIconRight.width(), st::emojiFooterHeight, width())); p.setOpacity(1.); } } void StickersListWidget::Footer::resizeEvent(QResizeEvent *e) { if (_searchField) { resizeSearchControls(); } refreshIconsGeometry(ValidateIconAnimations::None); } void StickersListWidget::Footer::resizeSearchControls() { Expects(_searchField != nullptr); Expects(_searchCancel != nullptr); const auto fieldWidth = width() - st::gifsSearchFieldPosition.x() - st::gifsSearchCancelPosition.x() - st::gifsSearchCancel.width; _searchField->resizeToWidth(fieldWidth); _searchField->moveToLeft(st::gifsSearchFieldPosition.x(), st::gifsSearchFieldPosition.y()); _searchCancel->moveToRight(st::gifsSearchCancelPosition.x(), st::gifsSearchCancelPosition.y()); } void StickersListWidget::Footer::mousePressEvent(QMouseEvent *e) { if (e->button() != Qt::LeftButton) { return; } _iconsMousePos = e ? e->globalPos() : QCursor::pos(); updateSelected(); if (_iconOver == SpecialOver::Settings) { Ui::show(Box(hasOnlyFeaturedSets() ? StickersBox::Section::Featured : StickersBox::Section::Installed)); } else if (_iconOver == SpecialOver::Search) { toggleSearch(true); } else { _iconDown = _iconOver; _iconsMouseDown = _iconsMousePos; _iconsStartX = qRound(_iconsX.current()); } } void StickersListWidget::Footer::mouseMoveEvent(QMouseEvent *e) { _iconsMousePos = e ? e->globalPos() : QCursor::pos(); updateSelected(); if (!_iconsDragging && !_icons.isEmpty() && base::get_if(&_iconDown) != nullptr) { if ((_iconsMousePos - _iconsMouseDown).manhattanLength() >= QApplication::startDragDistance()) { _iconsDragging = true; } } if (_iconsDragging) { auto newX = snap(_iconsStartX + (rtl() ? -1 : 1) * (_iconsMouseDown.x() - _iconsMousePos.x()), 0, _iconsMax); if (newX != qRound(_iconsX.current())) { _iconsX = anim::value(newX, newX); _iconsStartAnim = 0; _a_icons.stop(); update(); } } } void StickersListWidget::Footer::mouseReleaseEvent(QMouseEvent *e) { if (_icons.isEmpty()) { return; } const auto wasDown = std::exchange(_iconDown, SpecialOver::None); _iconsMousePos = e ? e->globalPos() : QCursor::pos(); if (_iconsDragging) { finishDragging(); return; } updateSelected(); if (wasDown == _iconOver) { if (const auto index = base::get_if(&_iconOver)) { _iconSelX = anim::value( *index * st::stickerIconWidth, *index * st::stickerIconWidth); _pan->showStickerSet(_icons[*index].setId); } } } void StickersListWidget::Footer::finishDragging() { auto newX = snap(_iconsStartX + _iconsMouseDown.x() - _iconsMousePos.x(), 0, _iconsMax); if (newX != qRound(_iconsX.current())) { _iconsX = anim::value(newX, newX); _iconsStartAnim = 0; _a_icons.stop(); update(); } _iconsDragging = false; updateSelected(); } bool StickersListWidget::Footer::event(QEvent *e) { if (e->type() == QEvent::TouchBegin) { } else if (e->type() == QEvent::Wheel) { if (!_icons.isEmpty() && (base::get_if(&_iconOver) != nullptr) && (_iconDown == SpecialOver::None)) { scrollByWheelEvent(static_cast(e)); } } return InnerFooter::event(e); } void StickersListWidget::Footer::scrollByWheelEvent( not_null e) { auto horizontal = (e->angleDelta().x() != 0 || e->orientation() == Qt::Horizontal); auto vertical = (e->angleDelta().y() != 0 || e->orientation() == Qt::Vertical); if (horizontal) { _horizontal = true; } auto newX = qRound(_iconsX.current()); if (/*_horizontal && */horizontal) { newX = snap(newX - (rtl() ? -1 : 1) * (e->pixelDelta().x() ? e->pixelDelta().x() : e->angleDelta().x()), 0, _iconsMax); } else if (/*!_horizontal && */vertical) { newX = snap(newX - (e->pixelDelta().y() ? e->pixelDelta().y() : e->angleDelta().y()), 0, _iconsMax); } if (newX != qRound(_iconsX.current())) { _iconsX = anim::value(newX, newX); _iconsStartAnim = 0; _a_icons.stop(); updateSelected(); update(); } } void StickersListWidget::Footer::updateSelected() { if (_iconDown >= 0) { return; } auto p = mapFromGlobal(_iconsMousePos); auto x = p.x(), y = p.y(); if (rtl()) x = width() - x; const auto settingsLeft = width() - _iconsRight; const auto searchLeft = _iconsLeft - st::stickerIconWidth; auto newOver = OverState(SpecialOver::None); if (x >= searchLeft && x < searchLeft + st::stickerIconWidth && y >= _iconsTop && y < _iconsTop + st::emojiFooterHeight) { newOver = SpecialOver::Search; } else if (x >= settingsLeft && x < settingsLeft + st::stickerIconWidth && y >= _iconsTop && y < _iconsTop + st::emojiFooterHeight) { if (!_icons.isEmpty() && !hasOnlyFeaturedSets()) { newOver = SpecialOver::Settings; } } else if (!_icons.isEmpty()) { if (y >= _iconsTop && y < _iconsTop + st::emojiFooterHeight && x >= _iconsLeft && x < width() - _iconsRight) { x += qRound(_iconsX.current()) - _iconsLeft; if (x < _icons.size() * st::stickerIconWidth) { newOver = qFloor(x / st::stickerIconWidth); } } } if (newOver != _iconOver) { if (newOver == SpecialOver::None) { setCursor(style::cur_default); } else if (_iconOver == SpecialOver::None) { setCursor(style::cur_pointer); } _iconOver = newOver; } } void StickersListWidget::Footer::refreshIcons( ValidateIconAnimations animations) { _pan->fillIcons(_icons); refreshIconsGeometry(animations); } void StickersListWidget::Footer::refreshIconsGeometry( ValidateIconAnimations animations) { _iconOver = _iconDown = SpecialOver::None; _iconsX.finish(); _iconSelX.finish(); _iconsStartAnim = 0; _a_icons.stop(); _iconsMax = std::max( _iconsLeft + int(_icons.size()) * st::stickerIconWidth + _iconsRight - width(), 0); if (_iconsX.current() > _iconsMax) { _iconsX = anim::value(_iconsMax, _iconsMax); } updateSelected(); _pan->validateSelectedIcon(animations); update(); } bool StickersListWidget::Footer::hasOnlyFeaturedSets() const { return (_icons.size() == 1) && (_icons[0].setId == Stickers::FeaturedSetId); } void StickersListWidget::Footer::paintStickerSettingsIcon(Painter &p) const { const auto settingsLeft = width() - _iconsRight; st::stickersSettings.paint(p, settingsLeft + (st::stickerIconWidth - st::stickersSettings.width()) / 2, _iconsTop + st::emojiCategory.iconPosition.y(), width()); } void StickersListWidget::Footer::paintSearchIcon(Painter &p) const { const auto searchLeft = _iconsLeft - st::stickerIconWidth; st::stickersSearch.paint(p, searchLeft + (st::stickerIconWidth - st::stickersSearch.width()) / 2, _iconsTop + st::emojiCategory.iconPosition.y(), width()); } void StickersListWidget::Footer::paintFeaturedStickerSetsBadge(Painter &p, int iconLeft) const { if (auto unread = Auth().data().featuredStickerSetsUnreadCount()) { Dialogs::Layout::UnreadBadgeStyle unreadSt; unreadSt.sizeId = Dialogs::Layout::UnreadBadgeInStickersPanel; unreadSt.size = st::stickersSettingsUnreadSize; int unreadRight = iconLeft + st::stickerIconWidth - st::stickersSettingsUnreadPosition.x(); if (rtl()) unreadRight = width() - unreadRight; int unreadTop = _iconsTop + st::stickersSettingsUnreadPosition.y(); Dialogs::Layout::paintUnreadCount(p, QString::number(unread), unreadRight, unreadTop, unreadSt); } } void StickersListWidget::Footer::paintSetIcon( Painter &p, const StickerIcon &icon, int x) const { if (icon.sticker) { const auto origin = icon.sticker->stickerSetOrigin(); if (const auto thumb = icon.sticker->thumbnail()) { thumb->load(origin); auto pix = thumb->pix(origin, icon.pixw, icon.pixh); p.drawPixmapLeft(x + (st::stickerIconWidth - icon.pixw) / 2, _iconsTop + (st::emojiFooterHeight - icon.pixh) / 2, width(), pix); } } else if (icon.megagroup) { icon.megagroup->paintUserpicLeft(p, x + (st::stickerIconWidth - st::stickerGroupCategorySize) / 2, _iconsTop + (st::emojiFooterHeight - st::stickerGroupCategorySize) / 2, width(), st::stickerGroupCategorySize); } else { auto getSpecialSetIcon = [](uint64 setId) { if (setId == Stickers::FeaturedSetId) { return &st::stickersTrending; //} else if (setId == Stickers::FavedSetId) { // return &st::stickersFaved; } return &st::emojiRecent; }; auto paintedIcon = getSpecialSetIcon(icon.setId); paintedIcon->paint(p, x + (st::stickerIconWidth - paintedIcon->width()) / 2, _iconsTop + st::emojiCategory.iconPosition.y(), width()); if (icon.setId == Stickers::FeaturedSetId) { paintFeaturedStickerSetsBadge(p, x); } } } void StickersListWidget::Footer::step_icons(TimeMs ms, bool timer) { if (anim::Disabled()) { ms += st::stickerIconMove; } if (_iconsStartAnim) { auto dt = (ms - _iconsStartAnim) / float64(st::stickerIconMove); if (dt >= 1) { _iconsStartAnim = 0; _iconsX.finish(); _iconSelX.finish(); } else { _iconsX.update(dt, anim::linear); _iconSelX.update(dt, anim::linear); } } if (timer) update(); if (!_iconsStartAnim) { _a_icons.stop(); } } StickersListWidget::StickersListWidget(QWidget *parent, not_null controller) : Inner(parent, controller) , _section(Section::Stickers) , _megagroupSetAbout(st::columnMinimalWidthThird - st::emojiScroll.width - st::emojiPanHeaderLeft) , _addText(lang(lng_stickers_featured_add).toUpper()) , _addWidth(st::stickersTrendingAdd.font->width(_addText)) , _settings(this, lang(lng_stickers_you_have)) , _previewTimer([=] { showPreview(); }) , _searchRequestTimer([=] { sendSearchRequest(); }) { setMouseTracking(true); setAttribute(Qt::WA_OpaquePaintEvent); _settings->addClickHandler([] { Ui::show(Box(StickersBox::Section::Installed)); }); subscribe(Auth().downloaderTaskFinished(), [=] { if (isVisible()) { update(); readVisibleSets(); } }); subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::ChannelStickersChanged, [this](const Notify::PeerUpdate &update) { if (update.peer == _megagroupSet) { refreshStickers(); } })); } rpl::producer> StickersListWidget::chosen() const { return _chosen.events(); } rpl::producer<> StickersListWidget::scrollUpdated() const { return _scrollUpdated.events(); } rpl::producer<> StickersListWidget::checkForHide() const { return _checkForHide.events(); } object_ptr StickersListWidget::createFooter() { Expects(_footer == nullptr); auto result = object_ptr