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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright (c) 2012 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 "ash/launcher/launcher_button.h"
#include <algorithm>
#include "ash/launcher/launcher_button_host.h"
#include "grit/ash_resources.h"
#include "skia/ext/image_operations.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/animation/animation_delegate.h"
#include "ui/base/animation/throb_animation.h"
#include "ui/base/events/event_constants.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/views/controls/image_view.h"
namespace {
// Size of the bar. This is along the opposite axis of the shelf. For example,
// if the shelf is aligned horizontally then this is the height of the bar.
const int kBarSize = 3;
const int kBarSpacing = 5;
const int kIconSize = 32;
const int kHopSpacing = 2;
const int kIconPad = 8;
const int kHopUpMS = 0;
const int kHopDownMS = 200;
const int kAttentionThrobDurationMS = 1000;
bool ShouldHop(int state) {
return state & ash::internal::LauncherButton::STATE_HOVERED ||
state & ash::internal::LauncherButton::STATE_ACTIVE ||
state & ash::internal::LauncherButton::STATE_FOCUSED;
}
} // namespace
namespace ash {
namespace internal {
////////////////////////////////////////////////////////////////////////////////
// LauncherButton::BarView
class LauncherButton::BarView : public views::ImageView,
public ui::AnimationDelegate {
public:
BarView() : ALLOW_THIS_IN_INITIALIZER_LIST(animation_(this)) {
animation_.SetThrobDuration(kAttentionThrobDurationMS);
animation_.SetTweenType(ui::Tween::SMOOTH_IN_OUT);
}
// View overrides.
bool HitTestRect(const gfx::Rect& rect) const OVERRIDE {
// Allow Mouse...() messages to go to the parent view.
return false;
}
void OnPaint(gfx::Canvas* canvas) OVERRIDE {
if (animation_.is_animating()) {
int alpha = animation_.CurrentValueBetween(0, 255);
canvas->SaveLayerAlpha(alpha);
views::ImageView::OnPaint(canvas);
canvas->Restore();
} else {
views::ImageView::OnPaint(canvas);
}
}
// ui::AnimationDelegate overrides.
void AnimationProgressed(const ui::Animation* animation) OVERRIDE {
SchedulePaint();
}
void ShowAttention(bool show) {
if (show) {
// It's less disruptive if we don't start the pulsing at 0.
animation_.Reset(0.375);
animation_.StartThrobbing(-1);
} else {
animation_.Reset(0.0);
}
}
private:
ui::ThrobAnimation animation_;
DISALLOW_COPY_AND_ASSIGN(BarView);
};
////////////////////////////////////////////////////////////////////////////////
// LauncherButton::IconView
LauncherButton::IconView::IconView() : icon_size_(kIconSize) {
}
LauncherButton::IconView::~IconView() {
}
bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const {
// Return false so that LauncherButton gets all the mouse events.
return false;
}
////////////////////////////////////////////////////////////////////////////////
// LauncherButton
LauncherButton* LauncherButton::Create(views::ButtonListener* listener,
LauncherButtonHost* host) {
LauncherButton* button = new LauncherButton(listener, host);
button->Init();
return button;
}
LauncherButton::LauncherButton(views::ButtonListener* listener,
LauncherButtonHost* host)
: CustomButton(listener),
host_(host),
icon_view_(NULL),
bar_(new BarView),
state_(STATE_NORMAL) {
set_accessibility_focusable(true);
const gfx::ShadowValue kShadows[] = {
gfx::ShadowValue(gfx::Point(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
gfx::ShadowValue(gfx::Point(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
gfx::ShadowValue(gfx::Point(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
};
icon_shadows_.assign(kShadows, kShadows + arraysize(kShadows));
AddChildView(bar_);
}
LauncherButton::~LauncherButton() {
}
void LauncherButton::SetShadowedImage(const gfx::ImageSkia& image) {
icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
image, icon_shadows_));
}
void LauncherButton::SetImage(const gfx::ImageSkia& image) {
if (image.isNull()) {
// TODO: need an empty image.
icon_view_->SetImage(image);
return;
}
if (icon_view_->icon_size() == 0) {
SetShadowedImage(image);
return;
}
// Resize the image maintaining our aspect ratio.
int pref = icon_view_->icon_size();
float aspect_ratio =
static_cast<float>(image.width()) / static_cast<float>(image.height());
int height = pref;
int width = static_cast<int>(aspect_ratio * height);
if (width > pref) {
width = pref;
height = static_cast<int>(width / aspect_ratio);
}
if (width == image.width() && height == image.height()) {
SetShadowedImage(image);
return;
}
SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(image,
skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height)));
}
void LauncherButton::AddState(State state) {
if (!(state_ & state)) {
if (ShouldHop(state) || !ShouldHop(state_)) {
ui::ScopedLayerAnimationSettings scoped_setter(
icon_view_->layer()->GetAnimator());
scoped_setter.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kHopUpMS));
state_ |= state;
UpdateState();
} else {
state_ |= state;
UpdateState();
}
if (state & STATE_ATTENTION)
bar_->ShowAttention(true);
}
}
void LauncherButton::ClearState(State state) {
if (state_ & state) {
if (!ShouldHop(state) || ShouldHop(state_)) {
ui::ScopedLayerAnimationSettings scoped_setter(
icon_view_->layer()->GetAnimator());
scoped_setter.SetTweenType(ui::Tween::LINEAR);
scoped_setter.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kHopDownMS));
state_ &= ~state;
UpdateState();
} else {
state_ &= ~state;
UpdateState();
}
if (state & STATE_ATTENTION)
bar_->ShowAttention(false);
}
}
gfx::Rect LauncherButton::GetIconBounds() const {
return icon_view_->bounds();
}
bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) {
CustomButton::OnMousePressed(event);
host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event);
return true;
}
void LauncherButton::OnMouseReleased(const ui::MouseEvent& event) {
CustomButton::OnMouseReleased(event);
host_->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE, false);
}
void LauncherButton::OnMouseCaptureLost() {
ClearState(STATE_HOVERED);
host_->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE, true);
CustomButton::OnMouseCaptureLost();
}
bool LauncherButton::OnMouseDragged(const ui::MouseEvent& event) {
CustomButton::OnMouseDragged(event);
host_->PointerDraggedOnButton(this, LauncherButtonHost::MOUSE, event);
return true;
}
void LauncherButton::OnMouseMoved(const ui::MouseEvent& event) {
CustomButton::OnMouseMoved(event);
host_->MouseMovedOverButton(this);
}
void LauncherButton::OnMouseEntered(const ui::MouseEvent& event) {
AddState(STATE_HOVERED);
CustomButton::OnMouseEntered(event);
host_->MouseEnteredButton(this);
}
void LauncherButton::OnMouseExited(const ui::MouseEvent& event) {
ClearState(STATE_HOVERED);
CustomButton::OnMouseExited(event);
host_->MouseExitedButton(this);
}
void LauncherButton::GetAccessibleState(ui::AccessibleViewState* state) {
state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
state->name = host_->GetAccessibleName(this);
}
void LauncherButton::Layout() {
const gfx::Rect button_bounds(GetContentsBounds());
int x_offset = 0, y_offset = 0;
gfx::Rect icon_bounds;
if (host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM) {
icon_bounds.SetRect(
button_bounds.x(), button_bounds.y() + kIconPad,
button_bounds.width(), kIconSize);
if (ShouldHop(state_))
y_offset -= kHopSpacing;
} else {
icon_bounds.SetRect(
button_bounds.x() + kIconPad, button_bounds.y(),
kIconSize, button_bounds.height());
if (!ShouldHop(state_))
x_offset += kHopSpacing;
}
if (host_->GetShelfAlignment() == SHELF_ALIGNMENT_LEFT)
x_offset = -x_offset;
icon_bounds.Offset(x_offset, y_offset);
icon_view_->SetBoundsRect(icon_bounds);
bar_->SetBoundsRect(GetContentsBounds());
}
void LauncherButton::ChildPreferredSizeChanged(views::View* child) {
Layout();
}
void LauncherButton::OnFocus() {
AddState(STATE_FOCUSED);
CustomButton::OnFocus();
}
void LauncherButton::OnBlur() {
ClearState(STATE_FOCUSED);
CustomButton::OnBlur();
}
void LauncherButton::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
case ui::ET_GESTURE_TAP_DOWN:
AddState(STATE_HOVERED);
return CustomButton::OnGestureEvent(event);
case ui::ET_GESTURE_END:
ClearState(STATE_HOVERED);
return CustomButton::OnGestureEvent(event);
case ui::ET_GESTURE_SCROLL_BEGIN:
host_->PointerPressedOnButton(this, LauncherButtonHost::TOUCH, *event);
event->SetHandled();
return;
case ui::ET_GESTURE_SCROLL_UPDATE:
host_->PointerDraggedOnButton(this, LauncherButtonHost::TOUCH, *event);
event->SetHandled();
return;
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START:
host_->PointerReleasedOnButton(this, LauncherButtonHost::TOUCH, false);
event->SetHandled();
return;
default:
return CustomButton::OnGestureEvent(event);
}
}
void LauncherButton::Init() {
icon_view_ = CreateIconView();
// TODO: refactor the layers so each button doesn't require 2.
icon_view_->SetPaintToLayer(true);
icon_view_->SetFillsBoundsOpaquely(false);
icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
AddChildView(icon_view_);
}
LauncherButton::IconView* LauncherButton::CreateIconView() {
return new IconView;
}
bool LauncherButton::IsShelfHorizontal() const {
return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM;
}
void LauncherButton::UpdateState() {
if (state_ == STATE_NORMAL) {
bar_->SetVisible(false);
} else {
int bar_id;
if (IsShelfHorizontal()) {
if (state_ & STATE_ACTIVE)
bar_id = IDR_AURA_LAUNCHER_UNDERLINE_BOTTOM_ACTIVE;
else if (state_ & (STATE_HOVERED | STATE_FOCUSED | STATE_ATTENTION))
bar_id = IDR_AURA_LAUNCHER_UNDERLINE_BOTTOM_HOVER;
else
bar_id = IDR_AURA_LAUNCHER_UNDERLINE_BOTTOM_RUNNING;
} else {
if (state_ & STATE_ACTIVE)
bar_id = IDR_AURA_LAUNCHER_UNDERLINE_LEFT_ACTIVE;
else if (state_ & (STATE_HOVERED | STATE_FOCUSED | STATE_ATTENTION))
bar_id = IDR_AURA_LAUNCHER_UNDERLINE_LEFT_HOVER;
else
bar_id = IDR_AURA_LAUNCHER_UNDERLINE_LEFT_RUNNING;
}
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
bar_->SetImage(rb.GetImageNamed(bar_id).ToImageSkia());
bar_->SetVisible(true);
}
switch (host_->GetShelfAlignment()) {
case SHELF_ALIGNMENT_BOTTOM:
bar_->SetHorizontalAlignment(views::ImageView::CENTER);
bar_->SetVerticalAlignment(views::ImageView::TRAILING);
break;
case SHELF_ALIGNMENT_LEFT:
bar_->SetHorizontalAlignment(
base::i18n::IsRTL() ? views::ImageView::TRAILING :
views::ImageView::LEADING);
bar_->SetVerticalAlignment(views::ImageView::CENTER);
break;
case SHELF_ALIGNMENT_RIGHT:
bar_->SetHorizontalAlignment(
base::i18n::IsRTL() ? views::ImageView::LEADING :
views::ImageView::TRAILING);
bar_->SetVerticalAlignment(views::ImageView::CENTER);
break;
}
Layout();
SchedulePaint();
}
} // namespace internal
} // namespace ash
|