Chromium Code Reviews| 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/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" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 } | 89 } |
| 88 | 90 |
| 89 bool IconLabelBubbleView::OnKeyReleased(const ui::KeyEvent& event) { | 91 bool IconLabelBubbleView::OnKeyReleased(const ui::KeyEvent& event) { |
| 90 if (event.key_code() == ui::VKEY_SPACE) | 92 if (event.key_code() == ui::VKEY_SPACE) |
| 91 return OnActivate(event); | 93 return OnActivate(event); |
| 92 return false; | 94 return false; |
| 93 } | 95 } |
| 94 | 96 |
| 95 void IconLabelBubbleView::Layout() { | 97 void IconLabelBubbleView::Layout() { |
| 96 // Compute the image bounds. Leading and trailing padding are the same. | 98 // Compute the image bounds. Leading and trailing padding are the same. |
| 97 int image_x = LocationBarView::kHorizontalPadding; | 99 int image_x = 0; |
|
Peter Kasting
2017/01/24 21:26:39
Doesn't this effectively make |image_x| 0 everywhe
Evan Stade
2017/01/25 00:37:49
I guess it does. I was worried that width() - imag
| |
| 98 int bubble_trailing_padding = image_x; | 100 int bubble_trailing_padding = GetPostSeparatorPadding(); |
| 99 | 101 |
| 100 // If ShouldShowLabel() is true, then either we show a label in the | 102 // If ShouldShowLabel() is true, then either we show a label in the |
| 101 // steady state, or we're not yet in the last portion of the animation. In | 103 // steady state, or we're not yet in the last portion of the animation. In |
| 102 // these cases, we leave the leading and trailing padding alone. If this is | 104 // these cases, we leave the leading and trailing padding alone. If this is |
| 103 // false, however, then we're only showing the image, and either the view | 105 // false, however, then we're only showing the image, and either the view |
| 104 // width is the image width, or it's animating downwards and getting close to | 106 // width is the image width, or it's animating downwards and getting close to |
| 105 // it. In these cases, we want to shrink the trailing padding first, so the | 107 // it. In these cases, we want to shrink the trailing padding first, so the |
| 106 // image slides all the way to the trailing edge before slowing or stopping; | 108 // image slides all the way to the trailing edge before slowing or stopping; |
| 107 // then we want to shrink the leading padding down to zero. | 109 // then we want to shrink the leading padding down to zero. |
|
Peter Kasting
2017/01/24 21:26:40
This code, and the code in GetSizeForLabelWidth(),
Evan Stade
2017/01/25 00:37:49
I too was afraid of touching this more than I had
| |
| 108 const int image_preferred_width = image_->GetPreferredSize().width(); | 110 const int image_preferred_width = GetImageViewSize().width(); |
| 109 if (!ShouldShowLabel()) { | 111 if (!ShouldShowLabel()) { |
| 110 image_x = std::min(image_x, width() - image_preferred_width); | 112 image_x = std::min(image_x, width() - image_preferred_width); |
| 111 bubble_trailing_padding = std::min( | 113 bubble_trailing_padding = std::min( |
| 112 bubble_trailing_padding, width() - image_preferred_width - image_x); | 114 bubble_trailing_padding, width() - image_preferred_width - image_x); |
| 113 } | 115 } |
| 114 | 116 |
| 115 // Now that we've computed the padding values, give the image all the | 117 // 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 | 118 // 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 | 119 // the first portion of the animation; during the very beginning there may not |
| 118 // be enough room to show the image at all. | 120 // be enough room to show the image at all. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { | 165 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { |
| 164 return color_utils::DeriveDefaultIconColor(GetTextColor()); | 166 return color_utils::DeriveDefaultIconColor(GetTextColor()); |
| 165 } | 167 } |
| 166 | 168 |
| 167 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 169 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 168 return GetNativeTheme()->GetSystemColor( | 170 return GetNativeTheme()->GetSystemColor( |
| 169 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 171 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 170 } | 172 } |
| 171 | 173 |
| 172 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 174 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 173 gfx::Size size(image_->GetPreferredSize()); | 175 gfx::Size size(GetImageViewSize()); |
| 174 const bool shrinking = IsShrinking(); | 176 const bool shrinking = IsShrinking(); |
| 175 // Animation continues for the last few pixels even after the label is not | 177 // Animation continues for the last few pixels even after the label is not |
| 176 // visible in order to slide the icon into its final position. Therefore it | 178 // visible in order to slide the icon into its final position. Therefore it |
| 177 // is necessary to animate |total_width| even when the background is hidden | 179 // is necessary to animate |total_width| even when the background is hidden |
| 178 // as long as the animation is still shrinking. | 180 // as long as the animation is still shrinking. |
| 179 if (ShouldShowLabel() || shrinking) { | 181 if (ShouldShowLabel() || shrinking) { |
| 180 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For | 182 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For |
| 181 // higher scale factors, we simply take the separator px out of the | 183 // higher scale factors, we simply take the separator px out of the |
| 182 // kSpaceBesideSeparator region before the separator, as that results in a | 184 // kSpaceBesideSeparator region before the separator, as that results in a |
| 183 // width closer to the desired gap than if we added a whole DIP for the | 185 // width closer to the desired gap than if we added a whole DIP for the |
| 184 // separator px. (For scale 2, the two methods have equal error: 1 px.) | 186 // separator px. (For scale 2, the two methods have equal error: 1 px.) |
| 185 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; | 187 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; |
| 186 const int post_label_width = | 188 const int post_label_width = |
| 187 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); | 189 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); |
| 188 | 190 |
| 189 // |multiplier| grows from zero to one, stays equal to one and then shrinks | 191 // |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 | 192 // 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 | 193 // 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 | 194 // 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 | 195 // zero, since this would mean the view would completely disappear and then |
| 194 // pop back to an icon after the animation finishes. | 196 // pop back to an icon after the animation finishes. |
| 195 const int max_width = LocationBarView::kHorizontalPadding + | 197 const int max_width = GetImageViewSize().width() + GetInternalSpacing() + |
| 196 image_->GetPreferredSize().width() + | 198 label_width + post_label_width; |
| 197 GetInternalSpacing() + label_width + post_label_width; | |
| 198 const int current_width = WidthMultiplier() * max_width; | 199 const int current_width = WidthMultiplier() * max_width; |
| 199 size.set_width(shrinking ? std::max(current_width, size.width()) | 200 size.set_width(shrinking ? std::max(current_width, size.width()) |
| 200 : current_width); | 201 : current_width); |
| 201 } | 202 } |
| 202 return size; | 203 return size; |
| 203 } | 204 } |
| 204 | 205 |
| 205 int IconLabelBubbleView::GetInternalSpacing() const { | 206 int IconLabelBubbleView::GetInternalSpacing() const { |
| 206 return image_->GetPreferredSize().IsEmpty() | 207 return GetImageViewSize().IsEmpty() |
| 207 ? 0 | 208 ? 0 |
| 208 : LocationBarView::kHorizontalPadding; | 209 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); |
| 209 } | 210 } |
| 210 | 211 |
| 211 int IconLabelBubbleView::GetPostSeparatorPadding() const { | 212 int IconLabelBubbleView::GetPostSeparatorPadding() const { |
| 212 // The location bar will add LocationBarView::kHorizontalPadding after us. | 213 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. |
| 213 return kSpaceBesideSeparator - LocationBarView::kHorizontalPadding; | 214 return kSpaceBesideSeparator - |
| 215 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - | |
| 216 next_element_interior_padding_; | |
| 214 } | 217 } |
| 215 | 218 |
| 216 float IconLabelBubbleView::GetScaleFactor() const { | 219 float IconLabelBubbleView::GetScaleFactor() const { |
| 217 const views::Widget* widget = GetWidget(); | 220 const views::Widget* widget = GetWidget(); |
| 218 // There may be no widget in tests, and in ash there may be no compositor if | 221 // 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. | 222 // the native view of the Widget doesn't have a parent. |
| 220 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 223 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
| 221 return compositor ? compositor->device_scale_factor() : 1.0f; | 224 return compositor ? compositor->device_scale_factor() : 1.0f; |
| 222 } | 225 } |
| 223 | 226 |
| 227 gfx::Size IconLabelBubbleView::GetImageViewSize() const { | |
| 228 gfx::Rect image_rect(image_->GetPreferredSize()); | |
| 229 if (image_rect.IsEmpty()) | |
| 230 return gfx::Size(); | |
| 231 image_rect.Inset(-gfx::Insets(LocationBarView::kIconInteriorPadding)); | |
|
Peter Kasting
2017/01/24 21:26:40
Nit: Simpler:
if (!image_rect.IsEmpty())
im
Evan Stade
2017/01/25 00:37:49
Done.
| |
| 232 return image_rect.size(); | |
| 233 } | |
| 234 | |
| 224 const char* IconLabelBubbleView::GetClassName() const { | 235 const char* IconLabelBubbleView::GetClassName() const { |
| 225 return "IconLabelBubbleView"; | 236 return "IconLabelBubbleView"; |
| 226 } | 237 } |
| 227 | 238 |
| 228 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 239 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 229 if (!ShouldShowLabel()) | 240 if (!ShouldShowLabel()) |
| 230 return; | 241 return; |
| 231 | 242 |
| 232 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( | 243 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( |
| 233 ui::NativeTheme::kColorId_TextfieldDefaultColor); | 244 ui::NativeTheme::kColorId_TextfieldDefaultColor); |
| 234 const SkColor separator_color = SkColorSetA( | 245 const SkColor separator_color = SkColorSetA( |
| 235 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | 246 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); |
| 236 | 247 |
| 237 gfx::Rect bounds(GetLocalBounds()); | 248 gfx::Rect bounds(GetLocalBounds()); |
| 238 const int kSeparatorHeight = 16; | 249 const int kSeparatorHeight = 16; |
| 239 bounds.Inset(GetPostSeparatorPadding(), | 250 bounds.Inset(GetPostSeparatorPadding(), |
| 240 (bounds.height() - kSeparatorHeight) / 2); | 251 (bounds.height() - kSeparatorHeight) / 2); |
| 241 | 252 |
| 242 // Draw the 1 px separator. | 253 // Draw the 1 px separator. |
| 243 gfx::ScopedCanvas scoped_canvas(canvas); | 254 gfx::ScopedCanvas scoped_canvas(canvas); |
| 244 const float scale = canvas->UndoDeviceScaleFactor(); | 255 const float scale = canvas->UndoDeviceScaleFactor(); |
| 245 // Keep the separator aligned on a pixel center. | 256 // Keep the separator aligned on a pixel center. |
| 246 const gfx::RectF pixel_aligned_bounds = | 257 const gfx::RectF pixel_aligned_bounds = |
| 247 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); | 258 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); |
| 248 canvas->DrawLine(pixel_aligned_bounds.top_right(), | 259 canvas->DrawLine(pixel_aligned_bounds.top_right(), |
| 249 pixel_aligned_bounds.bottom_right(), separator_color); | 260 pixel_aligned_bounds.bottom_right(), separator_color); |
| 250 } | 261 } |
| OLD | NEW |