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/workspace/phantom_window_controller.h"
9 #include "ash/ash_switches.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/wm/coordinate_conversion.h"
13 #include "grit/ash_resources.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/skia_util.h"
21 #include "ui/views/background.h"
22 #include "ui/views/painter.h"
23 #include "ui/views/view.h"
24 #include "ui/views/widget/widget.h"
29 // The duration of the show animation.
30 const int kAnimationDurationMs
= 200;
32 // The size of the phantom window at the beginning of the show animation in
33 // relation to the size of the phantom window at the end of the animation when
34 // using the alternate caption button style.
35 const float kAlternateStyleStartBoundsRatio
= 0.85f
;
37 // The amount of pixels that the phantom window's shadow should extend past
38 // the bounds passed into Show(). There is no shadow when not using the
39 // alternate caption button style.
40 const int kAlternateStyleShadowThickness
= 15;
42 // The minimum size of a phantom window including the shadow when using the
43 // alternate caption button style. The minimum size is derived from the size of
44 // the IDR_AURA_PHANTOM_WINDOW image assets.
45 const int kAlternateStyleMinSizeWithShadow
= 100;
47 // Adjusts the phantom window's bounds so that the bounds:
48 // - Include the size of the shadow.
49 // - Have a size equal to or larger than the minimize phantom window size.
50 gfx::Rect
GetAdjustedBoundsForAlternateStyle(const gfx::Rect
& bounds
) {
51 int x_inset
= std::max(
53 ceil((kAlternateStyleMinSizeWithShadow
- bounds
.width()) / 2.0f
)),
54 kAlternateStyleShadowThickness
);
55 int y_inset
= std::max(
57 ceil((kAlternateStyleMinSizeWithShadow
- bounds
.height()) / 2.0f
)),
58 kAlternateStyleShadowThickness
);
60 gfx::Rect
adjusted_bounds(bounds
);
61 adjusted_bounds
.Inset(-x_inset
, -y_inset
);
62 return adjusted_bounds
;
65 // Starts an animation of |widget| to |new_bounds_in_screen|. No-op if |widget|
67 void AnimateToBounds(views::Widget
* widget
,
68 const gfx::Rect
& new_bounds_in_screen
) {
72 ui::ScopedLayerAnimationSettings
scoped_setter(
73 widget
->GetNativeWindow()->layer()->GetAnimator());
74 scoped_setter
.SetTweenType(gfx::Tween::EASE_IN
);
75 scoped_setter
.SetPreemptionStrategy(
76 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
77 scoped_setter
.SetTransitionDuration(
78 base::TimeDelta::FromMilliseconds(kAnimationDurationMs
));
79 widget
->SetBounds(new_bounds_in_screen
);
82 // EdgePainter ----------------------------------------------------------------
84 // Paints the background of the phantom window for window snapping.
85 class EdgePainter
: public views::Painter
{
88 virtual ~EdgePainter();
91 virtual gfx::Size
GetMinimumSize() const OVERRIDE
;
92 virtual void Paint(gfx::Canvas
* canvas
, const gfx::Size
& size
) OVERRIDE
;
95 DISALLOW_COPY_AND_ASSIGN(EdgePainter
);
98 EdgePainter::EdgePainter() {
101 EdgePainter::~EdgePainter() {
104 gfx::Size
EdgePainter::GetMinimumSize() const {
108 void EdgePainter::Paint(gfx::Canvas
* canvas
, const gfx::Size
& size
) {
109 const int kInsetSize
= 4;
112 int w
= size
.width() - kInsetSize
* 2;
113 int h
= size
.height() - kInsetSize
* 2;
114 bool inset
= (w
> 0 && h
> 0);
122 paint
.setColor(SkColorSetARGB(100, 0, 0, 0));
123 paint
.setStyle(SkPaint::kFill_Style
);
124 paint
.setAntiAlias(true);
125 const int kRoundRectSize
= 4;
126 canvas
->sk_canvas()->drawRoundRect(
127 gfx::RectToSkRect(gfx::Rect(x
, y
, w
, h
)),
128 SkIntToScalar(kRoundRectSize
), SkIntToScalar(kRoundRectSize
), paint
);
132 paint
.setColor(SkColorSetARGB(200, 255, 255, 255));
133 paint
.setStyle(SkPaint::kStroke_Style
);
134 paint
.setStrokeWidth(SkIntToScalar(2));
135 canvas
->sk_canvas()->drawRoundRect(
136 gfx::RectToSkRect(gfx::Rect(x
, y
, w
, h
)), SkIntToScalar(kRoundRectSize
),
137 SkIntToScalar(kRoundRectSize
), paint
);
142 // PhantomWindowController ----------------------------------------------------
144 PhantomWindowController::PhantomWindowController(aura::Window
* window
)
146 phantom_below_window_(NULL
) {
149 PhantomWindowController::~PhantomWindowController() {
152 void PhantomWindowController::Show(const gfx::Rect
& bounds_in_screen
) {
153 if (switches::UseAlternateFrameCaptionButtonStyle())
154 ShowAlternate(bounds_in_screen
);
156 ShowLegacy(bounds_in_screen
);
159 void PhantomWindowController::ShowAlternate(const gfx::Rect
& bounds_in_screen
) {
160 gfx::Rect adjusted_bounds_in_screen
=
161 GetAdjustedBoundsForAlternateStyle(bounds_in_screen
);
162 if (adjusted_bounds_in_screen
== target_bounds_in_screen_
)
164 target_bounds_in_screen_
= adjusted_bounds_in_screen
;
166 gfx::Rect start_bounds_in_screen
= target_bounds_in_screen_
;
167 int start_width
= std::max(
168 kAlternateStyleMinSizeWithShadow
,
170 start_bounds_in_screen
.width() * kAlternateStyleStartBoundsRatio
));
171 int start_height
= std::max(
172 kAlternateStyleMinSizeWithShadow
,
174 start_bounds_in_screen
.height() * kAlternateStyleStartBoundsRatio
));
175 start_bounds_in_screen
.Inset(
176 floor((start_bounds_in_screen
.width() - start_width
) / 2.0f
),
177 floor((start_bounds_in_screen
.height() - start_height
) / 2.0f
));
178 phantom_widget_in_target_root_
= CreatePhantomWidget(
179 wm::GetRootWindowMatching(target_bounds_in_screen_
),
180 start_bounds_in_screen
);
182 AnimateToBounds(phantom_widget_in_target_root_
.get(),
183 target_bounds_in_screen_
);
186 void PhantomWindowController::ShowLegacy(const gfx::Rect
& bounds_in_screen
) {
187 if (bounds_in_screen
== target_bounds_in_screen_
)
189 target_bounds_in_screen_
= bounds_in_screen
;
191 gfx::Rect start_bounds_in_screen
;
192 if (!phantom_widget_in_target_root_
) {
193 start_bounds_in_screen
= window_
->GetBoundsInScreen();
195 start_bounds_in_screen
=
196 phantom_widget_in_target_root_
->GetWindowBoundsInScreen();
199 aura::Window
* target_root
=
200 wm::GetRootWindowMatching(target_bounds_in_screen_
);
201 if (!phantom_widget_in_target_root_
||
202 phantom_widget_in_target_root_
->GetNativeWindow()->GetRootWindow() !=
204 phantom_widget_in_target_root_
=
205 CreatePhantomWidget(target_root
, start_bounds_in_screen
);
207 AnimateToBounds(phantom_widget_in_target_root_
.get(),
208 target_bounds_in_screen_
);
210 // Create a secondary widget in a second screen if |start_bounds_in_screen|
211 // lies at least partially in another screen. This allows animations to start
212 // or restart in one root window and progress to another root.
213 aura::Window
* start_root
= wm::GetRootWindowMatching(start_bounds_in_screen
);
214 if (start_root
== target_root
) {
215 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
216 for (size_t i
= 0; i
< root_windows
.size(); ++i
) {
217 if (root_windows
[i
] != target_root
&&
218 root_windows
[i
]->GetBoundsInScreen().Intersects(
219 start_bounds_in_screen
)) {
220 start_root
= root_windows
[i
];
225 if (start_root
== target_root
) {
226 phantom_widget_in_start_root_
.reset();
228 if (!phantom_widget_in_start_root_
||
229 phantom_widget_in_start_root_
->GetNativeWindow()->GetRootWindow() !=
231 phantom_widget_in_start_root_
=
232 CreatePhantomWidget(start_root
, start_bounds_in_screen
);
234 AnimateToBounds(phantom_widget_in_start_root_
.get(),
235 target_bounds_in_screen_
);
239 scoped_ptr
<views::Widget
> PhantomWindowController::CreatePhantomWidget(
240 aura::Window
* root_window
,
241 const gfx::Rect
& bounds_in_screen
) {
242 scoped_ptr
<views::Widget
> phantom_widget(new views::Widget
);
243 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
244 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
245 // PhantomWindowController is used by FrameMaximizeButton to highlight the
246 // launcher button. Put the phantom in the same window as the launcher so that
247 // the phantom is visible.
248 params
.parent
= Shell::GetContainer(root_window
,
249 kShellWindowId_ShelfContainer
);
250 params
.can_activate
= false;
251 params
.keep_on_top
= true;
252 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
253 phantom_widget
->set_focus_on_creation(false);
254 phantom_widget
->Init(params
);
255 phantom_widget
->SetVisibilityChangedAnimationsEnabled(false);
256 phantom_widget
->GetNativeWindow()->SetName("PhantomWindow");
257 phantom_widget
->GetNativeWindow()->set_id(kShellWindowId_PhantomWindow
);
258 phantom_widget
->SetBounds(bounds_in_screen
);
259 if (phantom_below_window_
)
260 phantom_widget
->StackBelow(phantom_below_window_
);
262 phantom_widget
->StackAbove(window_
);
264 views::Painter
* background_painter
= NULL
;
265 if (switches::UseAlternateFrameCaptionButtonStyle()) {
266 const int kImages
[] = IMAGE_GRID(IDR_AURA_PHANTOM_WINDOW
);
267 background_painter
= views::Painter::CreateImageGridPainter(kImages
);
269 background_painter
= new EdgePainter
;
271 views::View
* content_view
= new views::View
;
272 content_view
->set_background(
273 views::Background::CreateBackgroundPainter(true, background_painter
));
274 phantom_widget
->SetContentsView(content_view
);
276 // Show the widget after all the setups.
277 phantom_widget
->Show();
279 // Fade the window in.
280 ui::Layer
* widget_layer
= phantom_widget
->GetNativeWindow()->layer();
281 widget_layer
->SetOpacity(0);
282 ui::ScopedLayerAnimationSettings
scoped_setter(widget_layer
->GetAnimator());
283 scoped_setter
.SetTransitionDuration(
284 base::TimeDelta::FromMilliseconds(kAnimationDurationMs
));
285 widget_layer
->SetOpacity(1);
287 return phantom_widget
.Pass();