stickers_box.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "stickers_box.h"
  8. #include "data/data_document.h"
  9. #include "data/data_session.h"
  10. #include "data/data_channel.h"
  11. #include "data/data_file_origin.h"
  12. #include "data/data_document_media.h"
  13. #include "data/data_premium_limits.h"
  14. #include "data/stickers/data_stickers.h"
  15. #include "core/application.h"
  16. #include "lang/lang_keys.h"
  17. #include "mainwidget.h"
  18. #include "mainwindow.h"
  19. #include "ui/boxes/boost_box.h"
  20. #include "ui/boxes/confirm_box.h"
  21. #include "boxes/peers/edit_peer_color_box.h"
  22. #include "boxes/sticker_set_box.h"
  23. #include "apiwrap.h"
  24. #include "storage/storage_account.h"
  25. #include "lottie/lottie_single_player.h"
  26. #include "chat_helpers/compose/compose_show.h"
  27. #include "chat_helpers/stickers_lottie.h"
  28. #include "ui/widgets/buttons.h"
  29. #include "ui/widgets/labels.h"
  30. #include "ui/widgets/scroll_area.h"
  31. #include "ui/widgets/shadow.h"
  32. #include "ui/effects/ripple_animation.h"
  33. #include "ui/effects/slide_animation.h"
  34. #include "ui/widgets/discrete_sliders.h"
  35. #include "ui/widgets/fields/input_field.h"
  36. #include "ui/widgets/fields/special_fields.h"
  37. #include "ui/image/image.h"
  38. #include "ui/cached_round_corners.h"
  39. #include "ui/painter.h"
  40. #include "ui/unread_badge_paint.h"
  41. #include "media/clip/media_clip_reader.h"
  42. #include "main/main_session.h"
  43. #include "styles/style_layers.h"
  44. #include "styles/style_boxes.h"
  45. #include "styles/style_chat_helpers.h"
  46. namespace {
  47. using Data::StickersSet;
  48. using Data::StickersSetsOrder;
  49. using Data::StickersSetThumbnailView;
  50. using SetFlag = Data::StickersSetFlag;
  51. constexpr auto kArchivedLimitFirstRequest = 10;
  52. constexpr auto kArchivedLimitPerPage = 30;
  53. constexpr auto kHandleMegagroupSetAddressChangeTimeout = crl::time(1000);
  54. [[nodiscard]] QString FillSetTitle(
  55. not_null<StickersSet*> set,
  56. int maxNameWidth,
  57. int *outTitleWidth) {
  58. auto result = set->title;
  59. auto titleWidth = st::contactsNameStyle.font->width(result);
  60. if (titleWidth > maxNameWidth) {
  61. result = st::contactsNameStyle.font->elided(result, maxNameWidth);
  62. titleWidth = st::contactsNameStyle.font->width(result);
  63. }
  64. if (outTitleWidth) {
  65. *outTitleWidth = titleWidth;
  66. }
  67. return result;
  68. }
  69. } // namespace
  70. class StickersBox::CounterWidget : public Ui::RpWidget {
  71. public:
  72. CounterWidget(QWidget *parent, rpl::producer<int> count);
  73. protected:
  74. void paintEvent(QPaintEvent *e) override;
  75. private:
  76. void setCounter(int counter);
  77. QString _text;
  78. Ui::UnreadBadgeStyle _st;
  79. };
  80. // This class is hold in header because it requires Qt preprocessing.
  81. class StickersBox::Inner : public Ui::RpWidget {
  82. public:
  83. using Section = StickersBox::Section;
  84. Inner(
  85. QWidget *parent,
  86. std::shared_ptr<ChatHelpers::Show> show,
  87. Section section);
  88. Inner(
  89. QWidget *parent,
  90. std::shared_ptr<ChatHelpers::Show> show,
  91. not_null<ChannelData*> megagroup,
  92. bool isEmoji);
  93. [[nodiscard]] Main::Session &session() const;
  94. rpl::producer<int> scrollsToY() const {
  95. return _scrollsToY.events();
  96. }
  97. void setInnerFocus();
  98. void saveGroupSet(Fn<void()> done);
  99. void rebuild(bool masks);
  100. void updateSize(int newWidth = 0);
  101. void updateRows(); // refresh only pack cover stickers
  102. bool appendSet(not_null<StickersSet*> set);
  103. StickersSetsOrder order() const;
  104. StickersSetsOrder fullOrder() const;
  105. StickersSetsOrder removedSets() const;
  106. void setFullOrder(const StickersSetsOrder &order);
  107. void setRemovedSets(const StickersSetsOrder &removed);
  108. void setRowRemovedBySetId(uint64 setId, bool removed);
  109. void setInstallSetCallback(Fn<void(uint64 setId)> callback) {
  110. _installSetCallback = std::move(callback);
  111. }
  112. void setRemoveSetCallback(Fn<void(uint64 setId)> callback) {
  113. _removeSetCallback = std::move(callback);
  114. }
  115. void setLoadMoreCallback(Fn<void()> callback) {
  116. _loadMoreCallback = std::move(callback);
  117. }
  118. int getVisibleTop() const {
  119. return _visibleTop;
  120. }
  121. [[nodiscard]] rpl::producer<int> draggingScrollDelta() const {
  122. return _draggingScrollDelta.events();
  123. }
  124. ~Inner();
  125. protected:
  126. void visibleTopBottomUpdated(
  127. int visibleTop,
  128. int visibleBottom) override;
  129. void paintEvent(QPaintEvent *e) override;
  130. void resizeEvent(QResizeEvent *e) override;
  131. void mousePressEvent(QMouseEvent *e) override;
  132. void mouseMoveEvent(QMouseEvent *e) override;
  133. void mouseReleaseEvent(QMouseEvent *e) override;
  134. void leaveEventHook(QEvent *e) override;
  135. void leaveToChildEvent(QEvent *e, QWidget *child) override;
  136. private:
  137. struct Row {
  138. Row(
  139. not_null<StickersSet*> set,
  140. DocumentData *sticker,
  141. int32 count,
  142. const QString &title,
  143. int titleWidth,
  144. Data::StickersSetFlags flagsOverride,
  145. bool removed,
  146. int32 pixw,
  147. int32 pixh);
  148. ~Row();
  149. [[nodiscard]] bool isRecentSet() const;
  150. [[nodiscard]] bool isMasksSet() const;
  151. [[nodiscard]] bool isEmojiSet() const;
  152. [[nodiscard]] bool isInstalled() const;
  153. [[nodiscard]] bool isUnread() const;
  154. [[nodiscard]] bool isArchived() const;
  155. const not_null<StickersSet*> set;
  156. DocumentData *sticker = nullptr;
  157. std::shared_ptr<Data::DocumentMedia> stickerMedia;
  158. std::shared_ptr<StickersSetThumbnailView> thumbnailMedia;
  159. int32 count = 0;
  160. QString title;
  161. int titleWidth = 0;
  162. Data::StickersSetFlags flagsOverride;
  163. bool removed = false;
  164. int32 pixw = 0;
  165. int32 pixh = 0;
  166. anim::value yadd;
  167. std::unique_ptr<Ui::RippleAnimation> ripple;
  168. std::unique_ptr<Lottie::SinglePlayer> lottie;
  169. Media::Clip::ReaderPointer webm;
  170. };
  171. struct MegagroupSet {
  172. inline bool operator==(const MegagroupSet &other) const {
  173. return true;
  174. }
  175. inline bool operator!=(const MegagroupSet &other) const {
  176. return false;
  177. }
  178. };
  179. using SelectedRow = std::variant<v::null_t, MegagroupSet, int>;
  180. class AddressField : public Ui::UsernameInput {
  181. public:
  182. using UsernameInput::UsernameInput;
  183. protected:
  184. void correctValue(
  185. const QString &was,
  186. int wasCursor,
  187. QString &now,
  188. int &nowCursor) override;
  189. };
  190. template <typename Check>
  191. StickersSetsOrder collectSets(Check check) const;
  192. void updateSelected();
  193. void checkGroupLevel(Fn<void()> done);
  194. void checkLoadMore();
  195. void updateScrollbarWidth();
  196. int getRowIndex(uint64 setId) const;
  197. void setRowRemoved(int index, bool removed);
  198. void setSelected(SelectedRow selected);
  199. void setActionDown(int newActionDown);
  200. void setPressed(SelectedRow pressed);
  201. void setup();
  202. QRect relativeButtonRect(bool removeButton, bool installedSet) const;
  203. void ensureRipple(
  204. const style::RippleAnimation &st,
  205. QImage mask,
  206. bool removeButton,
  207. bool installedSet);
  208. bool shiftingAnimationCallback(crl::time now);
  209. void paintRow(Painter &p, not_null<Row*> row, int index);
  210. void paintRowThumbnail(Painter &p, not_null<Row*> row, int left);
  211. void paintFakeButton(Painter &p, not_null<Row*> row, int index);
  212. void clear();
  213. void updateCursor();
  214. void setActionSel(int32 actionSel);
  215. float64 aboveShadowOpacity() const;
  216. void validateLottieAnimation(not_null<Row*> row);
  217. void validateWebmAnimation(not_null<Row*> row);
  218. void validateAnimation(not_null<Row*> row);
  219. void updateRowThumbnail(not_null<Row*> row);
  220. void clipCallback(
  221. not_null<Row*> row,
  222. Media::Clip::Notification notification);
  223. void readVisibleSets();
  224. void updateControlsGeometry();
  225. void rebuildAppendSet(not_null<StickersSet*> set);
  226. void fillSetCover(not_null<StickersSet*> set, DocumentData **outSticker, int *outWidth, int *outHeight) const;
  227. int fillSetCount(not_null<StickersSet*> set) const;
  228. [[nodiscard]] Data::StickersSetFlags fillSetFlags(
  229. not_null<StickersSet*> set) const;
  230. void rebuildMegagroupSet();
  231. void handleMegagroupSetAddressChange();
  232. void setMegagroupSelectedSet(const StickerSetIdentifier &set);
  233. int countMaxNameWidth(bool installedSet) const;
  234. [[nodiscard]] bool skipPremium() const;
  235. const style::PeerListItem &_st;
  236. const std::shared_ptr<ChatHelpers::Show> _show;
  237. const not_null<Main::Session*> _session;
  238. MTP::Sender _api;
  239. const Section _section;
  240. const bool _isInstalledTab;
  241. Ui::RoundRect _buttonBgOver, _buttonBg, _inactiveButtonBg;
  242. int32 _rowHeight = 0;
  243. std::vector<std::unique_ptr<Row>> _rows;
  244. std::vector<std::unique_ptr<Row>> _oldRows;
  245. std::vector<crl::time> _shiftingStartTimes;
  246. crl::time _aboveShadowFadeStart = 0;
  247. anim::value _aboveShadowFadeOpacity;
  248. Ui::Animations::Basic _shiftingAnimation;
  249. Fn<void(uint64 setId)> _installSetCallback;
  250. Fn<void(uint64 setId)> _removeSetCallback;
  251. Fn<void()> _loadMoreCallback;
  252. int _visibleTop = 0;
  253. int _visibleBottom = 0;
  254. int _itemsTop = 0;
  255. int _actionSel = -1;
  256. int _actionDown = -1;
  257. QString _addText;
  258. int _addWidth = 0;
  259. QString _undoText;
  260. int _undoWidth = 0;
  261. QString _installedText;
  262. int _installedWidth = 0;
  263. QPoint _mouse;
  264. bool _inDragArea = false;
  265. SelectedRow _selected;
  266. SelectedRow _pressed;
  267. QPoint _dragStart;
  268. int _started = -1;
  269. int _dragging = -1;
  270. int _above = -1;
  271. rpl::event_stream<int> _draggingScrollDelta;
  272. rpl::event_stream<int> _scrollsToY;
  273. int _minHeight = 0;
  274. int _scrollbar = 0;
  275. ChannelData *_megagroupSet = nullptr;
  276. bool _megagroupSetEmoji = false;
  277. bool _checkingGroupLevel = false;
  278. StickerSetIdentifier _megagroupSetInput;
  279. std::unique_ptr<Row> _megagroupSelectedSet;
  280. object_ptr<AddressField> _megagroupSetField = { nullptr };
  281. object_ptr<Ui::PlainShadow> _megagroupSelectedShadow = { nullptr };
  282. object_ptr<Ui::CrossButton> _megagroupSelectedRemove = { nullptr };
  283. object_ptr<Ui::BoxContentDivider> _megagroupDivider = { nullptr };
  284. object_ptr<Ui::FlatLabel> _megagroupSubTitle = { nullptr };
  285. base::Timer _megagroupSetAddressChangedTimer;
  286. mtpRequestId _megagroupSetRequestId = 0;
  287. };
  288. StickersBox::CounterWidget::CounterWidget(
  289. QWidget *parent,
  290. rpl::producer<int> count)
  291. : RpWidget(parent) {
  292. setAttribute(Qt::WA_TransparentForMouseEvents);
  293. _st.sizeId = Dialogs::Ui::UnreadBadgeSize::StickersBox;
  294. _st.textTop = st::stickersFeaturedBadgeTextTop;
  295. _st.size = st::stickersFeaturedBadgeSize;
  296. _st.padding = st::stickersFeaturedBadgePadding;
  297. _st.font = st::stickersFeaturedBadgeFont;
  298. std::move(
  299. count
  300. ) | rpl::start_with_next([=](int count) {
  301. setCounter(count);
  302. update();
  303. }, lifetime());
  304. }
  305. void StickersBox::CounterWidget::setCounter(int counter) {
  306. _text = (counter > 0) ? QString::number(counter) : QString();
  307. auto dummy = QImage(1, 1, QImage::Format_ARGB32_Premultiplied);
  308. auto p = QPainter(&dummy);
  309. const auto badge = Ui::PaintUnreadBadge(p, _text, 0, 0, _st);
  310. resize(badge.width(), st::stickersFeaturedBadgeSize);
  311. }
  312. void StickersBox::CounterWidget::paintEvent(QPaintEvent *e) {
  313. Painter p(this);
  314. if (!_text.isEmpty()) {
  315. const auto unreadRight = rtl() ? 0 : width();
  316. const auto unreadTop = 0;
  317. Ui::PaintUnreadBadge(p, _text, unreadRight, unreadTop, _st);
  318. }
  319. }
  320. template <typename ...Args>
  321. StickersBox::Tab::Tab(int index, Args&&... args)
  322. : _index(index)
  323. , _widget(std::forward<Args>(args)...)
  324. , _weak(_widget) {
  325. }
  326. object_ptr<StickersBox::Inner> StickersBox::Tab::takeWidget() {
  327. return std::move(_widget);
  328. }
  329. void StickersBox::Tab::returnWidget(object_ptr<Inner> widget) {
  330. _widget = std::move(widget);
  331. Assert(_widget == _weak);
  332. }
  333. StickersBox::Inner *StickersBox::Tab::widget() const {
  334. return _weak;
  335. }
  336. int StickersBox::Tab::index() const {
  337. return _index;
  338. }
  339. void StickersBox::Tab::saveScrollTop() {
  340. _scrollTop = widget()->getVisibleTop();
  341. }
  342. StickersBox::StickersBox(
  343. QWidget*,
  344. std::shared_ptr<ChatHelpers::Show> show,
  345. Section section,
  346. bool masks)
  347. : _st(st::stickersRowItem)
  348. , _show(std::move(show))
  349. , _session(&_show->session())
  350. , _api(&_session->mtp())
  351. , _tabs(this, st::stickersTabs)
  352. , _unreadBadge(
  353. this,
  354. _session->data().stickers().featuredSetsUnreadCountValue())
  355. , _section(section)
  356. , _isMasks(masks)
  357. , _isEmoji(false)
  358. , _installed(_isMasks ? Tab() : Tab(0, this, _show, Section::Installed))
  359. , _masks(_isMasks ? Tab(0, this, _show, Section::Masks) : Tab())
  360. , _featured(_isMasks ? Tab() : Tab(1, this, _show, Section::Featured))
  361. , _archived((_isMasks ? 1 : 2), this, _show, Section::Archived) {
  362. _tabs->setRippleTopRoundRadius(st::boxRadius);
  363. }
  364. StickersBox::StickersBox(
  365. QWidget*,
  366. std::shared_ptr<ChatHelpers::Show> show,
  367. not_null<ChannelData*> megagroup,
  368. bool isEmoji)
  369. : _st(st::stickersRowItem)
  370. , _show(std::move(show))
  371. , _session(&_show->session())
  372. , _api(&_session->mtp())
  373. , _section(Section::Installed)
  374. , _isMasks(false)
  375. , _isEmoji(isEmoji)
  376. , _installed(0, this, _show, megagroup, isEmoji)
  377. , _megagroupSet(megagroup) {
  378. _installed.widget()->scrollsToY(
  379. ) | rpl::start_with_next([=](int y) {
  380. scrollToY(y);
  381. }, lifetime());
  382. }
  383. StickersBox::StickersBox(
  384. QWidget*,
  385. std::shared_ptr<ChatHelpers::Show> show,
  386. const QVector<MTPStickerSetCovered> &attachedSets)
  387. : _st(st::stickersRowItem)
  388. , _show(std::move(show))
  389. , _session(&_show->session())
  390. , _api(&_session->mtp())
  391. , _section(Section::Attached)
  392. , _isMasks(false)
  393. , _isEmoji(false)
  394. , _attached(0, this, _show, Section::Attached)
  395. , _attachedType(Data::StickersType::Stickers)
  396. , _attachedSets(attachedSets) {
  397. }
  398. StickersBox::StickersBox(
  399. QWidget*,
  400. std::shared_ptr<ChatHelpers::Show> show,
  401. const std::vector<StickerSetIdentifier> &emojiSets)
  402. : _st(st::stickersRowItem)
  403. , _show(std::move(show))
  404. , _session(&_show->session())
  405. , _api(&_session->mtp())
  406. , _section(Section::Attached)
  407. , _isMasks(false)
  408. , _isEmoji(true)
  409. , _attached(0, this, _show, Section::Attached)
  410. , _attachedType(Data::StickersType::Emoji)
  411. , _emojiSets(emojiSets) {
  412. }
  413. Main::Session &StickersBox::session() const {
  414. return *_session;
  415. }
  416. void StickersBox::showAttachedStickers() {
  417. const auto stickers = &session().data().stickers();
  418. auto addedSet = false;
  419. const auto add = [&](not_null<StickersSet*> set) {
  420. if (_attached.widget()->appendSet(set)) {
  421. addedSet = true;
  422. if (set->stickers.isEmpty()
  423. || (set->flags & SetFlag::NotLoaded)) {
  424. session().api().scheduleStickerSetRequest(
  425. set->id,
  426. set->accessHash);
  427. }
  428. }
  429. };
  430. for (const auto &set : _attachedSets) {
  431. add(stickers->feedSet(set));
  432. }
  433. for (const auto &setId : _emojiSets) {
  434. const auto i = stickers->sets().find(setId.id);
  435. if (i != end(stickers->sets())) {
  436. add(i->second.get());
  437. }
  438. }
  439. if (addedSet) {
  440. _attached.widget()->updateSize();
  441. }
  442. if (_section == Section::Attached && addedSet) {
  443. session().api().requestStickerSets();
  444. }
  445. }
  446. void StickersBox::getArchivedDone(
  447. const MTPmessages_ArchivedStickers &result,
  448. uint64 offsetId) {
  449. _archivedRequestId = 0;
  450. _archivedLoaded = true;
  451. if (result.type() != mtpc_messages_archivedStickers) {
  452. return;
  453. }
  454. auto &stickers = result.c_messages_archivedStickers();
  455. auto &archived = archivedSetsOrderRef();
  456. if (offsetId) {
  457. auto index = archived.indexOf(offsetId);
  458. if (index >= 0) {
  459. archived = archived.mid(0, index + 1);
  460. }
  461. } else {
  462. archived.clear();
  463. }
  464. auto addedSet = false;
  465. auto changedSets = false;
  466. for (const auto &data : stickers.vsets().v) {
  467. const auto set = session().data().stickers().feedSet(data);
  468. const auto index = archived.indexOf(set->id);
  469. if (archived.isEmpty() || index != archived.size() - 1) {
  470. changedSets = true;
  471. if (index >= 0 && index < archived.size() - 1) {
  472. archived.removeAt(index);
  473. }
  474. archived.push_back(set->id);
  475. }
  476. if (_archived.widget()->appendSet(set)) {
  477. addedSet = true;
  478. if (set->flags & SetFlag::NotLoaded) {
  479. session().api().scheduleStickerSetRequest(
  480. set->id,
  481. set->accessHash);
  482. }
  483. }
  484. }
  485. if (addedSet) {
  486. _archived.widget()->updateSize();
  487. } else {
  488. _allArchivedLoaded = stickers.vsets().v.isEmpty()
  489. || (!changedSets && offsetId != 0);
  490. if (changedSets) {
  491. loadMoreArchived();
  492. }
  493. }
  494. refreshTabs();
  495. _someArchivedLoaded = true;
  496. if (_section == Section::Archived && addedSet) {
  497. session().api().requestStickerSets();
  498. }
  499. }
  500. void StickersBox::prepare() {
  501. if (_section == Section::Installed) {
  502. if (_tabs) {
  503. if (_isMasks) {
  504. session().local().readArchivedMasks();
  505. } else {
  506. session().local().readArchivedStickers();
  507. }
  508. } else {
  509. setTitle(_isEmoji
  510. ? tr::lng_emoji_group_set()
  511. : tr::lng_stickers_group_set());
  512. }
  513. } else if (_section == Section::Archived) {
  514. requestArchivedSets();
  515. } else if (_section == Section::Attached) {
  516. setTitle(_attachedType == Data::StickersType::Emoji
  517. ? tr::lng_custom_emoji_used_sets()
  518. : tr::lng_stickers_attached_sets());
  519. }
  520. if (_tabs) {
  521. if (archivedSetsOrder().isEmpty()) {
  522. preloadArchivedSets();
  523. }
  524. setNoContentMargin(true);
  525. _tabs->sectionActivated(
  526. ) | rpl::filter([=] {
  527. return !_ignoreTabActivation;
  528. }) | rpl::start_with_next(
  529. [this] { switchTab(); },
  530. lifetime());
  531. refreshTabs();
  532. }
  533. if (_installed.widget() && _section != Section::Installed) {
  534. _installed.widget()->hide();
  535. }
  536. if (_masks.widget() && _section != Section::Masks) {
  537. _masks.widget()->hide();
  538. }
  539. if (_featured.widget() && _section != Section::Featured) {
  540. _featured.widget()->hide();
  541. }
  542. if (_archived.widget() && _section != Section::Archived) {
  543. _archived.widget()->hide();
  544. }
  545. if (_attached.widget() && _section != Section::Attached) {
  546. _attached.widget()->hide();
  547. }
  548. {
  549. const auto installCallback = [=](uint64 setId) { installSet(setId); };
  550. const auto markAsInstalledCallback = [=](uint64 setId) {
  551. if (_installed.widget()) {
  552. _installed.widget()->setRowRemovedBySetId(setId, false);
  553. }
  554. if (_featured.widget()) {
  555. _featured.widget()->setRowRemovedBySetId(setId, false);
  556. }
  557. };
  558. const auto markAsRemovedCallback = [=](uint64 setId) {
  559. if (_installed.widget()) {
  560. _installed.widget()->setRowRemovedBySetId(setId, true);
  561. }
  562. if (_featured.widget()) {
  563. _featured.widget()->setRowRemovedBySetId(setId, true);
  564. }
  565. };
  566. if (const auto installed = _installed.widget()) {
  567. installed->setInstallSetCallback(markAsInstalledCallback);
  568. installed->setRemoveSetCallback(markAsRemovedCallback);
  569. }
  570. if (const auto featured = _featured.widget()) {
  571. featured->setInstallSetCallback([=](uint64 setId) {
  572. installCallback(setId);
  573. markAsInstalledCallback(setId);
  574. });
  575. featured->setRemoveSetCallback(markAsRemovedCallback);
  576. }
  577. if (const auto archived = _archived.widget()) {
  578. archived->setInstallSetCallback(installCallback);
  579. archived->setLoadMoreCallback([=] { loadMoreArchived(); });
  580. }
  581. if (const auto attached = _attached.widget()) {
  582. attached->setInstallSetCallback(installCallback);
  583. attached->setLoadMoreCallback([=] { showAttachedStickers(); });
  584. }
  585. }
  586. if (_megagroupSet) {
  587. addButton(tr::lng_settings_save(), [=] {
  588. _installed.widget()->saveGroupSet(crl::guard(this, [=] {
  589. closeBox();
  590. }));
  591. });
  592. addButton(tr::lng_cancel(), [=] { closeBox(); });
  593. } else {
  594. const auto close = _section == Section::Attached;
  595. addButton(
  596. close ? tr::lng_close() : tr::lng_about_done(),
  597. [=] { closeBox(); });
  598. }
  599. if (_section == Section::Installed) {
  600. _tab = &_installed;
  601. } else if (_section == Section::Masks) {
  602. _tab = &_masks;
  603. } else if (_section == Section::Archived) {
  604. _tab = &_archived;
  605. } else if (_section == Section::Attached) {
  606. _tab = &_attached;
  607. } else { // _section == Section::Featured
  608. _tab = &_featured;
  609. }
  610. setInnerWidget(_tab->takeWidget(), topSkip());
  611. setDimensions(st::boxWideWidth, st::boxMaxListHeight);
  612. session().data().stickers().updated(_isEmoji
  613. ? Data::StickersType::Emoji
  614. : _isMasks
  615. ? Data::StickersType::Masks
  616. : Data::StickersType::Stickers
  617. ) | rpl::start_with_next([=] {
  618. handleStickersUpdated();
  619. }, lifetime());
  620. if (_isEmoji) {
  621. session().api().updateCustomEmoji();
  622. } else if (_isMasks) {
  623. session().api().updateMasks();
  624. } else {
  625. session().api().updateStickers();
  626. }
  627. for (const auto &widget : { _installed.widget(), _masks.widget() }) {
  628. if (widget) {
  629. widget->draggingScrollDelta(
  630. ) | rpl::start_with_next([=](int delta) {
  631. scrollByDraggingDelta(delta);
  632. }, widget->lifetime());
  633. }
  634. }
  635. if (!_megagroupSet) {
  636. boxClosing() | rpl::start_with_next([=] {
  637. saveChanges();
  638. }, lifetime());
  639. }
  640. if (_tabs) {
  641. _tabs->raise();
  642. _unreadBadge->raise();
  643. }
  644. rebuildList();
  645. }
  646. void StickersBox::refreshTabs() {
  647. if (!_tabs) {
  648. return;
  649. }
  650. auto &stickers = session().data().stickers();
  651. _tabIndices.clear();
  652. auto sections = std::vector<QString>();
  653. if (_installed.widget()) {
  654. sections.push_back(tr::lng_stickers_installed_tab(tr::now));
  655. _tabIndices.push_back(Section::Installed);
  656. }
  657. if (_masks.widget()) {
  658. sections.push_back(tr::lng_stickers_masks_tab(tr::now));
  659. _tabIndices.push_back(Section::Masks);
  660. }
  661. if (!stickers.featuredSetsOrder().isEmpty() && _featured.widget()) {
  662. sections.push_back(tr::lng_stickers_featured_tab(tr::now));
  663. _tabIndices.push_back(Section::Featured);
  664. }
  665. if (!archivedSetsOrder().isEmpty() && _archived.widget()) {
  666. sections.push_back(tr::lng_stickers_archived_tab(tr::now));
  667. _tabIndices.push_back(Section::Archived);
  668. }
  669. _tabs->setSections(sections);
  670. if ((_tab == &_archived && !_tabIndices.contains(Section::Archived))
  671. || (_tab == &_featured && !_tabIndices.contains(Section::Featured))
  672. || (_tab == &_masks && !_tabIndices.contains(Section::Masks))) {
  673. switchTab();
  674. } else {
  675. _ignoreTabActivation = true;
  676. _tabs->setActiveSectionFast(_tabIndices.indexOf((_tab == &_archived)
  677. ? Section::Archived
  678. : (_tab == &_featured)
  679. ? Section::Featured
  680. : (_tab == &_masks)
  681. ? Section::Masks
  682. : Section::Installed));
  683. _ignoreTabActivation = false;
  684. }
  685. updateTabsGeometry();
  686. }
  687. void StickersBox::loadMoreArchived() {
  688. if (_section != Section::Archived
  689. || _allArchivedLoaded
  690. || _archivedRequestId) {
  691. return;
  692. }
  693. uint64 lastId = 0;
  694. const auto &order = archivedSetsOrder();
  695. const auto &sets = session().data().stickers().sets();
  696. for (auto setIt = order.cend(), e = order.cbegin(); setIt != e;) {
  697. --setIt;
  698. auto it = sets.find(*setIt);
  699. if (it != sets.cend()) {
  700. if (it->second->flags & SetFlag::Archived) {
  701. lastId = it->second->id;
  702. break;
  703. }
  704. }
  705. }
  706. const auto flags = _isMasks
  707. ? MTPmessages_GetArchivedStickers::Flag::f_masks
  708. : MTPmessages_GetArchivedStickers::Flags(0);
  709. _archivedRequestId = _api.request(MTPmessages_GetArchivedStickers(
  710. MTP_flags(flags),
  711. MTP_long(lastId),
  712. MTP_int(kArchivedLimitPerPage)
  713. )).done([=](const MTPmessages_ArchivedStickers &result) {
  714. getArchivedDone(result, lastId);
  715. }).send();
  716. }
  717. void StickersBox::paintEvent(QPaintEvent *e) {
  718. BoxContent::paintEvent(e);
  719. Painter p(this);
  720. if (_slideAnimation) {
  721. _slideAnimation->paintFrame(p, 0, topSkip(), width());
  722. if (!_slideAnimation->animating()) {
  723. _slideAnimation.reset();
  724. setInnerVisible(true);
  725. update();
  726. }
  727. }
  728. }
  729. void StickersBox::updateTabsGeometry() {
  730. if (!_tabs) return;
  731. const auto maxTabs = _isMasks ? 2 : 3;
  732. _tabs->resizeToWidth(_tabIndices.size() * width() / maxTabs);
  733. _unreadBadge->setVisible(_tabIndices.contains(Section::Featured));
  734. setInnerTopSkip(topSkip());
  735. auto featuredLeft = width() / maxTabs;
  736. auto featuredRight = 2 * width() / maxTabs;
  737. auto featuredTextWidth = st::stickersTabs.labelStyle.font->width(tr::lng_stickers_featured_tab(tr::now));
  738. auto featuredTextRight = featuredLeft + (featuredRight - featuredLeft - featuredTextWidth) / 2 + featuredTextWidth;
  739. auto unreadBadgeLeft = featuredTextRight - st::stickersFeaturedBadgeSkip;
  740. auto unreadBadgeTop = st::stickersFeaturedBadgeTop;
  741. if (unreadBadgeLeft + _unreadBadge->width() > featuredRight) {
  742. unreadBadgeLeft = featuredRight - _unreadBadge->width();
  743. }
  744. _unreadBadge->moveToLeft(unreadBadgeLeft, unreadBadgeTop);
  745. _tabs->moveToLeft(0, 0);
  746. }
  747. int StickersBox::topSkip() const {
  748. return _tabs ? (_tabs->height() - st::lineWidth) : 0;
  749. }
  750. void StickersBox::switchTab() {
  751. if (!_tabs) return;
  752. auto tab = _tabs->activeSection();
  753. Assert(tab >= 0 && tab < _tabIndices.size());
  754. auto newSection = _tabIndices[tab];
  755. auto newTab = _tab;
  756. if (newSection == Section::Installed) {
  757. newTab = &_installed;
  758. } else if (newSection == Section::Featured) {
  759. newTab = &_featured;
  760. } else if (newSection == Section::Archived) {
  761. newTab = &_archived;
  762. requestArchivedSets();
  763. } else if (newSection == Section::Masks) {
  764. newTab = &_masks;
  765. session().api().updateMasks();
  766. }
  767. if (_tab == newTab) {
  768. scrollToY(0);
  769. return;
  770. }
  771. if (_tab == &_installed) {
  772. _localOrder = _tab->widget()->fullOrder();
  773. _localRemoved = _tab->widget()->removedSets();
  774. }
  775. auto wasCache = grabContentCache();
  776. auto wasIndex = _tab->index();
  777. _tab->saveScrollTop();
  778. auto widget = takeInnerWidget<Inner>();
  779. widget->setParent(this);
  780. widget->hide();
  781. _tab->returnWidget(std::move(widget));
  782. _tab = newTab;
  783. _section = newSection;
  784. setInnerWidget(_tab->takeWidget(), topSkip());
  785. _tabs->raise();
  786. _unreadBadge->raise();
  787. _tab->widget()->show();
  788. rebuildList();
  789. scrollToY(_tab->scrollTop());
  790. setInnerVisible(true);
  791. auto nowCache = grabContentCache();
  792. auto nowIndex = _tab->index();
  793. _slideAnimation = std::make_unique<Ui::SlideAnimation>();
  794. _slideAnimation->setSnapshots(std::move(wasCache), std::move(nowCache));
  795. auto slideLeft = wasIndex > nowIndex;
  796. _slideAnimation->start(slideLeft, [=] { update(); }, st::slideDuration);
  797. setInnerVisible(false);
  798. setFocus();
  799. update();
  800. }
  801. QPixmap StickersBox::grabContentCache() {
  802. _tabs->hide();
  803. auto result = grabInnerCache();
  804. _tabs->show();
  805. return result;
  806. }
  807. std::array<StickersBox::Inner*, 5> StickersBox::widgets() const {
  808. return {
  809. _installed.widget(),
  810. _featured.widget(),
  811. _archived.widget(),
  812. _attached.widget(),
  813. _masks.widget()
  814. };
  815. }
  816. void StickersBox::installSet(uint64 setId) {
  817. const auto &sets = session().data().stickers().sets();
  818. const auto it = sets.find(setId);
  819. if (it == sets.cend()) {
  820. rebuildList();
  821. return;
  822. }
  823. const auto set = it->second.get();
  824. if (_localRemoved.contains(setId)) {
  825. _localRemoved.removeOne(setId);
  826. for (const auto &widget : widgets()) {
  827. if (widget) {
  828. widget->setRemovedSets(_localRemoved);
  829. }
  830. }
  831. }
  832. if (!(set->flags & SetFlag::Installed)
  833. || (set->flags & SetFlag::Archived)) {
  834. _api.request(MTPmessages_InstallStickerSet(
  835. set->mtpInput(),
  836. MTP_boolFalse()
  837. )).done([=](const MTPmessages_StickerSetInstallResult &result) {
  838. installDone(result);
  839. }).fail([=](const MTP::Error &error) {
  840. installFail(error, setId);
  841. }).send();
  842. session().data().stickers().installLocally(setId);
  843. }
  844. }
  845. void StickersBox::installDone(
  846. const MTPmessages_StickerSetInstallResult &result) const {
  847. if (result.type() == mtpc_messages_stickerSetInstallResultArchive) {
  848. session().data().stickers().applyArchivedResult(
  849. result.c_messages_stickerSetInstallResultArchive());
  850. }
  851. }
  852. void StickersBox::installFail(const MTP::Error &error, uint64 setId) {
  853. const auto &sets = session().data().stickers().sets();
  854. const auto it = sets.find(setId);
  855. if (it == sets.cend()) {
  856. rebuildList();
  857. } else {
  858. session().data().stickers().undoInstallLocally(setId);
  859. }
  860. }
  861. void StickersBox::preloadArchivedSets() {
  862. if (!_tabs) {
  863. return;
  864. }
  865. if (!_archivedRequestId) {
  866. const auto flags = _isMasks
  867. ? MTPmessages_GetArchivedStickers::Flag::f_masks
  868. : MTPmessages_GetArchivedStickers::Flags(0);
  869. _archivedRequestId = _api.request(MTPmessages_GetArchivedStickers(
  870. MTP_flags(flags),
  871. MTP_long(0),
  872. MTP_int(kArchivedLimitFirstRequest)
  873. )).done([=](const MTPmessages_ArchivedStickers &result) {
  874. getArchivedDone(result, 0);
  875. }).send();
  876. }
  877. }
  878. void StickersBox::requestArchivedSets() {
  879. // Reload the archived list.
  880. if (!_archivedLoaded) {
  881. preloadArchivedSets();
  882. }
  883. const auto &sets = session().data().stickers().sets();
  884. const auto &order = archivedSetsOrder();
  885. for (const auto setId : order) {
  886. auto it = sets.find(setId);
  887. if (it != sets.cend()) {
  888. const auto set = it->second.get();
  889. if (set->stickers.isEmpty()
  890. && (set->flags & SetFlag::NotLoaded)) {
  891. session().api().scheduleStickerSetRequest(
  892. setId,
  893. set->accessHash);
  894. }
  895. }
  896. }
  897. session().api().requestStickerSets();
  898. }
  899. void StickersBox::resizeEvent(QResizeEvent *e) {
  900. BoxContent::resizeEvent(e);
  901. if (_tabs) {
  902. updateTabsGeometry();
  903. }
  904. if (_titleShadow) {
  905. _titleShadow->setGeometry(0, 0, width(), st::lineWidth);
  906. }
  907. for (const auto &widget : widgets()) {
  908. if (widget) {
  909. widget->resize(width(), widget->height());
  910. }
  911. }
  912. }
  913. void StickersBox::handleStickersUpdated() {
  914. if (_section == Section::Installed
  915. || _section == Section::Featured
  916. || _section == Section::Masks) {
  917. rebuildList();
  918. } else {
  919. _tab->widget()->updateRows();
  920. }
  921. if (archivedSetsOrder().isEmpty()) {
  922. preloadArchivedSets();
  923. } else {
  924. refreshTabs();
  925. }
  926. }
  927. void StickersBox::rebuildList(Tab *tab) {
  928. if (_section == Section::Attached) {
  929. return;
  930. }
  931. if (!tab) {
  932. tab = _tab;
  933. }
  934. if ((tab == &_installed) || (tab == &_masks) || (_tab == &_featured)) {
  935. _localOrder = tab->widget()->fullOrder();
  936. _localRemoved = tab->widget()->removedSets();
  937. }
  938. tab->widget()->rebuild(_isMasks);
  939. if ((tab == &_installed) || (tab == &_masks) || (_tab == &_featured)) {
  940. tab->widget()->setFullOrder(_localOrder);
  941. }
  942. tab->widget()->setRemovedSets(_localRemoved);
  943. }
  944. void StickersBox::saveChanges() {
  945. const auto installed = _installed.widget();
  946. const auto masks = _masks.widget();
  947. // Make sure that our changes in other tabs are applied in the Installed tab.
  948. if (installed) {
  949. rebuildList(&_installed);
  950. }
  951. if (masks) {
  952. rebuildList(&_masks);
  953. }
  954. if (_someArchivedLoaded) {
  955. if (_isMasks) {
  956. session().local().writeArchivedMasks();
  957. } else {
  958. session().local().writeArchivedStickers();
  959. }
  960. }
  961. if (installed) {
  962. session().api().saveStickerSets(
  963. installed->order(),
  964. installed->removedSets(),
  965. Data::StickersType::Stickers);
  966. }
  967. if (masks) {
  968. session().api().saveStickerSets(
  969. masks->order(),
  970. masks->removedSets(),
  971. Data::StickersType::Masks);
  972. }
  973. }
  974. void StickersBox::setInnerFocus() {
  975. if (_megagroupSet) {
  976. _installed.widget()->setInnerFocus();
  977. } else {
  978. BoxContent::setInnerFocus();
  979. }
  980. }
  981. const Data::StickersSetsOrder &StickersBox::archivedSetsOrder() const {
  982. return !_isMasks
  983. ? session().data().stickers().archivedSetsOrder()
  984. : session().data().stickers().archivedMaskSetsOrder();
  985. }
  986. Data::StickersSetsOrder &StickersBox::archivedSetsOrderRef() const {
  987. return !_isMasks
  988. ? session().data().stickers().archivedSetsOrderRef()
  989. : session().data().stickers().archivedMaskSetsOrderRef();
  990. }
  991. StickersBox::~StickersBox() = default;
  992. StickersBox::Inner::Row::Row(
  993. not_null<StickersSet*> set,
  994. DocumentData *sticker,
  995. int32 count,
  996. const QString &title,
  997. int titleWidth,
  998. Data::StickersSetFlags flagsOverride,
  999. bool removed,
  1000. int32 pixw,
  1001. int32 pixh)
  1002. : set(set)
  1003. , sticker(sticker)
  1004. , count(count)
  1005. , title(title)
  1006. , titleWidth(titleWidth)
  1007. , flagsOverride(flagsOverride)
  1008. , removed(removed)
  1009. , pixw(pixw)
  1010. , pixh(pixh) {
  1011. ++set->locked;
  1012. }
  1013. StickersBox::Inner::Row::~Row() {
  1014. if (!--set->locked) {
  1015. const auto installed = !!(set->flags & SetFlag::Installed);
  1016. const auto featured = !!(set->flags & SetFlag::Featured);
  1017. const auto special = !!(set->flags & SetFlag::Special);
  1018. const auto archived = !!(set->flags & SetFlag::Archived);
  1019. const auto emoji = !!(set->flags & SetFlag::Emoji);
  1020. if (!installed && !featured && !special && !archived && !emoji) {
  1021. auto &sets = set->owner().stickers().setsRef();
  1022. if (const auto i = sets.find(set->id); i != end(sets)) {
  1023. sets.erase(i);
  1024. }
  1025. }
  1026. }
  1027. }
  1028. bool StickersBox::Inner::Row::isRecentSet() const {
  1029. return (set->id == Data::Stickers::CloudRecentSetId)
  1030. || (set->id == Data::Stickers::CloudRecentAttachedSetId);
  1031. }
  1032. bool StickersBox::Inner::Row::isMasksSet() const {
  1033. return (set->type() == Data::StickersType::Masks);
  1034. }
  1035. bool StickersBox::Inner::Row::isEmojiSet() const {
  1036. return (set->type() == Data::StickersType::Emoji);
  1037. }
  1038. bool StickersBox::Inner::Row::isInstalled() const {
  1039. return (flagsOverride & SetFlag::Installed);
  1040. }
  1041. bool StickersBox::Inner::Row::isUnread() const {
  1042. return (flagsOverride & SetFlag::Unread);
  1043. }
  1044. bool StickersBox::Inner::Row::isArchived() const {
  1045. return (flagsOverride & SetFlag::Archived);
  1046. }
  1047. StickersBox::Inner::Inner(
  1048. QWidget *parent,
  1049. std::shared_ptr<ChatHelpers::Show> show,
  1050. StickersBox::Section section)
  1051. : RpWidget(parent)
  1052. , _st(st::stickersRowItem)
  1053. , _show(std::move(show))
  1054. , _session(&_show->session())
  1055. , _api(&_session->mtp())
  1056. , _section(section)
  1057. , _isInstalledTab(_section == Section::Installed
  1058. || _section == Section::Masks)
  1059. , _buttonBgOver(
  1060. ImageRoundRadius::Large,
  1061. (_isInstalledTab
  1062. ? st::stickersUndoRemove
  1063. : st::stickersTrendingAdd).textBgOver)
  1064. , _buttonBg(
  1065. ImageRoundRadius::Large,
  1066. (_isInstalledTab
  1067. ? st::stickersUndoRemove
  1068. : st::stickersTrendingAdd).textBg)
  1069. , _inactiveButtonBg(
  1070. ImageRoundRadius::Large,
  1071. st::stickersTrendingInstalled.textBg)
  1072. , _rowHeight(_st.height)
  1073. , _shiftingAnimation([=](crl::time now) {
  1074. return shiftingAnimationCallback(now);
  1075. })
  1076. , _itemsTop(st::lineWidth)
  1077. , _addText(tr::lng_stickers_featured_add(tr::now))
  1078. , _addWidth(st::stickersTrendingAdd.style.font->width(_addText))
  1079. , _undoText(tr::lng_stickers_return(tr::now))
  1080. , _undoWidth(st::stickersUndoRemove.style.font->width(_undoText))
  1081. , _installedText(tr::lng_stickers_featured_installed(tr::now))
  1082. , _installedWidth(st::stickersTrendingInstalled.style.font->width(
  1083. _installedText)) {
  1084. setup();
  1085. }
  1086. StickersBox::Inner::Inner(
  1087. QWidget *parent,
  1088. std::shared_ptr<ChatHelpers::Show> show,
  1089. not_null<ChannelData*> megagroup,
  1090. bool isEmoji)
  1091. : RpWidget(parent)
  1092. , _st(st::stickersRowItem)
  1093. , _show(std::move(show))
  1094. , _session(&_show->session())
  1095. , _api(&_session->mtp())
  1096. , _section(StickersBox::Section::Installed)
  1097. , _isInstalledTab(_section == Section::Installed
  1098. || _section == Section::Masks)
  1099. , _buttonBgOver(
  1100. ImageRoundRadius::Large,
  1101. (_isInstalledTab
  1102. ? st::stickersUndoRemove
  1103. : st::stickersTrendingAdd).textBgOver)
  1104. , _buttonBg(
  1105. ImageRoundRadius::Large,
  1106. (_isInstalledTab
  1107. ? st::stickersUndoRemove
  1108. : st::stickersTrendingAdd).textBg)
  1109. , _inactiveButtonBg(
  1110. ImageRoundRadius::Large,
  1111. st::stickersTrendingInstalled.textBg)
  1112. , _rowHeight(_st.height)
  1113. , _shiftingAnimation([=](crl::time now) {
  1114. return shiftingAnimationCallback(now);
  1115. })
  1116. , _itemsTop(st::lineWidth)
  1117. , _megagroupSet(megagroup)
  1118. , _megagroupSetEmoji(isEmoji)
  1119. , _megagroupSetInput(isEmoji
  1120. ? _megagroupSet->mgInfo->emojiSet
  1121. : _megagroupSet->mgInfo->stickerSet)
  1122. , _megagroupSetField(
  1123. this,
  1124. st::groupStickersField,
  1125. rpl::single(isEmoji ? u"emojipack"_q : u"stickerset"_q),
  1126. QString(),
  1127. _session->createInternalLink(QString()))
  1128. , _megagroupDivider(this)
  1129. , _megagroupSubTitle(
  1130. this,
  1131. (isEmoji
  1132. ? tr::lng_emoji_group_from_your
  1133. : tr::lng_stickers_group_from_your)(tr::now),
  1134. st::boxTitle) {
  1135. _megagroupSetField->setLinkPlaceholder(
  1136. _session->createInternalLink(
  1137. isEmoji ? u"addemoji/"_q : u"addstickers/"_q));
  1138. _megagroupSetField->setPlaceholderHidden(false);
  1139. _megagroupSetAddressChangedTimer.setCallback([this] {
  1140. handleMegagroupSetAddressChange();
  1141. });
  1142. connect(
  1143. _megagroupSetField,
  1144. &Ui::MaskedInputField::changed,
  1145. [=] {
  1146. _megagroupSetAddressChangedTimer.callOnce(
  1147. kHandleMegagroupSetAddressChangeTimeout);
  1148. });
  1149. connect(
  1150. _megagroupSetField,
  1151. &Ui::MaskedInputField::submitted,
  1152. [=] {
  1153. _megagroupSetAddressChangedTimer.cancel();
  1154. handleMegagroupSetAddressChange();
  1155. });
  1156. setup();
  1157. }
  1158. Main::Session &StickersBox::Inner::session() const {
  1159. return *_session;
  1160. }
  1161. void StickersBox::Inner::setup() {
  1162. session().downloaderTaskFinished(
  1163. ) | rpl::start_with_next([=] {
  1164. update();
  1165. readVisibleSets();
  1166. }, lifetime());
  1167. setMouseTracking(true);
  1168. }
  1169. void StickersBox::Inner::setInnerFocus() {
  1170. if (_megagroupSetField) {
  1171. _megagroupSetField->setFocusFast();
  1172. }
  1173. }
  1174. void StickersBox::Inner::paintEvent(QPaintEvent *e) {
  1175. Painter p(this);
  1176. auto clip = e->rect();
  1177. p.fillRect(clip, st::boxBg);
  1178. p.setClipRect(clip);
  1179. if (_megagroupSelectedSet) {
  1180. auto setTop = _megagroupDivider->y() - _rowHeight;
  1181. p.translate(0, setTop);
  1182. paintRow(p, _megagroupSelectedSet.get(), -1);
  1183. p.translate(0, -setTop);
  1184. }
  1185. auto y = _itemsTop;
  1186. if (_rows.empty()) {
  1187. p.setFont(st::noContactsFont);
  1188. p.setPen(st::noContactsColor);
  1189. p.drawText(QRect(0, y, width(), st::noContactsHeight), tr::lng_contacts_loading(tr::now), style::al_center);
  1190. } else {
  1191. p.translate(0, _itemsTop);
  1192. int32 yFrom = clip.y() - _itemsTop, yTo = clip.y() + clip.height() - _itemsTop;
  1193. int32 from = floorclamp(yFrom - _rowHeight, _rowHeight, 0, _rows.size());
  1194. int32 to = ceilclamp(yTo + _rowHeight, _rowHeight, 0, _rows.size());
  1195. p.translate(0, from * _rowHeight);
  1196. for (int32 i = from; i < to; ++i) {
  1197. if (i != _above) {
  1198. paintRow(p, _rows[i].get(), i);
  1199. }
  1200. p.translate(0, _rowHeight);
  1201. }
  1202. if (from <= _above && _above < to) {
  1203. p.translate(0, (_above - to) * _rowHeight);
  1204. paintRow(p, _rows[_above].get(), _above);
  1205. }
  1206. }
  1207. }
  1208. void StickersBox::Inner::resizeEvent(QResizeEvent *e) {
  1209. updateControlsGeometry();
  1210. }
  1211. void StickersBox::Inner::updateControlsGeometry() {
  1212. if (_megagroupSet) {
  1213. auto top = st::groupStickersFieldPadding.top();
  1214. auto fieldLeft = st::boxTitlePosition.x();
  1215. _megagroupSetField->setGeometryToLeft(fieldLeft, top, width() - fieldLeft - st::groupStickersFieldPadding.right(), _megagroupSetField->height());
  1216. top += _megagroupSetField->height() + st::groupStickersFieldPadding.bottom();
  1217. if (_megagroupSelectedRemove) {
  1218. _megagroupSelectedShadow->setGeometryToLeft(0, top, width(), st::lineWidth);
  1219. top += st::lineWidth;
  1220. _megagroupSelectedRemove->moveToRight(st::groupStickersRemovePosition.x(), top + st::groupStickersRemovePosition.y());
  1221. top += _rowHeight;
  1222. }
  1223. _megagroupDivider->setGeometryToLeft(0, top, width(), _megagroupDivider->height());
  1224. top += _megagroupDivider->height();
  1225. _megagroupSubTitle->resizeToNaturalWidth(width() - 2 * st::boxTitlePosition.x());
  1226. _megagroupSubTitle->moveToLeft(st::boxTitlePosition.x(), top + st::boxTitlePosition.y());
  1227. }
  1228. }
  1229. QRect StickersBox::Inner::relativeButtonRect(
  1230. bool removeButton,
  1231. bool installedSet) const {
  1232. auto buttonw = st::stickersRemove.width;
  1233. auto buttonh = st::stickersRemove.height;
  1234. auto buttonshift = st::stickersRemoveSkip;
  1235. if (!removeButton) {
  1236. const auto &st = installedSet
  1237. ? st::stickersTrendingInstalled
  1238. : _isInstalledTab
  1239. ? st::stickersUndoRemove
  1240. : st::stickersTrendingAdd;
  1241. const auto textWidth = installedSet
  1242. ? _installedWidth
  1243. : _isInstalledTab
  1244. ? _undoWidth
  1245. : _addWidth;
  1246. buttonw = textWidth - st.width;
  1247. buttonh = st.height;
  1248. buttonshift = 0;
  1249. }
  1250. auto buttonx = width() - st::contactsPadding.right() - buttonw + buttonshift;
  1251. auto buttony = (_st.height - buttonh) / 2;
  1252. return QRect(buttonx, buttony, buttonw, buttonh);
  1253. }
  1254. void StickersBox::Inner::paintRow(Painter &p, not_null<Row*> row, int index) {
  1255. auto xadd = 0, yadd = qRound(row->yadd.current());
  1256. if (xadd || yadd) p.translate(xadd, yadd);
  1257. if (_megagroupSet) {
  1258. auto selectedIndex = [&] {
  1259. if (auto index = std::get_if<int>(&_selected)) {
  1260. return *index;
  1261. }
  1262. return -1;
  1263. }();
  1264. if (index >= 0 && index == selectedIndex) {
  1265. p.fillRect(0, 0, width(), _rowHeight, _st.button.textBgOver);
  1266. if (row->ripple) {
  1267. row->ripple->paint(p, 0, 0, width());
  1268. }
  1269. }
  1270. }
  1271. if (_isInstalledTab) {
  1272. if (index >= 0 && index == _above) {
  1273. auto current = _aboveShadowFadeOpacity.current();
  1274. if (_started >= 0) {
  1275. auto reachedOpacity = aboveShadowOpacity();
  1276. if (reachedOpacity > current) {
  1277. _aboveShadowFadeOpacity = anim::value(reachedOpacity, reachedOpacity);
  1278. current = reachedOpacity;
  1279. }
  1280. }
  1281. auto rect = myrtlrect(_st.photoPosition.x() / 2, _st.photoPosition.y() / 2, width() - _st.photoPosition.x() - _scrollbar, _rowHeight - _st.photoPosition.y());
  1282. p.setOpacity(current);
  1283. Ui::Shadow::paint(p, rect, width(), st::boxRoundShadow);
  1284. p.setOpacity(1);
  1285. Ui::FillRoundRect(p, rect, st::boxBg, Ui::BoxCorners);
  1286. p.setOpacity(1. - current);
  1287. paintFakeButton(p, row, index);
  1288. p.setOpacity(1.);
  1289. } else if (!_megagroupSet) {
  1290. paintFakeButton(p, row, index);
  1291. }
  1292. } else if (!_megagroupSet) {
  1293. paintFakeButton(p, row, index);
  1294. }
  1295. if (row->removed && _isInstalledTab) {
  1296. p.setOpacity(st::stickersRowDisabledOpacity);
  1297. }
  1298. auto stickerskip = 0;
  1299. if (!_megagroupSet && _isInstalledTab) {
  1300. stickerskip += st::stickersReorderIcon.width() + st::stickersReorderSkip;
  1301. if (!row->isRecentSet()) {
  1302. st::stickersReorderIcon.paint(p, _st.photoPosition.x(), (_rowHeight - st::stickersReorderIcon.height()) / 2, width());
  1303. }
  1304. }
  1305. if (row->sticker) {
  1306. paintRowThumbnail(p, row, stickerskip + _st.photoPosition.x());
  1307. }
  1308. int namex = stickerskip + _st.namePosition.x();
  1309. int namey = _st.namePosition.y();
  1310. int statusx = stickerskip + _st.statusPosition.x();
  1311. int statusy = _st.statusPosition.y();
  1312. p.setFont(st::contactsNameStyle.font);
  1313. p.setPen(_st.nameFg);
  1314. p.drawTextLeft(namex, namey, width(), row->title, row->titleWidth);
  1315. if (row->isUnread()) {
  1316. p.setPen(Qt::NoPen);
  1317. p.setBrush(st::stickersFeaturedUnreadBg);
  1318. {
  1319. PainterHighQualityEnabler hq(p);
  1320. p.drawEllipse(style::rtlrect(namex + row->titleWidth + st::stickersFeaturedUnreadSkip, namey + st::stickersFeaturedUnreadTop, st::stickersFeaturedUnreadSize, st::stickersFeaturedUnreadSize, width()));
  1321. }
  1322. }
  1323. const auto statusText = (row->count == 0)
  1324. ? tr::lng_contacts_loading(tr::now)
  1325. : row->isEmojiSet()
  1326. ? tr::lng_custom_emoji_count(tr::now, lt_count, row->count)
  1327. : row->isMasksSet()
  1328. ? tr::lng_masks_count(tr::now, lt_count, row->count)
  1329. : tr::lng_stickers_count(tr::now, lt_count, row->count);
  1330. p.setFont(st::contactsStatusFont);
  1331. p.setPen(_st.statusFg);
  1332. p.drawTextLeft(statusx, statusy, width(), statusText);
  1333. p.setOpacity(1);
  1334. if (xadd || yadd) p.translate(-xadd, -yadd);
  1335. }
  1336. void StickersBox::Inner::paintRowThumbnail(
  1337. Painter &p,
  1338. not_null<Row*> row,
  1339. int left) {
  1340. const auto origin = Data::FileOriginStickerSet(
  1341. row->set->id,
  1342. row->set->accessHash);
  1343. if (row->set->hasThumbnail()) {
  1344. if (!row->thumbnailMedia) {
  1345. row->thumbnailMedia = row->set->createThumbnailView();
  1346. row->set->loadThumbnail();
  1347. }
  1348. } else if (row->sticker) {
  1349. if (!row->stickerMedia) {
  1350. row->stickerMedia = row->sticker->createMediaView();
  1351. row->stickerMedia->thumbnailWanted(origin);
  1352. }
  1353. }
  1354. validateAnimation(row);
  1355. const auto thumb = row->thumbnailMedia
  1356. ? row->thumbnailMedia->image()
  1357. : row->stickerMedia
  1358. ? row->stickerMedia->thumbnail()
  1359. : nullptr;
  1360. const auto paused = _show->paused(ChatHelpers::PauseReason::Layer);
  1361. const auto x = left + (_st.photoSize - row->pixw) / 2;
  1362. const auto y = _st.photoPosition.y() + (_st.photoSize - row->pixh) / 2;
  1363. if (row->lottie && row->lottie->ready()) {
  1364. const auto frame = row->lottie->frame();
  1365. const auto size = frame.size() / style::DevicePixelRatio();
  1366. p.drawImage(
  1367. QRect(
  1368. left + (_st.photoSize - size.width()) / 2,
  1369. _st.photoPosition.y() + (_st.photoSize - size.height()) / 2,
  1370. size.width(),
  1371. size.height()),
  1372. frame);
  1373. if (!paused) {
  1374. row->lottie->markFrameShown();
  1375. }
  1376. } else if (row->webm && row->webm->started()) {
  1377. p.drawImage(
  1378. x,
  1379. y,
  1380. row->webm->current(
  1381. { .frame = { row->pixw, row->pixh }, .keepAlpha = true },
  1382. paused ? 0 : crl::now()));
  1383. } else if (thumb) {
  1384. p.drawPixmapLeft(
  1385. x,
  1386. y,
  1387. width(),
  1388. thumb->pix(row->pixw, row->pixh));
  1389. }
  1390. }
  1391. void StickersBox::Inner::validateLottieAnimation(not_null<Row*> row) {
  1392. if (row->lottie
  1393. || !ChatHelpers::HasLottieThumbnail(
  1394. row->set->thumbnailType(),
  1395. row->thumbnailMedia.get(),
  1396. row->stickerMedia.get())) {
  1397. return;
  1398. }
  1399. auto player = ChatHelpers::LottieThumbnail(
  1400. row->thumbnailMedia.get(),
  1401. row->stickerMedia.get(),
  1402. ChatHelpers::StickerLottieSize::SetsListThumbnail,
  1403. QSize(_st.photoSize, _st.photoSize) * style::DevicePixelRatio());
  1404. if (!player) {
  1405. return;
  1406. }
  1407. row->lottie = std::move(player);
  1408. row->lottie->updates(
  1409. ) | rpl::start_with_next([=] {
  1410. updateRowThumbnail(row);
  1411. }, lifetime());
  1412. }
  1413. void StickersBox::Inner::validateWebmAnimation(not_null<Row*> row) {
  1414. if (row->webm
  1415. || !ChatHelpers::HasWebmThumbnail(
  1416. row->set->thumbnailType(),
  1417. row->thumbnailMedia.get(),
  1418. row->stickerMedia.get())) {
  1419. return;
  1420. }
  1421. auto callback = [=](Media::Clip::Notification notification) {
  1422. clipCallback(row, notification);
  1423. };
  1424. row->webm = ChatHelpers::WebmThumbnail(
  1425. row->thumbnailMedia.get(),
  1426. row->stickerMedia.get(),
  1427. std::move(callback));
  1428. }
  1429. void StickersBox::Inner::clipCallback(
  1430. not_null<Row*> row,
  1431. Media::Clip::Notification notification) {
  1432. using namespace Media::Clip;
  1433. switch (notification) {
  1434. case Notification::Reinit: {
  1435. if (!row->webm) {
  1436. return;
  1437. } else if (row->webm->state() == State::Error) {
  1438. row->webm.setBad();
  1439. } else if (row->webm->ready() && !row->webm->started()) {
  1440. row->webm->start({
  1441. .frame = { row->pixw, row->pixh },
  1442. .keepAlpha = true,
  1443. });
  1444. }
  1445. } break;
  1446. case Notification::Repaint: break;
  1447. }
  1448. updateRowThumbnail(row);
  1449. }
  1450. void StickersBox::Inner::validateAnimation(not_null<Row*> row) {
  1451. validateWebmAnimation(row);
  1452. validateLottieAnimation(row);
  1453. }
  1454. void StickersBox::Inner::updateRowThumbnail(not_null<Row*> row) {
  1455. const auto rowTop = [&] {
  1456. if (row == _megagroupSelectedSet.get()) {
  1457. return _megagroupDivider->y() - _rowHeight;
  1458. }
  1459. auto top = _itemsTop;
  1460. for (const auto &entry : _rows) {
  1461. if (entry.get() == row) {
  1462. return top + qRound(row->yadd.current());
  1463. }
  1464. top += _rowHeight;
  1465. }
  1466. Unexpected("StickersBox::Inner::updateRowThumbnail: row not found");
  1467. }();
  1468. const auto left = _st.photoPosition.x()
  1469. + ((!_megagroupSet && _isInstalledTab)
  1470. ? st::stickersReorderIcon.width() + st::stickersReorderSkip
  1471. : 0);
  1472. const auto top = rowTop + _st.photoPosition.y();
  1473. update(left, top, _st.photoSize, _st.photoSize);
  1474. }
  1475. void StickersBox::Inner::paintFakeButton(Painter &p, not_null<Row*> row, int index) {
  1476. const auto removeButton = (_isInstalledTab && !row->removed);
  1477. if (!_isInstalledTab && row->isInstalled() && !row->isArchived() && !row->removed) {
  1478. // Round button "Added" after installed from Trending or Archived.
  1479. const auto rect = relativeButtonRect(removeButton, true);
  1480. const auto &st = st::stickersTrendingInstalled;
  1481. const auto textWidth = _installedWidth;
  1482. const auto &text = _installedText;
  1483. _inactiveButtonBg.paint(p, myrtlrect(rect));
  1484. if (row->ripple) {
  1485. row->ripple->paint(p, rect.x(), rect.y(), width());
  1486. if (row->ripple->empty()) {
  1487. row->ripple.reset();
  1488. }
  1489. }
  1490. p.setFont(st.style.font);
  1491. p.setPen(st.textFg);
  1492. p.drawTextLeft(rect.x() - (st.width / 2), rect.y() + st.textTop, width(), text, textWidth);
  1493. } else {
  1494. const auto rect = relativeButtonRect(removeButton, false);
  1495. auto selected = (index == _actionSel && _actionDown < 0) || (index == _actionDown);
  1496. if (removeButton) {
  1497. // Trash icon button when not disabled in Installed.
  1498. if (row->ripple) {
  1499. row->ripple->paint(p, rect.x(), rect.y(), width());
  1500. if (row->ripple->empty()) {
  1501. row->ripple.reset();
  1502. }
  1503. }
  1504. auto &icon = selected ? st::stickersRemove.iconOver : st::stickersRemove.icon;
  1505. auto position = st::stickersRemove.iconPosition;
  1506. if (position.x() < 0) position.setX((rect.width() - icon.width()) / 2);
  1507. if (position.y() < 0) position.setY((rect.height() - icon.height()) / 2);
  1508. icon.paint(p, rect.topLeft() + position, width());
  1509. } else {
  1510. // Round button ADD when not installed from Trending or Archived.
  1511. // Or round button UNDO after disabled from Installed.
  1512. const auto &st = _isInstalledTab
  1513. ? st::stickersUndoRemove
  1514. : st::stickersTrendingAdd;
  1515. const auto textWidth = _isInstalledTab ? _undoWidth : _addWidth;
  1516. const auto &text = _isInstalledTab ? _undoText : _addText;
  1517. (selected ? _buttonBgOver : _buttonBg).paint(p, myrtlrect(rect));
  1518. if (row->ripple) {
  1519. row->ripple->paint(p, rect.x(), rect.y(), width());
  1520. if (row->ripple->empty()) {
  1521. row->ripple.reset();
  1522. }
  1523. }
  1524. p.setFont(st.style.font);
  1525. p.setPen(selected ? st.textFgOver : st.textFg);
  1526. p.drawTextLeft(rect.x() - (st.width / 2), rect.y() + st.textTop, width(), text, textWidth);
  1527. }
  1528. }
  1529. }
  1530. void StickersBox::Inner::mousePressEvent(QMouseEvent *e) {
  1531. if (_dragging >= 0) {
  1532. mouseReleaseEvent(e);
  1533. }
  1534. _mouse = e->globalPos();
  1535. updateSelected();
  1536. setPressed(_selected);
  1537. if (_actionSel >= 0) {
  1538. setActionDown(_actionSel);
  1539. update(0, _itemsTop + _actionSel * _rowHeight, width(), _rowHeight);
  1540. } else if (auto selectedIndex = std::get_if<int>(&_selected)) {
  1541. if (_isInstalledTab && !_rows[*selectedIndex]->isRecentSet() && _inDragArea) {
  1542. _above = _dragging = _started = *selectedIndex;
  1543. _dragStart = mapFromGlobal(_mouse);
  1544. }
  1545. }
  1546. }
  1547. void StickersBox::Inner::setActionDown(int newActionDown) {
  1548. if (_actionDown == newActionDown) {
  1549. return;
  1550. }
  1551. if (_actionDown >= 0 && _actionDown < _rows.size()) {
  1552. update(0, _itemsTop + _actionDown * _rowHeight, width(), _rowHeight);
  1553. const auto row = _rows[_actionDown].get();
  1554. if (row->ripple) {
  1555. row->ripple->lastStop();
  1556. }
  1557. }
  1558. _actionDown = newActionDown;
  1559. if (_actionDown >= 0 && _actionDown < _rows.size()) {
  1560. update(0, _itemsTop + _actionDown * _rowHeight, width(), _rowHeight);
  1561. const auto row = _rows[_actionDown].get();
  1562. auto removeButton = (_isInstalledTab && !row->removed);
  1563. if (!row->ripple) {
  1564. if (_isInstalledTab) {
  1565. if (row->removed) {
  1566. auto rippleSize = QSize(_undoWidth - st::stickersUndoRemove.width, st::stickersUndoRemove.height);
  1567. auto rippleMask = Ui::RippleAnimation::RoundRectMask(rippleSize, st::roundRadiusLarge);
  1568. ensureRipple(st::stickersUndoRemove.ripple, std::move(rippleMask), removeButton, false);
  1569. } else {
  1570. auto rippleSize = st::stickersRemove.rippleAreaSize;
  1571. auto rippleMask = Ui::RippleAnimation::EllipseMask(QSize(rippleSize, rippleSize));
  1572. ensureRipple(st::stickersRemove.ripple, std::move(rippleMask), removeButton, false);
  1573. }
  1574. } else {
  1575. const auto installedSet = row->isInstalled()
  1576. && !row->isArchived()
  1577. && !row->removed;
  1578. const auto &st = installedSet
  1579. ? st::stickersTrendingInstalled
  1580. : st::stickersTrendingAdd;
  1581. const auto buttonTextWidth = installedSet
  1582. ? _installedWidth
  1583. : _addWidth;
  1584. auto rippleMask = Ui::RippleAnimation::RoundRectMask(
  1585. QSize(buttonTextWidth - st.width, st.height),
  1586. st::roundRadiusLarge);
  1587. ensureRipple(
  1588. st.ripple,
  1589. std::move(rippleMask),
  1590. removeButton,
  1591. installedSet);
  1592. }
  1593. }
  1594. if (row->ripple) {
  1595. auto rect = relativeButtonRect(removeButton, false);
  1596. row->ripple->add(mapFromGlobal(QCursor::pos()) - QPoint(myrtlrect(rect).x(), _itemsTop + _actionDown * _rowHeight + rect.y()));
  1597. }
  1598. }
  1599. }
  1600. void StickersBox::Inner::setSelected(SelectedRow selected) {
  1601. if (_selected == selected) {
  1602. return;
  1603. }
  1604. auto countSelectedIndex = [&] {
  1605. if (auto index = std::get_if<int>(&_selected)) {
  1606. return *index;
  1607. }
  1608. return -1;
  1609. };
  1610. auto selectedIndex = countSelectedIndex();
  1611. if (_megagroupSet && selectedIndex >= 0 && selectedIndex < _rows.size()) {
  1612. update(0, _itemsTop + selectedIndex * _rowHeight, width(), _rowHeight);
  1613. }
  1614. _selected = selected;
  1615. updateCursor();
  1616. selectedIndex = countSelectedIndex();
  1617. if (_megagroupSet && selectedIndex >= 0 && selectedIndex < _rows.size()) {
  1618. update(0, _itemsTop + selectedIndex * _rowHeight, width(), _rowHeight);
  1619. }
  1620. }
  1621. void StickersBox::Inner::setPressed(SelectedRow pressed) {
  1622. if (_pressed == pressed) {
  1623. return;
  1624. }
  1625. auto countPressedIndex = [&] {
  1626. if (auto index = std::get_if<int>(&_pressed)) {
  1627. return *index;
  1628. }
  1629. return -1;
  1630. };
  1631. auto pressedIndex = countPressedIndex();
  1632. if (_megagroupSet && pressedIndex >= 0 && pressedIndex < _rows.size()) {
  1633. update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight);
  1634. const auto row = _rows[pressedIndex].get();
  1635. if (row->ripple) {
  1636. row->ripple->lastStop();
  1637. }
  1638. }
  1639. _pressed = pressed;
  1640. pressedIndex = countPressedIndex();
  1641. if (_megagroupSet && pressedIndex >= 0 && pressedIndex < _rows.size()) {
  1642. update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight);
  1643. auto &set = _rows[pressedIndex];
  1644. if (!set->ripple) {
  1645. auto rippleMask = Ui::RippleAnimation::RectMask(QSize(width(), _rowHeight));
  1646. set->ripple = std::make_unique<Ui::RippleAnimation>(st::defaultRippleAnimation, std::move(rippleMask), [this, pressedIndex] {
  1647. update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight);
  1648. });
  1649. }
  1650. set->ripple->add(mapFromGlobal(QCursor::pos()) - QPoint(0, _itemsTop + pressedIndex * _rowHeight));
  1651. }
  1652. }
  1653. void StickersBox::Inner::ensureRipple(
  1654. const style::RippleAnimation &st,
  1655. QImage mask,
  1656. bool removeButton,
  1657. bool installedSet) {
  1658. const auto dy = _itemsTop + _actionDown * _rowHeight;
  1659. _rows[_actionDown]->ripple = std::make_unique<Ui::RippleAnimation>(st, std::move(mask), [=] {
  1660. update(myrtlrect(relativeButtonRect(removeButton, installedSet).translated(0, dy)));
  1661. });
  1662. }
  1663. void StickersBox::Inner::mouseMoveEvent(QMouseEvent *e) {
  1664. _mouse = e->globalPos();
  1665. updateSelected();
  1666. }
  1667. void StickersBox::Inner::updateSelected() {
  1668. auto local = mapFromGlobal(_mouse);
  1669. if (_dragging >= 0) {
  1670. auto shift = 0;
  1671. auto now = crl::now();
  1672. int firstSetIndex = 0;
  1673. if (_rows.at(firstSetIndex)->isRecentSet()) {
  1674. ++firstSetIndex;
  1675. }
  1676. if (_dragStart.y() > local.y() && _dragging > 0) {
  1677. shift = -floorclamp(_dragStart.y() - local.y() + (_rowHeight / 2), _rowHeight, 0, _dragging - firstSetIndex);
  1678. for (int32 from = _dragging, to = _dragging + shift; from > to; --from) {
  1679. qSwap(_rows[from], _rows[from - 1]);
  1680. _rows[from]->yadd = anim::value(_rows[from]->yadd.current() - _rowHeight, 0);
  1681. _shiftingStartTimes[from] = now;
  1682. }
  1683. } else if (_dragStart.y() < local.y() && _dragging + 1 < _rows.size()) {
  1684. shift = floorclamp(local.y() - _dragStart.y() + (_rowHeight / 2), _rowHeight, 0, _rows.size() - _dragging - 1);
  1685. for (int32 from = _dragging, to = _dragging + shift; from < to; ++from) {
  1686. qSwap(_rows[from], _rows[from + 1]);
  1687. _rows[from]->yadd = anim::value(_rows[from]->yadd.current() + _rowHeight, 0);
  1688. _shiftingStartTimes[from] = now;
  1689. }
  1690. }
  1691. if (shift) {
  1692. _dragging += shift;
  1693. _above = _dragging;
  1694. _dragStart.setY(_dragStart.y() + shift * _rowHeight);
  1695. if (!_shiftingAnimation.animating()) {
  1696. _shiftingAnimation.start();
  1697. }
  1698. }
  1699. _rows[_dragging]->yadd = anim::value(local.y() - _dragStart.y(), local.y() - _dragStart.y());
  1700. _shiftingStartTimes[_dragging] = 0;
  1701. shiftingAnimationCallback(now);
  1702. _draggingScrollDelta.fire_copy([&] {
  1703. if (local.y() < _visibleTop) {
  1704. return local.y() - _visibleTop;
  1705. } else if (local.y() >= _visibleBottom) {
  1706. return local.y() + 1 - _visibleBottom;
  1707. }
  1708. return 0;
  1709. }());
  1710. } else {
  1711. bool in = rect().marginsRemoved(QMargins(0, _itemsTop, 0, st::membersMarginBottom)).contains(local);
  1712. auto selected = SelectedRow();
  1713. auto actionSel = -1;
  1714. auto inDragArea = false;
  1715. if (in && !_rows.empty()) {
  1716. auto selectedIndex = floorclamp(local.y() - _itemsTop, _rowHeight, 0, _rows.size() - 1);
  1717. selected = selectedIndex;
  1718. local.setY(local.y() - _itemsTop - selectedIndex * _rowHeight);
  1719. const auto row = _rows[selectedIndex].get();
  1720. if (!_megagroupSet
  1721. && (_isInstalledTab
  1722. || (_section == Section::Featured)
  1723. || !row->isInstalled()
  1724. || row->isArchived()
  1725. || row->removed)) {
  1726. auto removeButton = (_isInstalledTab && !row->removed);
  1727. const auto installedSetButton = !_isInstalledTab
  1728. && row->isInstalled()
  1729. && !row->isArchived()
  1730. && !row->removed;
  1731. auto rect = myrtlrect(relativeButtonRect(removeButton, installedSetButton));
  1732. actionSel = rect.contains(local) ? selectedIndex : -1;
  1733. } else {
  1734. actionSel = -1;
  1735. }
  1736. if (!_megagroupSet && _isInstalledTab && !row->isRecentSet()) {
  1737. auto dragAreaWidth = _st.photoPosition.x() + st::stickersReorderIcon.width() + st::stickersReorderSkip;
  1738. auto dragArea = myrtlrect(0, 0, dragAreaWidth, _rowHeight);
  1739. inDragArea = dragArea.contains(local);
  1740. }
  1741. } else if (_megagroupSelectedSet) {
  1742. auto setTop = _megagroupDivider->y() - _rowHeight;
  1743. if (QRect(0, setTop, width(), _rowHeight).contains(local)) {
  1744. selected = MegagroupSet();
  1745. }
  1746. }
  1747. setSelected(selected);
  1748. if (_inDragArea != inDragArea) {
  1749. _inDragArea = inDragArea;
  1750. updateCursor();
  1751. }
  1752. setActionSel(actionSel);
  1753. _draggingScrollDelta.fire(0);
  1754. }
  1755. }
  1756. void StickersBox::Inner::updateCursor() {
  1757. setCursor(_inDragArea
  1758. ? style::cur_sizeall
  1759. : (!_megagroupSet && _isInstalledTab)
  1760. ? ((_actionSel >= 0 && (_actionDown < 0 || _actionDown == _actionSel))
  1761. ? style::cur_pointer
  1762. : style::cur_default)
  1763. : (!v::is_null(_selected) || !v::is_null(_pressed))
  1764. ? style::cur_pointer
  1765. : style::cur_default);
  1766. }
  1767. float64 StickersBox::Inner::aboveShadowOpacity() const {
  1768. if (_above < 0) return 0;
  1769. auto dx = 0;
  1770. auto dy = qAbs(_above * _rowHeight + qRound(_rows[_above]->yadd.current()) - _started * _rowHeight);
  1771. return qMin((dx + dy) * 2. / _rowHeight, 1.);
  1772. }
  1773. void StickersBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
  1774. auto pressed = std::exchange(_pressed, SelectedRow());
  1775. updateCursor();
  1776. _mouse = e->globalPos();
  1777. updateSelected();
  1778. const auto down = _actionDown;
  1779. setActionDown(-1);
  1780. if (down == _actionSel && _actionSel >= 0) {
  1781. const auto row = _rows[down].get();
  1782. const auto installedSet = row->isInstalled()
  1783. && !row->isArchived()
  1784. && !row->removed;
  1785. const auto callback = installedSet
  1786. ? _removeSetCallback
  1787. : _installSetCallback;
  1788. if (callback) {
  1789. row->ripple.reset();
  1790. callback(row->set->id);
  1791. }
  1792. } else if (_dragging >= 0) {
  1793. _rows[_dragging]->yadd.start(0.);
  1794. _aboveShadowFadeStart = _shiftingStartTimes[_dragging] = crl::now();
  1795. _aboveShadowFadeOpacity = anim::value(aboveShadowOpacity(), 0);
  1796. if (!_shiftingAnimation.animating()) {
  1797. _shiftingAnimation.start();
  1798. }
  1799. _dragging = _started = -1;
  1800. } else if (pressed == _selected && _actionSel < 0 && down < 0) {
  1801. const auto selectedIndex = [&] {
  1802. if (auto index = std::get_if<int>(&_selected)) {
  1803. return *index;
  1804. }
  1805. return -1;
  1806. }();
  1807. const auto showSetByRow = [&](const Row &row) {
  1808. setSelected(SelectedRow());
  1809. _show->showBox(Box<StickerSetBox>(_show, row.set));
  1810. };
  1811. if (selectedIndex >= 0 && !_inDragArea) {
  1812. const auto row = _rows[selectedIndex].get();
  1813. if (!row->isRecentSet()) {
  1814. if (_megagroupSet) {
  1815. setMegagroupSelectedSet(row->set->identifier());
  1816. } else {
  1817. showSetByRow(*row);
  1818. }
  1819. }
  1820. } else if (_megagroupSelectedSet && v::is<MegagroupSet>(_selected)) {
  1821. showSetByRow(*_megagroupSelectedSet);
  1822. }
  1823. }
  1824. }
  1825. void StickersBox::Inner::saveGroupSet(Fn<void()> done) {
  1826. Expects(_megagroupSet != nullptr);
  1827. auto oldId = _megagroupSetEmoji
  1828. ? _megagroupSet->mgInfo->emojiSet.id
  1829. : _megagroupSet->mgInfo->stickerSet.id;
  1830. auto newId = _megagroupSetInput.id;
  1831. if (newId == oldId) {
  1832. done();
  1833. } else if (_megagroupSetEmoji) {
  1834. checkGroupLevel(done);
  1835. } else {
  1836. session().api().setGroupStickerSet(_megagroupSet, _megagroupSetInput);
  1837. session().data().stickers().notifyStickerSetInstalled(
  1838. Data::Stickers::MegagroupSetId);
  1839. }
  1840. }
  1841. void StickersBox::Inner::checkGroupLevel(Fn<void()> done) {
  1842. Expects(_megagroupSet != nullptr);
  1843. Expects(_megagroupSetEmoji);
  1844. const auto peer = _megagroupSet;
  1845. const auto save = [=] {
  1846. session().api().setGroupEmojiSet(peer, _megagroupSetInput);
  1847. session().data().stickers().notifyEmojiSetInstalled(
  1848. Data::Stickers::MegagroupSetId);
  1849. done();
  1850. };
  1851. if (!_megagroupSetInput) {
  1852. save();
  1853. return;
  1854. } else if (_checkingGroupLevel) {
  1855. return;
  1856. }
  1857. _checkingGroupLevel = true;
  1858. const auto weak = Ui::MakeWeak(this);
  1859. CheckBoostLevel(_show, peer, [=](int level) {
  1860. if (!weak) {
  1861. return std::optional<Ui::AskBoostReason>();
  1862. }
  1863. _checkingGroupLevel = false;
  1864. const auto required = Data::LevelLimits(
  1865. &peer->session()).groupEmojiStickersLevelMin();
  1866. if (level >= required) {
  1867. save();
  1868. return std::optional<Ui::AskBoostReason>();
  1869. }
  1870. return std::make_optional(Ui::AskBoostReason{
  1871. Ui::AskBoostEmojiPack{ required }
  1872. });
  1873. }, [=] { _checkingGroupLevel = false; });
  1874. }
  1875. void StickersBox::Inner::setRowRemovedBySetId(uint64 setId, bool removed) {
  1876. const auto index = getRowIndex(setId);
  1877. if (index >= 0) {
  1878. setRowRemoved(index, removed);
  1879. }
  1880. }
  1881. void StickersBox::Inner::setRowRemoved(int index, bool removed) {
  1882. auto &row = _rows[index];
  1883. if (row->removed != removed) {
  1884. row->removed = removed;
  1885. row->ripple.reset();
  1886. update(0, _itemsTop + index * _rowHeight, width(), _rowHeight);
  1887. updateSelected();
  1888. }
  1889. }
  1890. void StickersBox::Inner::leaveEventHook(QEvent *e) {
  1891. _mouse = QPoint(-1, -1);
  1892. updateSelected();
  1893. }
  1894. void StickersBox::Inner::leaveToChildEvent(QEvent *e, QWidget *child) {
  1895. _mouse = QPoint(-1, -1);
  1896. updateSelected();
  1897. }
  1898. bool StickersBox::Inner::shiftingAnimationCallback(crl::time now) {
  1899. if (anim::Disabled()) {
  1900. now += st::stickersRowDuration;
  1901. }
  1902. auto animating = false;
  1903. auto updateMin = -1;
  1904. auto updateMax = 0;
  1905. for (auto i = 0, count = int(_shiftingStartTimes.size()); i != count; ++i) {
  1906. const auto start = _shiftingStartTimes[i];
  1907. if (start) {
  1908. if (updateMin < 0) {
  1909. updateMin = i;
  1910. }
  1911. updateMax = i;
  1912. if (start + st::stickersRowDuration > now && now >= start) {
  1913. _rows[i]->yadd.update(float64(now - start) / st::stickersRowDuration, anim::sineInOut);
  1914. animating = true;
  1915. } else {
  1916. _rows[i]->yadd.finish();
  1917. _shiftingStartTimes[i] = 0;
  1918. }
  1919. }
  1920. }
  1921. if (_aboveShadowFadeStart) {
  1922. if (updateMin < 0 || updateMin > _above) updateMin = _above;
  1923. if (updateMax < _above) updateMin = _above;
  1924. if (_aboveShadowFadeStart + st::stickersRowDuration > now && now > _aboveShadowFadeStart) {
  1925. _aboveShadowFadeOpacity.update(float64(now - _aboveShadowFadeStart) / st::stickersRowDuration, anim::sineInOut);
  1926. animating = true;
  1927. } else {
  1928. _aboveShadowFadeOpacity.finish();
  1929. _aboveShadowFadeStart = 0;
  1930. }
  1931. }
  1932. if (_dragging >= 0) {
  1933. if (updateMin < 0 || updateMin > _dragging) {
  1934. updateMin = _dragging;
  1935. }
  1936. if (updateMax < _dragging) updateMax = _dragging;
  1937. }
  1938. if (updateMin == 1 && _rows[0]->isRecentSet()) {
  1939. updateMin = 0; // Repaint from the very top of the content.
  1940. }
  1941. if (updateMin >= 0) {
  1942. update(0, _itemsTop + _rowHeight * (updateMin - 1), width(), _rowHeight * (updateMax - updateMin + 3));
  1943. }
  1944. if (!animating) {
  1945. _above = _dragging;
  1946. }
  1947. return animating;
  1948. }
  1949. void StickersBox::Inner::clear() {
  1950. _rows.clear();
  1951. _shiftingStartTimes.clear();
  1952. _aboveShadowFadeStart = 0;
  1953. _aboveShadowFadeOpacity = anim::value();
  1954. _shiftingAnimation.stop();
  1955. _above = _dragging = _started = -1;
  1956. setSelected(SelectedRow());
  1957. setPressed(SelectedRow());
  1958. setActionSel(-1);
  1959. setActionDown(-1);
  1960. update();
  1961. }
  1962. void StickersBox::Inner::setActionSel(int32 actionSel) {
  1963. if (actionSel != _actionSel) {
  1964. if (_actionSel >= 0) update(0, _itemsTop + _actionSel * _rowHeight, width(), _rowHeight);
  1965. _actionSel = actionSel;
  1966. if (_actionSel >= 0) update(0, _itemsTop + _actionSel * _rowHeight, width(), _rowHeight);
  1967. updateCursor();
  1968. }
  1969. }
  1970. void StickersBox::Inner::AddressField::correctValue(
  1971. const QString &was,
  1972. int wasCursor,
  1973. QString &now,
  1974. int &nowCursor) {
  1975. auto newText = now;
  1976. auto newCursor = nowCursor;
  1977. auto removeFromBeginning = {
  1978. u"http://"_q,
  1979. u"https://"_q,
  1980. u"www.t.me/"_q,
  1981. u"www.telegram.me/"_q,
  1982. u"www.telegram.dog/"_q,
  1983. u"t.me/"_q,
  1984. u"telegram.me/"_q,
  1985. u"telegram.dog/"_q,
  1986. u"addstickers/"_q,
  1987. };
  1988. for (auto &removePhrase : removeFromBeginning) {
  1989. if (newText.startsWith(removePhrase)) {
  1990. newText = newText.mid(removePhrase.size());
  1991. newCursor = newText.size();
  1992. }
  1993. }
  1994. setCorrectedText(now, nowCursor, newText, newCursor);
  1995. }
  1996. void StickersBox::Inner::handleMegagroupSetAddressChange() {
  1997. auto text = _megagroupSetField->getLastText().trimmed();
  1998. if (text.isEmpty()) {
  1999. if (_megagroupSelectedSet) {
  2000. const auto &sets = session().data().stickers().sets();
  2001. const auto it = sets.find(_megagroupSelectedSet->set->id);
  2002. if (it != sets.cend() && !it->second->shortName.isEmpty()) {
  2003. setMegagroupSelectedSet({});
  2004. }
  2005. }
  2006. } else if (!_megagroupSetRequestId) {
  2007. _megagroupSetRequestId = _api.request(MTPmessages_GetStickerSet(
  2008. MTP_inputStickerSetShortName(MTP_string(text)),
  2009. MTP_int(0) // hash
  2010. )).done([=](const MTPmessages_StickerSet &result) {
  2011. _megagroupSetRequestId = 0;
  2012. result.match([&](const MTPDmessages_stickerSet &data) {
  2013. const auto set = session().data().stickers().feedSetFull(data);
  2014. setMegagroupSelectedSet(set->identifier());
  2015. }, [](const MTPDmessages_stickerSetNotModified &) {
  2016. LOG(("API Error: Unexpected messages.stickerSetNotModified."));
  2017. });
  2018. }).fail([=] {
  2019. _megagroupSetRequestId = 0;
  2020. setMegagroupSelectedSet({});
  2021. }).send();
  2022. } else {
  2023. _megagroupSetAddressChangedTimer.callOnce(kHandleMegagroupSetAddressChangeTimeout);
  2024. }
  2025. }
  2026. void StickersBox::Inner::rebuildMegagroupSet() {
  2027. Expects(_megagroupSet != nullptr);
  2028. const auto clearCurrent = [&] {
  2029. if (_megagroupSelectedSet) {
  2030. _megagroupSetField->setText(QString());
  2031. _megagroupSetField->finishAnimating();
  2032. }
  2033. _megagroupSelectedSet = nullptr;
  2034. _megagroupSelectedRemove.destroy();
  2035. _megagroupSelectedShadow.destroy();
  2036. };
  2037. if (!_megagroupSetInput.id) {
  2038. clearCurrent();
  2039. return;
  2040. }
  2041. auto setId = _megagroupSetInput.id;
  2042. const auto &sets = session().data().stickers().sets();
  2043. auto it = sets.find(setId);
  2044. if (it == sets.cend()
  2045. || (it->second->flags & SetFlag::NotLoaded)) {
  2046. // It may have been in sets and stored in _megagroupSelectedSet
  2047. // already, but then removed from sets. We need to clear the stored
  2048. // pointer, otherwise we may crash in paint event while loading.
  2049. clearCurrent();
  2050. session().api().scheduleStickerSetRequest(
  2051. _megagroupSetInput.id,
  2052. _megagroupSetInput.accessHash);
  2053. return;
  2054. }
  2055. const auto set = it->second.get();
  2056. auto count = fillSetCount(set);
  2057. auto sticker = (DocumentData*)nullptr;
  2058. auto pixw = 0, pixh = 0;
  2059. fillSetCover(set, &sticker, &pixw, &pixh);
  2060. auto flagsOverride = SetFlag::Installed;
  2061. auto removed = false;
  2062. auto maxNameWidth = countMaxNameWidth(!_isInstalledTab);
  2063. auto titleWidth = 0;
  2064. auto title = FillSetTitle(set, maxNameWidth, &titleWidth);
  2065. if (!_megagroupSelectedSet
  2066. || _megagroupSelectedSet->set->id != set->id) {
  2067. _megagroupSetField->setText(set->shortName);
  2068. _megagroupSetField->finishAnimating();
  2069. }
  2070. _megagroupSelectedSet = std::make_unique<Row>(
  2071. set,
  2072. sticker,
  2073. count,
  2074. title,
  2075. titleWidth,
  2076. flagsOverride,
  2077. removed,
  2078. pixw,
  2079. pixh);
  2080. _itemsTop += st::lineWidth + _rowHeight;
  2081. if (!_megagroupSelectedRemove) {
  2082. _megagroupSelectedRemove.create(this, st::groupStickersRemove);
  2083. _megagroupSelectedRemove->show(anim::type::instant);
  2084. _megagroupSelectedRemove->setClickedCallback([this] {
  2085. setMegagroupSelectedSet({});
  2086. });
  2087. _megagroupSelectedShadow.create(this);
  2088. updateControlsGeometry();
  2089. }
  2090. }
  2091. void StickersBox::Inner::rebuild(bool masks) {
  2092. _itemsTop = st::lineWidth;
  2093. if (_megagroupSet) {
  2094. _itemsTop += st::groupStickersFieldPadding.top() + _megagroupSetField->height() + st::groupStickersFieldPadding.bottom();
  2095. _itemsTop += _megagroupDivider->height() + st::groupStickersSubTitleHeight;
  2096. rebuildMegagroupSet();
  2097. }
  2098. _oldRows = std::move(_rows);
  2099. clear();
  2100. const auto &order = ([&]() -> const StickersSetsOrder & {
  2101. if (_section == Section::Installed) {
  2102. auto &result = _megagroupSetEmoji
  2103. ? session().data().stickers().emojiSetsOrder()
  2104. : session().data().stickers().setsOrder();
  2105. if (_megagroupSet && result.empty()) {
  2106. return _megagroupSetEmoji
  2107. ? session().data().stickers().featuredEmojiSetsOrder()
  2108. : session().data().stickers().featuredSetsOrder();
  2109. }
  2110. return result;
  2111. } else if (_section == Section::Masks) {
  2112. return session().data().stickers().maskSetsOrder();
  2113. } else if (_section == Section::Featured) {
  2114. return session().data().stickers().featuredSetsOrder();
  2115. }
  2116. return masks
  2117. ? session().data().stickers().archivedMaskSetsOrder()
  2118. : session().data().stickers().archivedSetsOrder();
  2119. })();
  2120. _rows.reserve(order.size() + 1);
  2121. _shiftingStartTimes.reserve(order.size() + 1);
  2122. const auto &sets = session().data().stickers().sets();
  2123. if (_megagroupSet) {
  2124. auto usingFeatured = _megagroupSetEmoji
  2125. ? session().data().stickers().emojiSetsOrder().empty()
  2126. : session().data().stickers().setsOrder().empty();
  2127. _megagroupSubTitle->setText(usingFeatured
  2128. ? (_megagroupSetEmoji
  2129. ? tr::lng_stickers_group_from_featured(tr::now)
  2130. : tr::lng_emoji_group_from_featured(tr::now))
  2131. : _megagroupSetEmoji
  2132. ? tr::lng_emoji_group_from_your(tr::now)
  2133. : tr::lng_stickers_group_from_your(tr::now));
  2134. updateControlsGeometry();
  2135. } else if (_isInstalledTab) {
  2136. const auto cloudIt = sets.find((_section == Section::Masks)
  2137. ? Data::Stickers::CloudRecentAttachedSetId
  2138. : Data::Stickers::CloudRecentSetId); // Section::Installed.
  2139. if (cloudIt != sets.cend() && !cloudIt->second->stickers.isEmpty()) {
  2140. rebuildAppendSet(cloudIt->second.get());
  2141. }
  2142. }
  2143. for (const auto setId : order) {
  2144. auto it = sets.find(setId);
  2145. if (it == sets.cend()) {
  2146. continue;
  2147. }
  2148. const auto set = it->second.get();
  2149. rebuildAppendSet(set);
  2150. if (set->stickers.isEmpty()
  2151. || (set->flags & SetFlag::NotLoaded)) {
  2152. session().api().scheduleStickerSetRequest(
  2153. set->id,
  2154. set->accessHash);
  2155. }
  2156. }
  2157. _oldRows.clear();
  2158. session().api().requestStickerSets();
  2159. updateSize();
  2160. }
  2161. void StickersBox::Inner::setMegagroupSelectedSet(
  2162. const StickerSetIdentifier &set) {
  2163. _megagroupSetInput = set;
  2164. rebuild(false);
  2165. _scrollsToY.fire(0);
  2166. updateSelected();
  2167. }
  2168. void StickersBox::Inner::updateSize(int newWidth) {
  2169. auto naturalHeight = _itemsTop + int(_rows.size()) * _rowHeight + st::membersMarginBottom;
  2170. resize(newWidth ? newWidth : width(), qMax(_minHeight, naturalHeight));
  2171. updateControlsGeometry();
  2172. checkLoadMore();
  2173. }
  2174. void StickersBox::Inner::updateRows() {
  2175. const auto maxNameWidth = countMaxNameWidth(false);
  2176. const auto maxNameWidthInstalled = countMaxNameWidth(true);
  2177. const auto &sets = session().data().stickers().sets();
  2178. for (const auto &row : _rows) {
  2179. const auto it = sets.find(row->set->id);
  2180. if (it == sets.cend()) {
  2181. continue;
  2182. }
  2183. const auto set = it->second.get();
  2184. if (!row->sticker) {
  2185. auto sticker = (DocumentData*)nullptr;
  2186. auto pixw = 0, pixh = 0;
  2187. fillSetCover(set, &sticker, &pixw, &pixh);
  2188. if (sticker) {
  2189. if (row->sticker != sticker && !row->thumbnailMedia) {
  2190. row->lottie = nullptr;
  2191. row->stickerMedia = nullptr;
  2192. }
  2193. row->sticker = sticker;
  2194. row->pixw = pixw;
  2195. row->pixh = pixh;
  2196. }
  2197. }
  2198. if (!row->isRecentSet()) {
  2199. auto wasInstalled = row->isInstalled();
  2200. auto wasArchived = row->isArchived();
  2201. row->flagsOverride = fillSetFlags(set);
  2202. if (_isInstalledTab) {
  2203. row->flagsOverride &= ~SetFlag::Archived;
  2204. }
  2205. if (row->isInstalled() != wasInstalled
  2206. || row->isArchived() != wasArchived) {
  2207. row->ripple.reset();
  2208. }
  2209. }
  2210. const auto installedSet = (!_isInstalledTab
  2211. && row->isInstalled()
  2212. && !row->isArchived()
  2213. && !row->removed);
  2214. row->title = FillSetTitle(
  2215. set,
  2216. installedSet ? maxNameWidthInstalled : maxNameWidth,
  2217. &row->titleWidth);
  2218. row->count = fillSetCount(set);
  2219. }
  2220. update();
  2221. }
  2222. bool StickersBox::Inner::appendSet(not_null<StickersSet*> set) {
  2223. for (const auto &row : _rows) {
  2224. if (row->set == set) {
  2225. return false;
  2226. }
  2227. }
  2228. rebuildAppendSet(set);
  2229. return true;
  2230. }
  2231. bool StickersBox::Inner::skipPremium() const {
  2232. return !_session->premiumPossible();
  2233. }
  2234. int StickersBox::Inner::countMaxNameWidth(bool installedSet) const {
  2235. int namex = _st.namePosition.x();
  2236. if (!_megagroupSet && _isInstalledTab) {
  2237. namex += st::stickersReorderIcon.width() + st::stickersReorderSkip;
  2238. }
  2239. int namew = st::boxWideWidth - namex - st::contactsPadding.right();
  2240. if (_isInstalledTab) {
  2241. if (!_megagroupSet) {
  2242. namew -= _undoWidth - st::stickersUndoRemove.width;
  2243. }
  2244. } else {
  2245. namew -= installedSet
  2246. ? (_installedWidth - st::stickersTrendingInstalled.width)
  2247. : (_addWidth - st::stickersTrendingAdd.width);
  2248. if (_section == Section::Featured) {
  2249. namew -= st::stickersFeaturedUnreadSize + st::stickersFeaturedUnreadSkip;
  2250. }
  2251. }
  2252. return namew;
  2253. }
  2254. void StickersBox::Inner::rebuildAppendSet(not_null<StickersSet*> set) {
  2255. auto flagsOverride = (set->id != Data::Stickers::CloudRecentSetId)
  2256. ? fillSetFlags(set)
  2257. : SetFlag::Installed;
  2258. auto removed = false;
  2259. if (_isInstalledTab && (flagsOverride & SetFlag::Archived)) {
  2260. return;
  2261. }
  2262. DocumentData *sticker = nullptr;
  2263. int pixw = 0, pixh = 0;
  2264. fillSetCover(set, &sticker, &pixw, &pixh);
  2265. const auto maxNameWidth = countMaxNameWidth(!_isInstalledTab
  2266. && (flagsOverride & SetFlag::Installed)
  2267. && !(flagsOverride & SetFlag::Archived)
  2268. && !removed);
  2269. int titleWidth = 0;
  2270. QString title = FillSetTitle(set, maxNameWidth, &titleWidth);
  2271. int count = fillSetCount(set);
  2272. const auto existing = [&]{
  2273. const auto now = int(_rows.size());
  2274. const auto setProj = [](const std::unique_ptr<Row> &row) {
  2275. return row ? row->set.get() : nullptr;
  2276. };
  2277. if (_oldRows.size() > now
  2278. && setProj(_oldRows[now]) == set.get()) {
  2279. return _oldRows.begin() + now;
  2280. }
  2281. return ranges::find(_oldRows, set.get(), setProj);
  2282. }();
  2283. if (existing != end(_oldRows)) {
  2284. const auto raw = existing->get();
  2285. raw->sticker = sticker;
  2286. raw->count = count;
  2287. raw->title = title;
  2288. raw->titleWidth = titleWidth;
  2289. raw->flagsOverride = flagsOverride;
  2290. raw->removed = removed;
  2291. raw->pixw = pixw;
  2292. raw->pixh = pixh;
  2293. raw->yadd = {};
  2294. auto oldStickerMedia = std::move(raw->stickerMedia);
  2295. auto oldThumbnailMedia = std::move(raw->thumbnailMedia);
  2296. raw->stickerMedia = sticker ? sticker->activeMediaView() : nullptr;
  2297. raw->thumbnailMedia = set->activeThumbnailView();
  2298. if (raw->thumbnailMedia != oldThumbnailMedia
  2299. || (!raw->thumbnailMedia && raw->stickerMedia != oldStickerMedia)) {
  2300. raw->lottie = nullptr;
  2301. }
  2302. _rows.push_back(std::move(*existing));
  2303. } else {
  2304. _rows.push_back(std::make_unique<Row>(
  2305. set,
  2306. sticker,
  2307. count,
  2308. title,
  2309. titleWidth,
  2310. flagsOverride,
  2311. removed,
  2312. pixw,
  2313. pixh));
  2314. }
  2315. _shiftingStartTimes.push_back(0);
  2316. }
  2317. void StickersBox::Inner::fillSetCover(
  2318. not_null<StickersSet*> set,
  2319. DocumentData **outSticker,
  2320. int *outWidth,
  2321. int *outHeight) const {
  2322. if (set->stickers.isEmpty()) {
  2323. *outSticker = nullptr;
  2324. *outWidth = *outHeight = 0;
  2325. return;
  2326. }
  2327. auto sticker = *outSticker = set->stickers.front();
  2328. const auto size = set->hasThumbnail()
  2329. ? QSize(
  2330. set->thumbnailLocation().width(),
  2331. set->thumbnailLocation().height())
  2332. : sticker->hasThumbnail()
  2333. ? QSize(
  2334. sticker->thumbnailLocation().width(),
  2335. sticker->thumbnailLocation().height())
  2336. : QSize(1, 1);
  2337. auto pixw = size.width();
  2338. auto pixh = size.height();
  2339. if (pixw > _st.photoSize) {
  2340. if (pixw > pixh) {
  2341. pixh = (pixh * _st.photoSize) / pixw;
  2342. pixw = _st.photoSize;
  2343. } else {
  2344. pixw = (pixw * _st.photoSize) / pixh;
  2345. pixh = _st.photoSize;
  2346. }
  2347. } else if (pixh > _st.photoSize) {
  2348. pixw = (pixw * _st.photoSize) / pixh;
  2349. pixh = _st.photoSize;
  2350. }
  2351. *outWidth = pixw;
  2352. *outHeight = pixh;
  2353. }
  2354. int StickersBox::Inner::fillSetCount(not_null<StickersSet*> set) const {
  2355. const auto skipPremium = this->skipPremium();
  2356. int result = set->stickers.isEmpty()
  2357. ? set->count
  2358. : set->stickers.size();
  2359. if (skipPremium && !set->stickers.isEmpty()) {
  2360. result -= ranges::count(
  2361. set->stickers,
  2362. true,
  2363. &DocumentData::isPremiumSticker);
  2364. }
  2365. auto added = 0;
  2366. if (set->id == Data::Stickers::CloudRecentSetId) {
  2367. const auto &sets = session().data().stickers().sets();
  2368. const auto &recent = session().data().stickers().getRecentPack();
  2369. auto customIt = sets.find(Data::Stickers::CustomSetId);
  2370. if (customIt != sets.cend()) {
  2371. auto &custom = customIt->second->stickers;
  2372. added = custom.size();
  2373. if (skipPremium) {
  2374. added -= ranges::count(
  2375. custom,
  2376. true,
  2377. &DocumentData::isPremiumSticker);
  2378. }
  2379. for (const auto &sticker : recent) {
  2380. if (skipPremium && sticker.first->isPremiumSticker()) {
  2381. continue;
  2382. } else if (customIt->second->stickers.indexOf(sticker.first) < 0) {
  2383. ++added;
  2384. }
  2385. }
  2386. } else {
  2387. added = recent.size();
  2388. }
  2389. }
  2390. return result + added;
  2391. }
  2392. Data::StickersSetFlags StickersBox::Inner::fillSetFlags(
  2393. not_null<StickersSet*> set) const {
  2394. const auto result = set->flags;
  2395. return (_section == Section::Featured)
  2396. ? result
  2397. : (result & ~SetFlag::Unread);
  2398. }
  2399. template <typename Check>
  2400. StickersSetsOrder StickersBox::Inner::collectSets(Check check) const {
  2401. StickersSetsOrder result;
  2402. result.reserve(_rows.size());
  2403. for (const auto &row : _rows) {
  2404. if (check(row.get())) {
  2405. result.push_back(row->set->id);
  2406. }
  2407. }
  2408. return result;
  2409. }
  2410. StickersSetsOrder StickersBox::Inner::order() const {
  2411. return collectSets([](Row *row) {
  2412. return !row->isArchived() && !row->removed && !row->isRecentSet();
  2413. });
  2414. }
  2415. StickersSetsOrder StickersBox::Inner::fullOrder() const {
  2416. return collectSets([](Row *row) {
  2417. return !row->isRecentSet();
  2418. });
  2419. }
  2420. StickersSetsOrder StickersBox::Inner::removedSets() const {
  2421. return collectSets([](Row *row) {
  2422. return row->removed;
  2423. });
  2424. }
  2425. int StickersBox::Inner::getRowIndex(uint64 setId) const {
  2426. for (auto i = 0, count = int(_rows.size()); i != count; ++i) {
  2427. auto &row = _rows[i];
  2428. if (row->set->id == setId) {
  2429. return i;
  2430. }
  2431. }
  2432. return -1;
  2433. }
  2434. void StickersBox::Inner::setFullOrder(const StickersSetsOrder &order) {
  2435. for (const auto setId : order) {
  2436. auto index = getRowIndex(setId);
  2437. if (index >= 0) {
  2438. auto row = std::move(_rows[index]);
  2439. auto count = _rows.size();
  2440. for (auto i = index + 1; i != count; ++i) {
  2441. _rows[i - 1] = std::move(_rows[i]);
  2442. }
  2443. _rows[count - 1] = std::move(row);
  2444. }
  2445. }
  2446. }
  2447. void StickersBox::Inner::setRemovedSets(const StickersSetsOrder &removed) {
  2448. for (auto i = 0, count = int(_rows.size()); i != count; ++i) {
  2449. setRowRemoved(i, removed.contains(_rows[i]->set->id));
  2450. }
  2451. }
  2452. void StickersBox::Inner::visibleTopBottomUpdated(
  2453. int visibleTop,
  2454. int visibleBottom) {
  2455. _visibleTop = visibleTop;
  2456. _visibleBottom = visibleBottom;
  2457. updateScrollbarWidth();
  2458. if (_section == Section::Featured) {
  2459. readVisibleSets();
  2460. }
  2461. checkLoadMore();
  2462. }
  2463. void StickersBox::Inner::checkLoadMore() {
  2464. if (_loadMoreCallback) {
  2465. auto scrollHeight = (_visibleBottom - _visibleTop);
  2466. int scrollTop = _visibleTop, scrollTopMax = height() - scrollHeight;
  2467. if (scrollTop + PreloadHeightsCount * scrollHeight >= scrollTopMax) {
  2468. _loadMoreCallback();
  2469. }
  2470. }
  2471. }
  2472. void StickersBox::Inner::readVisibleSets() {
  2473. auto itemsVisibleTop = _visibleTop - _itemsTop;
  2474. auto itemsVisibleBottom = _visibleBottom - _itemsTop;
  2475. int rowFrom = floorclamp(itemsVisibleTop, _rowHeight, 0, _rows.size());
  2476. int rowTo = ceilclamp(itemsVisibleBottom, _rowHeight, 0, _rows.size());
  2477. for (int i = rowFrom; i < rowTo; ++i) {
  2478. const auto row = _rows[i].get();
  2479. if (!row->isUnread()) {
  2480. continue;
  2481. }
  2482. if ((i * _rowHeight < itemsVisibleTop)
  2483. || ((i + 1) * _rowHeight > itemsVisibleBottom)) {
  2484. continue;
  2485. }
  2486. const auto thumbnailLoading = row->set->hasThumbnail()
  2487. ? row->set->thumbnailLoading()
  2488. : row->sticker
  2489. ? row->sticker->thumbnailLoading()
  2490. : false;
  2491. const auto thumbnailLoaded = row->set->hasThumbnail()
  2492. ? (row->thumbnailMedia
  2493. && (row->thumbnailMedia->image()
  2494. || !row->thumbnailMedia->content().isEmpty()))
  2495. : row->sticker
  2496. ? (row->stickerMedia && row->stickerMedia->loaded())
  2497. : true;
  2498. if (!thumbnailLoading || thumbnailLoaded) {
  2499. session().api().readFeaturedSetDelayed(row->set->id);
  2500. }
  2501. }
  2502. }
  2503. void StickersBox::Inner::updateScrollbarWidth() {
  2504. auto width = (_visibleBottom - _visibleTop < height()) ? (st::boxScroll.width - st::boxScroll.deltax) : 0;
  2505. if (_scrollbar != width) {
  2506. _scrollbar = width;
  2507. update();
  2508. }
  2509. }
  2510. StickersBox::Inner::~Inner() {
  2511. clear();
  2512. }