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/always_on_top_controller.h" 6 7 #include "ash/shell.h" 8 #include "ash/shell_window_ids.h" 9 #include "ui/aura/client/aura_constants.h" 10 #include "ui/aura/window.h" 11 12 namespace ash { 13 namespace internal { 14 15 AlwaysOnTopController::AlwaysOnTopController() 16 : always_on_top_container_(NULL) { 17 } 18 19 AlwaysOnTopController::~AlwaysOnTopController() { 20 if (always_on_top_container_) 21 always_on_top_container_->RemoveObserver(this); 22 } 23 24 void AlwaysOnTopController::SetAlwaysOnTopContainer( 25 aura::Window* always_on_top_container) { 26 // Container should be empty. 27 DCHECK(always_on_top_container->children().empty()); 28 29 // We are not handling any containers yet. 30 DCHECK(always_on_top_container_ == NULL); 31 32 always_on_top_container_ = always_on_top_container; 33 always_on_top_container_->AddObserver(this); 34 } 35 36 aura::Window* AlwaysOnTopController::GetContainer(aura::Window* window) const { 37 DCHECK(always_on_top_container_); 38 if (window->GetProperty(aura::client::kAlwaysOnTopKey)) 39 return always_on_top_container_; 40 return Shell::GetContainer(always_on_top_container_->GetRootWindow(), 41 kShellWindowId_DefaultContainer); 42 } 43 44 void AlwaysOnTopController::OnWindowAdded(aura::Window* child) { 45 // Observe direct child of the containers. 46 if (child->parent() == always_on_top_container_) 47 child->AddObserver(this); 48 } 49 50 void AlwaysOnTopController::OnWillRemoveWindow(aura::Window* child) { 51 child->RemoveObserver(this); 52 } 53 54 void AlwaysOnTopController::OnWindowPropertyChanged(aura::Window* window, 55 const void* key, 56 intptr_t old) { 57 if (key == aura::client::kAlwaysOnTopKey) { 58 DCHECK(window->type() == aura::client::WINDOW_TYPE_NORMAL || 59 window->type() == aura::client::WINDOW_TYPE_POPUP); 60 aura::Window* container = GetContainer(window); 61 if (window->parent() != container) 62 container->AddChild(window); 63 } 64 } 65 66 void AlwaysOnTopController::OnWindowDestroyed(aura::Window* window) { 67 if (window == always_on_top_container_) 68 always_on_top_container_ = NULL; 69 } 70 71 } // namespace internal 72 } // namespace ash 73