Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ash / shelf / shelf_button.cc
blob8ff66b9fdde09d075461c9dbb34c0d96fa2e9682
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/geometry/vector2d.h"
24 #include "ui/gfx/image/image.h"
25 #include "ui/gfx/image/image_skia_operations.h"
26 #include "ui/gfx/skbitmap_operations.h"
27 #include "ui/views/controls/image_view.h"
29 namespace {
31 // Size of the bar. This is along the opposite axis of the shelf. For example,
32 // if the shelf is aligned horizontally then this is the height of the bar.
33 const int kBarSize = 3;
34 const int kIconSize = 32;
35 const int kIconPad = 5;
36 const int kIconPadVertical = 6;
37 const int kAttentionThrobDurationMS = 800;
39 // Simple AnimationDelegate that owns a single ThrobAnimation instance to
40 // keep all Draw Attention animations in sync.
41 class ShelfButtonAnimation : public gfx::AnimationDelegate {
42 public:
43 class Observer {
44 public:
45 virtual void AnimationProgressed() = 0;
47 protected:
48 virtual ~Observer() {}
51 static ShelfButtonAnimation* GetInstance() {
52 static ShelfButtonAnimation* s_instance = new ShelfButtonAnimation();
53 return s_instance;
56 void AddObserver(Observer* observer) {
57 observers_.AddObserver(observer);
60 void RemoveObserver(Observer* observer) {
61 observers_.RemoveObserver(observer);
62 if (!observers_.might_have_observers())
63 animation_.Stop();
66 int GetAlpha() {
67 return GetThrobAnimation().CurrentValueBetween(0, 255);
70 double GetAnimation() {
71 return GetThrobAnimation().GetCurrentValue();
74 private:
75 ShelfButtonAnimation()
76 : animation_(this) {
77 animation_.SetThrobDuration(kAttentionThrobDurationMS);
78 animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT);
81 ~ShelfButtonAnimation() override {}
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 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 base::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 ~BarView() override {
124 if (show_attention_)
125 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
128 // views::View:
129 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 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. Cap bounds length at kIconSize to prevent visual
169 // flutter while centering bar within further expanding bounds.
170 double animation = ShelfButtonAnimation::GetInstance()->GetAnimation();
171 double scale = .35 + .65 * animation;
172 if (host_->shelf_layout_manager()->GetAlignment() ==
173 SHELF_ALIGNMENT_BOTTOM) {
174 int width = base_bounds_.width() * scale;
175 bounds.set_width(std::min(width, kIconSize));
176 int x_offset = (base_bounds_.width() - bounds.width()) / 2;
177 bounds.set_x(base_bounds_.x() + x_offset);
178 } else {
179 int height = base_bounds_.height() * scale;
180 bounds.set_height(std::min(height, kIconSize));
181 int y_offset = (base_bounds_.height() - bounds.height()) / 2;
182 bounds.set_y(base_bounds_.y() + y_offset);
185 SetBoundsRect(bounds);
188 ShelfButton* host_;
189 bool show_attention_;
190 gfx::Rect base_bounds_;
192 DISALLOW_COPY_AND_ASSIGN(BarView);
195 ////////////////////////////////////////////////////////////////////////////////
196 // ShelfButton::IconView
198 ShelfButton::IconView::IconView() : icon_size_(kIconSize) {
199 // Do not make this interactive, so that events are sent to ShelfView for
200 // handling.
201 set_interactive(false);
204 ShelfButton::IconView::~IconView() {
207 ////////////////////////////////////////////////////////////////////////////////
208 // ShelfButton
210 // static
211 const char ShelfButton::kViewClassName[] = "ash/ShelfButton";
213 ShelfButton* ShelfButton::Create(views::ButtonListener* listener,
214 ShelfButtonHost* host,
215 ShelfLayoutManager* shelf_layout_manager) {
216 ShelfButton* button = new ShelfButton(listener, host, shelf_layout_manager);
217 button->Init();
218 return button;
221 ShelfButton::ShelfButton(views::ButtonListener* listener,
222 ShelfButtonHost* host,
223 ShelfLayoutManager* shelf_layout_manager)
224 : CustomButton(listener),
225 host_(host),
226 icon_view_(NULL),
227 bar_(new BarView(this)),
228 state_(STATE_NORMAL),
229 shelf_layout_manager_(shelf_layout_manager),
230 destroyed_flag_(NULL) {
231 SetAccessibilityFocusable(true);
233 const gfx::ShadowValue kShadows[] = {
234 gfx::ShadowValue(gfx::Vector2d(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
235 gfx::ShadowValue(gfx::Vector2d(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
236 gfx::ShadowValue(gfx::Vector2d(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
238 icon_shadows_.assign(kShadows, kShadows + arraysize(kShadows));
240 AddChildView(bar_);
243 ShelfButton::~ShelfButton() {
244 if (destroyed_flag_)
245 *destroyed_flag_ = true;
248 void ShelfButton::SetShadowedImage(const gfx::ImageSkia& image) {
249 icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
250 image, icon_shadows_));
253 void ShelfButton::SetImage(const gfx::ImageSkia& image) {
254 if (image.isNull()) {
255 // TODO: need an empty image.
256 icon_view_->SetImage(image);
257 return;
260 if (icon_view_->icon_size() == 0) {
261 SetShadowedImage(image);
262 return;
265 // Resize the image maintaining our aspect ratio.
266 int pref = icon_view_->icon_size();
267 float aspect_ratio =
268 static_cast<float>(image.width()) / static_cast<float>(image.height());
269 int height = pref;
270 int width = static_cast<int>(aspect_ratio * height);
271 if (width > pref) {
272 width = pref;
273 height = static_cast<int>(width / aspect_ratio);
276 if (width == image.width() && height == image.height()) {
277 SetShadowedImage(image);
278 return;
281 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(image,
282 skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height)));
285 const gfx::ImageSkia& ShelfButton::GetImage() const {
286 return icon_view_->GetImage();
289 void ShelfButton::AddState(State state) {
290 if (!(state_ & state)) {
291 state_ |= state;
292 Layout();
293 if (state & STATE_ATTENTION)
294 bar_->ShowAttention(true);
298 void ShelfButton::ClearState(State state) {
299 if (state_ & state) {
300 state_ &= ~state;
301 Layout();
302 if (state & STATE_ATTENTION)
303 bar_->ShowAttention(false);
307 gfx::Rect ShelfButton::GetIconBounds() const {
308 return icon_view_->bounds();
311 void ShelfButton::ShowContextMenu(const gfx::Point& p,
312 ui::MenuSourceType source_type) {
313 if (!context_menu_controller())
314 return;
316 bool destroyed = false;
317 destroyed_flag_ = &destroyed;
319 CustomButton::ShowContextMenu(p, source_type);
321 if (!destroyed) {
322 destroyed_flag_ = NULL;
323 // The menu will not propagate mouse events while its shown. To address,
324 // the hover state gets cleared once the menu was shown (and this was not
325 // destroyed).
326 ClearState(STATE_HOVERED);
330 const char* ShelfButton::GetClassName() const {
331 return kViewClassName;
334 bool ShelfButton::OnMousePressed(const ui::MouseEvent& event) {
335 CustomButton::OnMousePressed(event);
336 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
337 return true;
340 void ShelfButton::OnMouseReleased(const ui::MouseEvent& event) {
341 CustomButton::OnMouseReleased(event);
342 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
345 void ShelfButton::OnMouseCaptureLost() {
346 ClearState(STATE_HOVERED);
347 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
348 CustomButton::OnMouseCaptureLost();
351 bool ShelfButton::OnMouseDragged(const ui::MouseEvent& event) {
352 CustomButton::OnMouseDragged(event);
353 host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
354 return true;
357 void ShelfButton::OnMouseMoved(const ui::MouseEvent& event) {
358 CustomButton::OnMouseMoved(event);
359 host_->MouseMovedOverButton(this);
362 void ShelfButton::OnMouseEntered(const ui::MouseEvent& event) {
363 AddState(STATE_HOVERED);
364 CustomButton::OnMouseEntered(event);
365 host_->MouseEnteredButton(this);
368 void ShelfButton::OnMouseExited(const ui::MouseEvent& event) {
369 ClearState(STATE_HOVERED);
370 CustomButton::OnMouseExited(event);
371 host_->MouseExitedButton(this);
374 void ShelfButton::GetAccessibleState(ui::AXViewState* state) {
375 state->role = ui::AX_ROLE_BUTTON;
376 state->name = host_->GetAccessibleName(this);
379 void ShelfButton::Layout() {
380 const gfx::Rect button_bounds(GetContentsBounds());
381 int icon_pad =
382 shelf_layout_manager_->GetAlignment() != SHELF_ALIGNMENT_BOTTOM ?
383 kIconPadVertical : kIconPad;
384 int x_offset = shelf_layout_manager_->PrimaryAxisValue(0, icon_pad);
385 int y_offset = shelf_layout_manager_->PrimaryAxisValue(icon_pad, 0);
387 int icon_width = std::min(kIconSize,
388 button_bounds.width() - x_offset);
389 int icon_height = std::min(kIconSize,
390 button_bounds.height() - y_offset);
392 // If on the left or top 'invert' the inset so the constant gap is on
393 // the interior (towards the center of display) edge of the shelf.
394 if (SHELF_ALIGNMENT_LEFT == shelf_layout_manager_->GetAlignment())
395 x_offset = button_bounds.width() - (kIconSize + icon_pad);
397 if (SHELF_ALIGNMENT_TOP == shelf_layout_manager_->GetAlignment())
398 y_offset = button_bounds.height() - (kIconSize + icon_pad);
400 // Center icon with respect to the secondary axis, and ensure
401 // that the icon doesn't occlude the bar highlight.
402 if (shelf_layout_manager_->IsHorizontalAlignment()) {
403 x_offset = std::max(0, button_bounds.width() - icon_width) / 2;
404 if (y_offset + icon_height + kBarSize > button_bounds.height())
405 icon_height = button_bounds.height() - (y_offset + kBarSize);
406 } else {
407 y_offset = std::max(0, button_bounds.height() - icon_height) / 2;
408 if (x_offset + icon_width + kBarSize > button_bounds.width())
409 icon_width = button_bounds.width() - (x_offset + kBarSize);
412 // Expand bounds to include shadows.
413 gfx::Insets insets_shadows = gfx::ShadowValue::GetMargin(icon_shadows_);
414 // Adjust offsets to center icon, not icon + shadow.
415 x_offset += (insets_shadows.left() - insets_shadows.right()) / 2;
416 y_offset += (insets_shadows.top() - insets_shadows.bottom()) / 2;
417 gfx::Rect icon_view_bounds =
418 gfx::Rect(button_bounds.x() + x_offset, button_bounds.y() + y_offset,
419 icon_width, icon_height);
420 icon_view_bounds.Inset(insets_shadows);
421 icon_view_->SetBoundsRect(icon_view_bounds);
423 // Icon size has been incorrect when running
424 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
425 // http://crbug.com/234854.
426 DCHECK_LE(icon_width, kIconSize);
427 DCHECK_LE(icon_height, kIconSize);
429 bar_->SetBarBoundsRect(button_bounds);
431 UpdateState();
434 void ShelfButton::ChildPreferredSizeChanged(views::View* child) {
435 Layout();
438 void ShelfButton::OnFocus() {
439 AddState(STATE_FOCUSED);
440 CustomButton::OnFocus();
443 void ShelfButton::OnBlur() {
444 ClearState(STATE_FOCUSED);
445 CustomButton::OnBlur();
448 void ShelfButton::OnPaint(gfx::Canvas* canvas) {
449 CustomButton::OnPaint(canvas);
450 if (HasFocus()) {
451 gfx::Rect paint_bounds(GetLocalBounds());
452 paint_bounds.Inset(1, 1, 1, 1);
453 canvas->DrawSolidFocusRect(paint_bounds, kFocusBorderColor);
457 void ShelfButton::OnGestureEvent(ui::GestureEvent* event) {
458 switch (event->type()) {
459 case ui::ET_GESTURE_TAP_DOWN:
460 AddState(STATE_HOVERED);
461 return CustomButton::OnGestureEvent(event);
462 case ui::ET_GESTURE_END:
463 ClearState(STATE_HOVERED);
464 return CustomButton::OnGestureEvent(event);
465 case ui::ET_GESTURE_SCROLL_BEGIN:
466 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event);
467 event->SetHandled();
468 return;
469 case ui::ET_GESTURE_SCROLL_UPDATE:
470 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event);
471 event->SetHandled();
472 return;
473 case ui::ET_GESTURE_SCROLL_END:
474 case ui::ET_SCROLL_FLING_START:
475 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false);
476 event->SetHandled();
477 return;
478 default:
479 return CustomButton::OnGestureEvent(event);
483 void ShelfButton::Init() {
484 icon_view_ = CreateIconView();
486 // TODO: refactor the layers so each button doesn't require 2.
487 icon_view_->SetPaintToLayer(true);
488 icon_view_->SetFillsBoundsOpaquely(false);
489 icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
490 icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
492 AddChildView(icon_view_);
495 ShelfButton::IconView* ShelfButton::CreateIconView() {
496 return new IconView;
499 bool ShelfButton::IsShelfHorizontal() const {
500 return shelf_layout_manager_->IsHorizontalAlignment();
503 void ShelfButton::UpdateState() {
504 UpdateBar();
506 icon_view_->SetHorizontalAlignment(
507 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::CENTER,
508 views::ImageView::LEADING));
509 icon_view_->SetVerticalAlignment(
510 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::LEADING,
511 views::ImageView::CENTER));
512 SchedulePaint();
515 void ShelfButton::UpdateBar() {
516 if (state_ & STATE_HIDDEN) {
517 bar_->SetVisible(false);
518 return;
521 int bar_id = 0;
522 if (state_ & (STATE_ACTIVE | STATE_ATTENTION))
523 bar_id = IDR_ASH_SHELF_UNDERLINE_ACTIVE;
524 else if (state_ & STATE_RUNNING)
525 bar_id = IDR_ASH_SHELF_UNDERLINE_RUNNING;
527 if (bar_id != 0) {
528 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
529 const gfx::ImageSkia* image = rb.GetImageNamed(bar_id).ToImageSkia();
530 if (shelf_layout_manager_->GetAlignment() == SHELF_ALIGNMENT_BOTTOM) {
531 bar_->SetImage(*image);
532 } else {
533 bar_->SetImage(gfx::ImageSkiaOperations::CreateRotatedImage(*image,
534 shelf_layout_manager_->SelectValueForShelfAlignment(
535 SkBitmapOperations::ROTATION_90_CW,
536 SkBitmapOperations::ROTATION_90_CW,
537 SkBitmapOperations::ROTATION_270_CW,
538 SkBitmapOperations::ROTATION_180_CW)));
540 bar_->SetHorizontalAlignment(
541 shelf_layout_manager_->SelectValueForShelfAlignment(
542 views::ImageView::CENTER,
543 views::ImageView::LEADING,
544 views::ImageView::TRAILING,
545 views::ImageView::CENTER));
546 bar_->SetVerticalAlignment(
547 shelf_layout_manager_->SelectValueForShelfAlignment(
548 views::ImageView::TRAILING,
549 views::ImageView::CENTER,
550 views::ImageView::CENTER,
551 views::ImageView::LEADING));
552 bar_->SchedulePaint();
555 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL);
558 } // namespace ash