[Telemetry] Add labels attribute to page as the new way to set label
[chromium-blink-merge.git] / ash / shelf / shelf_button.cc
blob98e6ec7d7dff4417d7013acfbbc4fa525dd47469
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
3 // found in the LICENSE file.
5 #include "ash/shelf/shelf_button.h"
7 #include <algorithm>
9 #include "ash/ash_constants.h"
10 #include "ash/ash_switches.h"
11 #include "ash/shelf/shelf_button_host.h"
12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "grit/ash_resources.h"
14 #include "skia/ext/image_operations.h"
15 #include "ui/accessibility/ax_view_state.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/events/event_constants.h"
20 #include "ui/gfx/animation/animation_delegate.h"
21 #include "ui/gfx/animation/throb_animation.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/image/image_skia_operations.h"
25 #include "ui/gfx/skbitmap_operations.h"
26 #include "ui/views/controls/image_view.h"
28 namespace {
30 // Size of the bar. This is along the opposite axis of the shelf. For example,
31 // if the shelf is aligned horizontally then this is the height of the bar.
32 const int kBarSize = 3;
33 const int kIconSize = 32;
34 const int kIconPad = 5;
35 const int kIconPadVertical = 6;
36 const int kAttentionThrobDurationMS = 800;
38 // Simple AnimationDelegate that owns a single ThrobAnimation instance to
39 // keep all Draw Attention animations in sync.
40 class ShelfButtonAnimation : public gfx::AnimationDelegate {
41 public:
42 class Observer {
43 public:
44 virtual void AnimationProgressed() = 0;
46 protected:
47 virtual ~Observer() {}
50 static ShelfButtonAnimation* GetInstance() {
51 static ShelfButtonAnimation* s_instance = new ShelfButtonAnimation();
52 return s_instance;
55 void AddObserver(Observer* observer) {
56 observers_.AddObserver(observer);
59 void RemoveObserver(Observer* observer) {
60 observers_.RemoveObserver(observer);
61 if (!observers_.might_have_observers())
62 animation_.Stop();
65 int GetAlpha() {
66 return GetThrobAnimation().CurrentValueBetween(0, 255);
69 double GetAnimation() {
70 return GetThrobAnimation().GetCurrentValue();
73 private:
74 ShelfButtonAnimation()
75 : animation_(this) {
76 animation_.SetThrobDuration(kAttentionThrobDurationMS);
77 animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT);
80 virtual ~ShelfButtonAnimation() {
83 gfx::ThrobAnimation& GetThrobAnimation() {
84 if (!animation_.is_animating()) {
85 animation_.Reset();
86 animation_.StartThrobbing(-1 /*throb indefinitely*/);
88 return animation_;
91 // gfx::AnimationDelegate
92 virtual void AnimationProgressed(const gfx::Animation* animation) override {
93 if (animation != &animation_)
94 return;
95 if (!animation_.is_animating())
96 return;
97 FOR_EACH_OBSERVER(Observer, observers_, AnimationProgressed());
100 gfx::ThrobAnimation animation_;
101 ObserverList<Observer> observers_;
103 DISALLOW_COPY_AND_ASSIGN(ShelfButtonAnimation);
106 } // namespace
108 namespace ash {
110 ////////////////////////////////////////////////////////////////////////////////
111 // ShelfButton::BarView
113 class ShelfButton::BarView : public views::ImageView,
114 public ShelfButtonAnimation::Observer {
115 public:
116 BarView(ShelfButton* host)
117 : host_(host),
118 show_attention_(false) {
119 // Make sure the events reach the parent view for handling.
120 set_interactive(false);
123 virtual ~BarView() {
124 if (show_attention_)
125 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
128 // views::View:
129 virtual void OnPaint(gfx::Canvas* canvas) override {
130 if (show_attention_) {
131 int alpha = ShelfButtonAnimation::GetInstance()->GetAlpha();
132 canvas->SaveLayerAlpha(alpha);
133 views::ImageView::OnPaint(canvas);
134 canvas->Restore();
135 } else {
136 views::ImageView::OnPaint(canvas);
140 // ShelfButtonAnimation::Observer
141 virtual void AnimationProgressed() override {
142 UpdateBounds();
143 SchedulePaint();
146 void SetBarBoundsRect(const gfx::Rect& bounds) {
147 base_bounds_ = bounds;
148 UpdateBounds();
151 void ShowAttention(bool show) {
152 if (show_attention_ != show) {
153 show_attention_ = show;
154 if (show_attention_)
155 ShelfButtonAnimation::GetInstance()->AddObserver(this);
156 else
157 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
159 UpdateBounds();
162 private:
163 void UpdateBounds() {
164 gfx::Rect bounds = base_bounds_;
165 if (show_attention_) {
166 // Scale from .35 to 1.0 of the total width (which is wider than the
167 // visible width of the image, so the animation "rests" briefly at full
168 // visible width.
169 double animation = ShelfButtonAnimation::GetInstance()->GetAnimation();
170 double scale = (.35 + .65 * animation);
171 if (host_->shelf_layout_manager()->GetAlignment() ==
172 SHELF_ALIGNMENT_BOTTOM) {
173 bounds.set_width(base_bounds_.width() * scale);
174 int x_offset = (base_bounds_.width() - bounds.width()) / 2;
175 bounds.set_x(base_bounds_.x() + x_offset);
176 } else {
177 bounds.set_height(base_bounds_.height() * scale);
178 int y_offset = (base_bounds_.height() - bounds.height()) / 2;
179 bounds.set_y(base_bounds_.y() + y_offset);
182 SetBoundsRect(bounds);
185 ShelfButton* host_;
186 bool show_attention_;
187 gfx::Rect base_bounds_;
189 DISALLOW_COPY_AND_ASSIGN(BarView);
192 ////////////////////////////////////////////////////////////////////////////////
193 // ShelfButton::IconView
195 ShelfButton::IconView::IconView() : icon_size_(kIconSize) {
196 // Do not make this interactive, so that events are sent to ShelfView for
197 // handling.
198 set_interactive(false);
201 ShelfButton::IconView::~IconView() {
204 ////////////////////////////////////////////////////////////////////////////////
205 // ShelfButton
207 // static
208 const char ShelfButton::kViewClassName[] = "ash/ShelfButton";
210 ShelfButton* ShelfButton::Create(views::ButtonListener* listener,
211 ShelfButtonHost* host,
212 ShelfLayoutManager* shelf_layout_manager) {
213 ShelfButton* button = new ShelfButton(listener, host, shelf_layout_manager);
214 button->Init();
215 return button;
218 ShelfButton::ShelfButton(views::ButtonListener* listener,
219 ShelfButtonHost* host,
220 ShelfLayoutManager* shelf_layout_manager)
221 : CustomButton(listener),
222 host_(host),
223 icon_view_(NULL),
224 bar_(new BarView(this)),
225 state_(STATE_NORMAL),
226 shelf_layout_manager_(shelf_layout_manager),
227 destroyed_flag_(NULL) {
228 SetAccessibilityFocusable(true);
230 const gfx::ShadowValue kShadows[] = {
231 gfx::ShadowValue(gfx::Point(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
232 gfx::ShadowValue(gfx::Point(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
233 gfx::ShadowValue(gfx::Point(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
235 icon_shadows_.assign(kShadows, kShadows + arraysize(kShadows));
237 AddChildView(bar_);
240 ShelfButton::~ShelfButton() {
241 if (destroyed_flag_)
242 *destroyed_flag_ = true;
245 void ShelfButton::SetShadowedImage(const gfx::ImageSkia& image) {
246 icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
247 image, icon_shadows_));
250 void ShelfButton::SetImage(const gfx::ImageSkia& image) {
251 if (image.isNull()) {
252 // TODO: need an empty image.
253 icon_view_->SetImage(image);
254 return;
257 if (icon_view_->icon_size() == 0) {
258 SetShadowedImage(image);
259 return;
262 // Resize the image maintaining our aspect ratio.
263 int pref = icon_view_->icon_size();
264 float aspect_ratio =
265 static_cast<float>(image.width()) / static_cast<float>(image.height());
266 int height = pref;
267 int width = static_cast<int>(aspect_ratio * height);
268 if (width > pref) {
269 width = pref;
270 height = static_cast<int>(width / aspect_ratio);
273 if (width == image.width() && height == image.height()) {
274 SetShadowedImage(image);
275 return;
278 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(image,
279 skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height)));
282 const gfx::ImageSkia& ShelfButton::GetImage() const {
283 return icon_view_->GetImage();
286 void ShelfButton::AddState(State state) {
287 if (!(state_ & state)) {
288 state_ |= state;
289 Layout();
290 if (state & STATE_ATTENTION)
291 bar_->ShowAttention(true);
295 void ShelfButton::ClearState(State state) {
296 if (state_ & state) {
297 state_ &= ~state;
298 Layout();
299 if (state & STATE_ATTENTION)
300 bar_->ShowAttention(false);
304 gfx::Rect ShelfButton::GetIconBounds() const {
305 return icon_view_->bounds();
308 void ShelfButton::ShowContextMenu(const gfx::Point& p,
309 ui::MenuSourceType source_type) {
310 if (!context_menu_controller())
311 return;
313 bool destroyed = false;
314 destroyed_flag_ = &destroyed;
316 CustomButton::ShowContextMenu(p, source_type);
318 if (!destroyed) {
319 destroyed_flag_ = NULL;
320 // The menu will not propagate mouse events while its shown. To address,
321 // the hover state gets cleared once the menu was shown (and this was not
322 // destroyed).
323 ClearState(STATE_HOVERED);
327 const char* ShelfButton::GetClassName() const {
328 return kViewClassName;
331 bool ShelfButton::OnMousePressed(const ui::MouseEvent& event) {
332 CustomButton::OnMousePressed(event);
333 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
334 return true;
337 void ShelfButton::OnMouseReleased(const ui::MouseEvent& event) {
338 CustomButton::OnMouseReleased(event);
339 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
342 void ShelfButton::OnMouseCaptureLost() {
343 ClearState(STATE_HOVERED);
344 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
345 CustomButton::OnMouseCaptureLost();
348 bool ShelfButton::OnMouseDragged(const ui::MouseEvent& event) {
349 CustomButton::OnMouseDragged(event);
350 host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
351 return true;
354 void ShelfButton::OnMouseMoved(const ui::MouseEvent& event) {
355 CustomButton::OnMouseMoved(event);
356 host_->MouseMovedOverButton(this);
359 void ShelfButton::OnMouseEntered(const ui::MouseEvent& event) {
360 AddState(STATE_HOVERED);
361 CustomButton::OnMouseEntered(event);
362 host_->MouseEnteredButton(this);
365 void ShelfButton::OnMouseExited(const ui::MouseEvent& event) {
366 ClearState(STATE_HOVERED);
367 CustomButton::OnMouseExited(event);
368 host_->MouseExitedButton(this);
371 void ShelfButton::GetAccessibleState(ui::AXViewState* state) {
372 state->role = ui::AX_ROLE_BUTTON;
373 state->name = host_->GetAccessibleName(this);
376 void ShelfButton::Layout() {
377 const gfx::Rect button_bounds(GetContentsBounds());
378 int icon_pad =
379 shelf_layout_manager_->GetAlignment() != SHELF_ALIGNMENT_BOTTOM ?
380 kIconPadVertical : kIconPad;
381 int x_offset = shelf_layout_manager_->PrimaryAxisValue(0, icon_pad);
382 int y_offset = shelf_layout_manager_->PrimaryAxisValue(icon_pad, 0);
384 int icon_width = std::min(kIconSize,
385 button_bounds.width() - x_offset);
386 int icon_height = std::min(kIconSize,
387 button_bounds.height() - y_offset);
389 // If on the left or top 'invert' the inset so the constant gap is on
390 // the interior (towards the center of display) edge of the shelf.
391 if (SHELF_ALIGNMENT_LEFT == shelf_layout_manager_->GetAlignment())
392 x_offset = button_bounds.width() - (kIconSize + icon_pad);
394 if (SHELF_ALIGNMENT_TOP == shelf_layout_manager_->GetAlignment())
395 y_offset = button_bounds.height() - (kIconSize + icon_pad);
397 // Center icon with respect to the secondary axis, and ensure
398 // that the icon doesn't occlude the bar highlight.
399 if (shelf_layout_manager_->IsHorizontalAlignment()) {
400 x_offset = std::max(0, button_bounds.width() - icon_width) / 2;
401 if (y_offset + icon_height + kBarSize > button_bounds.height())
402 icon_height = button_bounds.height() - (y_offset + kBarSize);
403 } else {
404 y_offset = std::max(0, button_bounds.height() - icon_height) / 2;
405 if (x_offset + icon_width + kBarSize > button_bounds.width())
406 icon_width = button_bounds.width() - (x_offset + kBarSize);
409 icon_view_->SetBoundsRect(gfx::Rect(
410 button_bounds.x() + x_offset,
411 button_bounds.y() + y_offset,
412 icon_width,
413 icon_height));
415 // Icon size has been incorrect when running
416 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
417 // http://crbug.com/234854.
418 DCHECK_LE(icon_width, kIconSize);
419 DCHECK_LE(icon_height, kIconSize);
421 bar_->SetBarBoundsRect(button_bounds);
423 UpdateState();
426 void ShelfButton::ChildPreferredSizeChanged(views::View* child) {
427 Layout();
430 void ShelfButton::OnFocus() {
431 AddState(STATE_FOCUSED);
432 CustomButton::OnFocus();
435 void ShelfButton::OnBlur() {
436 ClearState(STATE_FOCUSED);
437 CustomButton::OnBlur();
440 void ShelfButton::OnPaint(gfx::Canvas* canvas) {
441 CustomButton::OnPaint(canvas);
442 if (HasFocus()) {
443 gfx::Rect paint_bounds(GetLocalBounds());
444 paint_bounds.Inset(1, 1, 1, 1);
445 canvas->DrawSolidFocusRect(paint_bounds, kFocusBorderColor);
449 void ShelfButton::OnGestureEvent(ui::GestureEvent* event) {
450 switch (event->type()) {
451 case ui::ET_GESTURE_TAP_DOWN:
452 AddState(STATE_HOVERED);
453 return CustomButton::OnGestureEvent(event);
454 case ui::ET_GESTURE_END:
455 ClearState(STATE_HOVERED);
456 return CustomButton::OnGestureEvent(event);
457 case ui::ET_GESTURE_SCROLL_BEGIN:
458 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event);
459 event->SetHandled();
460 return;
461 case ui::ET_GESTURE_SCROLL_UPDATE:
462 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event);
463 event->SetHandled();
464 return;
465 case ui::ET_GESTURE_SCROLL_END:
466 case ui::ET_SCROLL_FLING_START:
467 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false);
468 event->SetHandled();
469 return;
470 default:
471 return CustomButton::OnGestureEvent(event);
475 void ShelfButton::Init() {
476 icon_view_ = CreateIconView();
478 // TODO: refactor the layers so each button doesn't require 2.
479 icon_view_->SetPaintToLayer(true);
480 icon_view_->SetFillsBoundsOpaquely(false);
481 icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
482 icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
484 AddChildView(icon_view_);
487 ShelfButton::IconView* ShelfButton::CreateIconView() {
488 return new IconView;
491 bool ShelfButton::IsShelfHorizontal() const {
492 return shelf_layout_manager_->IsHorizontalAlignment();
495 void ShelfButton::UpdateState() {
496 UpdateBar();
498 icon_view_->SetHorizontalAlignment(
499 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::CENTER,
500 views::ImageView::LEADING));
501 icon_view_->SetVerticalAlignment(
502 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::LEADING,
503 views::ImageView::CENTER));
504 SchedulePaint();
507 void ShelfButton::UpdateBar() {
508 if (state_ & STATE_HIDDEN) {
509 bar_->SetVisible(false);
510 return;
513 int bar_id = 0;
514 if (state_ & STATE_ACTIVE)
515 bar_id = IDR_ASH_SHELF_UNDERLINE_ACTIVE;
516 else if (state_ & STATE_RUNNING)
517 bar_id = IDR_ASH_SHELF_UNDERLINE_RUNNING;
519 if (bar_id != 0) {
520 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
521 const gfx::ImageSkia* image = rb.GetImageNamed(bar_id).ToImageSkia();
522 if (shelf_layout_manager_->GetAlignment() == SHELF_ALIGNMENT_BOTTOM) {
523 bar_->SetImage(*image);
524 } else {
525 bar_->SetImage(gfx::ImageSkiaOperations::CreateRotatedImage(*image,
526 shelf_layout_manager_->SelectValueForShelfAlignment(
527 SkBitmapOperations::ROTATION_90_CW,
528 SkBitmapOperations::ROTATION_90_CW,
529 SkBitmapOperations::ROTATION_270_CW,
530 SkBitmapOperations::ROTATION_180_CW)));
532 bar_->SetHorizontalAlignment(
533 shelf_layout_manager_->SelectValueForShelfAlignment(
534 views::ImageView::CENTER,
535 views::ImageView::LEADING,
536 views::ImageView::TRAILING,
537 views::ImageView::CENTER));
538 bar_->SetVerticalAlignment(
539 shelf_layout_manager_->SelectValueForShelfAlignment(
540 views::ImageView::TRAILING,
541 views::ImageView::CENTER,
542 views::ImageView::CENTER,
543 views::ImageView::LEADING));
544 bar_->SchedulePaint();
547 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL);
550 } // namespace ash