[GCM] Remove GCMDriver::Purge and GCMClient::CheckOut
[chromium-blink-merge.git] / ash / shelf / shelf_button.cc
bloba070fe5b7a2e2990a6398376bd79fcc70b3ea847
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 ~ShelfButtonAnimation() override {}
82 gfx::ThrobAnimation& GetThrobAnimation() {
83 if (!animation_.is_animating()) {
84 animation_.Reset();
85 animation_.StartThrobbing(-1 /*throb indefinitely*/);
87 return animation_;
90 // gfx::AnimationDelegate
91 void AnimationProgressed(const gfx::Animation* animation) override {
92 if (animation != &animation_)
93 return;
94 if (!animation_.is_animating())
95 return;
96 FOR_EACH_OBSERVER(Observer, observers_, AnimationProgressed());
99 gfx::ThrobAnimation animation_;
100 ObserverList<Observer> observers_;
102 DISALLOW_COPY_AND_ASSIGN(ShelfButtonAnimation);
105 } // namespace
107 namespace ash {
109 ////////////////////////////////////////////////////////////////////////////////
110 // ShelfButton::BarView
112 class ShelfButton::BarView : public views::ImageView,
113 public ShelfButtonAnimation::Observer {
114 public:
115 BarView(ShelfButton* host)
116 : host_(host),
117 show_attention_(false) {
118 // Make sure the events reach the parent view for handling.
119 set_interactive(false);
122 ~BarView() override {
123 if (show_attention_)
124 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
127 // views::View:
128 void OnPaint(gfx::Canvas* canvas) override {
129 if (show_attention_) {
130 int alpha = ShelfButtonAnimation::GetInstance()->GetAlpha();
131 canvas->SaveLayerAlpha(alpha);
132 views::ImageView::OnPaint(canvas);
133 canvas->Restore();
134 } else {
135 views::ImageView::OnPaint(canvas);
139 // ShelfButtonAnimation::Observer
140 void AnimationProgressed() override {
141 UpdateBounds();
142 SchedulePaint();
145 void SetBarBoundsRect(const gfx::Rect& bounds) {
146 base_bounds_ = bounds;
147 UpdateBounds();
150 void ShowAttention(bool show) {
151 if (show_attention_ != show) {
152 show_attention_ = show;
153 if (show_attention_)
154 ShelfButtonAnimation::GetInstance()->AddObserver(this);
155 else
156 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
158 UpdateBounds();
161 private:
162 void UpdateBounds() {
163 gfx::Rect bounds = base_bounds_;
164 if (show_attention_) {
165 // Scale from .35 to 1.0 of the total width (which is wider than the
166 // visible width of the image, so the animation "rests" briefly at full
167 // visible width.
168 double animation = ShelfButtonAnimation::GetInstance()->GetAnimation();
169 double scale = (.35 + .65 * animation);
170 if (host_->shelf_layout_manager()->GetAlignment() ==
171 SHELF_ALIGNMENT_BOTTOM) {
172 bounds.set_width(base_bounds_.width() * scale);
173 int x_offset = (base_bounds_.width() - bounds.width()) / 2;
174 bounds.set_x(base_bounds_.x() + x_offset);
175 } else {
176 bounds.set_height(base_bounds_.height() * scale);
177 int y_offset = (base_bounds_.height() - bounds.height()) / 2;
178 bounds.set_y(base_bounds_.y() + y_offset);
181 SetBoundsRect(bounds);
184 ShelfButton* host_;
185 bool show_attention_;
186 gfx::Rect base_bounds_;
188 DISALLOW_COPY_AND_ASSIGN(BarView);
191 ////////////////////////////////////////////////////////////////////////////////
192 // ShelfButton::IconView
194 ShelfButton::IconView::IconView() : icon_size_(kIconSize) {
195 // Do not make this interactive, so that events are sent to ShelfView for
196 // handling.
197 set_interactive(false);
200 ShelfButton::IconView::~IconView() {
203 ////////////////////////////////////////////////////////////////////////////////
204 // ShelfButton
206 // static
207 const char ShelfButton::kViewClassName[] = "ash/ShelfButton";
209 ShelfButton* ShelfButton::Create(views::ButtonListener* listener,
210 ShelfButtonHost* host,
211 ShelfLayoutManager* shelf_layout_manager) {
212 ShelfButton* button = new ShelfButton(listener, host, shelf_layout_manager);
213 button->Init();
214 return button;
217 ShelfButton::ShelfButton(views::ButtonListener* listener,
218 ShelfButtonHost* host,
219 ShelfLayoutManager* shelf_layout_manager)
220 : CustomButton(listener),
221 host_(host),
222 icon_view_(NULL),
223 bar_(new BarView(this)),
224 state_(STATE_NORMAL),
225 shelf_layout_manager_(shelf_layout_manager),
226 destroyed_flag_(NULL) {
227 SetAccessibilityFocusable(true);
229 const gfx::ShadowValue kShadows[] = {
230 gfx::ShadowValue(gfx::Point(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
231 gfx::ShadowValue(gfx::Point(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
232 gfx::ShadowValue(gfx::Point(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
234 icon_shadows_.assign(kShadows, kShadows + arraysize(kShadows));
236 AddChildView(bar_);
239 ShelfButton::~ShelfButton() {
240 if (destroyed_flag_)
241 *destroyed_flag_ = true;
244 void ShelfButton::SetShadowedImage(const gfx::ImageSkia& image) {
245 icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
246 image, icon_shadows_));
249 void ShelfButton::SetImage(const gfx::ImageSkia& image) {
250 if (image.isNull()) {
251 // TODO: need an empty image.
252 icon_view_->SetImage(image);
253 return;
256 if (icon_view_->icon_size() == 0) {
257 SetShadowedImage(image);
258 return;
261 // Resize the image maintaining our aspect ratio.
262 int pref = icon_view_->icon_size();
263 float aspect_ratio =
264 static_cast<float>(image.width()) / static_cast<float>(image.height());
265 int height = pref;
266 int width = static_cast<int>(aspect_ratio * height);
267 if (width > pref) {
268 width = pref;
269 height = static_cast<int>(width / aspect_ratio);
272 if (width == image.width() && height == image.height()) {
273 SetShadowedImage(image);
274 return;
277 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(image,
278 skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height)));
281 const gfx::ImageSkia& ShelfButton::GetImage() const {
282 return icon_view_->GetImage();
285 void ShelfButton::AddState(State state) {
286 if (!(state_ & state)) {
287 state_ |= state;
288 Layout();
289 if (state & STATE_ATTENTION)
290 bar_->ShowAttention(true);
294 void ShelfButton::ClearState(State state) {
295 if (state_ & state) {
296 state_ &= ~state;
297 Layout();
298 if (state & STATE_ATTENTION)
299 bar_->ShowAttention(false);
303 gfx::Rect ShelfButton::GetIconBounds() const {
304 return icon_view_->bounds();
307 void ShelfButton::ShowContextMenu(const gfx::Point& p,
308 ui::MenuSourceType source_type) {
309 if (!context_menu_controller())
310 return;
312 bool destroyed = false;
313 destroyed_flag_ = &destroyed;
315 CustomButton::ShowContextMenu(p, source_type);
317 if (!destroyed) {
318 destroyed_flag_ = NULL;
319 // The menu will not propagate mouse events while its shown. To address,
320 // the hover state gets cleared once the menu was shown (and this was not
321 // destroyed).
322 ClearState(STATE_HOVERED);
326 const char* ShelfButton::GetClassName() const {
327 return kViewClassName;
330 bool ShelfButton::OnMousePressed(const ui::MouseEvent& event) {
331 CustomButton::OnMousePressed(event);
332 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
333 return true;
336 void ShelfButton::OnMouseReleased(const ui::MouseEvent& event) {
337 CustomButton::OnMouseReleased(event);
338 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
341 void ShelfButton::OnMouseCaptureLost() {
342 ClearState(STATE_HOVERED);
343 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
344 CustomButton::OnMouseCaptureLost();
347 bool ShelfButton::OnMouseDragged(const ui::MouseEvent& event) {
348 CustomButton::OnMouseDragged(event);
349 host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
350 return true;
353 void ShelfButton::OnMouseMoved(const ui::MouseEvent& event) {
354 CustomButton::OnMouseMoved(event);
355 host_->MouseMovedOverButton(this);
358 void ShelfButton::OnMouseEntered(const ui::MouseEvent& event) {
359 AddState(STATE_HOVERED);
360 CustomButton::OnMouseEntered(event);
361 host_->MouseEnteredButton(this);
364 void ShelfButton::OnMouseExited(const ui::MouseEvent& event) {
365 ClearState(STATE_HOVERED);
366 CustomButton::OnMouseExited(event);
367 host_->MouseExitedButton(this);
370 void ShelfButton::GetAccessibleState(ui::AXViewState* state) {
371 state->role = ui::AX_ROLE_BUTTON;
372 state->name = host_->GetAccessibleName(this);
375 void ShelfButton::Layout() {
376 const gfx::Rect button_bounds(GetContentsBounds());
377 int icon_pad =
378 shelf_layout_manager_->GetAlignment() != SHELF_ALIGNMENT_BOTTOM ?
379 kIconPadVertical : kIconPad;
380 int x_offset = shelf_layout_manager_->PrimaryAxisValue(0, icon_pad);
381 int y_offset = shelf_layout_manager_->PrimaryAxisValue(icon_pad, 0);
383 int icon_width = std::min(kIconSize,
384 button_bounds.width() - x_offset);
385 int icon_height = std::min(kIconSize,
386 button_bounds.height() - y_offset);
388 // If on the left or top 'invert' the inset so the constant gap is on
389 // the interior (towards the center of display) edge of the shelf.
390 if (SHELF_ALIGNMENT_LEFT == shelf_layout_manager_->GetAlignment())
391 x_offset = button_bounds.width() - (kIconSize + icon_pad);
393 if (SHELF_ALIGNMENT_TOP == shelf_layout_manager_->GetAlignment())
394 y_offset = button_bounds.height() - (kIconSize + icon_pad);
396 // Center icon with respect to the secondary axis, and ensure
397 // that the icon doesn't occlude the bar highlight.
398 if (shelf_layout_manager_->IsHorizontalAlignment()) {
399 x_offset = std::max(0, button_bounds.width() - icon_width) / 2;
400 if (y_offset + icon_height + kBarSize > button_bounds.height())
401 icon_height = button_bounds.height() - (y_offset + kBarSize);
402 } else {
403 y_offset = std::max(0, button_bounds.height() - icon_height) / 2;
404 if (x_offset + icon_width + kBarSize > button_bounds.width())
405 icon_width = button_bounds.width() - (x_offset + kBarSize);
408 icon_view_->SetBoundsRect(gfx::Rect(
409 button_bounds.x() + x_offset,
410 button_bounds.y() + y_offset,
411 icon_width,
412 icon_height));
414 // Icon size has been incorrect when running
415 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
416 // http://crbug.com/234854.
417 DCHECK_LE(icon_width, kIconSize);
418 DCHECK_LE(icon_height, kIconSize);
420 bar_->SetBarBoundsRect(button_bounds);
422 UpdateState();
425 void ShelfButton::ChildPreferredSizeChanged(views::View* child) {
426 Layout();
429 void ShelfButton::OnFocus() {
430 AddState(STATE_FOCUSED);
431 CustomButton::OnFocus();
434 void ShelfButton::OnBlur() {
435 ClearState(STATE_FOCUSED);
436 CustomButton::OnBlur();
439 void ShelfButton::OnPaint(gfx::Canvas* canvas) {
440 CustomButton::OnPaint(canvas);
441 if (HasFocus()) {
442 gfx::Rect paint_bounds(GetLocalBounds());
443 paint_bounds.Inset(1, 1, 1, 1);
444 canvas->DrawSolidFocusRect(paint_bounds, kFocusBorderColor);
448 void ShelfButton::OnGestureEvent(ui::GestureEvent* event) {
449 switch (event->type()) {
450 case ui::ET_GESTURE_TAP_DOWN:
451 AddState(STATE_HOVERED);
452 return CustomButton::OnGestureEvent(event);
453 case ui::ET_GESTURE_END:
454 ClearState(STATE_HOVERED);
455 return CustomButton::OnGestureEvent(event);
456 case ui::ET_GESTURE_SCROLL_BEGIN:
457 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event);
458 event->SetHandled();
459 return;
460 case ui::ET_GESTURE_SCROLL_UPDATE:
461 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event);
462 event->SetHandled();
463 return;
464 case ui::ET_GESTURE_SCROLL_END:
465 case ui::ET_SCROLL_FLING_START:
466 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false);
467 event->SetHandled();
468 return;
469 default:
470 return CustomButton::OnGestureEvent(event);
474 void ShelfButton::Init() {
475 icon_view_ = CreateIconView();
477 // TODO: refactor the layers so each button doesn't require 2.
478 icon_view_->SetPaintToLayer(true);
479 icon_view_->SetFillsBoundsOpaquely(false);
480 icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
481 icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
483 AddChildView(icon_view_);
486 ShelfButton::IconView* ShelfButton::CreateIconView() {
487 return new IconView;
490 bool ShelfButton::IsShelfHorizontal() const {
491 return shelf_layout_manager_->IsHorizontalAlignment();
494 void ShelfButton::UpdateState() {
495 UpdateBar();
497 icon_view_->SetHorizontalAlignment(
498 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::CENTER,
499 views::ImageView::LEADING));
500 icon_view_->SetVerticalAlignment(
501 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::LEADING,
502 views::ImageView::CENTER));
503 SchedulePaint();
506 void ShelfButton::UpdateBar() {
507 if (state_ & STATE_HIDDEN) {
508 bar_->SetVisible(false);
509 return;
512 int bar_id = 0;
513 if (state_ & STATE_ACTIVE)
514 bar_id = IDR_ASH_SHELF_UNDERLINE_ACTIVE;
515 else if (state_ & STATE_RUNNING)
516 bar_id = IDR_ASH_SHELF_UNDERLINE_RUNNING;
518 if (bar_id != 0) {
519 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
520 const gfx::ImageSkia* image = rb.GetImageNamed(bar_id).ToImageSkia();
521 if (shelf_layout_manager_->GetAlignment() == SHELF_ALIGNMENT_BOTTOM) {
522 bar_->SetImage(*image);
523 } else {
524 bar_->SetImage(gfx::ImageSkiaOperations::CreateRotatedImage(*image,
525 shelf_layout_manager_->SelectValueForShelfAlignment(
526 SkBitmapOperations::ROTATION_90_CW,
527 SkBitmapOperations::ROTATION_90_CW,
528 SkBitmapOperations::ROTATION_270_CW,
529 SkBitmapOperations::ROTATION_180_CW)));
531 bar_->SetHorizontalAlignment(
532 shelf_layout_manager_->SelectValueForShelfAlignment(
533 views::ImageView::CENTER,
534 views::ImageView::LEADING,
535 views::ImageView::TRAILING,
536 views::ImageView::CENTER));
537 bar_->SetVerticalAlignment(
538 shelf_layout_manager_->SelectValueForShelfAlignment(
539 views::ImageView::TRAILING,
540 views::ImageView::CENTER,
541 views::ImageView::CENTER,
542 views::ImageView::LEADING));
543 bar_->SchedulePaint();
546 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL);
549 } // namespace ash