| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/bubble_icon_view.h" | 5 #include "chrome/browser/ui/views/location_bar/bubble_icon_view.h" |
| 6 | 6 |
| 7 #include "chrome/browser/command_updater.h" | 7 #include "chrome/browser/command_updater.h" |
| 8 #include "ui/accessibility/ax_view_state.h" | 8 #include "ui/accessibility/ax_view_state.h" |
| 9 #include "ui/base/material_design/material_design_controller.h" | 9 #include "ui/base/material_design/material_design_controller.h" |
| 10 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
| 11 #include "ui/gfx/color_palette.h" | 11 #include "ui/gfx/color_palette.h" |
| 12 #include "ui/gfx/color_utils.h" | 12 #include "ui/gfx/color_utils.h" |
| 13 #include "ui/gfx/paint_vector_icon.h" | 13 #include "ui/gfx/paint_vector_icon.h" |
| 14 #include "ui/native_theme/native_theme.h" | 14 #include "ui/native_theme/native_theme.h" |
| 15 #include "ui/views/animation/button_ink_drop_delegate.h" |
| 15 #include "ui/views/bubble/bubble_delegate.h" | 16 #include "ui/views/bubble/bubble_delegate.h" |
| 16 | 17 |
| 17 BubbleIconView::BubbleIconView(CommandUpdater* command_updater, int command_id) | 18 BubbleIconView::BubbleIconView(CommandUpdater* command_updater, int command_id) |
| 18 : command_updater_(command_updater), | 19 : image_(new views::ImageView()), |
| 20 command_updater_(command_updater), |
| 19 command_id_(command_id), | 21 command_id_(command_id), |
| 20 active_(false), | 22 active_(false), |
| 21 suppress_mouse_released_action_(false) { | 23 suppress_mouse_released_action_(false), |
| 22 SetAccessibilityFocusable(true); | 24 ink_drop_delegate_(new views::ButtonInkDropDelegate(this, this)) { |
| 25 AddChildView(image_); |
| 26 image_->set_interactive(false); |
| 27 image_->EnableCanvasFlippingForRTLUI(true); |
| 28 image_->SetAccessibilityFocusable(true); |
| 29 |
| 30 const int kInkDropLargeSize = 32; |
| 31 const int kInkDropLargeCornerRadius = 5; |
| 32 const int kInkDropSmallSize = 24; |
| 33 const int kInkDropSmallCornerRadius = 2; |
| 34 ink_drop_delegate_->SetInkDropSize( |
| 35 kInkDropLargeSize, kInkDropLargeCornerRadius, kInkDropSmallSize, |
| 36 kInkDropSmallCornerRadius); |
| 23 } | 37 } |
| 24 | 38 |
| 25 BubbleIconView::~BubbleIconView() { | 39 BubbleIconView::~BubbleIconView() { |
| 26 } | 40 } |
| 27 | 41 |
| 28 bool BubbleIconView::IsBubbleShowing() const { | 42 bool BubbleIconView::IsBubbleShowing() const { |
| 29 // If the bubble is being destroyed, it's considered showing though it may be | 43 // If the bubble is being destroyed, it's considered showing though it may be |
| 30 // already invisible currently. | 44 // already invisible currently. |
| 31 return GetBubble() != NULL; | 45 return GetBubble() != NULL; |
| 32 } | 46 } |
| 33 | 47 |
| 48 void BubbleIconView::SetImage(const gfx::ImageSkia* image_skia) { |
| 49 image_->SetImage(image_skia); |
| 50 } |
| 51 |
| 52 const gfx::ImageSkia& BubbleIconView::GetImage() const { |
| 53 return image_->GetImage(); |
| 54 } |
| 55 |
| 56 void BubbleIconView::SetTooltipText(const base::string16& tooltip) { |
| 57 image_->SetTooltipText(tooltip); |
| 58 } |
| 59 |
| 34 void BubbleIconView::GetAccessibleState(ui::AXViewState* state) { | 60 void BubbleIconView::GetAccessibleState(ui::AXViewState* state) { |
| 35 views::ImageView::GetAccessibleState(state); | 61 image_->GetAccessibleState(state); |
| 36 state->role = ui::AX_ROLE_BUTTON; | 62 state->role = ui::AX_ROLE_BUTTON; |
| 37 } | 63 } |
| 38 | 64 |
| 39 bool BubbleIconView::GetTooltipText(const gfx::Point& p, | 65 bool BubbleIconView::GetTooltipText(const gfx::Point& p, |
| 40 base::string16* tooltip) const { | 66 base::string16* tooltip) const { |
| 41 if (IsBubbleShowing()) | 67 return !IsBubbleShowing() && image_->GetTooltipText(p, tooltip); |
| 42 return false; | 68 } |
| 43 | 69 |
| 44 return views::ImageView::GetTooltipText(p, tooltip); | 70 gfx::Size BubbleIconView::GetPreferredSize() const { |
| 71 return image_->GetPreferredSize(); |
| 72 } |
| 73 |
| 74 void BubbleIconView::Layout() { |
| 75 View::Layout(); |
| 76 image_->SetBoundsRect(GetLocalBounds()); |
| 77 ink_drop_delegate_->OnLayout(); |
| 45 } | 78 } |
| 46 | 79 |
| 47 bool BubbleIconView::OnMousePressed(const ui::MouseEvent& event) { | 80 bool BubbleIconView::OnMousePressed(const ui::MouseEvent& event) { |
| 48 // If the bubble is showing then don't reshow it when the mouse is released. | 81 // If the bubble is showing then don't reshow it when the mouse is released. |
| 49 suppress_mouse_released_action_ = IsBubbleShowing(); | 82 suppress_mouse_released_action_ = IsBubbleShowing(); |
| 83 if (!suppress_mouse_released_action_ && event.IsOnlyLeftMouseButton()) |
| 84 ink_drop_delegate_->OnAction(views::InkDropState::ACTION_PENDING); |
| 50 | 85 |
| 51 // We want to show the bubble on mouse release; that is the standard behavior | 86 // We want to show the bubble on mouse release; that is the standard behavior |
| 52 // for buttons. | 87 // for buttons. |
| 53 return true; | 88 return true; |
| 54 } | 89 } |
| 55 | 90 |
| 56 void BubbleIconView::OnMouseReleased(const ui::MouseEvent& event) { | 91 void BubbleIconView::OnMouseReleased(const ui::MouseEvent& event) { |
| 57 // If this is the second click on this view then the bubble was showing on the | 92 // If this is the second click on this view then the bubble was showing on the |
| 58 // mouse pressed event and is hidden now. Prevent the bubble from reshowing by | 93 // mouse pressed event and is hidden now. Prevent the bubble from reshowing by |
| 59 // doing nothing here. | 94 // doing nothing here. |
| 60 if (suppress_mouse_released_action_) { | 95 if (suppress_mouse_released_action_) { |
| 61 suppress_mouse_released_action_ = false; | 96 suppress_mouse_released_action_ = false; |
| 62 return; | 97 return; |
| 63 } | 98 } |
| 99 if (!event.IsLeftMouseButton()) |
| 100 return; |
| 64 | 101 |
| 65 if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location())) | 102 const bool activated = HitTestPoint(event.location()); |
| 103 ink_drop_delegate_->OnAction( |
| 104 activated ? views::InkDropState::ACTIVATED : views::InkDropState::HIDDEN); |
| 105 if (activated) |
| 66 ExecuteCommand(EXECUTE_SOURCE_MOUSE); | 106 ExecuteCommand(EXECUTE_SOURCE_MOUSE); |
| 67 } | 107 } |
| 68 | 108 |
| 69 bool BubbleIconView::OnKeyPressed(const ui::KeyEvent& event) { | 109 bool BubbleIconView::OnKeyPressed(const ui::KeyEvent& event) { |
| 70 if (event.key_code() == ui::VKEY_SPACE || | 110 if (event.key_code() == ui::VKEY_SPACE || |
| 71 event.key_code() == ui::VKEY_RETURN) { | 111 event.key_code() == ui::VKEY_RETURN) { |
| 72 ExecuteCommand(EXECUTE_SOURCE_KEYBOARD); | 112 ExecuteCommand(EXECUTE_SOURCE_KEYBOARD); |
| 73 return true; | 113 return true; |
| 74 } | 114 } |
| 75 return false; | 115 return false; |
| 76 } | 116 } |
| 77 | 117 |
| 78 void BubbleIconView::ViewHierarchyChanged( | 118 void BubbleIconView::ViewHierarchyChanged( |
| 79 const ViewHierarchyChangedDetails& details) { | 119 const ViewHierarchyChangedDetails& details) { |
| 80 ImageView::ViewHierarchyChanged(details); | 120 View::ViewHierarchyChanged(details); |
| 81 if (details.is_add && GetNativeTheme()) | 121 if (details.is_add && GetNativeTheme()) |
| 82 UpdateIcon(); | 122 UpdateIcon(); |
| 83 } | 123 } |
| 84 | 124 |
| 85 void BubbleIconView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 125 void BubbleIconView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 86 UpdateIcon(); | 126 UpdateIcon(); |
| 87 } | 127 } |
| 88 | 128 |
| 89 void BubbleIconView::OnGestureEvent(ui::GestureEvent* event) { | 129 void BubbleIconView::OnGestureEvent(ui::GestureEvent* event) { |
| 90 if (event->type() == ui::ET_GESTURE_TAP) { | 130 if (event->type() == ui::ET_GESTURE_TAP) { |
| 131 ink_drop_delegate_->OnAction(views::InkDropState::ACTIVATED); |
| 91 ExecuteCommand(EXECUTE_SOURCE_GESTURE); | 132 ExecuteCommand(EXECUTE_SOURCE_GESTURE); |
| 92 event->SetHandled(); | 133 event->SetHandled(); |
| 93 } | 134 } |
| 94 } | 135 } |
| 95 | 136 |
| 137 void BubbleIconView::AddInkDropLayer(ui::Layer* ink_drop_layer) { |
| 138 image_->SetPaintToLayer(true); |
| 139 image_->SetFillsBoundsOpaquely(false); |
| 140 SetPaintToLayer(true); |
| 141 SetFillsBoundsOpaquely(false); |
| 142 layer()->Add(ink_drop_layer); |
| 143 layer()->StackAtBottom(ink_drop_layer); |
| 144 } |
| 145 |
| 146 void BubbleIconView::RemoveInkDropLayer(ui::Layer* ink_drop_layer) { |
| 147 layer()->Remove(ink_drop_layer); |
| 148 SetFillsBoundsOpaquely(true); |
| 149 SetPaintToLayer(false); |
| 150 image_->SetFillsBoundsOpaquely(true); |
| 151 image_->SetPaintToLayer(false); |
| 152 } |
| 153 |
| 154 void BubbleIconView::OnWidgetVisibilityChanged(views::Widget* widget, |
| 155 bool visible) { |
| 156 // |widget| is a bubble that has just got shown / hidden. |
| 157 if (!visible) |
| 158 ink_drop_delegate_->OnAction(views::InkDropState::DEACTIVATED); |
| 159 } |
| 160 |
| 96 void BubbleIconView::ExecuteCommand(ExecuteSource source) { | 161 void BubbleIconView::ExecuteCommand(ExecuteSource source) { |
| 97 OnExecuting(source); | 162 OnExecuting(source); |
| 98 if (command_updater_) | 163 if (command_updater_) |
| 99 command_updater_->ExecuteCommand(command_id_); | 164 command_updater_->ExecuteCommand(command_id_); |
| 100 } | 165 } |
| 101 | 166 |
| 167 gfx::VectorIconId BubbleIconView::GetVectorIcon() const { |
| 168 return gfx::VectorIconId::VECTOR_ICON_NONE; |
| 169 } |
| 170 |
| 102 bool BubbleIconView::SetRasterIcon() { | 171 bool BubbleIconView::SetRasterIcon() { |
| 103 return false; | 172 return false; |
| 104 } | 173 } |
| 105 | 174 |
| 106 void BubbleIconView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 175 void BubbleIconView::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 107 views::BubbleDelegateView* bubble = GetBubble(); | 176 views::BubbleDelegateView* bubble = GetBubble(); |
| 108 if (bubble) | 177 if (bubble) |
| 109 bubble->OnAnchorBoundsChanged(); | 178 bubble->OnAnchorBoundsChanged(); |
| 179 ink_drop_delegate_->OnLayout(); |
| 180 } |
| 181 |
| 182 gfx::Point BubbleIconView::CalculateInkDropCenter() const { |
| 183 return GetLocalBounds().CenterPoint(); |
| 184 } |
| 185 |
| 186 bool BubbleIconView::ShouldShowInkDropHover() const { |
| 187 // BubbleIconView views don't show hover effect. |
| 188 return false; |
| 110 } | 189 } |
| 111 | 190 |
| 112 void BubbleIconView::UpdateIcon() { | 191 void BubbleIconView::UpdateIcon() { |
| 113 if (SetRasterIcon()) | 192 if (SetRasterIcon()) |
| 114 return; | 193 return; |
| 115 | 194 |
| 116 const int icon_size = | 195 const int icon_size = |
| 117 ui::MaterialDesignController::IsModeMaterial() ? 16 : 18; | 196 ui::MaterialDesignController::IsModeMaterial() ? 16 : 18; |
| 118 const ui::NativeTheme* theme = GetNativeTheme(); | 197 const ui::NativeTheme* theme = GetNativeTheme(); |
| 119 SkColor icon_color = | 198 SkColor icon_color = |
| 120 active_ | 199 active_ |
| 121 ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) | 200 ? theme->GetSystemColor(ui::NativeTheme::kColorId_CallToActionColor) |
| 122 : color_utils::DeriveDefaultIconColor(theme->GetSystemColor( | 201 : color_utils::DeriveDefaultIconColor(theme->GetSystemColor( |
| 123 ui::NativeTheme::kColorId_TextfieldDefaultColor)); | 202 ui::NativeTheme::kColorId_TextfieldDefaultColor)); |
| 124 SetImage(gfx::CreateVectorIcon(GetVectorIcon(), icon_size, icon_color)); | 203 image_->SetImage( |
| 204 gfx::CreateVectorIcon(GetVectorIcon(), icon_size, icon_color)); |
| 125 } | 205 } |
| 126 | 206 |
| 127 void BubbleIconView::SetActiveInternal(bool active) { | 207 void BubbleIconView::SetActiveInternal(bool active) { |
| 128 if (active_ == active) | 208 if (active_ == active) |
| 129 return; | 209 return; |
| 130 active_ = active; | 210 active_ = active; |
| 131 UpdateIcon(); | 211 UpdateIcon(); |
| 132 } | 212 } |
| OLD | NEW |