Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: ash/shelf/shelf_button.cc

Issue 2187953002: mash: Migrate ShelfButton* to wm common types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ash/shelf/shelf_button.h"
6
7 #include <algorithm>
8
9 #include "ash/common/ash_constants.h"
10 #include "ash/common/ash_switches.h"
11 #include "ash/common/material_design/material_design_controller.h"
12 #include "ash/common/shelf/ink_drop_button_listener.h"
13 #include "ash/common/shelf/shelf_constants.h"
14 #include "ash/shelf/shelf.h"
15 #include "ash/shelf/shelf_layout_manager.h"
16 #include "ash/shelf/shelf_view.h"
17 #include "base/time/time.h"
18 #include "grit/ash_resources.h"
19 #include "skia/ext/image_operations.h"
20 #include "third_party/skia/include/core/SkPaint.h"
21 #include "ui/accessibility/ax_view_state.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/compositor/layer.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/event_constants.h"
26 #include "ui/gfx/animation/animation_delegate.h"
27 #include "ui/gfx/animation/throb_animation.h"
28 #include "ui/gfx/canvas.h"
29 #include "ui/gfx/geometry/vector2d.h"
30 #include "ui/gfx/image/image.h"
31 #include "ui/gfx/image/image_skia_operations.h"
32 #include "ui/gfx/skbitmap_operations.h"
33 #include "ui/views/animation/square_ink_drop_ripple.h"
34 #include "ui/views/controls/image_view.h"
35
36 namespace {
37
38 // Size of the bar. This is along the opposite axis of the shelf. For example,
39 // if the shelf is aligned horizontally then this is the height of the bar.
40 const int kBarSize = 3;
41 const int kIconSize = 32;
42 const int kIconPad = 5;
43 const int kIconPadVertical = 6;
44 const int kAttentionThrobDurationMS = 800;
45 const int kMaxAnimationSeconds = 10;
46 const int kIndicatorOffsetFromBottom = 2;
47 const int kIndicatorRadius = 2;
48 const SkColor kIndicatorColor = SK_ColorWHITE;
49
50 // Canvas scale to ensure that the activity indicator is not pixelated even at
51 // the highest possible device scale factors.
52 const int kIndicatorCanvasScale = 5;
53
54 // Shelf item ripple constants.
55 const int kInkDropSmallSize = 48;
56 const int kInkDropLargeSize = 60;
57 const int kInkDropLargeCornerRadius = 4;
58
59 // Paints an activity indicator on |canvas| whose |size| is specified in DIP.
60 void PaintIndicatorOnCanvas(gfx::Canvas* canvas, const gfx::Size& size) {
61 SkPaint paint;
62 paint.setColor(kIndicatorColor);
63 paint.setFlags(SkPaint::kAntiAlias_Flag);
64 canvas->DrawCircle(
65 gfx::Point(size.width() / 2,
66 size.height() - kIndicatorOffsetFromBottom - kIndicatorRadius),
67 kIndicatorRadius, paint);
68 }
69
70 // Simple AnimationDelegate that owns a single ThrobAnimation instance to
71 // keep all Draw Attention animations in sync.
72 class ShelfButtonAnimation : public gfx::AnimationDelegate {
73 public:
74 class Observer {
75 public:
76 virtual void AnimationProgressed() = 0;
77
78 protected:
79 virtual ~Observer() {}
80 };
81
82 static ShelfButtonAnimation* GetInstance() {
83 static ShelfButtonAnimation* s_instance = new ShelfButtonAnimation();
84 return s_instance;
85 }
86
87 void AddObserver(Observer* observer) { observers_.AddObserver(observer); }
88
89 void RemoveObserver(Observer* observer) {
90 observers_.RemoveObserver(observer);
91 if (!observers_.might_have_observers())
92 animation_.Stop();
93 }
94
95 int GetAlpha() { return GetThrobAnimation().CurrentValueBetween(0, 255); }
96
97 double GetAnimation() { return GetThrobAnimation().GetCurrentValue(); }
98
99 private:
100 ShelfButtonAnimation() : animation_(this) {
101 animation_.SetThrobDuration(kAttentionThrobDurationMS);
102 animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT);
103 }
104
105 ~ShelfButtonAnimation() override {}
106
107 gfx::ThrobAnimation& GetThrobAnimation() {
108 if (!animation_.is_animating()) {
109 animation_.Reset();
110 animation_.StartThrobbing(-1 /*throb indefinitely*/);
111 }
112 return animation_;
113 }
114
115 // gfx::AnimationDelegate
116 void AnimationProgressed(const gfx::Animation* animation) override {
117 if (animation != &animation_)
118 return;
119 if (!animation_.is_animating())
120 return;
121 FOR_EACH_OBSERVER(Observer, observers_, AnimationProgressed());
122 }
123
124 gfx::ThrobAnimation animation_;
125 base::ObserverList<Observer> observers_;
126
127 DISALLOW_COPY_AND_ASSIGN(ShelfButtonAnimation);
128 };
129
130 } // namespace
131
132 namespace ash {
133
134 ////////////////////////////////////////////////////////////////////////////////
135 // ShelfButton::BarView
136
137 class ShelfButton::BarView : public views::ImageView,
138 public ShelfButtonAnimation::Observer {
139 public:
140 BarView(Shelf* shelf)
141 : shelf_(shelf),
142 show_attention_(false),
143 animation_end_time_(base::TimeTicks()),
144 animating_(false) {
145 // Make sure the events reach the parent view for handling.
146 set_interactive(false);
147 }
148
149 ~BarView() override {
150 if (show_attention_)
151 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
152 }
153
154 // views::View:
155 void OnPaint(gfx::Canvas* canvas) override {
156 if (show_attention_) {
157 int alpha =
158 animating_ ? ShelfButtonAnimation::GetInstance()->GetAlpha() : 255;
159 canvas->SaveLayerAlpha(alpha);
160 views::ImageView::OnPaint(canvas);
161 canvas->Restore();
162 } else {
163 views::ImageView::OnPaint(canvas);
164 }
165 }
166
167 // ShelfButtonAnimation::Observer
168 void AnimationProgressed() override {
169 UpdateBounds();
170 SchedulePaint();
171 }
172
173 void SetBarBoundsRect(const gfx::Rect& bounds) {
174 base_bounds_ = bounds;
175 UpdateBounds();
176 }
177
178 void ShowAttention(bool show) {
179 if (show_attention_ != show) {
180 show_attention_ = show;
181 if (show_attention_) {
182 animating_ = true;
183 animation_end_time_ =
184 base::TimeTicks::Now() +
185 base::TimeDelta::FromSeconds(kMaxAnimationSeconds);
186 ShelfButtonAnimation::GetInstance()->AddObserver(this);
187 } else {
188 animating_ = false;
189 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
190 }
191 }
192 UpdateBounds();
193 }
194
195 private:
196 void UpdateBounds() {
197 gfx::Rect bounds = base_bounds_;
198 if (show_attention_) {
199 // Scale from .35 to 1.0 of the total width (which is wider than the
200 // visible width of the image), so the animation "rests" briefly at full
201 // visible width. Cap bounds length at kIconSize to prevent visual
202 // flutter while centering bar within further expanding bounds.
203 double animation =
204 animating_ ? ShelfButtonAnimation::GetInstance()->GetAnimation()
205 : 1.0;
206 double scale = .35 + .65 * animation;
207 if (shelf_->IsHorizontalAlignment()) {
208 int width = base_bounds_.width() * scale;
209 bounds.set_width(std::min(width, kIconSize));
210 int x_offset = (base_bounds_.width() - bounds.width()) / 2;
211 bounds.set_x(base_bounds_.x() + x_offset);
212 UpdateAnimating(bounds.width() == kIconSize);
213 } else {
214 int height = base_bounds_.height() * scale;
215 bounds.set_height(std::min(height, kIconSize));
216 int y_offset = (base_bounds_.height() - bounds.height()) / 2;
217 bounds.set_y(base_bounds_.y() + y_offset);
218 UpdateAnimating(bounds.height() == kIconSize);
219 }
220 }
221 SetBoundsRect(bounds);
222 }
223
224 void UpdateAnimating(bool max_length) {
225 if (!max_length)
226 return;
227 if (base::TimeTicks::Now() > animation_end_time_) {
228 animating_ = false;
229 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
230 }
231 }
232
233 Shelf* shelf_;
234 bool show_attention_;
235 base::TimeTicks animation_end_time_; // For attention throbbing underline.
236 bool animating_; // Is time-limited attention animation running?
237 gfx::Rect base_bounds_;
238
239 DISALLOW_COPY_AND_ASSIGN(BarView);
240 };
241
242 ////////////////////////////////////////////////////////////////////////////////
243 // ShelfButton
244
245 // static
246 const char ShelfButton::kViewClassName[] = "ash/ShelfButton";
247
248 ShelfButton::ShelfButton(InkDropButtonListener* listener, ShelfView* shelf_view)
249 : CustomButton(nullptr),
250 listener_(listener),
251 shelf_view_(shelf_view),
252 icon_view_(new views::ImageView()),
253 bar_(new BarView(shelf_view->shelf())),
254 state_(STATE_NORMAL),
255 destroyed_flag_(nullptr) {
256 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
257 if (ash::MaterialDesignController::IsShelfMaterial()) {
258 SetInkDropMode(InkDropMode::ON);
259 set_ink_drop_base_color(kShelfInkDropBaseColor);
260 set_ink_drop_visible_opacity(kShelfInkDropVisibleOpacity);
261 }
262
263 const gfx::ShadowValue kShadows[] = {
264 gfx::ShadowValue(gfx::Vector2d(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
265 gfx::ShadowValue(gfx::Vector2d(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
266 gfx::ShadowValue(gfx::Vector2d(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
267 };
268 icon_shadows_.assign(kShadows, kShadows + arraysize(kShadows));
269
270 // TODO: refactor the layers so each button doesn't require 2.
271 icon_view_->SetPaintToLayer(true);
272 icon_view_->layer()->SetFillsBoundsOpaquely(false);
273 icon_view_->SetHorizontalAlignment(views::ImageView::CENTER);
274 icon_view_->SetVerticalAlignment(views::ImageView::LEADING);
275 // Do not make this interactive, so that events are sent to ShelfView.
276 icon_view_->set_interactive(false);
277
278 AddChildView(bar_);
279 AddChildView(icon_view_);
280 }
281
282 ShelfButton::~ShelfButton() {
283 if (destroyed_flag_)
284 *destroyed_flag_ = true;
285 }
286
287 void ShelfButton::SetShadowedImage(const gfx::ImageSkia& image) {
288 icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
289 image, icon_shadows_));
290 }
291
292 void ShelfButton::SetImage(const gfx::ImageSkia& image) {
293 if (image.isNull()) {
294 // TODO: need an empty image.
295 icon_view_->SetImage(image);
296 return;
297 }
298
299 // Resize the image maintaining our aspect ratio.
300 float aspect_ratio =
301 static_cast<float>(image.width()) / static_cast<float>(image.height());
302 int height = kIconSize;
303 int width = static_cast<int>(aspect_ratio * height);
304 if (width > kIconSize) {
305 width = kIconSize;
306 height = static_cast<int>(width / aspect_ratio);
307 }
308
309 if (width == image.width() && height == image.height()) {
310 SetShadowedImage(image);
311 return;
312 }
313
314 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(
315 image, skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height)));
316 }
317
318 const gfx::ImageSkia& ShelfButton::GetImage() const {
319 return icon_view_->GetImage();
320 }
321
322 void ShelfButton::AddState(State state) {
323 if (!(state_ & state)) {
324 state_ |= state;
325 Layout();
326 if (state & STATE_ATTENTION)
327 bar_->ShowAttention(true);
328 }
329 }
330
331 void ShelfButton::ClearState(State state) {
332 if (state_ & state) {
333 state_ &= ~state;
334 Layout();
335 if (state & STATE_ATTENTION)
336 bar_->ShowAttention(false);
337 }
338 }
339
340 gfx::Rect ShelfButton::GetIconBounds() const {
341 return icon_view_->bounds();
342 }
343
344 void ShelfButton::OnDragStarted(const ui::LocatedEvent* event) {
345 AnimateInkDrop(views::InkDropState::HIDDEN, event);
346 }
347
348 void ShelfButton::ShowContextMenu(const gfx::Point& p,
349 ui::MenuSourceType source_type) {
350 if (!context_menu_controller())
351 return;
352
353 bool destroyed = false;
354 destroyed_flag_ = &destroyed;
355
356 CustomButton::ShowContextMenu(p, source_type);
357
358 if (!destroyed) {
359 destroyed_flag_ = nullptr;
360 // The menu will not propagate mouse events while its shown. To address,
361 // the hover state gets cleared once the menu was shown (and this was not
362 // destroyed).
363 ClearState(STATE_HOVERED);
364 }
365 }
366
367 const char* ShelfButton::GetClassName() const {
368 return kViewClassName;
369 }
370
371 bool ShelfButton::OnMousePressed(const ui::MouseEvent& event) {
372 CustomButton::OnMousePressed(event);
373 shelf_view_->PointerPressedOnButton(this, ShelfView::MOUSE, event);
374 return true;
375 }
376
377 void ShelfButton::OnMouseReleased(const ui::MouseEvent& event) {
378 CustomButton::OnMouseReleased(event);
379 shelf_view_->PointerReleasedOnButton(this, ShelfView::MOUSE, false);
380 }
381
382 void ShelfButton::OnMouseCaptureLost() {
383 ClearState(STATE_HOVERED);
384 shelf_view_->PointerReleasedOnButton(this, ShelfView::MOUSE, true);
385 CustomButton::OnMouseCaptureLost();
386 }
387
388 bool ShelfButton::OnMouseDragged(const ui::MouseEvent& event) {
389 CustomButton::OnMouseDragged(event);
390 shelf_view_->PointerDraggedOnButton(this, ShelfView::MOUSE, event);
391 return true;
392 }
393
394 void ShelfButton::GetAccessibleState(ui::AXViewState* state) {
395 state->role = ui::AX_ROLE_BUTTON;
396 state->name = shelf_view_->GetTitleForView(this);
397 }
398
399 void ShelfButton::Layout() {
400 const gfx::Rect button_bounds(GetContentsBounds());
401 Shelf* shelf = shelf_view_->shelf();
402 int icon_pad = shelf->PrimaryAxisValue(kIconPad, kIconPadVertical);
403 int x_offset = shelf->PrimaryAxisValue(0, icon_pad);
404 int y_offset = shelf->PrimaryAxisValue(icon_pad, 0);
405
406 int icon_width = std::min(kIconSize, button_bounds.width() - x_offset);
407 int icon_height = std::min(kIconSize, button_bounds.height() - y_offset);
408
409 // If on the left or top 'invert' the inset so the constant gap is on
410 // the interior (towards the center of display) edge of the shelf.
411 if (SHELF_ALIGNMENT_LEFT == shelf->alignment())
412 x_offset = button_bounds.width() - (kIconSize + icon_pad);
413
414 // Center icon with respect to the secondary axis, and ensure
415 // that the icon doesn't occlude the bar highlight.
416 if (shelf->IsHorizontalAlignment()) {
417 x_offset = std::max(0, button_bounds.width() - icon_width) / 2;
418 if (y_offset + icon_height + kBarSize > button_bounds.height())
419 icon_height = button_bounds.height() - (y_offset + kBarSize);
420 } else {
421 y_offset = std::max(0, button_bounds.height() - icon_height) / 2;
422 if (x_offset + icon_width + kBarSize > button_bounds.width())
423 icon_width = button_bounds.width() - (x_offset + kBarSize);
424 }
425
426 // Expand bounds to include shadows.
427 gfx::Insets insets_shadows = gfx::ShadowValue::GetMargin(icon_shadows_);
428 // Adjust offsets to center icon, not icon + shadow.
429 x_offset += (insets_shadows.left() - insets_shadows.right()) / 2;
430 y_offset += (insets_shadows.top() - insets_shadows.bottom()) / 2;
431 gfx::Rect icon_view_bounds =
432 gfx::Rect(button_bounds.x() + x_offset, button_bounds.y() + y_offset,
433 icon_width, icon_height);
434 icon_view_bounds.Inset(insets_shadows);
435 icon_view_->SetBoundsRect(icon_view_bounds);
436
437 // Icon size has been incorrect when running
438 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
439 // http://crbug.com/234854.
440 DCHECK_LE(icon_width, kIconSize);
441 DCHECK_LE(icon_height, kIconSize);
442
443 bar_->SetBarBoundsRect(button_bounds);
444
445 UpdateState();
446 }
447
448 void ShelfButton::ChildPreferredSizeChanged(views::View* child) {
449 Layout();
450 }
451
452 void ShelfButton::OnFocus() {
453 AddState(STATE_FOCUSED);
454 CustomButton::OnFocus();
455 }
456
457 void ShelfButton::OnBlur() {
458 ClearState(STATE_FOCUSED);
459 CustomButton::OnBlur();
460 }
461
462 void ShelfButton::OnPaint(gfx::Canvas* canvas) {
463 CustomButton::OnPaint(canvas);
464 if (HasFocus()) {
465 gfx::Rect paint_bounds(GetLocalBounds());
466 paint_bounds.Inset(1, 1, 1, 1);
467 canvas->DrawSolidFocusRect(paint_bounds, kFocusBorderColor);
468 }
469 }
470
471 void ShelfButton::OnGestureEvent(ui::GestureEvent* event) {
472 switch (event->type()) {
473 case ui::ET_GESTURE_TAP_DOWN:
474 AddState(STATE_HOVERED);
475 return CustomButton::OnGestureEvent(event);
476 case ui::ET_GESTURE_END:
477 ClearState(STATE_HOVERED);
478 return CustomButton::OnGestureEvent(event);
479 case ui::ET_GESTURE_SCROLL_BEGIN:
480 shelf_view_->PointerPressedOnButton(this, ShelfView::TOUCH, *event);
481 event->SetHandled();
482 return;
483 case ui::ET_GESTURE_SCROLL_UPDATE:
484 shelf_view_->PointerDraggedOnButton(this, ShelfView::TOUCH, *event);
485 event->SetHandled();
486 return;
487 case ui::ET_GESTURE_SCROLL_END:
488 case ui::ET_SCROLL_FLING_START:
489 shelf_view_->PointerReleasedOnButton(this, ShelfView::TOUCH, false);
490 event->SetHandled();
491 return;
492 default:
493 return CustomButton::OnGestureEvent(event);
494 }
495 }
496
497 std::unique_ptr<views::InkDropRipple> ShelfButton::CreateInkDropRipple() const {
498 return base::WrapUnique(new views::SquareInkDropRipple(
499 gfx::Size(kInkDropLargeSize, kInkDropLargeSize),
500 kInkDropLargeCornerRadius,
501 gfx::Size(kInkDropSmallSize, kInkDropSmallSize),
502 kInkDropSmallCornerRadius, GetLocalBounds().CenterPoint(),
503 GetInkDropBaseColor(), ink_drop_visible_opacity()));
504 }
505
506 bool ShelfButton::ShouldEnterPushedState(const ui::Event& event) {
507 if (!shelf_view_->ShouldEventActivateButton(this, event))
508 return false;
509
510 return CustomButton::ShouldEnterPushedState(event);
511 }
512
513 bool ShelfButton::ShouldShowInkDropHighlight() const {
514 return false;
515 }
516
517 void ShelfButton::NotifyClick(const ui::Event& event) {
518 CustomButton::NotifyClick(event);
519 if (listener_)
520 listener_->ButtonPressed(this, event, ink_drop());
521 }
522
523 void ShelfButton::UpdateState() {
524 UpdateBar();
525 Shelf* shelf = shelf_view_->shelf();
526 icon_view_->SetHorizontalAlignment(shelf->PrimaryAxisValue(
527 views::ImageView::CENTER, views::ImageView::LEADING));
528 icon_view_->SetVerticalAlignment(shelf->PrimaryAxisValue(
529 views::ImageView::LEADING, views::ImageView::CENTER));
530 SchedulePaint();
531 }
532
533 void ShelfButton::UpdateBar() {
534 if (state_ & STATE_HIDDEN) {
535 bar_->SetVisible(false);
536 return;
537 }
538
539 int bar_id = 0;
540 if (state_ & (STATE_ACTIVE))
541 bar_id = IDR_ASH_SHELF_UNDERLINE_ACTIVE;
542 else if (state_ & STATE_ATTENTION)
543 bar_id = IDR_ASH_SHELF_UNDERLINE_ATTENTION;
544 else if (state_ & STATE_RUNNING)
545 bar_id = IDR_ASH_SHELF_UNDERLINE_RUNNING;
546
547 if (bar_id != 0) {
548 Shelf* shelf = shelf_view_->shelf();
549 gfx::ImageSkia image;
550 if (ash::MaterialDesignController::IsShelfMaterial()) {
551 if (shelf->shelf_widget()->shelf_layout_manager()->IsVisible()) {
552 gfx::Size size(GetShelfConstant(SHELF_BUTTON_SIZE),
553 GetShelfConstant(SHELF_SIZE));
554 gfx::Canvas canvas(size, kIndicatorCanvasScale, true /* is_opaque */);
555 PaintIndicatorOnCanvas(&canvas, size);
556 image = gfx::ImageSkia(canvas.ExtractImageRep());
557 }
558 } else {
559 ResourceBundle* rb = &ResourceBundle::GetSharedInstance();
560 image = *rb->GetImageNamed(bar_id).ToImageSkia();
561 }
562 if (!shelf->IsHorizontalAlignment()) {
563 image = gfx::ImageSkiaOperations::CreateRotatedImage(
564 image, shelf->alignment() == SHELF_ALIGNMENT_LEFT
565 ? SkBitmapOperations::ROTATION_90_CW
566 : SkBitmapOperations::ROTATION_270_CW);
567 }
568 bar_->SetImage(image);
569 bar_->SetHorizontalAlignment(shelf->SelectValueForShelfAlignment(
570 views::ImageView::CENTER, views::ImageView::LEADING,
571 views::ImageView::TRAILING));
572 bar_->SetVerticalAlignment(shelf->SelectValueForShelfAlignment(
573 views::ImageView::TRAILING, views::ImageView::CENTER,
574 views::ImageView::CENTER));
575 bar_->SchedulePaint();
576 }
577 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL);
578 }
579
580 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698