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/drag_window_controller.h"
7 #include "ash/shell_window_ids.h"
8 #include "ash/wm/window_util.h"
9 #include "ui/aura/client/screen_position_client.h"
10 #include "ui/aura/window.h"
11 #include "ui/compositor/layer.h"
12 #include "ui/compositor/scoped_layer_animation_settings.h"
13 #include "ui/views/corewm/shadow_types.h"
14 #include "ui/views/corewm/window_util.h"
15 #include "ui/views/view.h"
16 #include "ui/views/widget/widget.h"
21 DragWindowController::DragWindowController(aura::Window
* window
)
27 DragWindowController::~DragWindowController() {
31 void DragWindowController::SetDestinationDisplay(
32 const gfx::Display
& dst_display
) {
33 dst_display_
= dst_display
;
36 void DragWindowController::Show() {
38 CreateDragWidget(window_
->GetBoundsInScreen());
42 void DragWindowController::SetBounds(const gfx::Rect
& bounds
) {
45 SetBoundsInternal(bounds
);
48 void DragWindowController::Hide() {
50 drag_widget_
->Close();
54 views::corewm::DeepDeleteLayers(layer_
);
59 void DragWindowController::SetOpacity(float opacity
) {
61 ui::Layer
* layer
= drag_widget_
->GetNativeWindow()->layer();
62 ui::ScopedLayerAnimationSettings
scoped_setter(layer
->GetAnimator());
63 layer
->SetOpacity(opacity
);
66 void DragWindowController::CreateDragWidget(const gfx::Rect
& bounds
) {
67 DCHECK(!drag_widget_
);
68 drag_widget_
= new views::Widget
;
69 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
70 params
.transparent
= true;
71 params
.parent
= window_
->parent();
72 params
.can_activate
= false;
73 params
.keep_on_top
= true;
74 drag_widget_
->set_focus_on_creation(false);
75 drag_widget_
->Init(params
);
76 drag_widget_
->SetVisibilityChangedAnimationsEnabled(false);
77 drag_widget_
->GetNativeWindow()->SetName("DragWindow");
78 drag_widget_
->GetNativeWindow()->set_id(kShellWindowId_PhantomWindow
);
79 // Show shadow for the dragging window.
80 SetShadowType(drag_widget_
->GetNativeWindow(),
81 views::corewm::SHADOW_TYPE_RECTANGULAR
);
82 SetBoundsInternal(bounds
);
83 drag_widget_
->StackAbove(window_
);
85 RecreateWindowLayers();
86 aura::Window
* window
= drag_widget_
->GetNativeWindow();
87 layer_
->SetVisible(true);
88 window
->layer()->Add(layer_
);
89 window
->layer()->StackAtTop(layer_
);
91 // Show the widget after all the setups.
94 // Fade the window in.
95 ui::Layer
* widget_layer
= drag_widget_
->GetNativeWindow()->layer();
96 widget_layer
->SetOpacity(0);
97 ui::ScopedLayerAnimationSettings
scoped_setter(widget_layer
->GetAnimator());
98 widget_layer
->SetOpacity(1);
101 void DragWindowController::SetBoundsInternal(const gfx::Rect
& bounds
) {
102 aura::Window
* window
= drag_widget_
->GetNativeWindow();
103 aura::client::ScreenPositionClient
* screen_position_client
=
104 aura::client::GetScreenPositionClient(window
->GetRootWindow());
105 if (screen_position_client
&& dst_display_
.is_valid())
106 screen_position_client
->SetBounds(window
, bounds
, dst_display_
);
108 drag_widget_
->SetBounds(bounds
);
111 void DragWindowController::RecreateWindowLayers() {
113 layer_
= views::corewm::RecreateWindowLayers(window_
, true);
114 layer_
->set_delegate(window_
->layer()->delegate());
115 // Place the layer at (0, 0) of the DragWindowController's window.
116 gfx::Rect layer_bounds
= layer_
->bounds();
117 layer_bounds
.set_origin(gfx::Point(0, 0));
118 layer_
->SetBounds(layer_bounds
);
119 layer_
->SetVisible(false);
120 // Detach it from the current container.
121 layer_
->parent()->Remove(layer_
);
124 } // namespace internal