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