Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: ui/views/layout/box_layout.h

Issue 2836313002: BoxLayout now suports per-view margins. (Closed)
Patch Set: Added unit tests. Fixed GetPreferredSize() calculations. Refactored some code. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef UI_VIEWS_LAYOUT_BOX_LAYOUT_H_ 5 #ifndef UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
6 #define UI_VIEWS_LAYOUT_BOX_LAYOUT_H_ 6 #define UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
7 7
8 #include <map> 8 #include <map>
9 9
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 // This causes the child view to stretch to fit the host in the cross axis. 51 // This causes the child view to stretch to fit the host in the cross axis.
52 CROSS_AXIS_ALIGNMENT_STRETCH, 52 CROSS_AXIS_ALIGNMENT_STRETCH,
53 CROSS_AXIS_ALIGNMENT_START, 53 CROSS_AXIS_ALIGNMENT_START,
54 CROSS_AXIS_ALIGNMENT_CENTER, 54 CROSS_AXIS_ALIGNMENT_CENTER,
55 CROSS_AXIS_ALIGNMENT_END, 55 CROSS_AXIS_ALIGNMENT_END,
56 }; 56 };
57 57
58 // Use |inside_border_horizontal_spacing| and 58 // Use |inside_border_horizontal_spacing| and
59 // |inside_border_vertical_spacing| to add additional space between the child 59 // |inside_border_vertical_spacing| to add additional space between the child
60 // view area and the host view border. |between_child_spacing| controls the 60 // view area and the host view border. |between_child_spacing| controls the
61 // space in between child views. 61 // space in between child views. |collapse_margins_spacing| paramter controls
sky 2017/05/15 20:29:09 parameter
62 // whether or not spacing adjacent to view margins are collapsed base on the
sky 2017/05/15 20:29:09 How about: 'spacing between views along the main a
kylix_rd 2017/05/16 18:45:47 It's more than merely "adjacent views". The collap
63 // max of the two values
62 BoxLayout(Orientation orientation, 64 BoxLayout(Orientation orientation,
63 int inside_border_horizontal_spacing, 65 int inside_border_horizontal_spacing,
64 int inside_border_vertical_spacing, 66 int inside_border_vertical_spacing,
65 int between_child_spacing); 67 int between_child_spacing,
68 bool collapse_margins_spacing = false);
66 ~BoxLayout() override; 69 ~BoxLayout() override;
67 70
68 void set_main_axis_alignment(MainAxisAlignment main_axis_alignment) { 71 void set_main_axis_alignment(MainAxisAlignment main_axis_alignment) {
69 main_axis_alignment_ = main_axis_alignment; 72 main_axis_alignment_ = main_axis_alignment;
70 } 73 }
71 74
72 void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment) { 75 void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment) {
73 cross_axis_alignment_ = cross_axis_alignment; 76 cross_axis_alignment_ = cross_axis_alignment;
74 } 77 }
75 78
(...skipping 22 matching lines...) Expand all
98 void SetDefaultFlex(int default_flex); 101 void SetDefaultFlex(int default_flex);
99 102
100 // Overridden from views::LayoutManager: 103 // Overridden from views::LayoutManager:
101 void Installed(View* host) override; 104 void Installed(View* host) override;
102 void ViewRemoved(View* host, View* view) override; 105 void ViewRemoved(View* host, View* view) override;
103 void Layout(View* host) override; 106 void Layout(View* host) override;
104 gfx::Size GetPreferredSize(const View* host) const override; 107 gfx::Size GetPreferredSize(const View* host) const override;
105 int GetPreferredHeightForWidth(const View* host, int width) const override; 108 int GetPreferredHeightForWidth(const View* host, int width) const override;
106 109
107 private: 110 private:
111 // This struct is used internally to "wrap" a child view in order to obviate
112 // the need for the main layout logic to be fully aware of the per-view
113 // margins when |collapse_margin_spacing_| is false. Since each view is a
114 // rectangle of a certain size, this wrapper, coupled with any margins set
115 // will increase the apparent size of the view. All necessary view size/
116 // position methods required for the layout logic add or subtract the margins
117 // where appropriate to ensure the actual visible size of the view doesn't
118 // include the margins.
119 // When |collapse_margin_spacing_| is true, this wrapper provides quick access
120 // to the view's margins for use by the layout to collapse adjacent spacing
121 // to the largest of the several values.
122 class ViewWrapper {
123 public:
124 ViewWrapper();
125 ViewWrapper(const BoxLayout* layout, View* view);
126 ViewWrapper(const ViewWrapper& wrapper) = delete;
sky 2017/05/15 20:29:09 Is there a reason to delete this constructor and n
kylix_rd 2017/05/16 18:45:47 No, not really. Done.
127 ~ViewWrapper();
128 int GetHeightForWidth(int width) const;
129 const gfx::Insets& margins() const { return margins_; }
130 gfx::Size GetPreferredSize() const;
131 void SetBoundsRect(const gfx::Rect& bounds);
132 View* view() const { return view_; }
133 bool visible() const;
134
135 private:
136 View* view_;
137 const BoxLayout* layout_;
138 gfx::Insets margins_;
139 };
140
141 using FlexMap = std::map<const View*, int>;
142
108 // Returns the flex for the specified |view|. 143 // Returns the flex for the specified |view|.
109 int GetFlexForView(const View* view) const; 144 int GetFlexForView(const View* view) const;
110 145
111 // Returns the size and position along the main axis of |rect|. 146 // Returns the size and position along the main axis of |rect|.
112 int MainAxisSize(const gfx::Rect& rect) const; 147 int MainAxisSize(const gfx::Rect& rect) const;
113 int MainAxisPosition(const gfx::Rect& rect) const; 148 int MainAxisPosition(const gfx::Rect& rect) const;
114 149
115 // Sets the size and position along the main axis of |rect|. 150 // Sets the size and position along the main axis of |rect|.
116 void SetMainAxisSize(int size, gfx::Rect* rect) const; 151 void SetMainAxisSize(int size, gfx::Rect* rect) const;
117 void SetMainAxisPosition(int position, gfx::Rect* rect) const; 152 void SetMainAxisPosition(int position, gfx::Rect* rect) const;
118 153
119 // Returns the size and position along the cross axis of |rect|. 154 // Returns the size and position along the cross axis of |rect|.
120 int CrossAxisSize(const gfx::Rect& rect) const; 155 int CrossAxisSize(const gfx::Rect& rect) const;
121 int CrossAxisPosition(const gfx::Rect& rect) const; 156 int CrossAxisPosition(const gfx::Rect& rect) const;
122 157
123 // Sets the size and position along the cross axis of |rect|. 158 // Sets the size and position along the cross axis of |rect|.
124 void SetCrossAxisSize(int size, gfx::Rect* rect) const; 159 void SetCrossAxisSize(int size, gfx::Rect* rect) const;
125 void SetCrossAxisPosition(int size, gfx::Rect* rect) const; 160 void SetCrossAxisPosition(int size, gfx::Rect* rect) const;
126 161
127 // Returns the main axis size for the given view. |child_area_width| is needed 162 // Returns the main axis size for the given view. |child_area_width| is needed
128 // to calculate the height of the view when the orientation is vertical. 163 // to calculate the height of the view when the orientation is vertical.
129 int MainAxisSizeForView(const View* view, int child_area_width) const; 164 int MainAxisSizeForView(const ViewWrapper& view, int child_area_width) const;
165
166 // Returns the main axis margin spacing between the two views which is the max
167 // of the right margin from the |left| view, the left margin of the |right|
168 // view and |between_child_spacing_|.
169 int MainAxisMarginBetweenViews(const ViewWrapper& left,
170 const ViewWrapper& right) const;
171
172 // Return the outer margin along the main axis as insets.
173 gfx::Insets MainAxisOuterMargin() const;
174
175 // Return the outer margin along the cross axis as insets.
176 gfx::Insets CrossAxisOuterMargin() const;
177
178 // Adjusts the main axis for |rect| by collapsing the left or top margin of
179 // the first view with corresponding side of |inside_border_insets_| and the
180 // right or bottom margin of the last view with the corresponding side of
181 // |inside_border_insets_|.
182 void AdjustMainAxisForMargin(gfx::Rect* rect) const;
183
184 // Adjust the cross axis of |rect| by collapsing the max of the cross axis
185 // margins of all children and the corresponding side of
186 // |inside_border_insets_|
187 void AdjustCrossAxisForMargin(gfx::Rect* rect) const;
130 188
131 // Returns the cross axis size for the given view. 189 // Returns the cross axis size for the given view.
132 int CrossAxisSizeForView(const View* view) const; 190 int CrossAxisSizeForView(const ViewWrapper& view) const;
133 191
134 // The preferred size for the dialog given the width of the child area. 192 // The preferred size for the dialog given the width of the child area.
135 gfx::Size GetPreferredSizeForChildWidth(const View* host, 193 gfx::Size GetPreferredSizeForChildWidth(const View* host,
136 int child_area_width) const; 194 int child_area_width) const;
137 195
138 // The amount of space the layout requires in addition to any space for the 196 // The amount of space the layout requires in addition to any space for the
139 // child views. 197 // child views.
140 gfx::Size NonChildSize(const View* host) const; 198 gfx::Size NonChildSize(const View* host) const;
141 199
200 // The next visible view at > index. If no other views are visible, return
201 // nullptr.
202 View* NextVisibleView(int index) const;
203
204 // Return the first visible view in the host or nullptr if none are visible.
205 View* FirstVisibleView() const;
206
207 // Return the last visible view in the host or nullptr if none are visible.
208 View* LastVisibleView() const;
209
142 const Orientation orientation_; 210 const Orientation orientation_;
143 211
144 // Spacing between child views and host view border. 212 // Spacing between child views and host view border.
145 gfx::Insets inside_border_insets_; 213 gfx::Insets inside_border_insets_;
146 214
147 // Spacing to put in between child views. 215 // Spacing to put in between child views.
148 const int between_child_spacing_; 216 const int between_child_spacing_;
149 217
150 // The alignment of children in the main axis. This is 218 // The alignment of children in the main axis. This is
151 // MAIN_AXIS_ALIGNMENT_START by default. 219 // MAIN_AXIS_ALIGNMENT_START by default.
152 MainAxisAlignment main_axis_alignment_; 220 MainAxisAlignment main_axis_alignment_;
153 221
154 // The alignment of children in the cross axis. This is 222 // The alignment of children in the cross axis. This is
155 // CROSS_AXIS_ALIGNMENT_STRETCH by default. 223 // CROSS_AXIS_ALIGNMENT_STRETCH by default.
156 CrossAxisAlignment cross_axis_alignment_; 224 CrossAxisAlignment cross_axis_alignment_;
157 225
158 // A map of views to their flex weights. 226 // A map of views to their flex weights.
159 std::map<const View*, int> flex_map_; 227 FlexMap flex_map_;
160 228
161 // The flex weight for views if none is set. Defaults to 0. 229 // The flex weight for views if none is set. Defaults to 0.
162 int default_flex_; 230 int default_flex_;
163 231
164 // The minimum cross axis size for the layout. 232 // The minimum cross axis size for the layout.
165 int minimum_cross_axis_size_; 233 int minimum_cross_axis_size_;
166 234
235 // Adjacent view margins and spacing should be collapsed.
236 const bool collapse_margins_spacing_;
237
167 // The view that this BoxLayout is managing the layout for. 238 // The view that this BoxLayout is managing the layout for.
168 views::View* host_; 239 views::View* host_;
169 240
170 DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout); 241 DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout);
171 }; 242 };
172 243
173 } // namespace views 244 } // namespace views
174 245
175 #endif // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_ 246 #endif // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698