Prevents double-clicks on a tab close button from aslo maximizing the browser
[chromium-blink-merge.git] / ash / wm / drag_window_controller.cc
blobeee66e46c5ed5db0a10c448b31353b9fd2f85c10
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/aura/window_event_dispatcher.h"
12 #include "ui/compositor/layer.h"
13 #include "ui/compositor/layer_tree_owner.h"
14 #include "ui/compositor/scoped_layer_animation_settings.h"
15 #include "ui/views/view.h"
16 #include "ui/views/widget/widget.h"
17 #include "ui/wm/core/shadow_types.h"
18 #include "ui/wm/core/window_util.h"
20 namespace ash {
22 DragWindowController::DragWindowController(aura::Window* window)
23 : window_(window),
24 drag_widget_(NULL) {
27 DragWindowController::~DragWindowController() {
28 Hide();
31 void DragWindowController::SetDestinationDisplay(
32 const gfx::Display& dst_display) {
33 dst_display_ = dst_display;
36 void DragWindowController::Show() {
37 if (!drag_widget_)
38 CreateDragWidget(window_->GetBoundsInScreen());
39 drag_widget_->Show();
42 void DragWindowController::SetBounds(const gfx::Rect& bounds) {
43 DCHECK(drag_widget_);
44 bounds_ = bounds;
45 SetBoundsInternal(bounds);
48 void DragWindowController::Hide() {
49 if (drag_widget_) {
50 drag_widget_->Close();
51 drag_widget_ = NULL;
53 layer_owner_.reset();
56 void DragWindowController::SetOpacity(float opacity) {
57 DCHECK(drag_widget_);
58 ui::Layer* layer = drag_widget_->GetNativeWindow()->layer();
59 ui::ScopedLayerAnimationSettings scoped_setter(layer->GetAnimator());
60 layer->SetOpacity(opacity);
63 void DragWindowController::CreateDragWidget(const gfx::Rect& bounds) {
64 DCHECK(!drag_widget_);
65 drag_widget_ = new views::Widget;
66 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
67 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
68 params.parent = window_->parent();
69 params.can_activate = false;
70 params.keep_on_top = true;
71 drag_widget_->set_focus_on_creation(false);
72 drag_widget_->Init(params);
73 drag_widget_->SetVisibilityChangedAnimationsEnabled(false);
74 drag_widget_->GetNativeWindow()->SetName("DragWindow");
75 drag_widget_->GetNativeWindow()->set_id(kShellWindowId_PhantomWindow);
76 // Show shadow for the dragging window.
77 SetShadowType(drag_widget_->GetNativeWindow(),
78 ::wm::SHADOW_TYPE_RECTANGULAR);
79 SetBoundsInternal(bounds);
80 drag_widget_->StackAbove(window_);
82 RecreateWindowLayers();
83 aura::Window* window = drag_widget_->GetNativeWindow();
84 layer_owner_->root()->SetVisible(true);
85 window->layer()->Add(layer_owner_->root());
86 window->layer()->StackAtTop(layer_owner_->root());
88 // Show the widget after all the setups.
89 drag_widget_->Show();
91 // Fade the window in.
92 ui::Layer* widget_layer = drag_widget_->GetNativeWindow()->layer();
93 widget_layer->SetOpacity(0);
94 ui::ScopedLayerAnimationSettings scoped_setter(widget_layer->GetAnimator());
95 widget_layer->SetOpacity(1);
98 void DragWindowController::SetBoundsInternal(const gfx::Rect& bounds) {
99 aura::Window* window = drag_widget_->GetNativeWindow();
100 aura::client::ScreenPositionClient* screen_position_client =
101 aura::client::GetScreenPositionClient(window->GetRootWindow());
102 if (screen_position_client && dst_display_.is_valid())
103 screen_position_client->SetBounds(window, bounds, dst_display_);
104 else
105 drag_widget_->SetBounds(bounds);
108 void DragWindowController::RecreateWindowLayers() {
109 DCHECK(!layer_owner_.get());
110 layer_owner_ = ::wm::RecreateLayers(window_);
111 layer_owner_->root()->set_delegate(window_->layer()->delegate());
112 // Place the layer at (0, 0) of the DragWindowController's window.
113 gfx::Rect layer_bounds = layer_owner_->root()->bounds();
114 layer_bounds.set_origin(gfx::Point(0, 0));
115 layer_owner_->root()->SetBounds(layer_bounds);
116 layer_owner_->root()->SetVisible(false);
117 // Detach it from the current container.
118 layer_owner_->root()->parent()->Remove(layer_owner_->root());
121 } // namespace ash