| 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" |
| 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 Loading... |
| 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 // We may not have horizontal room for both the image and the trailing |
| 97 int image_x = LocationBarView::kHorizontalPadding; | 101 // padding. When the view is expanding (or showing-label steady state), the |
| 98 int bubble_trailing_padding = image_x; | 102 // image. When the view is contracting (or hidden-label steady state), whittle |
| 99 | 103 // away at the trailing padding instead. |
| 100 // If ShouldShowLabel() is true, then either we show a label in the | 104 int bubble_trailing_padding = GetPostSeparatorPadding(); |
| 101 // steady state, or we're not yet in the last portion of the animation. In | 105 int image_width = image_->GetPreferredSize().width(); |
| 102 // these cases, we leave the leading and trailing padding alone. If this is | 106 const int space_shortage = image_width + bubble_trailing_padding - width(); |
| 103 // false, however, then we're only showing the image, and either the view | 107 if (space_shortage > 0) { |
| 104 // width is the image width, or it's animating downwards and getting close to | 108 if (ShouldShowLabel()) |
| 105 // it. In these cases, we want to shrink the trailing padding first, so the | 109 image_width -= space_shortage; |
| 106 // image slides all the way to the trailing edge before slowing or stopping; | 110 else |
| 107 // then we want to shrink the leading padding down to zero. | 111 bubble_trailing_padding -= space_shortage; |
| 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 } | 112 } |
| 114 | 113 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 | 114 |
| 124 // Compute the label bounds. The label gets whatever size is left over after | 115 // 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 | 116 // 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 | 117 // 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. | 118 // value to be, since it won't be visible. |
| 128 const int label_x = image_x + image_width + GetInternalSpacing(); | 119 const int label_x = image_->bounds().right() + GetInternalSpacing(); |
| 129 const int label_width = | 120 const int label_width = |
| 130 std::max(0, width() - label_x - bubble_trailing_padding); | 121 std::max(0, width() - label_x - bubble_trailing_padding); |
| 131 label_->SetBounds(label_x, 0, label_width, height()); | 122 label_->SetBounds(label_x, 0, label_width, height()); |
| 132 } | 123 } |
| 133 | 124 |
| 134 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 125 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 135 label_->GetAccessibleNodeData(node_data); | 126 label_->GetAccessibleNodeData(node_data); |
| 136 } | 127 } |
| 137 | 128 |
| 138 void IconLabelBubbleView::OnNativeThemeChanged( | 129 void IconLabelBubbleView::OnNativeThemeChanged( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; | 176 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; |
| 186 const int post_label_width = | 177 const int post_label_width = |
| 187 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); | 178 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); |
| 188 | 179 |
| 189 // |multiplier| grows from zero to one, stays equal to one and then shrinks | 180 // |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 | 181 // 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 | 182 // 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 | 183 // 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 | 184 // zero, since this would mean the view would completely disappear and then |
| 194 // pop back to an icon after the animation finishes. | 185 // pop back to an icon after the animation finishes. |
| 195 const int max_width = LocationBarView::kHorizontalPadding + | 186 const int max_width = |
| 196 image_->GetPreferredSize().width() + | 187 size.width() + GetInternalSpacing() + label_width + post_label_width; |
| 197 GetInternalSpacing() + label_width + post_label_width; | |
| 198 const int current_width = WidthMultiplier() * max_width; | 188 const int current_width = WidthMultiplier() * max_width; |
| 199 size.set_width(shrinking ? std::max(current_width, size.width()) | 189 size.set_width(shrinking ? std::max(current_width, size.width()) |
| 200 : current_width); | 190 : current_width); |
| 201 } | 191 } |
| 202 return size; | 192 return size; |
| 203 } | 193 } |
| 204 | 194 |
| 205 int IconLabelBubbleView::GetInternalSpacing() const { | 195 int IconLabelBubbleView::GetInternalSpacing() const { |
| 206 return image_->GetPreferredSize().IsEmpty() | 196 return image_->GetPreferredSize().IsEmpty() |
| 207 ? 0 | 197 ? 0 |
| 208 : LocationBarView::kHorizontalPadding; | 198 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); |
| 209 } | 199 } |
| 210 | 200 |
| 211 int IconLabelBubbleView::GetPostSeparatorPadding() const { | 201 int IconLabelBubbleView::GetPostSeparatorPadding() const { |
| 212 // The location bar will add LocationBarView::kHorizontalPadding after us. | 202 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. |
| 213 return kSpaceBesideSeparator - LocationBarView::kHorizontalPadding; | 203 return kSpaceBesideSeparator - |
| 204 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - |
| 205 next_element_interior_padding_; |
| 214 } | 206 } |
| 215 | 207 |
| 216 float IconLabelBubbleView::GetScaleFactor() const { | 208 float IconLabelBubbleView::GetScaleFactor() const { |
| 217 const views::Widget* widget = GetWidget(); | 209 const views::Widget* widget = GetWidget(); |
| 218 // There may be no widget in tests, and in ash there may be no compositor if | 210 // 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. | 211 // the native view of the Widget doesn't have a parent. |
| 220 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 212 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
| 221 return compositor ? compositor->device_scale_factor() : 1.0f; | 213 return compositor ? compositor->device_scale_factor() : 1.0f; |
| 222 } | 214 } |
| 223 | 215 |
| 224 const char* IconLabelBubbleView::GetClassName() const { | 216 const char* IconLabelBubbleView::GetClassName() const { |
| 225 return "IconLabelBubbleView"; | 217 return "IconLabelBubbleView"; |
| 226 } | 218 } |
| 227 | 219 |
| 228 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | 220 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { |
| 229 if (!ShouldShowLabel()) | 221 if (!ShouldShowLabel()) |
| 230 return; | 222 return; |
| 231 | 223 |
| 232 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( | 224 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( |
| 233 ui::NativeTheme::kColorId_TextfieldDefaultColor); | 225 ui::NativeTheme::kColorId_TextfieldDefaultColor); |
| 234 const SkColor separator_color = SkColorSetA( | 226 const SkColor separator_color = SkColorSetA( |
| 235 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | 227 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); |
| 236 | 228 |
| 237 gfx::Rect bounds(GetLocalBounds()); | 229 gfx::Rect bounds(label_->bounds()); |
| 238 const int kSeparatorHeight = 16; | 230 const int kSeparatorHeight = 16; |
| 239 bounds.Inset(GetPostSeparatorPadding(), | 231 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); |
| 240 (bounds.height() - kSeparatorHeight) / 2); | |
| 241 | 232 |
| 242 // Draw the 1 px separator. | 233 // Draw the 1 px separator. |
| 243 gfx::ScopedCanvas scoped_canvas(canvas); | 234 gfx::ScopedCanvas scoped_canvas(canvas); |
| 244 const float scale = canvas->UndoDeviceScaleFactor(); | 235 const float scale = canvas->UndoDeviceScaleFactor(); |
| 245 // Keep the separator aligned on a pixel center. | 236 // Keep the separator aligned on a pixel center. |
| 246 const gfx::RectF pixel_aligned_bounds = | 237 const gfx::RectF pixel_aligned_bounds = |
| 247 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); | 238 gfx::ScaleRect(gfx::RectF(bounds), scale) - gfx::Vector2dF(0.5f, 0); |
| 248 canvas->DrawLine(pixel_aligned_bounds.top_right(), | 239 canvas->DrawLine(pixel_aligned_bounds.top_right(), |
| 249 pixel_aligned_bounds.bottom_right(), separator_color); | 240 pixel_aligned_bounds.bottom_right(), separator_color); |
| 250 } | 241 } |
| OLD | NEW |