blob: ab397c9b9170b93b15dd26426be2c49bd44d7ec6 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)a36e5922013-08-05 13:57:33 +01007#include "ash/shell.h"
8#include "ash/shell_window_ids.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include "ui/aura/client/aura_constants.h"
10#include "ui/aura/window.h"
11
12namespace ash {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013
14AlwaysOnTopController::AlwaysOnTopController()
15 : always_on_top_container_(NULL) {
16}
17
18AlwaysOnTopController::~AlwaysOnTopController() {
19 if (always_on_top_container_)
20 always_on_top_container_->RemoveObserver(this);
21}
22
23void 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
35aura::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)a36e5922013-08-05 13:57:33 +010039 return Shell::GetContainer(always_on_top_container_->GetRootWindow(),
Ben Murdoch2385ea32013-08-06 11:01:04 +010040 kShellWindowId_DefaultContainer);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041}
42
43void 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
49void AlwaysOnTopController::OnWillRemoveWindow(aura::Window* child) {
50 child->RemoveObserver(this);
51}
52
53void AlwaysOnTopController::OnWindowPropertyChanged(aura::Window* window,
54 const void* key,
55 intptr_t old) {
56 if (key == aura::client::kAlwaysOnTopKey) {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000057 DCHECK(window->type() == ui::wm::WINDOW_TYPE_NORMAL ||
58 window->type() == ui::wm::WINDOW_TYPE_POPUP);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000059 aura::Window* container = GetContainer(window);
60 if (window->parent() != container)
61 container->AddChild(window);
62 }
63}
64
65void AlwaysOnTopController::OnWindowDestroyed(aura::Window* window) {
66 if (window == always_on_top_container_)
67 always_on_top_container_ = NULL;
68}
69
Torne (Richard Coles)58218062012-11-14 11:43:16 +000070} // namespace ash