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/root_window.h"
11 #include "ui/aura/window.h"
12 #include "ui/compositor/layer.h"
13 #include "ui/compositor/scoped_layer_animation_settings.h"
14 #include "ui/views/corewm/shadow_types.h"
15 #include "ui/views/corewm/window_util.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
22 DragWindowController::DragWindowController(aura::Window
* window
)
28 DragWindowController::~DragWindowController() {
32 void DragWindowController::SetDestinationDisplay(
33 const gfx::Display
& dst_display
) {
34 dst_display_
= dst_display
;
37 void DragWindowController::Show() {
39 CreateDragWidget(window_
->GetBoundsInScreen());
43 void DragWindowController::SetBounds(const gfx::Rect
& bounds
) {
46 SetBoundsInternal(bounds
);
49 void DragWindowController::Hide() {
51 drag_widget_
->Close();
55 views::corewm::DeepDeleteLayers(layer_
);
60 void DragWindowController::SetOpacity(float opacity
) {
62 ui::Layer
* layer
= drag_widget_
->GetNativeWindow()->layer();
63 ui::ScopedLayerAnimationSettings
scoped_setter(layer
->GetAnimator());
64 layer
->SetOpacity(opacity
);
67 void DragWindowController::CreateDragWidget(const gfx::Rect
& bounds
) {
68 DCHECK(!drag_widget_
);
69 drag_widget_
= new views::Widget
;
70 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
71 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
72 params
.parent
= window_
->parent();
73 params
.can_activate
= false;
74 params
.keep_on_top
= true;
75 drag_widget_
->set_focus_on_creation(false);
76 drag_widget_
->Init(params
);
77 drag_widget_
->SetVisibilityChangedAnimationsEnabled(false);
78 drag_widget_
->GetNativeWindow()->SetName("DragWindow");
79 drag_widget_
->GetNativeWindow()->set_id(kShellWindowId_PhantomWindow
);
80 // Show shadow for the dragging window.
81 SetShadowType(drag_widget_
->GetNativeWindow(),
82 views::corewm::SHADOW_TYPE_RECTANGULAR
);
83 SetBoundsInternal(bounds
);
84 drag_widget_
->StackAbove(window_
);
86 RecreateWindowLayers();
87 aura::Window
* window
= drag_widget_
->GetNativeWindow();
88 layer_
->SetVisible(true);
89 window
->layer()->Add(layer_
);
90 window
->layer()->StackAtTop(layer_
);
92 // Show the widget after all the setups.
95 // Fade the window in.
96 ui::Layer
* widget_layer
= drag_widget_
->GetNativeWindow()->layer();
97 widget_layer
->SetOpacity(0);
98 ui::ScopedLayerAnimationSettings
scoped_setter(widget_layer
->GetAnimator());
99 widget_layer
->SetOpacity(1);
102 void DragWindowController::SetBoundsInternal(const gfx::Rect
& bounds
) {
103 aura::Window
* window
= drag_widget_
->GetNativeWindow();
104 aura::client::ScreenPositionClient
* screen_position_client
=
105 aura::client::GetScreenPositionClient(window
->GetRootWindow());
106 if (screen_position_client
&& dst_display_
.is_valid())
107 screen_position_client
->SetBounds(window
, bounds
, dst_display_
);
109 drag_widget_
->SetBounds(bounds
);
112 void DragWindowController::RecreateWindowLayers() {
114 layer_
= views::corewm::RecreateWindowLayers(window_
, true);
115 layer_
->set_delegate(window_
->layer()->delegate());
116 // Place the layer at (0, 0) of the DragWindowController's window.
117 gfx::Rect layer_bounds
= layer_
->bounds();
118 layer_bounds
.set_origin(gfx::Point(0, 0));
119 layer_
->SetBounds(layer_bounds
);
120 layer_
->SetVisible(false);
121 // Detach it from the current container.
122 layer_
->parent()->Remove(layer_
);
125 } // namespace internal