Home | History | Annotate | Download | only in wm
      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.
      4 
      5 #include "ash/wm/drag_window_controller.h"
      6 
      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"
     19 
     20 namespace ash {
     21 
     22 DragWindowController::DragWindowController(aura::Window* window)
     23     : window_(window),
     24       drag_widget_(NULL) {
     25 }
     26 
     27 DragWindowController::~DragWindowController() {
     28   Hide();
     29 }
     30 
     31 void DragWindowController::SetDestinationDisplay(
     32     const gfx::Display& dst_display) {
     33   dst_display_ = dst_display;
     34 }
     35 
     36 void DragWindowController::Show() {
     37   if (!drag_widget_)
     38     CreateDragWidget(window_->GetBoundsInScreen());
     39   drag_widget_->Show();
     40 }
     41 
     42 void DragWindowController::SetBounds(const gfx::Rect& bounds) {
     43   DCHECK(drag_widget_);
     44   bounds_ = bounds;
     45   SetBoundsInternal(bounds);
     46 }
     47 
     48 void DragWindowController::Hide() {
     49   if (drag_widget_) {
     50     drag_widget_->Close();
     51     drag_widget_ = NULL;
     52   }
     53   layer_owner_.reset();
     54 }
     55 
     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);
     61 }
     62 
     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.keep_on_top = true;
     70   drag_widget_->set_focus_on_creation(false);
     71   drag_widget_->Init(params);
     72   drag_widget_->SetVisibilityChangedAnimationsEnabled(false);
     73   drag_widget_->GetNativeWindow()->SetName("DragWindow");
     74   drag_widget_->GetNativeWindow()->set_id(kShellWindowId_PhantomWindow);
     75   // Show shadow for the dragging window.
     76   SetShadowType(drag_widget_->GetNativeWindow(),
     77                 ::wm::SHADOW_TYPE_RECTANGULAR);
     78   SetBoundsInternal(bounds);
     79   drag_widget_->StackAbove(window_);
     80 
     81   RecreateWindowLayers();
     82   aura::Window* window = drag_widget_->GetNativeWindow();
     83   layer_owner_->root()->SetVisible(true);
     84   window->layer()->Add(layer_owner_->root());
     85   window->layer()->StackAtTop(layer_owner_->root());
     86 
     87   // Show the widget after all the setups.
     88   drag_widget_->Show();
     89 
     90   // Fade the window in.
     91   ui::Layer* widget_layer = drag_widget_->GetNativeWindow()->layer();
     92   widget_layer->SetOpacity(0);
     93   ui::ScopedLayerAnimationSettings scoped_setter(widget_layer->GetAnimator());
     94   widget_layer->SetOpacity(1);
     95 }
     96 
     97 void DragWindowController::SetBoundsInternal(const gfx::Rect& bounds) {
     98   aura::Window* window = drag_widget_->GetNativeWindow();
     99   aura::client::ScreenPositionClient* screen_position_client =
    100       aura::client::GetScreenPositionClient(window->GetRootWindow());
    101   if (screen_position_client && dst_display_.is_valid())
    102     screen_position_client->SetBounds(window, bounds, dst_display_);
    103   else
    104     drag_widget_->SetBounds(bounds);
    105 }
    106 
    107 void DragWindowController::RecreateWindowLayers() {
    108   DCHECK(!layer_owner_.get());
    109   layer_owner_ = ::wm::RecreateLayers(window_);
    110   layer_owner_->root()->set_delegate(window_->layer()->delegate());
    111   // Place the layer at (0, 0) of the DragWindowController's window.
    112   gfx::Rect layer_bounds = layer_owner_->root()->bounds();
    113   layer_bounds.set_origin(gfx::Point(0, 0));
    114   layer_owner_->root()->SetBounds(layer_bounds);
    115   layer_owner_->root()->SetVisible(false);
    116   // Detach it from the current container.
    117   layer_owner_->root()->parent()->Remove(layer_owner_->root());
    118 }
    119 
    120 }  // namespace ash
    121