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