Slightly refactored StickersBox class.

This commit is contained in:
23rd 2023-08-23 17:16:37 +03:00
parent 0d4a83ea47
commit aeb593dd77
2 changed files with 52 additions and 63 deletions

View file

@ -53,6 +53,22 @@ constexpr auto kArchivedLimitFirstRequest = 10;
constexpr auto kArchivedLimitPerPage = 30; constexpr auto kArchivedLimitPerPage = 30;
constexpr auto kHandleMegagroupSetAddressChangeTimeout = crl::time(1000); constexpr auto kHandleMegagroupSetAddressChangeTimeout = crl::time(1000);
[[nodiscard]] QString FillSetTitle(
not_null<StickersSet*> set,
int maxNameWidth,
int *outTitleWidth) {
auto result = set->title;
auto titleWidth = st::contactsNameStyle.font->width(result);
if (titleWidth > maxNameWidth) {
result = st::contactsNameStyle.font->elided(result, maxNameWidth);
titleWidth = st::contactsNameStyle.font->width(result);
}
if (outTitleWidth) {
*outTitleWidth = titleWidth;
}
return result;
}
} // namespace } // namespace
class StickersBox::CounterWidget : public Ui::RpWidget { class StickersBox::CounterWidget : public Ui::RpWidget {
@ -98,9 +114,9 @@ public:
void updateRows(); // refresh only pack cover stickers void updateRows(); // refresh only pack cover stickers
bool appendSet(not_null<StickersSet*> set); bool appendSet(not_null<StickersSet*> set);
StickersSetsOrder getOrder() const; StickersSetsOrder order() const;
StickersSetsOrder getFullOrder() const; StickersSetsOrder fullOrder() const;
StickersSetsOrder getRemovedSets() const; StickersSetsOrder removedSets() const;
void setFullOrder(const StickersSetsOrder &order); void setFullOrder(const StickersSetsOrder &order);
void setRemovedSets(const StickersSetsOrder &removed); void setRemovedSets(const StickersSetsOrder &removed);
@ -117,8 +133,6 @@ public:
_loadMoreCallback = std::move(callback); _loadMoreCallback = std::move(callback);
} }
void setMinHeight(int newWidth, int minHeight);
int getVisibleTop() const { int getVisibleTop() const {
return _visibleTop; return _visibleTop;
} }
@ -156,13 +170,13 @@ private:
int32 pixh); int32 pixh);
~Row(); ~Row();
bool isRecentSet() const; [[nodiscard]] bool isRecentSet() const;
bool isMasksSet() const; [[nodiscard]] bool isMasksSet() const;
bool isEmojiSet() const; [[nodiscard]] bool isEmojiSet() const;
bool isWebm() const; [[nodiscard]] bool isWebm() const;
bool isInstalled() const; [[nodiscard]] bool isInstalled() const;
bool isUnread() const; [[nodiscard]] bool isUnread() const;
bool isArchived() const; [[nodiscard]] bool isArchived() const;
const not_null<StickersSet*> set; const not_null<StickersSet*> set;
DocumentData *sticker = nullptr; DocumentData *sticker = nullptr;
@ -246,10 +260,6 @@ private:
void rebuildAppendSet(not_null<StickersSet*> set); void rebuildAppendSet(not_null<StickersSet*> set);
void fillSetCover(not_null<StickersSet*> set, DocumentData **outSticker, int *outWidth, int *outHeight) const; void fillSetCover(not_null<StickersSet*> set, DocumentData **outSticker, int *outWidth, int *outHeight) const;
int fillSetCount(not_null<StickersSet*> set) const; int fillSetCount(not_null<StickersSet*> set) const;
[[nodiscard]] QString fillSetTitle(
not_null<StickersSet*> set,
int maxNameWidth,
int *outTitleWidth) const;
[[nodiscard]] Data::StickersSetFlags fillSetFlags( [[nodiscard]] Data::StickersSetFlags fillSetFlags(
not_null<StickersSet*> set) const; not_null<StickersSet*> set) const;
void rebuildMegagroupSet(); void rebuildMegagroupSet();
@ -670,7 +680,7 @@ void StickersBox::prepare() {
} else { // _section == Section::Featured } else { // _section == Section::Featured
_tab = &_featured; _tab = &_featured;
} }
setInnerWidget(_tab->takeWidget(), getTopSkip()); setInnerWidget(_tab->takeWidget(), topSkip());
setDimensions(st::boxWideWidth, st::boxMaxListHeight); setDimensions(st::boxWideWidth, st::boxMaxListHeight);
session().data().stickers().updated(_isEmoji session().data().stickers().updated(_isEmoji
@ -793,7 +803,7 @@ void StickersBox::paintEvent(QPaintEvent *e) {
Painter p(this); Painter p(this);
if (_slideAnimation) { if (_slideAnimation) {
_slideAnimation->paintFrame(p, 0, getTopSkip(), width()); _slideAnimation->paintFrame(p, 0, topSkip(), width());
if (!_slideAnimation->animating()) { if (!_slideAnimation->animating()) {
_slideAnimation.reset(); _slideAnimation.reset();
setInnerVisible(true); setInnerVisible(true);
@ -810,7 +820,7 @@ void StickersBox::updateTabsGeometry() {
_tabs->resizeToWidth(_tabIndices.size() * width() / maxTabs); _tabs->resizeToWidth(_tabIndices.size() * width() / maxTabs);
_unreadBadge->setVisible(_tabIndices.contains(Section::Featured)); _unreadBadge->setVisible(_tabIndices.contains(Section::Featured));
setInnerTopSkip(getTopSkip()); setInnerTopSkip(topSkip());
auto featuredLeft = width() / maxTabs; auto featuredLeft = width() / maxTabs;
auto featuredRight = 2 * width() / maxTabs; auto featuredRight = 2 * width() / maxTabs;
@ -826,7 +836,7 @@ void StickersBox::updateTabsGeometry() {
_tabs->moveToLeft(0, 0); _tabs->moveToLeft(0, 0);
} }
int StickersBox::getTopSkip() const { int StickersBox::topSkip() const {
return _tabs ? (_tabs->height() - st::lineWidth) : 0; return _tabs ? (_tabs->height() - st::lineWidth) : 0;
} }
@ -855,8 +865,8 @@ void StickersBox::switchTab() {
} }
if (_tab == &_installed) { if (_tab == &_installed) {
_localOrder = _tab->widget()->getFullOrder(); _localOrder = _tab->widget()->fullOrder();
_localRemoved = _tab->widget()->getRemovedSets(); _localRemoved = _tab->widget()->removedSets();
} }
auto wasCache = grabContentCache(); auto wasCache = grabContentCache();
auto wasIndex = _tab->index(); auto wasIndex = _tab->index();
@ -867,12 +877,12 @@ void StickersBox::switchTab() {
_tab->returnWidget(std::move(widget)); _tab->returnWidget(std::move(widget));
_tab = newTab; _tab = newTab;
_section = newSection; _section = newSection;
setInnerWidget(_tab->takeWidget(), getTopSkip()); setInnerWidget(_tab->takeWidget(), topSkip());
_tabs->raise(); _tabs->raise();
_unreadBadge->raise(); _unreadBadge->raise();
_tab->widget()->show(); _tab->widget()->show();
rebuildList(); rebuildList();
scrollToY(_tab->getScrollTop()); scrollToY(_tab->scrollTop());
setInnerVisible(true); setInnerVisible(true);
auto nowCache = grabContentCache(); auto nowCache = grabContentCache();
auto nowIndex = _tab->index(); auto nowIndex = _tab->index();
@ -937,7 +947,7 @@ void StickersBox::installSet(uint64 setId) {
} }
void StickersBox::installDone( void StickersBox::installDone(
const MTPmessages_StickerSetInstallResult &result) { const MTPmessages_StickerSetInstallResult &result) const {
if (result.type() == mtpc_messages_stickerSetInstallResultArchive) { if (result.type() == mtpc_messages_stickerSetInstallResultArchive) {
session().data().stickers().applyArchivedResult( session().data().stickers().applyArchivedResult(
result.c_messages_stickerSetInstallResultArchive()); result.c_messages_stickerSetInstallResultArchive());
@ -1035,8 +1045,8 @@ void StickersBox::rebuildList(Tab *tab) {
} }
if ((tab == &_installed) || (tab == &_masks) || (_tab == &_featured)) { if ((tab == &_installed) || (tab == &_masks) || (_tab == &_featured)) {
_localOrder = tab->widget()->getFullOrder(); _localOrder = tab->widget()->fullOrder();
_localRemoved = tab->widget()->getRemovedSets(); _localRemoved = tab->widget()->removedSets();
} }
tab->widget()->rebuild(_isMasks); tab->widget()->rebuild(_isMasks);
if ((tab == &_installed) || (tab == &_masks) || (_tab == &_featured)) { if ((tab == &_installed) || (tab == &_masks) || (_tab == &_featured)) {
@ -1066,14 +1076,14 @@ void StickersBox::saveChanges() {
} }
if (installed) { if (installed) {
session().api().saveStickerSets( session().api().saveStickerSets(
installed->getOrder(), installed->order(),
installed->getRemovedSets(), installed->removedSets(),
Data::StickersType::Stickers); Data::StickersType::Stickers);
} }
if (masks) { if (masks) {
session().api().saveStickerSets( session().api().saveStickerSets(
masks->getOrder(), masks->order(),
masks->getRemovedSets(), masks->removedSets(),
Data::StickersType::Masks); Data::StickersType::Masks);
} }
} }
@ -1092,7 +1102,7 @@ const Data::StickersSetsOrder &StickersBox::archivedSetsOrder() const {
: session().data().stickers().archivedMaskSetsOrder(); : session().data().stickers().archivedMaskSetsOrder();
} }
Data::StickersSetsOrder &StickersBox::archivedSetsOrderRef() { Data::StickersSetsOrder &StickersBox::archivedSetsOrderRef() const {
return !_isMasks return !_isMasks
? session().data().stickers().archivedSetsOrderRef() ? session().data().stickers().archivedSetsOrderRef()
: session().data().stickers().archivedMaskSetsOrderRef(); : session().data().stickers().archivedMaskSetsOrderRef();
@ -2180,7 +2190,7 @@ void StickersBox::Inner::rebuildMegagroupSet() {
auto removed = false; auto removed = false;
auto maxNameWidth = countMaxNameWidth(!_isInstalledTab); auto maxNameWidth = countMaxNameWidth(!_isInstalledTab);
auto titleWidth = 0; auto titleWidth = 0;
auto title = fillSetTitle(set, maxNameWidth, &titleWidth); auto title = FillSetTitle(set, maxNameWidth, &titleWidth);
if (!_megagroupSelectedSet if (!_megagroupSelectedSet
|| _megagroupSelectedSet->set->id != set->id) { || _megagroupSelectedSet->set->id != set->id) {
_megagroupSetField->setText(set->shortName); _megagroupSetField->setText(set->shortName);
@ -2283,11 +2293,6 @@ void StickersBox::Inner::setMegagroupSelectedSet(
updateSelected(); updateSelected();
} }
void StickersBox::Inner::setMinHeight(int newWidth, int minHeight) {
_minHeight = minHeight;
updateSize(newWidth);
}
void StickersBox::Inner::updateSize(int newWidth) { void StickersBox::Inner::updateSize(int newWidth) {
auto naturalHeight = _itemsTop + int(_rows.size()) * _rowHeight + st::membersMarginBottom; auto naturalHeight = _itemsTop + int(_rows.size()) * _rowHeight + st::membersMarginBottom;
resize(newWidth ? newWidth : width(), qMax(_minHeight, naturalHeight)); resize(newWidth ? newWidth : width(), qMax(_minHeight, naturalHeight));
@ -2335,7 +2340,7 @@ void StickersBox::Inner::updateRows() {
&& row->isInstalled() && row->isInstalled()
&& !row->isArchived() && !row->isArchived()
&& !row->removed); && !row->removed);
row->title = fillSetTitle( row->title = FillSetTitle(
set, set,
installedSet ? maxNameWidthInstalled : maxNameWidth, installedSet ? maxNameWidthInstalled : maxNameWidth,
&row->titleWidth); &row->titleWidth);
@ -2397,7 +2402,7 @@ void StickersBox::Inner::rebuildAppendSet(not_null<StickersSet*> set) {
&& !(flagsOverride & SetFlag::Archived) && !(flagsOverride & SetFlag::Archived)
&& !removed); && !removed);
int titleWidth = 0; int titleWidth = 0;
QString title = fillSetTitle(set, maxNameWidth, &titleWidth); QString title = FillSetTitle(set, maxNameWidth, &titleWidth);
int count = fillSetCount(set); int count = fillSetCount(set);
const auto existing = [&]{ const auto existing = [&]{
@ -2524,22 +2529,6 @@ int StickersBox::Inner::fillSetCount(not_null<StickersSet*> set) const {
return result + added; return result + added;
} }
QString StickersBox::Inner::fillSetTitle(
not_null<StickersSet*> set,
int maxNameWidth,
int *outTitleWidth) const {
auto result = set->title;
int titleWidth = st::contactsNameStyle.font->width(result);
if (titleWidth > maxNameWidth) {
result = st::contactsNameStyle.font->elided(result, maxNameWidth);
titleWidth = st::contactsNameStyle.font->width(result);
}
if (outTitleWidth) {
*outTitleWidth = titleWidth;
}
return result;
}
Data::StickersSetFlags StickersBox::Inner::fillSetFlags( Data::StickersSetFlags StickersBox::Inner::fillSetFlags(
not_null<StickersSet*> set) const { not_null<StickersSet*> set) const {
const auto result = set->flags; const auto result = set->flags;
@ -2560,19 +2549,19 @@ StickersSetsOrder StickersBox::Inner::collectSets(Check check) const {
return result; return result;
} }
StickersSetsOrder StickersBox::Inner::getOrder() const { StickersSetsOrder StickersBox::Inner::order() const {
return collectSets([](Row *row) { return collectSets([](Row *row) {
return !row->isArchived() && !row->removed && !row->isRecentSet(); return !row->isArchived() && !row->removed && !row->isRecentSet();
}); });
} }
StickersSetsOrder StickersBox::Inner::getFullOrder() const { StickersSetsOrder StickersBox::Inner::fullOrder() const {
return collectSets([](Row *row) { return collectSets([](Row *row) {
return !row->isRecentSet(); return !row->isRecentSet();
}); });
} }
StickersSetsOrder StickersBox::Inner::getRemovedSets() const { StickersSetsOrder StickersBox::Inner::removedSets() const {
return collectSets([](Row *row) { return collectSets([](Row *row) {
return row->removed; return row->removed;
}); });

View file

@ -104,7 +104,7 @@ private:
[[nodiscard]] int index() const; [[nodiscard]] int index() const;
void saveScrollTop(); void saveScrollTop();
int getScrollTop() const { int scrollTop() const {
return _scrollTop; return _scrollTop;
} }
@ -122,12 +122,12 @@ private:
void updateTabsGeometry(); void updateTabsGeometry();
void switchTab(); void switchTab();
void installSet(uint64 setId); void installSet(uint64 setId);
int getTopSkip() const; int topSkip() const;
void saveChanges(); void saveChanges();
QPixmap grabContentCache(); QPixmap grabContentCache();
void installDone(const MTPmessages_StickerSetInstallResult &result); void installDone(const MTPmessages_StickerSetInstallResult &result) const;
void installFail(const MTP::Error &error, uint64 setId); void installFail(const MTP::Error &error, uint64 setId);
void preloadArchivedSets(); void preloadArchivedSets();
@ -139,7 +139,7 @@ private:
void showAttachedStickers(); void showAttachedStickers();
const Data::StickersSetsOrder &archivedSetsOrder() const; const Data::StickersSetsOrder &archivedSetsOrder() const;
Data::StickersSetsOrder &archivedSetsOrderRef(); Data::StickersSetsOrder &archivedSetsOrderRef() const;
std::array<Inner*, 5> widgets() const; std::array<Inner*, 5> widgets() const;