window_watcher.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/shell/window_watcher.h"
6
7#include "ash/display/display_controller.h"
8#include "ash/launcher/launcher.h"
9#include "ash/launcher/launcher_model.h"
10#include "ash/shelf/shelf_widget.h"
11#include "ash/shell.h"
12#include "ash/shell_window_ids.h"
13#include "ui/aura/root_window.h"
14#include "ui/aura/window.h"
15#include "ui/gfx/display.h"
16
17namespace ash {
18namespace shell {
19
20class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
21 public:
22  explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {
23  }
24
25  virtual ~WorkspaceWindowWatcher() {
26  }
27
28  virtual void OnWindowAdded(aura::Window* new_window) OVERRIDE {
29    new_window->AddObserver(watcher_);
30  }
31
32  virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE {
33    DCHECK(window->children().empty());
34    window->RemoveObserver(watcher_);
35  }
36
37  void RootWindowAdded(aura::RootWindow* root) {
38    aura::Window* panel_container = ash::Shell::GetContainer(
39        root,
40        internal::kShellWindowId_PanelContainer);
41    panel_container->AddObserver(watcher_);
42
43    aura::Window* container =
44        Launcher::ForWindow(root)->shelf_widget()->window_container();
45    container->AddObserver(this);
46    for (size_t i = 0; i < container->children().size(); ++i)
47      container->children()[i]->AddObserver(watcher_);
48  }
49
50  void RootWindowRemoved(aura::RootWindow* root) {
51    aura::Window* panel_container = ash::Shell::GetContainer(
52        root,
53        internal::kShellWindowId_PanelContainer);
54    panel_container->RemoveObserver(watcher_);
55
56    aura::Window* container =
57        Launcher::ForWindow(root)->shelf_widget()->window_container();
58    container->RemoveObserver(this);
59    for (size_t i = 0; i < container->children().size(); ++i)
60      container->children()[i]->RemoveObserver(watcher_);
61  }
62
63 private:
64  WindowWatcher* watcher_;
65
66  DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
67};
68
69WindowWatcher::WindowWatcher() {
70  workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
71  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
72  for (Shell::RootWindowList::iterator iter = root_windows.begin();
73       iter != root_windows.end(); ++ iter) {
74    workspace_window_watcher_->RootWindowAdded(*iter);
75  }
76}
77
78WindowWatcher::~WindowWatcher() {
79  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
80  for (Shell::RootWindowList::iterator iter = root_windows.begin();
81       iter != root_windows.end(); ++ iter) {
82    workspace_window_watcher_->RootWindowRemoved(*iter);
83  }
84}
85
86aura::Window* WindowWatcher::GetWindowByID(ash::LauncherID id) {
87  IDToWindow::const_iterator i = id_to_window_.find(id);
88  return i != id_to_window_.end() ? i->second : NULL;
89}
90
91ash::LauncherID WindowWatcher::GetIDByWindow(aura::Window* window) const {
92  for (IDToWindow::const_iterator i = id_to_window_.begin();
93       i != id_to_window_.end(); ++i) {
94    if (i->second == window)
95      return i->first;
96  }
97  return 0;  // TODO: add a constant for this.
98}
99
100// aura::WindowObserver overrides:
101void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
102  if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL &&
103      new_window->type() != aura::client::WINDOW_TYPE_PANEL)
104    return;
105
106  static int image_count = 0;
107  ash::LauncherModel* model = Shell::GetInstance()->launcher_model();
108  ash::LauncherItem item;
109  item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL ?
110                                    ash::TYPE_APP_PANEL : ash::TYPE_TABBED;
111  id_to_window_[model->next_id()] = new_window;
112
113  SkBitmap icon_bitmap;
114  icon_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
115  icon_bitmap.allocPixels();
116  icon_bitmap.eraseARGB(255,
117                        image_count == 0 ? 255 : 0,
118                        image_count == 1 ? 255 : 0,
119                        image_count == 2 ? 255 : 0);
120  image_count = (image_count + 1) % 3;
121  item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap,
122                                                ui::SCALE_FACTOR_100P));
123
124  model->Add(item);
125}
126
127void WindowWatcher::OnWillRemoveWindow(aura::Window* window) {
128  for (IDToWindow::iterator i = id_to_window_.begin();
129       i != id_to_window_.end(); ++i) {
130    if (i->second == window) {
131      ash::LauncherModel* model = Shell::GetInstance()->launcher_model();
132      int index = model->ItemIndexByID(i->first);
133      DCHECK_NE(-1, index);
134      model->RemoveItemAt(index);
135      id_to_window_.erase(i);
136      break;
137    }
138  }
139}
140
141void WindowWatcher::OnDisplayBoundsChanged(const gfx::Display& display) {
142}
143
144void WindowWatcher::OnDisplayAdded(const gfx::Display& new_display) {
145  aura::RootWindow* root = Shell::GetInstance()->display_controller()->
146      GetRootWindowForDisplayId(new_display.id());
147  workspace_window_watcher_->RootWindowAdded(root);
148}
149
150void WindowWatcher::OnDisplayRemoved(const gfx::Display& old_display) {
151  // All windows in the display has already been removed, so no need to
152  // remove observers.
153}
154
155}  // namespace shell
156}  // namespace ash
157