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

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

Issue 2836313002: BoxLayout now suports per-view margins. (Closed)
Patch Set: Fixed comment describing the BoxLayout behavior. Created 3 years, 6 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. Use view->SetProperty(kMarginsKey,
62 // new gfx::Insets(xxx)) to add additional margins on a per-view basis. The
63 // |collapse_margins_spacing| parameter controls whether or not adjacent
64 // spacing/margins are collapsed based on the max of the two values. For the
65 // cross axis, |collapse_margins_spacing| will collapse to the max of
66 // inside_border_xxxxx_spacing and the corresponding margin edge from each
67 // view.
68 //
69 // Given the following views where V = view bounds, M = Margins property,
70 // B = between child spacing, S = inside border spacing and
71 // <space> = added margins for alignment
72 //
73 // MMMMM MMVVVVMM MMMM
74 // VVVVM MMMM
75 // VVVVM MMMM
76 // VVVVM VVVV
77 // MMMMM
78 //
79 // With collapse_margins_spacing = false, orientation = kHorizontal,
80 // inside_border_spacing_horizontal = 2, inside_border_spacing_vertical = 2
81 // and between_child_spacing = 1:
82 //
83 // -----------------------
84 // SSSSSSSSSSSSSSSSSSSSSSS
85 // SSSSSSSSSSSSSSSSSSSSSSS
86 // SS MBMM MMBMMMMSS
87 // SS MBMM MMBMMMMSS
88 // SSMMMMMBMM MMBMMMMSS
89 // SSVVVVMBMMVVVVMMBVVVVSS
90 // SSVVVVMBMMVVVVMMBVVVVSS
91 // SSVVVVMBMMVVVVMMBVVVVSS
92 // SSMMMMMBMMVVVVMMBVVVVSS
93 // SSSSSSSSSSSSSSSSSSSSSSS
94 // SSSSSSSSSSSSSSSSSSSSSSS
95 // -----------------------
96 //
97 // Same as above except, collapse_margins_spacing = true.
98 //
99 // --------------------
100 // SS MMMMMMSS
101 // SS MMMMMMSS
102 // SSMMMMMM MMMMMMSS
103 // SSVVVVMMVVVVMMVVVVSS
104 // SSVVVVMMVVVVMMVVVVSS
105 // SSVVVVMMVVVVMMVVVVSS
106 // SSSSSSSSSSSSSSSSSSSS
107 // SSSSSSSSSSSSSSSSSSSS
108 // --------------------
109 //
62 BoxLayout(Orientation orientation, 110 BoxLayout(Orientation orientation,
63 int inside_border_horizontal_spacing, 111 int inside_border_horizontal_spacing,
64 int inside_border_vertical_spacing, 112 int inside_border_vertical_spacing,
65 int between_child_spacing); 113 int between_child_spacing,
114 bool collapse_margins_spacing = false);
66 ~BoxLayout() override; 115 ~BoxLayout() override;
67 116
68 void set_main_axis_alignment(MainAxisAlignment main_axis_alignment) { 117 void set_main_axis_alignment(MainAxisAlignment main_axis_alignment) {
69 main_axis_alignment_ = main_axis_alignment; 118 main_axis_alignment_ = main_axis_alignment;
70 } 119 }
71 120
72 void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment) { 121 void set_cross_axis_alignment(CrossAxisAlignment cross_axis_alignment) {
73 cross_axis_alignment_ = cross_axis_alignment; 122 cross_axis_alignment_ = cross_axis_alignment;
74 } 123 }
75 124
(...skipping 22 matching lines...) Expand all
98 void SetDefaultFlex(int default_flex); 147 void SetDefaultFlex(int default_flex);
99 148
100 // Overridden from views::LayoutManager: 149 // Overridden from views::LayoutManager:
101 void Installed(View* host) override; 150 void Installed(View* host) override;
102 void ViewRemoved(View* host, View* view) override; 151 void ViewRemoved(View* host, View* view) override;
103 void Layout(View* host) override; 152 void Layout(View* host) override;
104 gfx::Size GetPreferredSize(const View* host) const override; 153 gfx::Size GetPreferredSize(const View* host) const override;
105 int GetPreferredHeightForWidth(const View* host, int width) const override; 154 int GetPreferredHeightForWidth(const View* host, int width) const override;
106 155
107 private: 156 private:
157 // This struct is used internally to "wrap" a child view in order to obviate
158 // the need for the main layout logic to be fully aware of the per-view
159 // margins when |collapse_margin_spacing_| is false. Since each view is a
160 // rectangle of a certain size, this wrapper, coupled with any margins set
161 // will increase the apparent size of the view along the main axis. All
162 // necessary view size/position methods required for the layout logic add or
163 // subtract the margins where appropriate to ensure the actual visible size of
164 // the view doesn't include the margins. For the cross axis, the margins are
165 // NOT included in the size/position calculations. BoxLayout will adjust the
166 // bounding rectangle of the space used for layout using the maximum margin
167 // for all views along the appropriate edge.
168 // When |collapse_margin_spacing_| is true, this wrapper provides quick access
169 // to the view's margins for use by the layout to collapse adjacent spacing
170 // to the largest of the several values.
171 class ViewWrapper {
172 public:
173 ViewWrapper();
174 ViewWrapper(const BoxLayout* layout, View* view);
175 ~ViewWrapper();
176 int GetHeightForWidth(int width) const;
sky 2017/06/05 21:04:10 optional: Generally we have newline after destruct
kylix_rd 2017/06/06 18:22:35 Done.
177 const gfx::Insets& margins() const { return margins_; }
178 gfx::Size GetPreferredSize() const;
179 void SetBoundsRect(const gfx::Rect& bounds);
180 View* view() const { return view_; }
181 bool visible() const;
182
183 private:
184 View* view_;
185 const BoxLayout* layout_;
186 const Orientation orientation_;
sky 2017/06/05 21:04:11 Why do you need to cache both the layout and orien
kylix_rd 2017/06/06 18:22:35 In previous iterations, it was accessed more frequ
187 gfx::Insets margins_;
188
189 DISALLOW_COPY_AND_ASSIGN(ViewWrapper);
190 };
191
192 using FlexMap = std::map<const View*, int>;
193
108 // Returns the flex for the specified |view|. 194 // Returns the flex for the specified |view|.
109 int GetFlexForView(const View* view) const; 195 int GetFlexForView(const View* view) const;
110 196
111 // Returns the size and position along the main axis of |rect|. 197 // Returns the size and position along the main axis of |rect|.
112 int MainAxisSize(const gfx::Rect& rect) const; 198 int MainAxisSize(const gfx::Rect& rect) const;
113 int MainAxisPosition(const gfx::Rect& rect) const; 199 int MainAxisPosition(const gfx::Rect& rect) const;
114 200
115 // Sets the size and position along the main axis of |rect|. 201 // Sets the size and position along the main axis of |rect|.
116 void SetMainAxisSize(int size, gfx::Rect* rect) const; 202 void SetMainAxisSize(int size, gfx::Rect* rect) const;
117 void SetMainAxisPosition(int position, gfx::Rect* rect) const; 203 void SetMainAxisPosition(int position, gfx::Rect* rect) const;
118 204
119 // Returns the size and position along the cross axis of |rect|. 205 // Returns the size and position along the cross axis of |rect|.
120 int CrossAxisSize(const gfx::Rect& rect) const; 206 int CrossAxisSize(const gfx::Rect& rect) const;
121 int CrossAxisPosition(const gfx::Rect& rect) const; 207 int CrossAxisPosition(const gfx::Rect& rect) const;
122 208
123 // Sets the size and position along the cross axis of |rect|. 209 // Sets the size and position along the cross axis of |rect|.
124 void SetCrossAxisSize(int size, gfx::Rect* rect) const; 210 void SetCrossAxisSize(int size, gfx::Rect* rect) const;
125 void SetCrossAxisPosition(int size, gfx::Rect* rect) const; 211 void SetCrossAxisPosition(int size, gfx::Rect* rect) const;
126 212
127 // Returns the main axis size for the given view. |child_area_width| is needed 213 // 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. 214 // to calculate the height of the view when the orientation is vertical.
129 int MainAxisSizeForView(const View* view, int child_area_width) const; 215 int MainAxisSizeForView(const ViewWrapper& view, int child_area_width) const;
216
217 // Returns the |left| or |top| edge of the given inset based on the value of
218 // |orientation_|.
219 int MainAxisLeadingInset(const gfx::Insets& insets) const;
220
221 // Returns the |right| or |bottom| edge of the given inset based on the value
222 // of |orientation_|.
223 int MainAxisTrailingInset(const gfx::Insets& insets) const;
224
225 // Returns the left (|x|) or top (|y|) edge of the given rect based on the
226 // value of |orientation_|.
227 int CrossAxisLeadingEdge(const gfx::Rect& rect) const;
228
229 // Returns the |left| or |top| edge of the given inset based on the value of
230 // |orientation_|.
231 int CrossAxisLeadingInset(const gfx::Insets& insets) const;
232
233 // Returns the |right| or |bottom| edge of the given inset based on the value
234 // of |orientation_|.
235 int CrossAxisTrailingInset(const gfx::Insets& insets) const;
236
237 // Returns the main axis margin spacing between the two views which is the max
238 // of the right margin from the |left| view, the left margin of the |right|
239 // view and |between_child_spacing_|.
240 int MainAxisMarginBetweenViews(const ViewWrapper& left,
241 const ViewWrapper& right) const;
242
243 // Returns the outer margin along the main axis as insets.
244 gfx::Insets MainAxisOuterMargin() const;
245
246 // Returns the maximum margin along the cross axis from all views as insets.
247 gfx::Insets CrossAxisMaxViewMargin() const;
248
249 // Adjusts the main axis for |rect| by collapsing the left or top margin of
250 // the first view with corresponding side of |inside_border_insets_| and the
251 // right or bottom margin of the last view with the corresponding side of
252 // |inside_border_insets_|.
253 void AdjustMainAxisForMargin(gfx::Rect* rect) const;
254
255 // Adjust the cross axis for |rect| using the appropriate sides of
256 // |inside_border_insets_|.
257 void AdjustCrossAxisForInsets(gfx::Rect* rect) const;
130 258
131 // Returns the cross axis size for the given view. 259 // Returns the cross axis size for the given view.
132 int CrossAxisSizeForView(const View* view) const; 260 int CrossAxisSizeForView(const ViewWrapper& view) const;
261
262 // Returns the total margin width for the given view or 0 when
263 // collapse_margins_spacing_ is true.
264 int CrossAxisMarginSizeForView(const ViewWrapper& view) const;
265
266 // Returns the Top or Left size of the margin for the given view or 0 when
267 // collapse_margins_spacing_ is true.
268 int CrossAxisLeadingMarginForView(const ViewWrapper& view) const;
269
270 // Adjust the cross axis for |rect| using the given leading and trailing
271 // values.
272 void InsetCrossAxis(gfx::Rect* rect, int leading, int trailing) const;
133 273
134 // The preferred size for the dialog given the width of the child area. 274 // The preferred size for the dialog given the width of the child area.
135 gfx::Size GetPreferredSizeForChildWidth(const View* host, 275 gfx::Size GetPreferredSizeForChildWidth(const View* host,
136 int child_area_width) const; 276 int child_area_width) const;
137 277
138 // The amount of space the layout requires in addition to any space for the 278 // The amount of space the layout requires in addition to any space for the
139 // child views. 279 // child views.
140 gfx::Size NonChildSize(const View* host) const; 280 gfx::Size NonChildSize(const View* host) const;
141 281
282 // The next visible view at > index. If no other views are visible, return
283 // nullptr.
284 View* NextVisibleView(int index) const;
285
286 // Return the first visible view in the host or nullptr if none are visible.
287 View* FirstVisibleView() const;
288
289 // Return the last visible view in the host or nullptr if none are visible.
290 View* LastVisibleView() const;
291
142 const Orientation orientation_; 292 const Orientation orientation_;
143 293
144 // Spacing between child views and host view border. 294 // Spacing between child views and host view border.
145 gfx::Insets inside_border_insets_; 295 gfx::Insets inside_border_insets_;
146 296
147 // Spacing to put in between child views. 297 // Spacing to put in between child views.
148 const int between_child_spacing_; 298 const int between_child_spacing_;
149 299
150 // The alignment of children in the main axis. This is 300 // The alignment of children in the main axis. This is
151 // MAIN_AXIS_ALIGNMENT_START by default. 301 // MAIN_AXIS_ALIGNMENT_START by default.
152 MainAxisAlignment main_axis_alignment_; 302 MainAxisAlignment main_axis_alignment_;
153 303
154 // The alignment of children in the cross axis. This is 304 // The alignment of children in the cross axis. This is
155 // CROSS_AXIS_ALIGNMENT_STRETCH by default. 305 // CROSS_AXIS_ALIGNMENT_STRETCH by default.
156 CrossAxisAlignment cross_axis_alignment_; 306 CrossAxisAlignment cross_axis_alignment_;
157 307
158 // A map of views to their flex weights. 308 // A map of views to their flex weights.
159 std::map<const View*, int> flex_map_; 309 FlexMap flex_map_;
160 310
161 // The flex weight for views if none is set. Defaults to 0. 311 // The flex weight for views if none is set. Defaults to 0.
162 int default_flex_; 312 int default_flex_;
163 313
164 // The minimum cross axis size for the layout. 314 // The minimum cross axis size for the layout.
165 int minimum_cross_axis_size_; 315 int minimum_cross_axis_size_;
166 316
317 // Adjacent view margins and spacing should be collapsed.
318 const bool collapse_margins_spacing_;
319
167 // The view that this BoxLayout is managing the layout for. 320 // The view that this BoxLayout is managing the layout for.
168 views::View* host_; 321 views::View* host_;
169 322
170 DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout); 323 DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout);
171 }; 324 };
172 325
173 } // namespace views 326 } // namespace views
174 327
175 #endif // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_ 328 #endif // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698