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