| 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 height for all containers to |height|. | |
| 74 void SetMinHeight(int height); | |
| 75 | |
| 76 // Set the minimum size for the given |container|. | |
| 77 void SetMinSize(Container container, const gfx::Size& size); | |
| 78 | |
| 79 // Get the minimum size for the given |container|. | |
| 80 gfx::Size GetMinSize(Container container); | |
| 81 | |
| 82 // Set the maximum size for the given |container|. | |
| 83 void SetMaxSize(Container container, const gfx::Size& size); | |
| 84 | |
| 85 // Adds the child |view| to the specified |container|. | |
| 86 void AddView(Container container, views::View* view); | |
| 87 | |
| 88 // Removes all the children from the specified |container|. If | |
| 89 // |delete_children| is true, the views are deleted, unless marked as not | |
| 90 // parent owned. | |
| 91 void RemoveAllChildren(Container container, bool delete_children); | |
| 92 | |
| 93 // During layout the |insets| are applied to the host views entire space | |
| 94 // before allocating the remaining space to the container views. | |
| 95 void SetInsets(const gfx::Insets& insets); | |
| 96 | |
| 97 // Sets the border for the given |container|. | |
| 98 void SetContainerBorder(Container container, | |
| 99 std::unique_ptr<views::Border> border); | |
| 100 | |
| 101 // Sets whether the |container| is visible. During a layout the space will be | |
| 102 // allocated to the visible containers only. i.e. non-visible containers will | |
| 103 // not be allocated any space. | |
| 104 void SetContainerVisible(Container container, bool visible); | |
| 105 | |
| 106 // Sets the flex weight for the given |container|. Using the preferred size as | |
| 107 // the basis, free space along the main axis is distributed to views in the | |
| 108 // ratio of their flex weights. Similarly, if the views will overflow the | |
| 109 // parent, space is subtracted in these ratios. | |
| 110 // | |
| 111 // A flex of 0 means this view is not resized. Flex values must not be | |
| 112 // negative. | |
| 113 // | |
| 114 // Note that non-zero flex values will take precedence over size constraints. | |
| 115 // i.e. even if |container| has a max size set the space allocated during | |
| 116 // layout may be larger if |flex| > 0 and similar for min size constraints. | |
| 117 void SetFlexForContainer(Container container, int flex); | |
| 118 | |
| 119 // Sets the |layout_manager| used by the given |container|. | |
| 120 void SetContainerLayout(Container container, | |
| 121 std::unique_ptr<views::LayoutManager> layout_manager); | |
| 122 | |
| 123 protected: | |
| 124 // View: | |
| 125 void ViewHierarchyChanged( | |
| 126 const views::View::ViewHierarchyChangedDetails& details) override; | |
| 127 const char* GetClassName() const override; | |
| 128 | |
| 129 private: | |
| 130 friend class TriViewTest; | |
| 131 | |
| 132 // Returns the View for the given |container|. | |
| 133 views::View* GetContainer(Container container); | |
| 134 | |
| 135 // Returns the layout manager for the given |container|. | |
| 136 SizeRangeLayout* GetLayoutManager(Container container); | |
| 137 | |
| 138 // Type spcific layout manager installed on |this|. Responsible for laying out | |
| 139 // the container Views. | |
| 140 views::BoxLayout* box_layout_; | |
| 141 | |
| 142 SizeRangeLayout* start_container_layout_manager_; | |
| 143 SizeRangeLayout* center_container_layout_manager_; | |
| 144 SizeRangeLayout* end_container_layout_manager_; | |
| 145 | |
| 146 // In order to detect direct manipulation of child views the | |
| 147 // ViewHierarchyChanged() event override fails on a DCHECK. However, we need | |
| 148 // to manipulate the child views during construction/destruction so this flag | |
| 149 // is used to disable the DCHECK during construction/destruction. | |
| 150 bool enable_hierarchy_changed_dcheck_ = false; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(TriView); | |
| 153 }; | |
| 154 | |
| 155 } // namespace ash | |
| 156 | |
| 157 #endif // ASH_COMMON_SYSTEM_TRAY_TRI_VIEW_H_ | |
| OLD | NEW |