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