vertical_layout.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "ui/rp_widget.h"
  9. #include "base/object_ptr.h"
  10. namespace Ui {
  11. class VerticalLayout : public RpWidget {
  12. public:
  13. using RpWidget::RpWidget;
  14. [[nodiscard]] int count() const {
  15. return _rows.size();
  16. }
  17. [[nodiscard]] not_null<RpWidget*> widgetAt(int index) const {
  18. Expects(index >= 0 && index < count());
  19. return _rows[index].widget.data();
  20. }
  21. template <
  22. typename Widget,
  23. typename = std::enable_if_t<
  24. std::is_base_of_v<RpWidget, Widget>>>
  25. Widget *insert(
  26. int atPosition,
  27. object_ptr<Widget> &&child,
  28. const style::margins &margin = style::margins()) {
  29. return static_cast<Widget*>(insertChild(
  30. atPosition,
  31. std::move(child),
  32. margin));
  33. }
  34. template <
  35. typename Widget,
  36. typename = std::enable_if_t<
  37. std::is_base_of_v<RpWidget, Widget>>>
  38. Widget *add(
  39. object_ptr<Widget> &&child,
  40. const style::margins &margin = style::margins()) {
  41. return insert(count(), std::move(child), margin);
  42. }
  43. QMargins getMargins() const override;
  44. int naturalWidth() const override;
  45. void setVerticalShift(int index, int shift);
  46. void reorderRows(int oldIndex, int newIndex);
  47. void clear();
  48. protected:
  49. int resizeGetHeight(int newWidth) override;
  50. void visibleTopBottomUpdated(
  51. int visibleTop,
  52. int visibleBottom) override;
  53. private:
  54. RpWidget *insertChild(
  55. int addPosition,
  56. object_ptr<RpWidget> child,
  57. const style::margins &margin);
  58. void childHeightUpdated(RpWidget *child);
  59. void removeChild(RpWidget *child);
  60. void updateChildGeometry(
  61. const style::margins &margins,
  62. RpWidget *child,
  63. const style::margins &margin,
  64. int width,
  65. int top) const;
  66. struct Row {
  67. object_ptr<RpWidget> widget;
  68. style::margins margin;
  69. int verticalShift = 0;
  70. };
  71. std::vector<Row> _rows;
  72. bool _inResize = false;
  73. rpl::lifetime _rowsLifetime;
  74. };
  75. } // namespace Ui