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_ripple.h" | |
| 16 #include "ui/views/border.h" | 21 #include "ui/views/border.h" |
| 17 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
| 18 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
| 19 | 24 |
| 20 namespace { | 25 namespace { |
| 21 | 26 |
| 22 // Amount of space on either side of the separator that appears after the label. | 27 // Amount of space on either side of the separator that appears after the label. |
| 23 constexpr int kSpaceBesideSeparator = 8; | 28 constexpr int kSpaceBesideSeparator = 8; |
| 24 | 29 |
| 30 // The length of the separator's fade animation. These values are empirical. | |
| 31 constexpr int kFadeInDurationMs = 250; | |
| 32 constexpr int kFadeOutDurationMs = 175; | |
| 33 | |
| 25 } // namespace | 34 } // namespace |
| 26 | 35 |
| 36 ////////////////////////////////////////////////////////////////// | |
| 37 // SeparatorView class | |
| 38 | |
| 39 IconLabelBubbleView::SeparatorView::SeparatorView(IconLabelBubbleView* owner) { | |
| 40 DCHECK(owner); | |
| 41 owner_ = owner; | |
| 42 } | |
| 43 | |
| 44 void IconLabelBubbleView::SeparatorView::OnPaint(gfx::Canvas* canvas) { | |
| 45 const SkColor plain_text_color = owner_->GetNativeTheme()->GetSystemColor( | |
| 46 ui::NativeTheme::kColorId_TextfieldDefaultColor); | |
| 47 const SkColor separator_color = SkColorSetA( | |
| 48 plain_text_color, color_utils::IsDark(plain_text_color) ? 0x59 : 0xCC); | |
| 49 canvas->Draw1pxLine(gfx::PointF(GetLocalBounds().origin()), | |
| 50 gfx::PointF(GetLocalBounds().bottom_left()), | |
| 51 separator_color); | |
| 52 } | |
| 53 | |
| 54 bool IconLabelBubbleView::SeparatorView::CanProcessEventsWithinSubtree() const { | |
| 55 return false; | |
|
sky
2017/05/12 13:11:47
Why do you need to override this? It doesn't seem
spqchan
2017/05/18 01:13:04
Whoops, sorry I missed your earlier comment. While
sky
2017/05/18 23:28:38
That should only matter if child views are added a
spqchan
2017/05/18 23:58:08
IconLabelBubbleView adds the child views (includin
| |
| 56 } | |
| 57 | |
| 58 void IconLabelBubbleView::SeparatorView::OnImplicitAnimationsCompleted() { | |
| 59 if (layer() && layer()->opacity() == 1.0f) | |
| 60 DestroyLayer(); | |
| 61 } | |
| 62 | |
| 63 void IconLabelBubbleView::SeparatorView::UpdateOpacity() { | |
| 64 if (!visible()) | |
| 65 return; | |
| 66 | |
| 67 views::InkDrop* ink_drop = owner_->GetInkDrop(); | |
| 68 DCHECK(ink_drop); | |
| 69 | |
| 70 // If an inkdrop highlight or ripple is animating in or visible, the | |
| 71 // separator should fade out. | |
| 72 views::InkDropState state = ink_drop->GetTargetInkDropState(); | |
| 73 float opacity = 0.0f; | |
| 74 float duration = kFadeOutDurationMs; | |
| 75 if (!ink_drop->IsHighlightFadingInOrVisible() && | |
| 76 (state == views::InkDropState::HIDDEN || | |
| 77 state == views::InkDropState::ACTION_TRIGGERED || | |
| 78 state == views::InkDropState::DEACTIVATED)) { | |
| 79 opacity = 1.0f; | |
| 80 duration = kFadeInDurationMs; | |
| 81 } | |
| 82 | |
| 83 if (!layer()) | |
| 84 SetPaintToLayer(); | |
| 85 layer()->SetFillsBoundsOpaquely(false); | |
| 86 | |
| 87 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator()); | |
| 88 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(duration)); | |
| 89 animation.SetTweenType(gfx::Tween::Type::EASE_IN); | |
| 90 animation.AddObserver(this); | |
| 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(nullptr), |
| 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_release_(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 separator_view_->SetVisible(ShouldShowLabel()); | |
| 119 AddChildView(separator_view_.get()); | |
| 120 | |
| 121 AddChildView(ink_drop_container_); | |
| 122 ink_drop_container_->SetVisible(false); | |
| 123 | |
| 44 // Bubbles are given the full internal height of the location bar so that all | 124 // 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 | 125 // 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 | 126 // the bubble should be smaller, so use an empty border to shrink down the |
| 47 // content bounds so the background gets painted correctly. | 127 // content bounds so the background gets painted correctly. |
| 48 SetBorder(views::CreateEmptyBorder( | 128 SetBorder(views::CreateEmptyBorder( |
| 49 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); | 129 gfx::Insets(LocationBarView::kBubbleVerticalPadding, 0))); |
| 50 | 130 |
| 51 // Flip the canvas in RTL so the separator is drawn on the correct side. | 131 // Flip the canvas in RTL so the separator is drawn on the correct side. |
| 52 EnableCanvasFlippingForRTLUI(true); | 132 separator_view_->EnableCanvasFlippingForRTLUI(true); |
| 53 } | 133 } |
| 54 | 134 |
| 55 IconLabelBubbleView::~IconLabelBubbleView() { | 135 IconLabelBubbleView::~IconLabelBubbleView() { |
| 56 } | 136 } |
| 57 | 137 |
| 138 void IconLabelBubbleView::InkDropAnimationStarted() { | |
| 139 separator_view_->UpdateOpacity(); | |
| 140 } | |
| 141 | |
| 58 void IconLabelBubbleView::SetLabel(const base::string16& label) { | 142 void IconLabelBubbleView::SetLabel(const base::string16& label) { |
| 59 label_->SetText(label); | 143 label_->SetText(label); |
| 144 separator_view_->SetVisible(ShouldShowLabel()); | |
| 60 } | 145 } |
| 61 | 146 |
| 62 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { | 147 void IconLabelBubbleView::SetImage(const gfx::ImageSkia& image_skia) { |
| 63 image_->SetImage(image_skia); | 148 image_->SetImage(image_skia); |
| 64 } | 149 } |
| 65 | 150 |
| 66 bool IconLabelBubbleView::ShouldShowLabel() const { | 151 bool IconLabelBubbleView::ShouldShowLabel() const { |
| 67 return label_->visible() && !label_->text().empty(); | 152 return label_->visible() && !label_->text().empty(); |
| 68 } | 153 } |
| 69 | 154 |
| 70 double IconLabelBubbleView::WidthMultiplier() const { | 155 double IconLabelBubbleView::WidthMultiplier() const { |
| 71 return 1.0; | 156 return 1.0; |
| 72 } | 157 } |
| 73 | 158 |
| 74 bool IconLabelBubbleView::IsShrinking() const { | 159 bool IconLabelBubbleView::IsShrinking() const { |
| 75 return false; | 160 return false; |
| 76 } | 161 } |
| 77 | 162 |
| 78 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { | 163 bool IconLabelBubbleView::ShowBubble(const ui::Event& event) { |
| 79 return false; | 164 return false; |
| 80 } | 165 } |
| 81 | 166 |
| 167 bool IconLabelBubbleView::IsBubbleShown() const { | |
| 168 return false; | |
| 169 } | |
| 170 | |
| 82 gfx::Size IconLabelBubbleView::GetPreferredSize() const { | 171 gfx::Size IconLabelBubbleView::GetPreferredSize() const { |
| 83 // Height will be ignored by the LocationBarView. | 172 // Height will be ignored by the LocationBarView. |
| 84 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); | 173 return GetSizeForLabelWidth(label_->GetPreferredSize().width()); |
| 85 } | 174 } |
| 86 | 175 |
| 87 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { | 176 bool IconLabelBubbleView::OnKeyPressed(const ui::KeyEvent& event) { |
|
sky
2017/05/12 13:11:47
Now that IconLabelBubbleView subclasses CustomButt
spqchan
2017/05/18 01:13:05
Done.
| |
| 88 if (event.key_code() == ui::VKEY_RETURN) | 177 if (event.key_code() == ui::VKEY_RETURN) |
| 89 return OnActivate(event); | 178 return OnActivate(event); |
| 90 return false; | 179 return false; |
| 91 } | 180 } |
| 92 | 181 |
| 93 bool IconLabelBubbleView::OnKeyReleased(const ui::KeyEvent& event) { | 182 bool IconLabelBubbleView::OnKeyReleased(const ui::KeyEvent& event) { |
| 94 if (event.key_code() == ui::VKEY_SPACE) | 183 if (event.key_code() == ui::VKEY_SPACE) |
| 95 return OnActivate(event); | 184 return OnActivate(event); |
| 96 return false; | 185 return false; |
| 97 } | 186 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 112 } | 201 } |
| 113 image_->SetBounds(0, 0, image_width, height()); | 202 image_->SetBounds(0, 0, image_width, height()); |
| 114 | 203 |
| 115 // Compute the label bounds. The label gets whatever size is left over after | 204 // 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 | 205 // 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 | 206 // 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. | 207 // value to be, since it won't be visible. |
| 119 const int label_x = image_->bounds().right() + GetInternalSpacing(); | 208 const int label_x = image_->bounds().right() + GetInternalSpacing(); |
| 120 const int label_width = std::max( | 209 const int label_width = std::max( |
| 121 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); | 210 0, width() - label_x - bubble_trailing_padding - kSpaceBesideSeparator); |
| 122 label_->SetBounds(label_x, 0, label_width, height()); | 211 const gfx::Rect label_bounds(label_x, 0, label_width, height()); |
| 212 label_->SetBoundsRect(label_bounds); | |
| 213 | |
| 214 const int kSeparatorHeight = 16; | |
|
sky
2017/05/12 13:11:47
Did you make sure all this looks right when RTL?
spqchan
2017/05/18 01:13:04
Good point, I changed the separator's position a b
| |
| 215 gfx::Rect separator_bounds(label_bounds); | |
| 216 separator_bounds.Inset(0, (separator_bounds.height() - kSeparatorHeight) / 2); | |
| 217 | |
| 218 separator_view_->SetBounds( | |
| 219 label_bounds.right() + kSpaceBesideSeparator - 1.0f, separator_bounds.y(), | |
| 220 1.0f, kSeparatorHeight); | |
| 221 | |
| 222 gfx::Rect ink_drop_bounds = GetLocalBounds(); | |
| 223 if (ShouldShowLabel()) { | |
| 224 ink_drop_bounds.set_width(ink_drop_bounds.width() - | |
| 225 GetPostSeparatorPadding()); | |
| 226 } | |
| 227 | |
| 228 ink_drop_container_->SetBoundsRect(ink_drop_bounds); | |
| 229 } | |
| 230 | |
| 231 bool IconLabelBubbleView::OnMousePressed(const ui::MouseEvent& event) { | |
| 232 suppress_button_release_ = IsBubbleShown(); | |
| 233 return CustomButton::OnMousePressed(event); | |
| 123 } | 234 } |
| 124 | 235 |
| 125 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 236 void IconLabelBubbleView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 126 label_->GetAccessibleNodeData(node_data); | 237 label_->GetAccessibleNodeData(node_data); |
| 127 } | 238 } |
| 128 | 239 |
| 240 void IconLabelBubbleView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
| 241 if (!IsMouseHovered()) { | |
| 242 GetInkDrop()->AnimateToState(views::InkDropState::HIDDEN); | |
| 243 GetInkDrop()->SetHovered(false); | |
| 244 } | |
| 245 CustomButton::OnBoundsChanged(previous_bounds); | |
| 246 } | |
| 247 | |
| 129 void IconLabelBubbleView::OnNativeThemeChanged( | 248 void IconLabelBubbleView::OnNativeThemeChanged( |
| 130 const ui::NativeTheme* native_theme) { | 249 const ui::NativeTheme* native_theme) { |
| 131 label_->SetEnabledColor(GetTextColor()); | 250 label_->SetEnabledColor(GetTextColor()); |
| 132 label_->SetBackgroundColor(GetParentBackgroundColor()); | 251 label_->SetBackgroundColor(GetParentBackgroundColor()); |
| 133 SchedulePaint(); | 252 SchedulePaint(); |
| 134 } | 253 } |
| 135 | 254 |
| 136 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { | 255 void IconLabelBubbleView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
| 137 image()->SetPaintToLayer(); | 256 ink_drop_container_->AddInkDropLayer(ink_drop_layer); |
| 138 image()->layer()->SetFillsBoundsOpaquely(false); | |
| 139 InkDropHostView::AddInkDropLayer(ink_drop_layer); | |
| 140 } | 257 } |
| 141 | 258 |
| 142 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { | 259 void IconLabelBubbleView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
| 143 InkDropHostView::RemoveInkDropLayer(ink_drop_layer); | 260 ink_drop_container_->RemoveInkDropLayer(ink_drop_layer); |
| 144 image()->DestroyLayer(); | 261 } |
| 262 | |
| 263 std::unique_ptr<views::InkDrop> IconLabelBubbleView::CreateInkDrop() { | |
| 264 std::unique_ptr<views::InkDropImpl> ink_drop = | |
| 265 CreateDefaultFloodFillInkDropImpl(); | |
| 266 ink_drop->SetShowHighlightOnFocus(true); | |
| 267 ink_drop->AddObserver(this); | |
| 268 return std::move(ink_drop); | |
| 145 } | 269 } |
| 146 | 270 |
| 147 std::unique_ptr<views::InkDropHighlight> | 271 std::unique_ptr<views::InkDropHighlight> |
| 148 IconLabelBubbleView::CreateInkDropHighlight() const { | 272 IconLabelBubbleView::CreateInkDropHighlight() const { |
| 149 // Only show a highlight effect when the label is empty/invisible. | 273 return InkDropHostView::CreateDefaultInkDropHighlight( |
| 150 return label()->visible() ? nullptr | 274 gfx::RectF(ink_drop_container_->bounds()).CenterPoint(), |
| 151 : InkDropHostView::CreateInkDropHighlight(); | 275 ink_drop_container_->size()); |
| 276 } | |
| 277 | |
| 278 std::unique_ptr<views::InkDropRipple> IconLabelBubbleView::CreateInkDropRipple() | |
| 279 const { | |
| 280 gfx::Point center_point = GetInkDropCenterBasedOnLastEvent(); | |
| 281 View::ConvertPointToTarget(this, ink_drop_container_, ¢er_point); | |
| 282 center_point.SetToMax(ink_drop_container_->origin()); | |
| 283 center_point.SetToMin(ink_drop_container_->bounds().bottom_right()); | |
| 284 | |
| 285 return base::MakeUnique<views::FloodFillInkDropRipple>( | |
| 286 ink_drop_container_->size(), center_point, GetInkDropBaseColor(), | |
| 287 ink_drop_visible_opacity()); | |
| 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 if (event.IsMouseEvent()) | |
| 297 return !IsBubbleShown() && !suppress_button_release_; | |
| 298 | |
| 299 return true; | |
| 300 } | |
| 301 | |
| 302 bool IconLabelBubbleView::ShouldUpdateInkDropOnClickCanceled() const { | |
| 303 // The click might be cancelled because the bubble is still opened. In this | |
| 304 // case, the ink drop state should not be hidden by CustomButton. | |
| 305 return false; | |
| 306 } | |
| 307 | |
| 308 void IconLabelBubbleView::NotifyClick(const ui::Event& event) { | |
| 309 CustomButton::NotifyClick(event); | |
| 310 OnActivate(event); | |
| 311 } | |
| 312 | |
| 313 void IconLabelBubbleView::OnWidgetDestroying(views::Widget* widget) { | |
| 314 widget->RemoveObserver(this); | |
| 315 } | |
| 316 | |
| 317 void IconLabelBubbleView::OnWidgetVisibilityChanged(views::Widget* widget, | |
| 318 bool visible) { | |
| 319 // |widget| is a bubble that has just got shown / hidden. | |
| 320 if (!visible) | |
| 321 AnimateInkDrop(views::InkDropState::HIDDEN, nullptr /* event */); | |
| 156 } | 322 } |
| 157 | 323 |
| 158 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { | 324 SkColor IconLabelBubbleView::GetParentBackgroundColor() const { |
| 159 return GetNativeTheme()->GetSystemColor( | 325 return GetNativeTheme()->GetSystemColor( |
| 160 ui::NativeTheme::kColorId_TextfieldDefaultBackground); | 326 ui::NativeTheme::kColorId_TextfieldDefaultBackground); |
| 161 } | 327 } |
| 162 | 328 |
| 163 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { | 329 gfx::Size IconLabelBubbleView::GetSizeForLabelWidth(int label_width) const { |
| 164 gfx::Size size(image_->GetPreferredSize()); | 330 gfx::Size size(image_->GetPreferredSize()); |
| 165 const bool shrinking = IsShrinking(); | 331 const bool shrinking = IsShrinking(); |
| 166 // Animation continues for the last few pixels even after the label is not | 332 // 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 | 333 // 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 | 334 // is necessary to animate |total_width| even when the background is hidden |
| 169 // as long as the animation is still shrinking. | 335 // as long as the animation is still shrinking. |
| 170 if (ShouldShowLabel() || shrinking) { | 336 if (ShouldShowLabel() || shrinking) { |
| 337 // |multiplier| grows from zero to one, stays equal to one and then shrinks | |
| 338 // to zero again. The view width should correspondingly grow from zero to | |
| 339 // fully showing both label and icon, stay there, then shrink to just large | |
| 340 // enough to show the icon. We don't want to shrink all the way back to | |
| 341 // zero, since this would mean the view would completely disappear and then | |
| 342 // pop back to an icon after the animation finishes. | |
| 343 const int current_width = | |
| 344 WidthMultiplier() * GetMaxSizeForLabelWidth(label_width).width(); | |
| 345 size.set_width(shrinking ? std::max(current_width, size.width()) | |
| 346 : current_width); | |
| 347 } | |
| 348 return size; | |
| 349 } | |
| 350 | |
| 351 gfx::Size IconLabelBubbleView::GetMaxSizeForLabelWidth(int label_width) const { | |
| 352 gfx::Size size(image_->GetPreferredSize()); | |
| 353 if (ShouldShowLabel() || IsShrinking()) { | |
| 171 // On scale factors < 2, we reserve 1 DIP for the 1 px separator. For | 354 // 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 | 355 // higher scale factors, we simply take the separator px out of the |
| 173 // kSpaceBesideSeparator region before the separator, as that results in a | 356 // 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 | 357 // 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.) | 358 // separator px. (For scale 2, the two methods have equal error: 1 px.) |
| 176 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; | 359 const int separator_width = (GetScaleFactor() >= 2) ? 0 : 1; |
| 177 const int post_label_width = | 360 const int post_label_width = |
| 178 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); | 361 (kSpaceBesideSeparator + separator_width + GetPostSeparatorPadding()); |
| 179 | 362 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 } | 363 } |
| 192 return size; | 364 return size; |
| 193 } | 365 } |
| 194 | 366 |
| 195 int IconLabelBubbleView::GetInternalSpacing() const { | 367 int IconLabelBubbleView::GetInternalSpacing() const { |
| 196 return image_->GetPreferredSize().IsEmpty() | 368 return image_->GetPreferredSize().IsEmpty() |
| 197 ? 0 | 369 ? 0 |
| 198 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); | 370 : GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); |
| 199 } | 371 } |
| 200 | 372 |
| 201 int IconLabelBubbleView::GetPostSeparatorPadding() const { | 373 int IconLabelBubbleView::GetPostSeparatorPadding() const { |
| 202 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. | 374 // The location bar will add LOCATION_BAR_ELEMENT_PADDING after us. |
| 203 return kSpaceBesideSeparator - | 375 return kSpaceBesideSeparator - |
| 204 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - | 376 GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) - |
| 205 next_element_interior_padding_; | 377 next_element_interior_padding_; |
| 206 } | 378 } |
| 207 | 379 |
| 208 float IconLabelBubbleView::GetScaleFactor() const { | 380 float IconLabelBubbleView::GetScaleFactor() const { |
| 209 const views::Widget* widget = GetWidget(); | 381 const views::Widget* widget = GetWidget(); |
| 210 // There may be no widget in tests, and in ash there may be no compositor if | 382 // 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. | 383 // the native view of the Widget doesn't have a parent. |
| 212 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; | 384 const ui::Compositor* compositor = widget ? widget->GetCompositor() : nullptr; |
| 213 return compositor ? compositor->device_scale_factor() : 1.0f; | 385 return compositor ? compositor->device_scale_factor() : 1.0f; |
| 214 } | 386 } |
| 215 | 387 |
| 388 bool IconLabelBubbleView::OnActivate(const ui::Event& event) { | |
| 389 if (ShowBubble(event)) { | |
| 390 AnimateInkDrop(views::InkDropState::ACTIVATED, nullptr); | |
| 391 return true; | |
| 392 } | |
| 393 | |
| 394 AnimateInkDrop(views::InkDropState::HIDDEN, nullptr); | |
| 395 return false; | |
| 396 } | |
| 397 | |
| 216 const char* IconLabelBubbleView::GetClassName() const { | 398 const char* IconLabelBubbleView::GetClassName() const { |
| 217 return "IconLabelBubbleView"; | 399 return "IconLabelBubbleView"; |
| 218 } | 400 } |
| 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 |