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