OLD | NEW |
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 #include "chrome/browser/ui/views/location_bar/location_bar_layout.h" | 5 #include "chrome/browser/ui/views/location_bar/location_bar_layout.h" |
6 | 6 |
7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
8 #include "ui/gfx/rect.h" | 8 #include "ui/gfx/rect.h" |
9 #include "ui/views/view.h" | 9 #include "ui/views/view.h" |
10 | 10 |
11 | 11 |
12 // Description of a decoration to be added inside the location bar, either to | 12 // Description of a decoration to be added inside the location bar, either to |
13 // the left or to the right. | 13 // the left or to the right. |
14 struct LocationBarDecoration { | 14 struct LocationBarDecoration { |
15 LocationBarDecoration(int y, | 15 LocationBarDecoration(int y, |
16 int height, | 16 int height, |
17 bool auto_collapse, | 17 bool auto_collapse, |
18 double max_fraction, | 18 double max_fraction, |
19 int edge_item_padding, | 19 int edge_item_padding, |
20 int item_padding, | 20 int item_padding, |
21 int builtin_padding, | |
22 views::View* view); | 21 views::View* view); |
23 | 22 |
24 // The y position of the view inside its parent. | 23 // The y position of the view inside its parent. |
25 int y; | 24 int y; |
26 | 25 |
27 // The height of the view. | 26 // The height of the view. |
28 int height; | 27 int height; |
29 | 28 |
30 // True means that, if there is not enough available space in the location | 29 // True means that, if there is not enough available space in the location |
31 // bar, the view will reduce its width either to its minimal width or to zero | 30 // bar, the view will reduce its width either to its minimal width or to zero |
32 // (making it invisible), whichever fits. If true, |max_fraction| must be 0. | 31 // (making it invisible), whichever fits. If true, |max_fraction| must be 0. |
33 bool auto_collapse; | 32 bool auto_collapse; |
34 | 33 |
35 // Used for resizeable decorations, indicates the maximum fraction of the | 34 // Used for resizeable decorations, indicates the maximum fraction of the |
36 // location bar that can be taken by this decoration, 0 for non-resizable | 35 // location bar that can be taken by this decoration, 0 for non-resizable |
37 // decorations. If non-zero, |auto_collapse| must be false. | 36 // decorations. If non-zero, |auto_collapse| must be false. |
38 double max_fraction; | 37 double max_fraction; |
39 | 38 |
40 // Padding to use if the decoration is the first element next to the edge. | 39 // Padding to use if the decoration is the first element next to the edge. |
41 int edge_item_padding; | 40 int edge_item_padding; |
42 | 41 |
43 // Padding to use if the decoration follows another decoration. | 42 // Padding to use if the decoration follows another decoration. |
44 int item_padding; | 43 int item_padding; |
45 | 44 |
46 // Padding built into the decoration and that should be removed, on | |
47 // both sides, during layout. | |
48 int builtin_padding; | |
49 | |
50 views::View* view; | 45 views::View* view; |
51 | 46 |
52 // The width computed by the layout process. | 47 // The width computed by the layout process. |
53 double computed_width; | 48 double computed_width; |
54 }; | 49 }; |
55 | 50 |
56 LocationBarDecoration::LocationBarDecoration(int y, | 51 LocationBarDecoration::LocationBarDecoration(int y, |
57 int height, | 52 int height, |
58 bool auto_collapse, | 53 bool auto_collapse, |
59 double max_fraction, | 54 double max_fraction, |
60 int edge_item_padding, | 55 int edge_item_padding, |
61 int item_padding, | 56 int item_padding, |
62 int builtin_padding, | |
63 views::View* view) | 57 views::View* view) |
64 : y(y), | 58 : y(y), |
65 height(height), | 59 height(height), |
66 auto_collapse(auto_collapse), | 60 auto_collapse(auto_collapse), |
67 max_fraction(max_fraction), | 61 max_fraction(max_fraction), |
68 edge_item_padding(edge_item_padding), | 62 edge_item_padding(edge_item_padding), |
69 item_padding(item_padding), | 63 item_padding(item_padding), |
70 builtin_padding(builtin_padding), | |
71 view(view), | 64 view(view), |
72 computed_width(0) { | 65 computed_width(0) { |
73 DCHECK((max_fraction == 0.0) || (!auto_collapse && (max_fraction > 0.0))); | 66 DCHECK((max_fraction == 0.0) || (!auto_collapse && (max_fraction > 0.0))); |
74 } | 67 } |
75 | 68 |
76 | 69 |
77 // LocationBarLayout --------------------------------------------------------- | 70 // LocationBarLayout --------------------------------------------------------- |
78 | 71 |
79 LocationBarLayout::LocationBarLayout(Position position, int item_edit_padding) | 72 LocationBarLayout::LocationBarLayout(Position position, int item_edit_padding) |
80 : position_(position), | 73 : position_(position), |
81 item_edit_padding_(item_edit_padding) { | 74 item_edit_padding_(item_edit_padding) { |
82 } | 75 } |
83 | 76 |
84 | 77 |
85 LocationBarLayout::~LocationBarLayout() { | 78 LocationBarLayout::~LocationBarLayout() { |
86 } | 79 } |
87 | 80 |
88 void LocationBarLayout::AddDecoration(int y, | 81 void LocationBarLayout::AddDecoration(int y, |
89 int height, | 82 int height, |
90 bool auto_collapse, | 83 bool auto_collapse, |
91 double max_fraction, | 84 double max_fraction, |
92 int edge_item_padding, | 85 int edge_item_padding, |
93 int item_padding, | 86 int item_padding, |
94 int builtin_padding, | |
95 views::View* view) { | 87 views::View* view) { |
96 decorations_.push_back(new LocationBarDecoration( | 88 decorations_.push_back(new LocationBarDecoration( |
97 y, height, auto_collapse, max_fraction, edge_item_padding, item_padding, | 89 y, height, auto_collapse, max_fraction, edge_item_padding, item_padding, |
98 builtin_padding, view)); | 90 view)); |
99 } | 91 } |
100 | 92 |
101 void LocationBarLayout::AddDecoration(int y, | 93 void LocationBarLayout::AddDecoration(int y, |
102 int height, | 94 int height, |
103 int builtin_padding, | |
104 views::View* view) { | 95 views::View* view) { |
105 decorations_.push_back(new LocationBarDecoration( | 96 decorations_.push_back(new LocationBarDecoration( |
106 y, height, false, 0, LocationBarView::kItemPadding, | 97 y, height, false, 0, LocationBarView::kItemPadding, |
107 LocationBarView::kItemPadding, builtin_padding, view)); | 98 LocationBarView::kItemPadding, view)); |
108 } | 99 } |
109 | 100 |
110 void LocationBarLayout::LayoutPass1(int* entry_width) { | 101 void LocationBarLayout::LayoutPass1(int* entry_width) { |
111 bool first_item = true; | 102 bool first_item = true; |
112 for (Decorations::iterator i(decorations_.begin()); i != decorations_.end(); | 103 for (Decorations::iterator i(decorations_.begin()); i != decorations_.end(); |
113 ++i) { | 104 ++i) { |
114 // Autocollapsing decorations are ignored in this pass. | 105 // Autocollapsing decorations are ignored in this pass. |
115 if (!(*i)->auto_collapse) { | 106 if (!(*i)->auto_collapse) { |
116 *entry_width -= -2 * (*i)->builtin_padding + | 107 *entry_width -= |
117 (first_item ? (*i)->edge_item_padding : (*i)->item_padding); | 108 (first_item ? (*i)->edge_item_padding : (*i)->item_padding); |
118 } | 109 } |
119 first_item = false; | 110 first_item = false; |
120 // Resizing decorations are ignored in this pass. | 111 // Resizing decorations are ignored in this pass. |
121 if (!(*i)->auto_collapse && ((*i)->max_fraction == 0.0)) { | 112 if (!(*i)->auto_collapse && ((*i)->max_fraction == 0.0)) { |
122 (*i)->computed_width = (*i)->view->GetPreferredSize().width(); | 113 (*i)->computed_width = (*i)->view->GetPreferredSize().width(); |
123 *entry_width -= (*i)->computed_width; | 114 *entry_width -= (*i)->computed_width; |
124 } | 115 } |
125 } | 116 } |
126 *entry_width -= item_edit_padding_; | 117 *entry_width -= item_edit_padding_; |
(...skipping 11 matching lines...) Expand all Loading... |
138 } | 129 } |
139 } | 130 } |
140 } | 131 } |
141 | 132 |
142 void LocationBarLayout::LayoutPass3(gfx::Rect* bounds, int* available_width) { | 133 void LocationBarLayout::LayoutPass3(gfx::Rect* bounds, int* available_width) { |
143 bool first_visible = true; | 134 bool first_visible = true; |
144 for (Decorations::iterator i(decorations_.begin()); i != decorations_.end(); | 135 for (Decorations::iterator i(decorations_.begin()); i != decorations_.end(); |
145 ++i) { | 136 ++i) { |
146 // Collapse decorations if needed. | 137 // Collapse decorations if needed. |
147 if ((*i)->auto_collapse) { | 138 if ((*i)->auto_collapse) { |
148 int padding = -2 * (*i)->builtin_padding + | 139 int padding = |
149 (first_visible ? (*i)->edge_item_padding : (*i)->item_padding); | 140 (first_visible ? (*i)->edge_item_padding : (*i)->item_padding); |
150 // Try preferred size, if it fails try minimum size, if it fails collapse. | 141 // Try preferred size, if it fails try minimum size, if it fails collapse. |
151 (*i)->computed_width = (*i)->view->GetPreferredSize().width(); | 142 (*i)->computed_width = (*i)->view->GetPreferredSize().width(); |
152 if ((*i)->computed_width + padding > *available_width) | 143 if ((*i)->computed_width + padding > *available_width) |
153 (*i)->computed_width = (*i)->view->GetMinimumSize().width(); | 144 (*i)->computed_width = (*i)->view->GetMinimumSize().width(); |
154 if ((*i)->computed_width + padding > *available_width) { | 145 if ((*i)->computed_width + padding > *available_width) { |
155 (*i)->computed_width = 0; | 146 (*i)->computed_width = 0; |
156 (*i)->view->SetVisible(false); | 147 (*i)->view->SetVisible(false); |
157 } else { | 148 } else { |
158 (*i)->view->SetVisible(true); | 149 (*i)->view->SetVisible(true); |
159 (*available_width) -= (*i)->computed_width + padding; | 150 (*available_width) -= (*i)->computed_width + padding; |
160 } | 151 } |
161 } else { | 152 } else { |
162 (*i)->view->SetVisible(true); | 153 (*i)->view->SetVisible(true); |
163 } | 154 } |
164 | 155 |
165 // Layout visible decorations. | 156 // Layout visible decorations. |
166 if (!(*i)->view->visible()) | 157 if (!(*i)->view->visible()) |
167 continue; | 158 continue; |
168 int padding = -(*i)->builtin_padding + | 159 int padding = |
169 (first_visible ? (*i)->edge_item_padding : (*i)->item_padding); | 160 (first_visible ? (*i)->edge_item_padding : (*i)->item_padding); |
170 first_visible = false; | 161 first_visible = false; |
171 int x = (position_ == LEFT_EDGE) ? (bounds->x() + padding) : | 162 int x = (position_ == LEFT_EDGE) ? (bounds->x() + padding) : |
172 (bounds->right() - padding - (*i)->computed_width); | 163 (bounds->right() - padding - (*i)->computed_width); |
173 (*i)->view->SetBounds(x, (*i)->y, (*i)->computed_width, (*i)->height); | 164 (*i)->view->SetBounds(x, (*i)->y, (*i)->computed_width, (*i)->height); |
174 bounds->set_width(bounds->width() - padding - (*i)->computed_width + | 165 bounds->set_width(bounds->width() - padding - (*i)->computed_width); |
175 (*i)->builtin_padding); | 166 if (position_ == LEFT_EDGE) |
176 if (position_ == LEFT_EDGE) { | 167 bounds->set_x(bounds->x() + padding + (*i)->computed_width); |
177 bounds->set_x( | |
178 bounds->x() + padding + (*i)->computed_width - (*i)->builtin_padding); | |
179 } | |
180 } | 168 } |
181 bounds->set_width(bounds->width() - item_edit_padding_); | 169 bounds->set_width(bounds->width() - item_edit_padding_); |
182 if (position_ == LEFT_EDGE) | 170 if (position_ == LEFT_EDGE) |
183 bounds->set_x(bounds->x() + item_edit_padding_); | 171 bounds->set_x(bounds->x() + item_edit_padding_); |
184 } | 172 } |
OLD | NEW |