Home | History | Annotate | Download | only in core
      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 "ui/wm/core/window_util.h"
      6 
      7 #include "ui/aura/window.h"
      8 #include "ui/aura/window_event_dispatcher.h"
      9 #include "ui/compositor/layer.h"
     10 #include "ui/compositor/layer_tree_owner.h"
     11 #include "ui/wm/core/transient_window_manager.h"
     12 #include "ui/wm/public/activation_client.h"
     13 
     14 namespace {
     15 
     16 // Invokes RecreateLayer() on all the children of |to_clone|, adding the newly
     17 // cloned children to |parent|.
     18 //
     19 // WARNING: It is assumed that |parent| is ultimately owned by a LayerTreeOwner.
     20 void CloneChildren(ui::Layer* to_clone, ui::Layer* parent) {
     21   typedef std::vector<ui::Layer*> Layers;
     22   // Make a copy of the children since RecreateLayer() mutates it.
     23   Layers children(to_clone->children());
     24   for (Layers::const_iterator i = children.begin(); i != children.end(); ++i) {
     25     ui::LayerOwner* owner = (*i)->owner();
     26     ui::Layer* old_layer = owner ? owner->RecreateLayer().release() : NULL;
     27     if (old_layer) {
     28       parent->Add(old_layer);
     29       // RecreateLayer() moves the existing children to the new layer. Create a
     30       // copy of those.
     31       CloneChildren(owner->layer(), old_layer);
     32     }
     33   }
     34 }
     35 
     36 }  // namespace
     37 
     38 namespace wm {
     39 
     40 void ActivateWindow(aura::Window* window) {
     41   DCHECK(window);
     42   DCHECK(window->GetRootWindow());
     43   aura::client::GetActivationClient(window->GetRootWindow())->ActivateWindow(
     44       window);
     45 }
     46 
     47 void DeactivateWindow(aura::Window* window) {
     48   DCHECK(window);
     49   DCHECK(window->GetRootWindow());
     50   aura::client::GetActivationClient(window->GetRootWindow())->DeactivateWindow(
     51       window);
     52 }
     53 
     54 bool IsActiveWindow(aura::Window* window) {
     55   DCHECK(window);
     56   if (!window->GetRootWindow())
     57     return false;
     58   aura::client::ActivationClient* client =
     59       aura::client::GetActivationClient(window->GetRootWindow());
     60   return client && client->GetActiveWindow() == window;
     61 }
     62 
     63 bool CanActivateWindow(aura::Window* window) {
     64   DCHECK(window);
     65   if (!window->GetRootWindow())
     66     return false;
     67   aura::client::ActivationClient* client =
     68       aura::client::GetActivationClient(window->GetRootWindow());
     69   return client && client->CanActivateWindow(window);
     70 }
     71 
     72 aura::Window* GetActivatableWindow(aura::Window* window) {
     73   aura::client::ActivationClient* client =
     74       aura::client::GetActivationClient(window->GetRootWindow());
     75   return client ? client->GetActivatableWindow(window) : NULL;
     76 }
     77 
     78 aura::Window* GetToplevelWindow(aura::Window* window) {
     79   aura::client::ActivationClient* client =
     80       aura::client::GetActivationClient(window->GetRootWindow());
     81   return client ? client->GetToplevelWindow(window) : NULL;
     82 }
     83 
     84 scoped_ptr<ui::LayerTreeOwner> RecreateLayers(ui::LayerOwner* root) {
     85   scoped_ptr<ui::LayerTreeOwner> old_layer(
     86       new ui::LayerTreeOwner(root->RecreateLayer().release()));
     87   if (old_layer->root())
     88     CloneChildren(root->layer(), old_layer->root());
     89   return old_layer.Pass();
     90 }
     91 
     92 aura::Window* GetTransientParent(aura::Window* window) {
     93   return const_cast<aura::Window*>(GetTransientParent(
     94                                  const_cast<const aura::Window*>(window)));
     95 }
     96 
     97 const aura::Window* GetTransientParent(const aura::Window* window) {
     98   const TransientWindowManager* manager = TransientWindowManager::Get(window);
     99   return manager ? manager->transient_parent() : NULL;
    100 }
    101 
    102 const std::vector<aura::Window*>& GetTransientChildren(
    103     const aura::Window* window) {
    104   const TransientWindowManager* manager = TransientWindowManager::Get(window);
    105   if (manager)
    106     return manager->transient_children();
    107 
    108   static std::vector<aura::Window*>* shared = new std::vector<aura::Window*>;
    109   return *shared;
    110 }
    111 
    112 void AddTransientChild(aura::Window* parent, aura::Window* child) {
    113   TransientWindowManager::Get(parent)->AddTransientChild(child);
    114 }
    115 
    116 void RemoveTransientChild(aura::Window* parent, aura::Window* child) {
    117   TransientWindowManager::Get(parent)->RemoveTransientChild(child);
    118 }
    119 
    120 bool HasTransientAncestor(const aura::Window* window,
    121                           const aura::Window* ancestor) {
    122   const aura::Window* transient_parent = GetTransientParent(window);
    123   if (transient_parent == ancestor)
    124     return true;
    125   return transient_parent ?
    126       HasTransientAncestor(transient_parent, ancestor) : false;
    127 }
    128 
    129 }  // namespace wm
    130