tri_view.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_SYSTEM_TRAY_TRI_VIEW_H_
  5. #define ASH_SYSTEM_TRAY_TRI_VIEW_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ui/gfx/geometry/insets.h"
  9. #include "ui/gfx/geometry/size.h"
  10. #include "ui/views/view.h"
  11. namespace views {
  12. class Border;
  13. class BoxLayout;
  14. class LayoutManager;
  15. } // namespace views
  16. namespace ash {
  17. class SizeRangeLayout;
  18. // A View which has 3 child containers (START, CENTER, END) which can be
  19. // arranged vertically or horizontally. The child containers can have minimum
  20. // and/or maximum preferred size defined as well as a flex weight that is used
  21. // to distribute excess space across the main axis, i.e. flexible width for the
  22. // horizontal orientation. By default all the containers have a flex weight of
  23. // 0, meaning no flexibility, and no minimum or maximum size.
  24. //
  25. // Child views should not be added to |this| directly via View::AddChildView()
  26. // or View::AddChildViewAt() and will fail a DCHECK() if attempted.
  27. //
  28. // Views added to the containers are laid out as per the LayoutManager that has
  29. // been installed on that container. By default a BoxLayout manager is installed
  30. // on each container with the same orientation as |this| has been created with.
  31. // The default BoxLayout will use a center alignment for both the main axis and
  32. // cross axis alignment.
  33. class ASH_EXPORT TriView : public views::View {
  34. public:
  35. enum class Orientation {
  36. HORIZONTAL,
  37. VERTICAL,
  38. };
  39. // The different containers that child Views can be added to.
  40. enum class Container { START = 0, CENTER = 1, END = 2 };
  41. // Constructs a layout with horizontal orientation and 0 padding between
  42. // containers.
  43. TriView();
  44. // Creates |this| with a Horizontal orientation and the specified padding
  45. // between containers.
  46. //
  47. // TODO(bruthig): The |padding_between_containers| can only be set on
  48. // BoxLayouts during construction. Investigate whether this can be a mutable
  49. // property of BoxLayouts and if so consider dropping it as a constructor
  50. // parameter here.
  51. explicit TriView(int padding_between_containers);
  52. // Creates |this| with the specified orientation and 0 padding between
  53. // containers.
  54. explicit TriView(Orientation orientation);
  55. // Creates this with the specified |orientation| and
  56. // |padding_between_containers|.
  57. TriView(Orientation orientation, int padding_between_containers);
  58. TriView(const TriView&) = delete;
  59. TriView& operator=(const TriView&) = delete;
  60. ~TriView() override;
  61. // Set the minimum height for all containers to |height|.
  62. void SetMinHeight(int height);
  63. // Set the minimum size for the given |container|.
  64. void SetMinSize(Container container, const gfx::Size& size);
  65. // Get the minimum size for the given |container|.
  66. gfx::Size GetMinSize(Container container);
  67. // Set the maximum size for the given |container|.
  68. void SetMaxSize(Container container, const gfx::Size& size);
  69. // Adds the child |view| to the specified |container|.
  70. void AddView(Container container, views::View* view);
  71. // Adds the child |view| to the specified |container| at the child index.
  72. void AddViewAt(Container container, views::View* view, int index);
  73. // During layout the |insets| are applied to the host views entire space
  74. // before allocating the remaining space to the container views.
  75. void SetInsets(const gfx::Insets& insets);
  76. // Sets the border for the given |container|.
  77. void SetContainerBorder(Container container,
  78. std::unique_ptr<views::Border> border);
  79. // Sets whether the |container| is visible. During a layout the space will be
  80. // allocated to the visible containers only. i.e. non-visible containers will
  81. // not be allocated any space.
  82. // Note: This will cause a relayout.
  83. void SetContainerVisible(Container container, bool visible);
  84. // Sets the flex weight for the given |container|. Using the preferred size as
  85. // the basis, free space along the main axis is distributed to views in the
  86. // ratio of their flex weights. Similarly, if the views will overflow the
  87. // parent, space is subtracted in these ratios.
  88. //
  89. // A flex of 0 means this view is not resized. Flex values must not be
  90. // negative.
  91. //
  92. // Note that non-zero flex values will take precedence over size constraints.
  93. // i.e. even if |container| has a max size set the space allocated during
  94. // layout may be larger if |flex| > 0 and similar for min size constraints.
  95. void SetFlexForContainer(Container container, int flex);
  96. // Sets the |layout_manager| used by the given |container|.
  97. void SetContainerLayout(Container container,
  98. std::unique_ptr<views::LayoutManager> layout_manager);
  99. protected:
  100. // View:
  101. void ViewHierarchyChanged(
  102. const views::ViewHierarchyChangedDetails& details) override;
  103. const char* GetClassName() const override;
  104. gfx::Rect GetAnchorBoundsInScreen() const override;
  105. private:
  106. friend class TriViewTest;
  107. // Returns the View for the given |container|.
  108. views::View* GetContainer(Container container);
  109. // Returns the layout manager for the given |container|.
  110. SizeRangeLayout* GetLayoutManager(Container container);
  111. // Type spcific layout manager installed on |this|. Responsible for laying out
  112. // the container Views.
  113. views::BoxLayout* box_layout_ = nullptr;
  114. SizeRangeLayout* start_container_layout_manager_ = nullptr;
  115. SizeRangeLayout* center_container_layout_manager_ = nullptr;
  116. SizeRangeLayout* end_container_layout_manager_ = nullptr;
  117. // In order to detect direct manipulation of child views the
  118. // ViewHierarchyChanged() event override fails on a DCHECK. However, we need
  119. // to manipulate the child views during construction/destruction so this flag
  120. // is used to disable the DCHECK during construction/destruction.
  121. bool enable_hierarchy_changed_dcheck_ = false;
  122. };
  123. } // namespace ash
  124. #endif // ASH_SYSTEM_TRAY_TRI_VIEW_H_