bluetooth: Initial JNI setup for device/bluetooth.
[chromium-blink-merge.git] / ash / shelf / shelf_button.cc
blob388bf48f8b6fa084b727ed80cdf6f20b2f99c0f0
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 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.
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::Vector2d(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
232 gfx::ShadowValue(gfx::Vector2d(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
233 gfx::ShadowValue(gfx::Vector2d(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 // Expand bounds to include shadows.
410 gfx::Insets insets_shadows = gfx::ShadowValue::GetMargin(icon_shadows_);
411 // Adjust offsets to center icon, not icon + shadow.
412 x_offset += (insets_shadows.left() - insets_shadows.right()) / 2;
413 y_offset += (insets_shadows.top() - insets_shadows.bottom()) / 2;
414 gfx::Rect icon_view_bounds =
415 gfx::Rect(button_bounds.x() + x_offset, button_bounds.y() + y_offset,
416 icon_width, icon_height);
417 icon_view_bounds.Inset(insets_shadows);
418 icon_view_->SetBoundsRect(icon_view_bounds);
420 // Icon size has been incorrect when running
421 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
422 // http://crbug.com/234854.
423 DCHECK_LE(icon_width, kIconSize);
424 DCHECK_LE(icon_height, kIconSize);
426 bar_->SetBarBoundsRect(button_bounds);
428 UpdateState();
431 void ShelfButton::ChildPreferredSizeChanged(views::View* child) {
432 Layout();
435 void ShelfButton::OnFocus() {
436 AddState(STATE_FOCUSED);
437 CustomButton::OnFocus();
440 void ShelfButton::OnBlur() {
441 ClearState(STATE_FOCUSED);
442 CustomButton::OnBlur();
445 void ShelfButton::OnPaint(gfx::Canvas* canvas) {
446 CustomButton::OnPaint(canvas);
447 if (HasFocus()) {
448 gfx::Rect paint_bounds(GetLocalBounds());
449 paint_bounds.Inset(1, 1, 1, 1);
450 canvas->DrawSolidFocusRect(paint_bounds, kFocusBorderColor);
454 void ShelfButton::OnGestureEvent(ui::GestureEvent* event) {
455 switch (event->type()) {
456 case ui::ET_GESTURE_TAP_DOWN:
457 AddState(STATE_HOVERED);
458 return CustomButton::OnGestureEvent(event);
459 case ui::ET_GESTURE_END:
460 ClearState(STATE_HOVERED);
461 return CustomButton::OnGestureEvent(event);
462 case ui::ET_GESTURE_SCROLL_BEGIN:
463 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event);
464 event->SetHandled();
465 return;
466 case ui::ET_GESTURE_SCROLL_UPDATE:
467 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event);
468 event->SetHandled();
469 return;
470 case ui::ET_GESTURE_SCROLL_END:
471 case ui::ET_SCROLL_FLING_START:
472 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false);
473 event->SetHandled();
474 return;
475 default:
476 return CustomButton::OnGestureEvent(event);
480 void ShelfButton::Init() {
481 icon_view_ = CreateIconView();
483 // TODO: refactor the layers so each button doesn't require 2.
484 icon_view_->SetPaintToLayer(true);
485 icon_view_->SetFillsBoundsOpaquely(false);
486 icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
487 icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
489 AddChildView(icon_view_);
492 ShelfButton::IconView* ShelfButton::CreateIconView() {
493 return new IconView;
496 bool ShelfButton::IsShelfHorizontal() const {
497 return shelf_layout_manager_->IsHorizontalAlignment();
500 void ShelfButton::UpdateState() {
501 UpdateBar();
503 icon_view_->SetHorizontalAlignment(
504 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::CENTER,
505 views::ImageView::LEADING));
506 icon_view_->SetVerticalAlignment(
507 shelf_layout_manager_->PrimaryAxisValue(views::ImageView::LEADING,
508 views::ImageView::CENTER));
509 SchedulePaint();
512 void ShelfButton::UpdateBar() {
513 if (state_ & STATE_HIDDEN) {
514 bar_->SetVisible(false);
515 return;
518 int bar_id = 0;
519 if (state_ & STATE_ACTIVE)
520 bar_id = IDR_ASH_SHELF_UNDERLINE_ACTIVE;
521 else if (state_ & STATE_RUNNING)
522 bar_id = IDR_ASH_SHELF_UNDERLINE_RUNNING;
524 if (bar_id != 0) {
525 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
526 const gfx::ImageSkia* image = rb.GetImageNamed(bar_id).ToImageSkia();
527 if (shelf_layout_manager_->GetAlignment() == SHELF_ALIGNMENT_BOTTOM) {
528 bar_->SetImage(*image);
529 } else {
530 bar_->SetImage(gfx::ImageSkiaOperations::CreateRotatedImage(*image,
531 shelf_layout_manager_->SelectValueForShelfAlignment(
532 SkBitmapOperations::ROTATION_90_CW,
533 SkBitmapOperations::ROTATION_90_CW,
534 SkBitmapOperations::ROTATION_270_CW,
535 SkBitmapOperations::ROTATION_180_CW)));
537 bar_->SetHorizontalAlignment(
538 shelf_layout_manager_->SelectValueForShelfAlignment(
539 views::ImageView::CENTER,
540 views::ImageView::LEADING,
541 views::ImageView::TRAILING,
542 views::ImageView::CENTER));
543 bar_->SetVerticalAlignment(
544 shelf_layout_manager_->SelectValueForShelfAlignment(
545 views::ImageView::TRAILING,
546 views::ImageView::CENTER,
547 views::ImageView::CENTER,
548 views::ImageView::LEADING));
549 bar_->SchedulePaint();
552 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL);
555 } // namespace ash