dialogs_list.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #pragma once
  8. #include "dialogs/dialogs_row.h"
  9. class PeerData;
  10. namespace Dialogs {
  11. enum class SortMode;
  12. class List final {
  13. public:
  14. List(SortMode sortMode, FilterId filterId = 0);
  15. List(const List &other) = delete;
  16. List &operator=(const List &other) = delete;
  17. List(List &&other) = default;
  18. List &operator=(List &&other) = default;
  19. ~List() = default;
  20. void clear() {
  21. _rows.clear();
  22. _rowByKey.clear();
  23. }
  24. [[nodiscard]] int size() const {
  25. return _rows.size();
  26. }
  27. [[nodiscard]] bool empty() const {
  28. return _rows.empty();
  29. }
  30. [[nodiscard]] int height() const {
  31. return _rows.empty()
  32. ? 0
  33. : (_rows.back()->top() + _rows.back()->height());
  34. }
  35. [[nodiscard]] bool contains(Key key) const {
  36. return _rowByKey.find(key) != _rowByKey.end();
  37. }
  38. [[nodiscard]] Row *getRow(Key key) const {
  39. const auto i = _rowByKey.find(key);
  40. return (i != _rowByKey.end()) ? i->second.get() : nullptr;
  41. }
  42. [[nodiscard]] Row *rowAtY(int y) const;
  43. not_null<Row*> addToEnd(Key key);
  44. Row *adjustByName(Key key);
  45. not_null<Row*> addByName(Key key);
  46. bool moveToTop(Key key);
  47. void adjustByDate(not_null<Row*> row);
  48. bool updateHeight(Key key, float64 narrowRatio);
  49. bool updateHeights(float64 narrowRatio);
  50. bool remove(Key key, Row *replacedBy = nullptr);
  51. using const_iterator = std::vector<not_null<Row*>>::const_iterator;
  52. using iterator = const_iterator;
  53. [[nodiscard]] const_iterator cbegin() const { return _rows.cbegin(); }
  54. [[nodiscard]] const_iterator cend() const { return _rows.cend(); }
  55. [[nodiscard]] const_iterator begin() const { return cbegin(); }
  56. [[nodiscard]] const_iterator end() const { return cend(); }
  57. [[nodiscard]] iterator begin() { return cbegin(); }
  58. [[nodiscard]] iterator end() { return cend(); }
  59. [[nodiscard]] const_iterator cfind(Row *value) const;
  60. [[nodiscard]] const_iterator find(Row *value) const {
  61. return cfind(value);
  62. }
  63. [[nodiscard]] iterator find(Row *value) { return cfind(value); }
  64. [[nodiscard]] iterator findByY(int y) const;
  65. private:
  66. void adjustByName(not_null<Row*> row);
  67. void rotate(
  68. std::vector<not_null<Row*>>::iterator first,
  69. std::vector<not_null<Row*>>::iterator middle,
  70. std::vector<not_null<Row*>>::iterator last);
  71. SortMode _sortMode = SortMode();
  72. FilterId _filterId = 0;
  73. float64 _narrowRatio = 0.;
  74. std::vector<not_null<Row*>> _rows;
  75. std::map<Key, std::unique_ptr<Row>> _rowByKey;
  76. };
  77. } // namespace Dialogs