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" | 7 #include "chrome/browser/ui/layout_constants.h" |
| 8 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" | 8 #include "chrome/browser/ui/views/location_bar/background_with_1_px_border.h" |
| 9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 9 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 10 #include "ui/accessibility/ax_node_data.h" | 10 #include "ui/accessibility/ax_node_data.h" |
| 11 #include "ui/compositor/layer_animator.h" | |
| 12 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 11 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 12 #include "ui/gfx/color_utils.h" | 14 #include "ui/gfx/color_utils.h" |
| 13 #include "ui/gfx/scoped_canvas.h" | 15 #include "ui/gfx/scoped_canvas.h" |
| 14 #include "ui/native_theme/native_theme.h" | 16 #include "ui/native_theme/native_theme.h" |
| 17 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" | |
| 15 #include "ui/views/animation/ink_drop_highlight.h" | 18 #include "ui/views/animation/ink_drop_highlight.h" |
| 19 #include "ui/views/animation/ink_drop_impl.h" | |
| 20 #include "ui/views/animation/ink_drop_mask.h" | |
| 21 #include "ui/views/animation/ink_drop_ripple.h" | |
| 16 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
| 17 #include "ui/views/controls/image_view.h" | 23 #include "ui/views/controls/image_view.h" |
| 18 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 19 | 25 |
| 20 namespace { | 26 namespace { |
| 21 | 27 |
| 22 // Amount of space on either side of the separator that appears after the label. | 28 // Amount of space on either side of the separator that appears after the label. |
| 23 constexpr int kSpaceBesideSeparator = 8; | 29 constexpr int kSpaceBesideSeparator = 8; |
| 24 | 30 |
| 31 // Radius of the ink drop mask's rounded rectangle shape. | |
| 32 constexpr int kInkDropMaskRadius = 2; | |
| 33 | |
| 34 // The length of the separator's fade animation. | |
| 35 constexpr int kFadeInDurationMs = 250; | |
| 36 constexpr int kFadeOutDurationMs = 175; | |
| 37 | |
| 25 } // namespace | 38 } // namespace |
| 26 | 39 |
| 40 ////////////////////////////////////////////////////////////////// | |
| 41 // SeparatorView class | |
| 42 | |
| 43 IconLabelBubbleView::SeparatorView::SeparatorView(IconLabelBubbleView* owner) { | |
| 44 DCHECK(owner); | |
| 45 owner_ = owner; | |
| 46 SetPaintToLayer(); | |
| 47 layer()->SetFillsBoundsOpaquely(false); | |
| 48 } | |
| 49 | |
| 50 void IconLabelBubbleView::SeparatorView::OnPaint(gfx::Canvas* canvas) { | |
| 51 if (!owner_->ShouldShowLabel()) | |
| 52 return; | |
| 53 | |
| 54 const SkColor plain_text_color = owner_->GetNativeTheme()->GetSystemColor( | |
| 55 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
| 56 const SkColor separator_color = SkColorSetA( | |
| 57 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
| 58 | |
| 59 gfx::Rect bounds(owner_->label()->bounds()); | |
| 60 const int kSeparatorHeight = 16; | |
| 61 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); | |
| 62 bounds.set_width(bounds.width() + kSpaceBesideSeparator); | |
| 63 canvas->Draw1pxLine(gfx::PointF(bounds.top_right()), | |
| 64 gfx::PointF(bounds.bottom_right()), separator_color); | |
| 65 } | |
| 66 | |
| 67 bool IconLabelBubbleView::SeparatorView::CanProcessEventsWithinSubtree() const { | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 void IconLabelBubbleView::SeparatorView::UpdateOpacity() { | |
| 72 views::InkDrop* ink_drop = owner_->GetInkDrop(); | |
| 73 DCHECK(ink_drop); | |
| 74 | |
| 75 // If an inkdrop highlight or ripple is animating in or visible, the | |
| 76 // separator should fade out. | |
| 77 views::InkDropState state = ink_drop->GetTargetInkDropState(); | |
| 78 float opacity = 0.0f; | |
| 79 float duration = kFadeOutDurationMs; | |
| 80 if (!ink_drop->IsHighlightFadingInOrVisible() && | |
| 81 (state == views::InkDropState::HIDDEN || | |
| 82 state == views::InkDropState::ACTION_TRIGGERED || | |
| 83 state == views::InkDropState::DEACTIVATED)) { | |
| 84 opacity = 1.0f; | |
| 85 duration = kFadeInDurationMs; | |
| 86 } | |
| 87 | |
| 88 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator()); | |
| 89 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(duration)); | |
| 90 animation.SetTweenType(gfx::Tween::Type::EASE_IN); | |
| 91 layer()->SetOpacity(opacity); | |
| 92 } | |
| 93 | |
| 94 ////////////////////////////////////////////////////////////////// | |
| 95 // IconLabelBubbleView class | |
| 96 | |
| 27 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, | 97 IconLabelBubbleView::IconLabelBubbleView(const gfx::FontList& font_list, |
| 28 bool elide_in_middle) | 98 bool elide_in_middle) |
| 29 : image_(new views::ImageView()), | 99 : CustomButton(this), |
| 30 label_(new views::Label(base::string16(), {font_list})) { | 100 image_(new views::ImageView()), |
| 101 label_(new views::Label(base::string16(), {font_list})), | |
| 102 ink_drop_container_(new views::InkDropContainerView()), | |
| 103 suppress_button_action_(false) { | |
| 31 // Disable separate hit testing for |image_|. This prevents views treating | 104 // Disable separate hit testing for |image_|. This prevents views treating |
| 32 // |image_| as a separate mouse hover region from |this|. | 105 // |image_| as a separate mouse hover region from |this|. |
| 33 image_->set_can_process_events_within_subtree(false); | 106 image_->set_can_process_events_within_subtree(false); |
| 34 image_->SetBorder(views::CreateEmptyBorder( | 107 image_->SetBorder(views::CreateEmptyBorder( |
| 35 gfx::Insets(LocationBarView::kIconInteriorPadding))); | 108 gfx::Insets(LocationBarView::kIconInteriorPadding))); |
| 36 AddChildView(image_); | 109 AddChildView(image_); |
| 37 | 110 |
| 38 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 111 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 39 | 112 |
| 40 if (elide_in_middle) | 113 if (elide_in_middle) |
| 41 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); | 114 label_->SetElideBehavior(gfx::ELIDE_MIDDLE); |
| 42 AddChildView(label_); | 115 AddChildView(label_); |
| 43 | 116 |
| 117 separator_view_.reset(new SeparatorView(this)); | |
| 118 AddChildView(separator_view_.get()); | |
| 119 | |
| 120 AddChildView(ink_drop_container_); | |
| 121 ink_drop_container_->SetVisible(false); | |
| 122 | |
| 44 // Bubbles are given the full internal height of the location bar so that all | 123 // Bubbles are given the full internal height of the location bar so that all |
| 45 // child views in the location bar have the same height. The visible height of | 124 // child views in the location bar have the same height. The visible height of |
| 46 // the bubble should be smaller, so use an empty border to shrink down the | 125 // the bubble should be smaller, so use an empty border to shrink down the |
| 47 // content bounds so the background gets painted correctly. | 126 // content bounds so the background gets painted correctly. |
| 48 SetBorder(views::CreateEmptyBorder( | 127 SetBorder(views::CreateEmptyBorder( |
| 49 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); | 128 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); |
| 50 | 129 |
| 51 // Flip the canvas in RTL so the separator is drawn on the correct side. | 130 // Flip the canvas in RTL so the separator is drawn on the correct side. |
| 52 EnableCanvasFlippingForRTLUI(true); | 131 EnableCanvasFlippingForRTLUI(true); |
| 53 } | 132 } |
| 54 | 133 |
| 55 IconLabelBubbleView::~IconLabelBubbleView() { | 134 IconLabelBubbleView::~IconLabelBubbleView() { |
| 56 } | 135 } |
| 57 | 136 |
| 137 void IconLabelBubbleView::ButtonPressed(Button* sender, | |
| 138 const ui::Event& event) { | |
| 139 if (!suppress_button_action_) | |
| 140 OnActivate(event); | |
| 141 } | |
| 142 | |
| 143 void IconLabelBubbleView::InkDropAnimationStarted() { | |
| 144 separator_view_->UpdateOpacity(); | |
| 145 } | |
| 146 | |
| 58 void IconLabelBubbleView::SetLabel(const base::string16& label) { | 147 void IconLabelBubbleView::SetLabel(const base::string16& label) { |
| 59 label_->SetText(label); | 148 label_->SetText(label); |
| 60 } | 149 } |
| 61 | 150 |
| 62 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 151 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
| 63 image_->SetImage(image_skia); | 152 image_->SetImage(image_skia); |
| 64 } | 153 } |
| 65 | 154 |
| 66 bool IconLabelBubbleView::ShouldShowLabel() const { | 155 bool IconLabelBubbleView::ShouldShowLabel() const { |
| 67 return label_->visible() && !label_->text().empty(); | 156 return label_->visible() && !label_->text().empty(); |
| 68 } | 157 } |
| 69 | 158 |
| 70 double IconLabelBubbleView::WidthMultiplier() const { | 159 double IconLabelBubbleView::WidthMultiplier() const { |
| 71 return 1.0; | 160 return 1.0; |
| 72 } | 161 } |
| 73 | 162 |
| 74 bool IconLabelBubbleView::IsShrinking() const { | 163 bool IconLabelBubbleView::IsShrinking() const { |
| 75 return false; | 164 return false; |
| 76 } | 165 } |
| 77 | 166 |
| 78 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { | 167 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { |
| 79 return false; | 168 return false; |
| 80 } | 169 } |
| 81 | 170 |
| 171 bool IconLabelBubbleView::IsBubbleShown() const { | |
| 172 return false; | |
| 173 } | |
| 174 | |
| 82 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 175 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 83 // Height will be ignored by the LocationBarView. | 176 // Height will be ignored by the LocationBarView. |
| 84 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 177 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 85 } | 178 } |
| 86 | 179 |
| 87 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { | 180 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { |
| 88 if (event.key_code() == ui::VKEY_RETURN) | 181 if (event.key_code() == ui::VKEY_RETURN) |
| 89 return OnActivate(event); | 182 return OnActivate(event); |
| 90 return false; | 183 return false; |
| 91 } | 184 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 113 image_->SetBounds(0, 0, image_width, height()); | 206 image_->SetBounds(0, 0, image_width, height()); |
| 114 | 207 |
| 115 // Compute the label bounds. The label gets whatever size is left over after | 208 // Compute the label bounds. The label gets whatever size is left over after |
| 116 // accounting for the preferred image width and padding amounts. Note that if | 209 // accounting for the preferred image width and padding amounts. Note that if |
| 117 // the label has zero size it doesn't actually matter what we compute its X | 210 // the label has zero size it doesn't actually matter what we compute its X |
| 118 // value to be, since it won't be visible. | 211 // value to be, since it won't be visible. |
| 119 const int label_x = image_->bounds().right() + GetInternalSpacing(); | 212 const int label_x = image_->bounds().right() + GetInternalSpacing(); |
| 120 const int label_width = std::max( | 213 const int label_width = std::max( |
| 121 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); | 214 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); |
| 122 label_->SetBounds(label_x, 0, label_width, height()); | 215 label_->SetBounds(label_x, 0, label_width, height()); |
| 216 separator_view_->SetBoundsRect(GetLocalBounds()); | |
| 217 | |
| 218 gfx::Rect ink_drop_bounds = GetLocalBounds(); | |
| 219 if (ShouldShowLabel()) { | |
| 220 ink_drop_bounds.set_width(ink_drop_bounds.width() - | |
|
bruthig
2017/03/24 20:48:03
Is this the correct layout for RTL layouts?
spqchan
2017/03/27 23:41:51
Yep
| |
| 221 GetPostSeparatorPadding()); | |
| 222 } | |
| 223 | |
| 224 ink_drop_container_->SetBoundsRect(ink_drop_bounds); | |
| 225 } | |
| 226 | |
| 227 void IconLabelBubbleView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
| 228 if (ink_drop_mask_) | |
| 229 ink_drop_mask_->UpdateLayerSize(size()); | |
| 230 | |
| 231 InkDropHostView::OnBoundsChanged(previous_bounds); | |
| 232 } | |
| 233 | |
| 234 bool IconLabelBubbleView::OnMousePressed(const ui::MouseEvent& event) { | |
| 235 suppress_button_action_ = IsBubbleShown(); | |
| 236 return CustomButton::OnMousePressed(event); | |
| 123 } | 237 } |
| 124 | 238 |
| 125 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 239 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 126 label_->GetAccessibleNodeData(node_data); | 240 label_->GetAccessibleNodeData(node_data); |
| 127 } | 241 } |
| 128 | 242 |
| 129 void IconLabelBubbleView::OnNativeThemeChanged( | 243 void IconLabelBubbleView::OnNativeThemeChanged( |
| 130 const ui::NativeTheme* native_theme) { | 244 const ui::NativeTheme* native_theme) { |
| 131 label_->SetEnabledColor(GetTextColor()); | 245 label_->SetEnabledColor(GetTextColor()); |
| 132 label_->SetBackgroundColor(GetParentBackgroundColor()); | 246 label_->SetBackgroundColor(GetParentBackgroundColor()); |
| 133 SchedulePaint(); | 247 SchedulePaint(); |
| 134 } | 248 } |
| 135 | 249 |
| 136 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | 250 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
| 137 image()->SetPaintToLayer(); | 251 ink_drop_mask_ = CreateInkDropMask(); |
| 138 image()->layer()->SetFillsBoundsOpaquely(false); | 252 if (ink_drop_mask_) |
| 139 InkDropHostView::AddInkDropLayer(ink_drop_layer); | 253 ink_drop_layer->SetMaskLayer(ink_drop_mask_->layer()); |
| 254 ink_drop_container_->AddInkDropLayer(ink_drop_layer); | |
| 140 } | 255 } |
| 141 | 256 |
| 142 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | 257 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
| 143 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); | 258 ink_drop_container_->RemoveInkDropLayer(ink_drop_layer); |
| 144 image()->DestroyLayer(); | 259 ink_drop_mask_.reset(); |
| 260 } | |
| 261 | |
| 262 std::unique_ptr<views::InkDrop> IconLabelBubbleView::CreateInkDrop() { | |
| 263 std::unique_ptr<views::InkDropImpl> ink_drop = | |
| 264 CreateDefaultFloodFillInkDropImpl(); | |
| 265 ink_drop->SetShowHighlightOnFocus(true); | |
| 266 ink_drop->AddObserver(this); | |
| 267 return std::move(ink_drop); | |
| 145 } | 268 } |
| 146 | 269 |
| 147 std::unique_ptr<views::InkDropHighlight> | 270 std::unique_ptr<views::InkDropHighlight> |
| 148 IconLabelBubbleView::CreateInkDropHighlight() const { | 271 IconLabelBubbleView::CreateInkDropHighlight() const { |
| 149 // Only show a highlight effect when the label is empty/invisible. | 272 return InkDropHostView::CreateDefaultInkDropHighlight( |
| 150 return label()->visible() ? nullptr | 273 gfx::RectF(ink_drop_container_->bounds()).CenterPoint(), |
| 151 : InkDropHostView::CreateInkDropHighlight(); | 274 ink_drop_container_->size()); |
| 275 } | |
| 276 | |
| 277 std::unique_ptr<views::InkDropRipple> IconLabelBubbleView::CreateInkDropRipple() | |
| 278 const { | |
| 279 return base::MakeUnique<views::FloodFillInkDropRipple>( | |
| 280 ink_drop_container_->size(), ink_drop_container_->bounds().CenterPoint(), | |
| 281 GetInkDropBaseColor(), ink_drop_visible_opacity()); | |
| 282 } | |
| 283 | |
| 284 std::unique_ptr<views::InkDropMask> IconLabelBubbleView::CreateInkDropMask() | |
| 285 const { | |
| 286 return base::MakeUnique<views::RoundRectInkDropMask>(size(), gfx::Insets(), | |
| 287 kInkDropMaskRadius); | |
| 152 } | 288 } |
| 153 | 289 |
| 154 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { | 290 SkColor IconLabelBubbleView::GetInkDropBaseColor() const { |
| 155 return color_utils::DeriveDefaultIconColor(GetTextColor()); | 291 return color_utils::DeriveDefaultIconColor(GetNativeTheme()->GetSystemColor( |
| 292 ui::NativeTheme::kColorId_TextfieldDefaultColor)); | |
| 293 } | |
| 294 | |
| 295 bool IconLabelBubbleView::IsTriggerableEvent(const ui::Event& event) { | |
| 296 return !IsBubbleShown(); | |
| 297 } | |
| 298 | |
| 299 void IconLabelBubbleView::OnClickCanceled(const ui::Event& event) { | |
| 300 // Overriden to prevent CustomButton from hiding the ink drop when the click | |
| 301 // is cancelled. The click might be cancelled because the bubble is still | |
| 302 // opened. In this case, the ink drop state should still be active. | |
| 303 } | |
| 304 | |
| 305 void IconLabelBubbleView::OnWidgetDestroying(views::Widget* widget) { | |
| 306 widget->RemoveObserver(this); | |
| 307 } | |
| 308 | |
| 309 void IconLabelBubbleView::OnWidgetVisibilityChanged(views::Widget* widget, | |
| 310 bool visible) { | |
| 311 // |widget| is a bubble that has just got shown / hidden. | |
| 312 if (!visible) | |
| 313 AnimateInkDrop(views::InkDropState::HIDDEN, nullptr /* event */); | |
| 156 } | 314 } |
| 157 | 315 |
| 158 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 316 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 159 return GetNativeTheme()->GetSystemColor( | 317 return GetNativeTheme()->GetSystemColor( |
| 160 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 318 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 161 } | 319 } |
| 162 | 320 |
| 163 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 321 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 164 gfx::Size size(image_->GetPreferredSize()); | 322 gfx::Size size(image_->GetPreferredSize()); |
| 165 const bool shrinking = IsShrinking(); | 323 const bool shrinking = IsShrinking(); |
| 166 // Animation continues for the last few pixels even after the label is not | 324 // Animation continues for the last few pixels even after the label is not |
| 167 // visible in order to slide the icon into its final position. Therefore it | 325 // visible in order to slide the icon into its final position. Therefore it |
| 168 // is necessary to animate |total_width| even when the background is hidden | 326 // is necessary to animate |total_width| even when the background is hidden |
| 169 // as long as the animation is still shrinking. | 327 // as long as the animation is still shrinking. |
| 170 if (ShouldShowLabel() || shrinking) { | 328 if (ShouldShowLabel() || shrinking) { |
| 329 // |multiplier| grows from zero to one, stays equal to one and then shrinks | |
| 330 // to zero again. The view width should correspondingly grow from zero to | |
| 331 // fully showing both label and icon, stay there, then shrink to just large | |
| 332 // enough to show the icon. We don't want to shrink all the way back to | |
| 333 // zero, since this would mean the view would completely disappear and then | |
| 334 // pop back to an icon after the animation finishes. | |
| 335 const int current_width = | |
| 336 WidthMultiplier() * GetMaxSizeForLabelWidth(label_width).width(); | |
| 337 size.set_width(shrinking ? std::max(current_width, size.width()) | |
| 338 : current_width); | |
| 339 } | |
| 340 return size; | |
| 341 } | |
| 342 | |
| 343 gfx::Size IconLabelBubbleView::GetMaxSizeForLabelWidth(int label_width) const { | |
| 344 gfx::Size size(image_->GetPreferredSize()); | |
| 345 if (ShouldShowLabel() || IsShrinking()) { | |
| 171 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For | 346 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For |
| 172 // higher scale factors, we simply take the separator px out of the | 347 // higher scale factors, we simply take the separator px out of the |
| 173 // kSpaceBesideSeparator region before the separator, as that results in a | 348 // kSpaceBesideSeparator region before the separator, as that results in a |
| 174 // width closer to the desired gap than if we added a whole DIP for the | 349 // width closer to the desired gap than if we added a whole DIP for the |
| 175 // separator px. (For scale 2, the two methods have equal error: 1 px.) | 350 // separator px. (For scale 2, the two methods have equal error: 1 px.) |
| 176 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; | 351 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; |
| 177 const int post_label_width = | 352 const int post_label_width = |
| 178 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); | 353 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); |
| 179 | 354 size.Enlarge(GetInternalSpacing() + label_width + post_label_width, 0); |
| 180 // |multiplier| grows from zero to one, stays equal to one and then shrinks | |
| 181 // to zero again. The view width should correspondingly grow from zero to | |
| 182 // fully showing both label and icon, stay there, then shrink to just large | |
| 183 // enough to show the icon. We don't want to shrink all the way back to | |
| 184 // zero, since this would mean the view would completely disappear and then | |
| 185 // pop back to an icon after the animation finishes. | |
| 186 const int max_width = | |
| 187 size.width() + GetInternalSpacing() + label_width + post_label_width; | |
| 188 const int current_width = WidthMultiplier() * max_width; | |
| 189 size.set_width(shrinking ? std::max(current_width, size.width()) | |
| 190 : current_width); | |
| 191 } | 355 } |
| 192 return size; | 356 return size; |
| 193 } | 357 } |
| 194 | 358 |
| 195 int IconLabelBubbleView::GetInternalSpacing() const { | 359 int IconLabelBubbleView::GetInternalSpacing() const { |
| 196 return image_->GetPreferredSize().IsEmpty() | 360 return image_->GetPreferredSize().IsEmpty() |
| 197 ? 0 | 361 ? 0 |
| 198 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); | 362 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); |
| 199 } | 363 } |
| 200 | 364 |
| 201 int IconLabelBubbleView::GetPostSeparatorPadding() const { | 365 int IconLabelBubbleView::GetPostSeparatorPadding() const { |
| 202 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. | 366 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. |
| 203 return kSpaceBesideSeparator - | 367 return kSpaceBesideSeparator - |
| 204 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - | 368 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - |
| 205 next_element_interior_padding_; | 369 next_element_interior_padding_; |
| 206 } | 370 } |
| 207 | 371 |
| 208 float IconLabelBubbleView::GetScaleFactor() const { | 372 float IconLabelBubbleView::GetScaleFactor() const { |
| 209 const views::Widget* widget = GetWidget(); | 373 const views::Widget* widget = GetWidget(); |
| 210 // There may be no widget in tests, and in ash there may be no compositor if | 374 // There may be no widget in tests, and in ash there may be no compositor if |
| 211 // the native view of the Widget doesn't have a parent. | 375 // the native view of the Widget doesn't have a parent. |
| 212 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 376 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
| 213 return compositor ? compositor->device_scale_factor() : 1.0f; | 377 return compositor ? compositor->device_scale_factor() : 1.0f; |
| 214 } | 378 } |
| 215 | 379 |
| 216 const char* IconLabelBubbleView::GetClassName() const { | 380 const char* IconLabelBubbleView::GetClassName() const { |
| 217 return "IconLabelBubbleView"; | 381 return "IconLabelBubbleView"; |
| 218 } | 382 } |
| 219 | |
| 220 void IconLabelBubbleView::OnPaint(gfx::Canvas* canvas) { | |
| 221 if (!ShouldShowLabel()) | |
| 222 return; | |
| 223 | |
| 224 const SkColor plain_text_color = GetNativeTheme()->GetSystemColor( | |
| 225 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
| 226 const SkColor separator_color = SkColorSetA( | |
| 227 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
| 228 | |
| 229 gfx::Rect bounds(label_->bounds()); | |
| 230 const int kSeparatorHeight = 16; | |
| 231 bounds.Inset(0, (bounds.height() - kSeparatorHeight) / 2); | |
| 232 bounds.set_width(bounds.width() + kSpaceBesideSeparator); | |
| 233 canvas->Draw1pxLine(gfx::PointF(bounds.top_right()), | |
| 234 gfx::PointF(bounds.bottom_right()), separator_color); | |
| 235 } | |
| OLD | NEW |