1 // Copyright (c) 2012 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/wm/gestures/long_press_affordance_handler.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/root_window_controller.h"
10 #include "ash/shell_window_ids.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #include "third_party/skia/include/core/SkPaint.h"
13 #include "third_party/skia/include/core/SkPath.h"
14 #include "third_party/skia/include/core/SkRect.h"
15 #include "third_party/skia/include/effects/SkGradientShader.h"
16 #include "ui/aura/client/screen_position_client.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/events/gesture_detection/gesture_configuration.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/screen.h"
23 #include "ui/gfx/transform.h"
24 #include "ui/views/view.h"
25 #include "ui/views/widget/widget.h"
30 const int kAffordanceOuterRadius
= 60;
31 const int kAffordanceInnerRadius
= 50;
33 // Angles from x-axis at which the outer and inner circles start.
34 const int kAffordanceOuterStartAngle
= -109;
35 const int kAffordanceInnerStartAngle
= -65;
37 const int kAffordanceGlowWidth
= 20;
38 // The following is half width to avoid division by 2.
39 const int kAffordanceArcWidth
= 3;
41 // Start and end values for various animations.
42 const double kAffordanceScaleStartValue
= 0.8;
43 const double kAffordanceScaleEndValue
= 1.0;
44 const double kAffordanceShrinkScaleEndValue
= 0.5;
45 const double kAffordanceOpacityStartValue
= 0.1;
46 const double kAffordanceOpacityEndValue
= 0.5;
47 const int kAffordanceAngleStartValue
= 0;
48 // The end angle is a bit greater than 360 to make sure the circle completes at
49 // the end of the animation.
50 const int kAffordanceAngleEndValue
= 380;
51 const int kAffordanceDelayBeforeShrinkMs
= 200;
52 const int kAffordanceShrinkAnimationDurationMs
= 100;
55 const SkColor kAffordanceGlowStartColor
= SkColorSetARGB(24, 255, 255, 255);
56 const SkColor kAffordanceGlowEndColor
= SkColorSetARGB(0, 255, 255, 255);
57 const SkColor kAffordanceArcColor
= SkColorSetARGB(80, 0, 0, 0);
58 const int kAffordanceFrameRateHz
= 60;
60 views::Widget
* CreateAffordanceWidget(aura::Window
* root_window
) {
61 views::Widget
* widget
= new views::Widget
;
62 views::Widget::InitParams params
;
63 params
.type
= views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
;
64 params
.keep_on_top
= true;
65 params
.accept_events
= false;
66 params
.activatable
= views::Widget::InitParams::ACTIVATABLE_NO
;
67 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
68 params
.context
= root_window
;
69 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
71 widget
->SetOpacity(0xFF);
72 GetRootWindowController(root_window
)->GetContainer(
73 kShellWindowId_OverlayContainer
)->AddChild(widget
->GetNativeWindow());
77 void PaintAffordanceArc(gfx::Canvas
* canvas
,
83 paint
.setStyle(SkPaint::kStroke_Style
);
84 paint
.setStrokeWidth(2 * kAffordanceArcWidth
);
85 paint
.setColor(kAffordanceArcColor
);
86 paint
.setAntiAlias(true);
89 arc_path
.addArc(SkRect::MakeXYWH(center
.x() - radius
,
93 start_angle
, end_angle
);
94 canvas
->DrawPath(arc_path
, paint
);
97 void PaintAffordanceGlow(gfx::Canvas
* canvas
,
105 int radius
= (end_radius
+ start_radius
) / 2;
106 int glow_width
= end_radius
- start_radius
;
107 sk_center
.iset(center
.x(), center
.y());
108 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(
109 SkGradientShader::CreateTwoPointRadial(
111 SkIntToScalar(start_radius
),
113 SkIntToScalar(end_radius
),
117 SkShader::kClamp_TileMode
));
120 paint
.setStyle(SkPaint::kStroke_Style
);
121 paint
.setStrokeWidth(glow_width
);
122 paint
.setShader(shader
.get());
123 paint
.setAntiAlias(true);
125 arc_path
.addArc(SkRect::MakeXYWH(center
.x() - radius
,
130 canvas
->DrawPath(arc_path
, paint
);
135 // View of the LongPressAffordanceHandler. Draws the actual contents and
136 // updates as the animation proceeds. It also maintains the views::Widget that
137 // the animation is shown in.
138 class LongPressAffordanceHandler::LongPressAffordanceView
139 : public views::View
{
141 LongPressAffordanceView(const gfx::Point
& event_location
,
142 aura::Window
* root_window
)
144 widget_(CreateAffordanceWidget(root_window
)),
145 current_angle_(kAffordanceAngleStartValue
),
146 current_scale_(kAffordanceScaleStartValue
) {
147 widget_
->SetContentsView(this);
148 widget_
->SetAlwaysOnTop(true);
150 // We are owned by the LongPressAffordance.
151 set_owned_by_client();
152 gfx::Point point
= event_location
;
153 aura::client::GetScreenPositionClient(root_window
)->ConvertPointToScreen(
154 root_window
, &point
);
155 widget_
->SetBounds(gfx::Rect(
156 point
.x() - (kAffordanceOuterRadius
+ kAffordanceGlowWidth
),
157 point
.y() - (kAffordanceOuterRadius
+ kAffordanceGlowWidth
),
158 GetPreferredSize().width(),
159 GetPreferredSize().height()));
161 widget_
->GetNativeView()->layer()->SetOpacity(kAffordanceOpacityStartValue
);
164 ~LongPressAffordanceView() override
{}
166 void UpdateWithGrowAnimation(gfx::Animation
* animation
) {
167 // Update the portion of the circle filled so far and re-draw.
168 current_angle_
= animation
->CurrentValueBetween(kAffordanceAngleStartValue
,
169 kAffordanceAngleEndValue
);
170 current_scale_
= animation
->CurrentValueBetween(kAffordanceScaleStartValue
,
171 kAffordanceScaleEndValue
);
172 widget_
->GetNativeView()->layer()->SetOpacity(
173 animation
->CurrentValueBetween(kAffordanceOpacityStartValue
,
174 kAffordanceOpacityEndValue
));
178 void UpdateWithShrinkAnimation(gfx::Animation
* animation
) {
179 current_scale_
= animation
->CurrentValueBetween(kAffordanceScaleEndValue
,
180 kAffordanceShrinkScaleEndValue
);
181 widget_
->GetNativeView()->layer()->SetOpacity(
182 animation
->CurrentValueBetween(kAffordanceOpacityEndValue
,
183 kAffordanceOpacityStartValue
));
188 // Overridden from views::View.
189 gfx::Size
GetPreferredSize() const override
{
190 return gfx::Size(2 * (kAffordanceOuterRadius
+ kAffordanceGlowWidth
),
191 2 * (kAffordanceOuterRadius
+ kAffordanceGlowWidth
));
194 void OnPaint(gfx::Canvas
* canvas
) override
{
195 gfx::Point
center(GetPreferredSize().width() / 2,
196 GetPreferredSize().height() / 2);
199 gfx::Transform scale
;
200 scale
.Scale(current_scale_
, current_scale_
);
201 // We want to scale from the center.
202 canvas
->Translate(center
.OffsetFromOrigin());
203 canvas
->Transform(scale
);
204 canvas
->Translate(-center
.OffsetFromOrigin());
206 // Paint affordance glow
207 int start_radius
= kAffordanceInnerRadius
- kAffordanceGlowWidth
;
208 int end_radius
= kAffordanceOuterRadius
+ kAffordanceGlowWidth
;
209 const int num_colors
= 3;
210 SkScalar pos
[num_colors
] = {0, 0.5, 1};
211 SkColor colors
[num_colors
] = {kAffordanceGlowEndColor
,
212 kAffordanceGlowStartColor
, kAffordanceGlowEndColor
};
213 PaintAffordanceGlow(canvas
, center
, start_radius
, end_radius
, colors
, pos
,
216 // Paint inner circle.
217 PaintAffordanceArc(canvas
, center
, kAffordanceInnerRadius
,
218 kAffordanceInnerStartAngle
, -current_angle_
);
219 // Paint outer circle.
220 PaintAffordanceArc(canvas
, center
, kAffordanceOuterRadius
,
221 kAffordanceOuterStartAngle
, current_angle_
);
226 scoped_ptr
<views::Widget
> widget_
;
228 double current_scale_
;
230 DISALLOW_COPY_AND_ASSIGN(LongPressAffordanceView
);
233 ////////////////////////////////////////////////////////////////////////////////
234 // LongPressAffordanceHandler, public
236 LongPressAffordanceHandler::LongPressAffordanceHandler()
237 : gfx::LinearAnimation(kAffordanceFrameRateHz
, NULL
),
238 tap_down_target_(NULL
),
239 current_animation_type_(NONE
) {}
241 LongPressAffordanceHandler::~LongPressAffordanceHandler() {
245 void LongPressAffordanceHandler::ProcessEvent(aura::Window
* target
,
246 ui::GestureEvent
* event
) {
247 // Once we have a target, we are only interested in events with that target.
248 if (tap_down_target_
&& tap_down_target_
!= target
)
250 switch (event
->type()) {
251 case ui::ET_GESTURE_TAP_DOWN
: {
252 // Start timer that will start animation on "semi-long-press".
253 tap_down_location_
= event
->root_location();
254 SetTapDownTarget(target
);
255 current_animation_type_
= GROW_ANIMATION
;
256 timer_
.Start(FROM_HERE
,
257 base::TimeDelta::FromMilliseconds(
258 ui::GestureConfiguration::GetInstance()
259 ->semi_long_press_time_in_ms()),
261 &LongPressAffordanceHandler::StartAnimation
);
264 case ui::ET_GESTURE_TAP
:
265 case ui::ET_GESTURE_TAP_CANCEL
:
268 case ui::ET_GESTURE_LONG_PRESS
:
276 ////////////////////////////////////////////////////////////////////////////////
277 // LongPressAffordanceHandler, private
279 void LongPressAffordanceHandler::StartAnimation() {
280 switch (current_animation_type_
) {
281 case GROW_ANIMATION
: {
282 aura::Window
* root_window
= tap_down_target_
->GetRootWindow();
287 view_
.reset(new LongPressAffordanceView(tap_down_location_
, root_window
));
289 ui::GestureConfiguration::GetInstance()->long_press_time_in_ms() -
290 ui::GestureConfiguration::GetInstance()
291 ->semi_long_press_time_in_ms() -
292 kAffordanceDelayBeforeShrinkMs
);
296 case SHRINK_ANIMATION
:
297 SetDuration(kAffordanceShrinkAnimationDurationMs
);
306 void LongPressAffordanceHandler::StopAffordance() {
307 if (timer_
.IsRunning())
309 // Since, Animation::Stop() calls AnimationStopped(), we need to reset the
310 // |current_animation_type_| before Stop(), otherwise AnimationStopped() may
311 // start the timer again.
312 current_animation_type_
= NONE
;
315 SetTapDownTarget(NULL
);
318 void LongPressAffordanceHandler::SetTapDownTarget(aura::Window
* target
) {
319 if (tap_down_target_
== target
)
322 if (tap_down_target_
)
323 tap_down_target_
->RemoveObserver(this);
324 tap_down_target_
= target
;
325 if (tap_down_target_
)
326 tap_down_target_
->AddObserver(this);
329 void LongPressAffordanceHandler::AnimateToState(double state
) {
331 switch (current_animation_type_
) {
333 view_
->UpdateWithGrowAnimation(this);
335 case SHRINK_ANIMATION
:
336 view_
->UpdateWithShrinkAnimation(this);
344 void LongPressAffordanceHandler::AnimationStopped() {
345 switch (current_animation_type_
) {
347 current_animation_type_
= SHRINK_ANIMATION
;
348 timer_
.Start(FROM_HERE
,
349 base::TimeDelta::FromMilliseconds(kAffordanceDelayBeforeShrinkMs
),
350 this, &LongPressAffordanceHandler::StartAnimation
);
352 case SHRINK_ANIMATION
:
353 current_animation_type_
= NONE
;
354 // fall through to reset the view.
357 SetTapDownTarget(NULL
);
362 void LongPressAffordanceHandler::OnWindowDestroying(aura::Window
* window
) {
363 DCHECK_EQ(tap_down_target_
, window
);