Chromium Code Reviews| 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_THREE_VIEW_LAYOUT_H_ | |
| 6 #define ASH_COMMON_SYSTEM_TRAY_THREE_VIEW_LAYOUT_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/layout/layout_manager.h" | |
| 15 | |
| 16 namespace views { | |
| 17 class Border; | |
| 18 class BoxLayout; | |
| 19 class View; | |
| 20 } // namespace views | |
| 21 | |
| 22 namespace ash { | |
| 23 class SizeRangeLayout; | |
| 24 | |
| 25 // A layout manager that has 3 child containers (START, CENTER, END) which can | |
| 26 // be arranged vertically or horizontally. The child containers can have | |
| 27 // minimum and/or maximum preferred size defined as well as a flex weight that | |
| 28 // is used to distribute excess space across the main axis, i.e. flexible width | |
| 29 // for the horizontal orientation. By default all the containers have a flex | |
| 30 // weight of 0, meaning no flexibility, and no minimum or maximum size. | |
| 31 // | |
| 32 // Views are laid out within each container as per the LayoutManager that has | |
| 33 // been installed on that container. By default a BoxLayout manager is installed | |
| 34 // on each container with the same orientation as |this| has been created with. | |
| 35 // The default BoxLayout will use a center alignment for both the main axis and | |
| 36 // cross axis alignment. | |
| 37 class ASH_EXPORT ThreeViewLayout : public views::LayoutManager { | |
| 38 public: | |
| 39 enum class Orientation { | |
| 40 HORIZONTAL, | |
| 41 VERTICAL, | |
| 42 }; | |
| 43 | |
| 44 // The different containers that child Views can be added to. | |
| 45 enum class Container { START, CENTER, END }; | |
| 46 | |
| 47 // Constructs a layout with horizontal orientation and 0 padding between | |
| 48 // containers. | |
| 49 ThreeViewLayout(); | |
| 50 | |
| 51 // Constructs a layout with Horizontal orientation and the specified padding | |
| 52 // between containers. | |
| 53 // | |
| 54 // TODO(bruthig): The |padding_between_containers| can only be set on | |
| 55 // BoxLayouts during construction. Investigate whether this can be a mutable | |
| 56 // property of BoxLayouts and if so consider dropping it as a constructor | |
| 57 // parameter here. | |
| 58 explicit ThreeViewLayout(int padding_between_containers); | |
| 59 | |
| 60 // Constructs a layout with the specified orientation and 0 padding between | |
| 61 // containers. | |
| 62 explicit ThreeViewLayout(Orientation orientation); | |
| 63 | |
| 64 // Constructs a layout with the specified orientation and padding between | |
| 65 // containers. | |
| 66 ThreeViewLayout(Orientation orientation, int padding_between_containers); | |
| 67 | |
| 68 ~ThreeViewLayout() override; | |
| 69 | |
| 70 // Set the minimum cross axis size for the layout, i.e. the minimum height for | |
| 71 // a horizontal orientation. | |
| 72 void SetMinCrossAxisSize(int min_size); | |
| 73 | |
| 74 // Set the minimum size for the given |container|. | |
| 75 void SetMinSize(Container container, const gfx::Size& size); | |
| 76 | |
| 77 // Set the maximum size for the given |container|. | |
| 78 void SetMaxSize(Container container, const gfx::Size& size); | |
| 79 | |
| 80 // Adds the child |view| to the specified |container|. | |
| 81 void AddView(Container container, views::View* view); | |
| 82 | |
| 83 // Removes all current children from the specified |container| and adds the | |
| 84 // child |view| to it. | |
| 85 void SetView(Container container, views::View* view); | |
| 86 | |
| 87 // During layout the |insets| are applied to the host views entire space | |
| 88 // before allocating the remaining space to the container views. | |
| 89 void SetInsets(const gfx::Insets& insets); | |
| 90 | |
| 91 // Sets the border for the given |container|. | |
| 92 void SetBorder(Container container, std::unique_ptr<views::Border> border); | |
| 93 | |
| 94 // Sets wither the |container| is visible. During a layout the space will be | |
| 95 // allocated to the visible containers only. i.e. non-visible containers will | |
| 96 // not be allocated any space. | |
| 97 void SetContainerVisible(Container container, bool visible); | |
| 98 | |
| 99 // Sets the flex weight for the given |container|. Using the preferred size as | |
| 100 // the basis, free space along the main axis is distributed to views in the | |
| 101 // ratio of their flex weights. Similarly, if the views will overflow the | |
| 102 // parent, space is subtracted in these ratios. | |
| 103 // | |
| 104 // A flex of 0 means this view is not resized. Flex values must not be | |
| 105 // negative. | |
| 106 // | |
| 107 // Note that positive flex values will take precedence over size constraints. | |
|
tdanderson
2016/10/20 18:16:49
micro nit: 'non-zero' instead of 'positive' since
bruthig
2016/10/20 19:01:34
Done.
| |
| 108 // i.e. even if |container| has a max size set the space allocated during | |
| 109 // layout may be larger if |flex| > 0 and similar for min size constraints. | |
| 110 void SetFlexForContainer(Container container, int flex); | |
| 111 | |
| 112 // Sets the |layout_manager| used by the given |container|. | |
| 113 void SetLayoutManager(Container container, | |
| 114 std::unique_ptr<LayoutManager> layout_manager); | |
| 115 | |
| 116 // LayoutManager: | |
| 117 void Installed(views::View* host) override; | |
| 118 void Layout(views::View* host) override; | |
| 119 gfx::Size GetPreferredSize(const views::View* host) const override; | |
| 120 int GetPreferredHeightForWidth(const views::View* host, | |
| 121 int width) const override; | |
| 122 void ViewAdded(views::View* host, views::View* view) override; | |
| 123 void ViewRemoved(views::View* host, views::View* view) override; | |
| 124 | |
| 125 private: | |
| 126 friend class ThreeViewLayoutTest; | |
| 127 | |
| 128 // Creates a default LayoutManager for the given |orientation|. | |
| 129 std::unique_ptr<views::LayoutManager> CreateDefaultLayoutManager( | |
| 130 Orientation orientation) const; | |
| 131 | |
| 132 // Returns the View for the given |container|. | |
| 133 views::View* GetContainer(Container container) const; | |
| 134 | |
| 135 // Returns the layout manager for the given |container|. | |
| 136 SizeRangeLayout* GetLayoutManager(Container container) const; | |
| 137 | |
| 138 // The layout manager that lays out the different Container Views. | |
| 139 std::unique_ptr<views::BoxLayout> box_layout_; | |
| 140 | |
| 141 // The host View that this layout manager has been installed on. | |
| 142 views::View* host_ = nullptr; | |
| 143 | |
| 144 views::View* start_container_ = nullptr; | |
| 145 SizeRangeLayout* start_container_layout_manager_ = nullptr; | |
| 146 | |
| 147 views::View* center_container_ = nullptr; | |
| 148 SizeRangeLayout* center_container_layout_manager_ = nullptr; | |
| 149 | |
| 150 views::View* end_container_ = nullptr; | |
| 151 SizeRangeLayout* end_container_layout_manager_ = nullptr; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(ThreeViewLayout); | |
| 154 }; | |
| 155 | |
| 156 } // namespace ash | |
| 157 | |
| 158 #endif // ASH_COMMON_SYSTEM_TRAY_THREE_VIEW_LAYOUT_H_ | |
| OLD | NEW |