Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 7 | #include "ash/shell.h" |
| 8 | #include "ash/shell_window_ids.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 9 | #include "ui/aura/client/aura_constants.h" |
| 10 | #include "ui/aura/window.h" |
| 11 | |
| 12 | namespace ash { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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_; |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 39 | return Shell::GetContainer(always_on_top_container_->GetRootWindow(), |
Ben Murdoch | 2385ea3 | 2013-08-06 11:01:04 +0100 | [diff] [blame] | 40 | kShellWindowId_DefaultContainer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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) { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 57 | DCHECK(window->type() == ui::wm::WINDOW_TYPE_NORMAL || |
| 58 | window->type() == ui::wm::WINDOW_TYPE_POPUP); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 70 | } // namespace ash |