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

Side by Side Diff: chrome/browser/ui/views/location_bar/icon_label_bubble_view.cc

Issue 2642893002: Adjust positioning of location bar icons. (Closed)
Patch Set: simplified Created 3 years, 11 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 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h" 5 #include "chrome/browser/ui/views/location_bar/icon_label_bubble_view.h"
6 6
7 #include "chrome/browser/ui/layout_constants.h"
8 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h"
7 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
8 #include "ui/accessibility/ax_node_data.h" 10 #include "ui/accessibility/ax_node_data.h"
9 #include "ui/gfx/canvas.h" 11 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/color_utils.h" 12 #include "ui/gfx/color_utils.h"
11 #include "ui/gfx/scoped_canvas.h" 13 #include "ui/gfx/scoped_canvas.h"
12 #include "ui/native_theme/native_theme.h" 14 #include "ui/native_theme/native_theme.h"
13 #include "ui/views/animation/ink_drop_highlight.h" 15 #include "ui/views/animation/ink_drop_highlight.h"
14 #include "ui/views/border.h" 16 #include "ui/views/border.h"
15 #include "ui/views/controls/image_view.h" 17 #include "ui/views/controls/image_view.h"
16 #include "ui/views/widget/widget.h" 18 #include "ui/views/widget/widget.h"
17 19
18 namespace { 20 namespace {
19 21
20 // Amount of space on either side of the separator that appears after the label. 22 // Amount of space on either side of the separator that appears after the label.
21 constexpr int kSpaceBesideSeparator = 8; 23 constexpr int kSpaceBesideSeparator = 8;
22 24
23 } // namespace 25 } // namespace
24 26
25 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, 27 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list,
26 bool elide_in_middle) 28 bool elide_in_middle)
27 : image_(new views::ImageView()), 29 : image_(new views::ImageView()),
28 label_(new views::Label(base::string16(), font_list)) { 30 label_(new views::Label(base::string16(), font_list)) {
29 // Disable separate hit testing for |image_|. This prevents views treating 31 // Disable separate hit testing for |image_|. This prevents views treating
30 // |image_| as a separate mouse hover region from |this|. 32 // |image_| as a separate mouse hover region from |this|.
31 image_->set_interactive(false); 33 image_->set_interactive(false);
34 image_->SetBorder(views::CreateEmptyBorder(
35 gfx::Insets(LocationBarView::kIconInteriorPadding)));
32 AddChildView(image_); 36 AddChildView(image_);
33 37
34 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 38 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
35 39
36 if (elide_in_middle) 40 if (elide_in_middle)
37 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); 41 label_->SetElideBehavior(gfx::ELIDE_MIDDLE);
38 AddChildView(label_); 42 AddChildView(label_);
39 43
40 // Bubbles are given the full internal height of the location bar so that all 44 // Bubbles are given the full internal height of the location bar so that all
41 // child views in the location bar have the same height. The visible height of 45 // child views in the location bar have the same height. The visible height of
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 return false; 90 return false;
87 } 91 }
88 92
89 bool IconLabelBubbleView::OnKeyReleased(const ui::KeyEvent& event) { 93 bool IconLabelBubbleView::OnKeyReleased(const ui::KeyEvent& event) {
90 if (event.key_code() == ui::VKEY_SPACE) 94 if (event.key_code() == ui::VKEY_SPACE)
91 return OnActivate(event); 95 return OnActivate(event);
92 return false; 96 return false;
93 } 97 }
94 98
95 void IconLabelBubbleView::Layout() { 99 void IconLabelBubbleView::Layout() {
96 // Compute the image bounds. Leading and trailing padding are the same. 100 // When the view is expanding, if we don't have horizontal room for both the
97 int image_x = LocationBarView::kHorizontalPadding; 101 // image and the trailing padding, we take space away from the image. When the
98 int bubble_trailing_padding = image_x; 102 // view is contracting, instead we whittle away at the trailing padding.
99 103 int bubble_trailing_padding = GetPostSeparatorPadding();
100 // If ShouldShowLabel() is true, then either we show a label in the 104 int image_width = image_->GetPreferredSize().width();
101 // steady state, or we're not yet in the last portion of the animation. In 105 const int space_shortage = image_width + bubble_trailing_padding - width();
102 // these cases, we leave the leading and trailing padding alone. If this is 106 if (space_shortage > 0) {
103 // false, however, then we're only showing the image, and either the view 107 if (ShouldShowLabel())
Peter Kasting 2017/01/25 04:25:18 The comment above seems to imply this should check
Evan Stade 2017/01/26 00:52:59 thinking of it in terms of expanding vs contractin
Peter Kasting 2017/01/26 01:25:13 The reason I'm anxious about referring to expandin
104 // width is the image width, or it's animating downwards and getting close to 108 image_width -= space_shortage;
105 // it. In these cases, we want to shrink the trailing padding first, so the 109 else
106 // image slides all the way to the trailing edge before slowing or stopping; 110 bubble_trailing_padding -= space_shortage;
107 // then we want to shrink the leading padding down to zero.
108 const int image_preferred_width = image_->GetPreferredSize().width();
109 if (!ShouldShowLabel()) {
110 image_x = std::min(image_x, width() - image_preferred_width);
111 bubble_trailing_padding = std::min(
112 bubble_trailing_padding, width() - image_preferred_width - image_x);
113 } 111 }
114 112 image_->SetBounds(0, 0, image_width, height());
115 // Now that we've computed the padding values, give the image all the
116 // remaining width. This will be less than the image's preferred width during
117 // the first portion of the animation; during the very beginning there may not
118 // be enough room to show the image at all.
119 const int image_width =
120 std::min(image_preferred_width,
121 std::max(0, width() - image_x - bubble_trailing_padding));
122 image_->SetBounds(image_x, 0, image_width, height());
123 113
124 // Compute the label bounds. The label gets whatever size is left over after 114 // Compute the label bounds. The label gets whatever size is left over after
125 // accounting for the preferred image width and padding amounts. Note that if 115 // accounting for the preferred image width and padding amounts. Note that if
126 // the label has zero size it doesn't actually matter what we compute its X 116 // the label has zero size it doesn't actually matter what we compute its X
127 // value to be, since it won't be visible. 117 // value to be, since it won't be visible.
128 const int label_x = image_x + image_width + GetInternalSpacing(); 118 const int label_x = image_->bounds().right() + GetInternalSpacing();
129 const int label_width = 119 const int label_width =
130 std::max(0, width() - label_x - bubble_trailing_padding); 120 std::max(0, width() - label_x - bubble_trailing_padding);
131 label_->SetBounds(label_x, 0, label_width, height()); 121 label_->SetBounds(label_x, 0, label_width, height());
132 } 122 }
133 123
134 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { 124 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
135 label_->GetAccessibleNodeData(node_data); 125 label_->GetAccessibleNodeData(node_data);
136 } 126 }
137 127
138 void IconLabelBubbleView::OnNativeThemeChanged( 128 void IconLabelBubbleView::OnNativeThemeChanged(
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; 175 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1;
186 const int post_label_width = 176 const int post_label_width =
187 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); 177 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding());
188 178
189 // |multiplier| grows from zero to one, stays equal to one and then shrinks 179 // |multiplier| grows from zero to one, stays equal to one and then shrinks
190 // to zero again. The view width should correspondingly grow from zero to 180 // to zero again. The view width should correspondingly grow from zero to
191 // fully showing both label and icon, stay there, then shrink to just large 181 // fully showing both label and icon, stay there, then shrink to just large
192 // enough to show the icon. We don't want to shrink all the way back to 182 // enough to show the icon. We don't want to shrink all the way back to
193 // zero, since this would mean the view would completely disappear and then 183 // zero, since this would mean the view would completely disappear and then
194 // pop back to an icon after the animation finishes. 184 // pop back to an icon after the animation finishes.
195 const int max_width = LocationBarView::kHorizontalPadding + 185 const int max_width = image_->GetPreferredSize().width() +
Peter Kasting 2017/01/25 04:25:18 Nit: Use |size|?
Evan Stade 2017/01/26 00:52:59 Done.
196 image_->GetPreferredSize().width() +
197 GetInternalSpacing() + label_width + post_label_width; 186 GetInternalSpacing() + label_width + post_label_width;
198 const int current_width = WidthMultiplier() * max_width; 187 const int current_width = WidthMultiplier() * max_width;
199 size.set_width(shrinking ? std::max(current_width, size.width()) 188 size.set_width(shrinking ? std::max(current_width, size.width())
200 : current_width); 189 : current_width);
201 } 190 }
202 return size; 191 return size;
203 } 192 }
204 193
205 int IconLabelBubbleView::GetInternalSpacing() const { 194 int IconLabelBubbleView::GetInternalSpacing() const {
206 return image_->GetPreferredSize().IsEmpty() 195 return image_->GetPreferredSize().IsEmpty()
207 ? 0 196 ? 0
208 : LocationBarView::kHorizontalPadding; 197 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING);
209 } 198 }
210 199
211 int IconLabelBubbleView::GetPostSeparatorPadding() const { 200 int IconLabelBubbleView::GetPostSeparatorPadding() const {
212 // The location bar will add LocationBarView::kHorizontalPadding after us. 201 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us.
213 return kSpaceBesideSeparator - LocationBarView::kHorizontalPadding; 202 return kSpaceBesideSeparator -
203 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) -
204 next_element_interior_padding_;
214 } 205 }
215 206
216 float IconLabelBubbleView::GetScaleFactor() const { 207 float IconLabelBubbleView::GetScaleFactor() const {
217 const views::Widget* widget = GetWidget(); 208 const views::Widget* widget = GetWidget();
218 // There may be no widget in tests, and in ash there may be no compositor if 209 // There may be no widget in tests, and in ash there may be no compositor if
219 // the native view of the Widget doesn't have a parent. 210 // the native view of the Widget doesn't have a parent.
220 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; 211 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr;
221 return compositor ? compositor->device_scale_factor() : 1.0f; 212 return compositor ? compositor->device_scale_factor() : 1.0f;
222 } 213 }
223 214
224 const char* IconLabelBubbleView::GetClassName() const { 215 const char* IconLabelBubbleView::GetClassName() const {
225 return "IconLabelBubbleView"; 216 return "IconLabelBubbleView";
226 } 217 }
227 218
228 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { 219 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) {
229 if (!ShouldShowLabel()) 220 if (!ShouldShowLabel())
230 return; 221 return;
231 222
232 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( 223 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor(
233 ui::NativeTheme::kColorId_TextfieldDefaultColor); 224 ui::NativeTheme::kColorId_TextfieldDefaultColor);
234 const SkColor separator_color = SkColorSetA( 225 const SkColor separator_color = SkColorSetA(
235 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); 226 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC);
236 227
237 gfx::Rect bounds(GetLocalBounds()); 228 gfx::Rect bounds(label_->bounds());
238 const int kSeparatorHeight = 16; 229 const int kSeparatorHeight = 16;
239 bounds.Inset(GetPostSeparatorPadding(), 230 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2);
240 (bounds.height() - kSeparatorHeight) / 2);
241 231
242 // Draw the 1 px separator. 232 // Draw the 1 px separator.
243 gfx::ScopedCanvas scoped_canvas(canvas); 233 gfx::ScopedCanvas scoped_canvas(canvas);
244 const float scale = canvas->UndoDeviceScaleFactor(); 234 const float scale = canvas->UndoDeviceScaleFactor();
245 // Keep the separator aligned on a pixel center. 235 // Keep the separator aligned on a pixel center.
246 const gfx::RectF pixel_aligned_bounds = 236 const gfx::RectF pixel_aligned_bounds =
247 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); 237 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0);
248 canvas->DrawLine(pixel_aligned_bounds.top_right(), 238 canvas->DrawLine(pixel_aligned_bounds.top_right(),
249 pixel_aligned_bounds.bottom_right(), separator_color); 239 pixel_aligned_bounds.bottom_right(), separator_color);
250 } 240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698