blob: 0711208b4c5ec1da9c9e90c3483a75ab00247f60 [file] [log] [blame]
[email protected]262f8bd2012-03-23 19:30:271// 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
[email protected]7a890e442013-01-23 00:48:207#include "ash/display/display_controller.h"
[email protected]262f8bd2012-03-23 19:30:278#include "ash/launcher/launcher.h"
9#include "ash/launcher/launcher_model.h"
10#include "ash/shell.h"
[email protected]7cf8dd62012-04-22 05:14:5811#include "ash/shell_window_ids.h"
[email protected]88d71122012-10-18 07:11:0112#include "ui/aura/root_window.h"
[email protected]262f8bd2012-03-23 19:30:2713#include "ui/aura/window.h"
[email protected]7a890e442013-01-23 00:48:2014#include "ui/gfx/display.h"
[email protected]262f8bd2012-03-23 19:30:2715
16namespace ash {
17namespace shell {
18
[email protected]95058572012-08-20 14:57:2919class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
20 public:
21 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {
[email protected]95058572012-08-20 14:57:2922 }
23
24 virtual ~WorkspaceWindowWatcher() {
[email protected]95058572012-08-20 14:57:2925 }
26
27 virtual void OnWindowAdded(aura::Window* new_window) OVERRIDE {
28 new_window->AddObserver(watcher_);
29 }
30
31 virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE {
32 DCHECK(window->children().empty());
33 window->RemoveObserver(watcher_);
34 }
35
[email protected]7a890e442013-01-23 00:48:2036 void RootWindowAdded(aura::RootWindow* root) {
37 aura::Window* panel_container = ash::Shell::GetContainer(
38 root,
39 internal::kShellWindowId_PanelContainer);
40 panel_container->AddObserver(watcher_);
41
42 aura::Window* container = Launcher::ForWindow(root)->window_container();
43 container->AddObserver(this);
44 for (size_t i = 0; i < container->children().size(); ++i)
45 container->children()[i]->AddObserver(watcher_);
46 }
47
48 void RootWindowRemoved(aura::RootWindow* root) {
49 aura::Window* panel_container = ash::Shell::GetContainer(
50 root,
51 internal::kShellWindowId_PanelContainer);
52 panel_container->RemoveObserver(watcher_);
53
54 aura::Window* container = Launcher::ForWindow(root)->window_container();
55 container->RemoveObserver(this);
56 for (size_t i = 0; i < container->children().size(); ++i)
57 container->children()[i]->RemoveObserver(watcher_);
58 }
59
[email protected]95058572012-08-20 14:57:2960 private:
61 WindowWatcher* watcher_;
62
63 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
64};
65
[email protected]7a890e442013-01-23 00:48:2066WindowWatcher::WindowWatcher() {
[email protected]c96b9812012-10-17 16:04:0467 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
[email protected]7a890e442013-01-23 00:48:2068 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
69 for (Shell::RootWindowList::iterator iter = root_windows.begin();
70 iter != root_windows.end(); ++ iter) {
71 workspace_window_watcher_->RootWindowAdded(*iter);
72 }
[email protected]262f8bd2012-03-23 19:30:2773}
74
75WindowWatcher::~WindowWatcher() {
[email protected]7a890e442013-01-23 00:48:2076 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
77 for (Shell::RootWindowList::iterator iter = root_windows.begin();
78 iter != root_windows.end(); ++ iter) {
79 workspace_window_watcher_->RootWindowRemoved(*iter);
80 }
[email protected]262f8bd2012-03-23 19:30:2781}
82
83aura::Window* WindowWatcher::GetWindowByID(ash::LauncherID id) {
84 IDToWindow::const_iterator i = id_to_window_.find(id);
85 return i != id_to_window_.end() ? i->second : NULL;
86}
87
88ash::LauncherID WindowWatcher::GetIDByWindow(aura::Window* window) const {
89 for (IDToWindow::const_iterator i = id_to_window_.begin();
90 i != id_to_window_.end(); ++i) {
91 if (i->second == window)
92 return i->first;
93 }
94 return 0; // TODO: add a constant for this.
95}
96
97// aura::WindowObserver overrides:
98void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
[email protected]7cf8dd62012-04-22 05:14:5899 if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL &&
100 new_window->type() != aura::client::WINDOW_TYPE_PANEL)
[email protected]262f8bd2012-03-23 19:30:27101 return;
102
103 static int image_count = 0;
[email protected]b913a3a2012-12-11 13:07:19104 ash::LauncherModel* model = Shell::GetInstance()->launcher_model();
[email protected]262f8bd2012-03-23 19:30:27105 ash::LauncherItem item;
[email protected]97f294572012-12-13 09:08:20106 item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL ?
107 ash::TYPE_APP_PANEL : ash::TYPE_TABBED;
[email protected]262f8bd2012-03-23 19:30:27108 id_to_window_[model->next_id()] = new_window;
[email protected]630a4602012-07-17 01:30:16109
110 SkBitmap icon_bitmap;
111 icon_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
112 icon_bitmap.allocPixels();
113 icon_bitmap.eraseARGB(255,
114 image_count == 0 ? 255 : 0,
115 image_count == 1 ? 255 : 0,
116 image_count == 2 ? 255 : 0);
[email protected]262f8bd2012-03-23 19:30:27117 image_count = (image_count + 1) % 3;
[email protected]630a4602012-07-17 01:30:16118 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap,
[email protected]0a4cf452012-11-14 07:28:40119 ui::SCALE_FACTOR_100P));
[email protected]630a4602012-07-17 01:30:16120
[email protected]b33ac3762012-03-24 01:19:52121 model->Add(item);
[email protected]262f8bd2012-03-23 19:30:27122}
123
124void WindowWatcher::OnWillRemoveWindow(aura::Window* window) {
125 for (IDToWindow::iterator i = id_to_window_.begin();
126 i != id_to_window_.end(); ++i) {
127 if (i->second == window) {
[email protected]b913a3a2012-12-11 13:07:19128 ash::LauncherModel* model = Shell::GetInstance()->launcher_model();
[email protected]262f8bd2012-03-23 19:30:27129 int index = model->ItemIndexByID(i->first);
130 DCHECK_NE(-1, index);
131 model->RemoveItemAt(index);
132 id_to_window_.erase(i);
133 break;
134 }
135 }
136}
137
[email protected]7a890e442013-01-23 00:48:20138void WindowWatcher::OnDisplayBoundsChanged(const gfx::Display& display) {
139}
140
141void WindowWatcher::OnDisplayAdded(const gfx::Display& new_display) {
142 aura::RootWindow* root = Shell::GetInstance()->display_controller()->
143 GetRootWindowForDisplayId(new_display.id());
144 workspace_window_watcher_->RootWindowAdded(root);
145}
146
147void WindowWatcher::OnDisplayRemoved(const gfx::Display& old_display) {
148 // All windows in the display has already been removed, so no need to
149 // remove observers.
150}
151
[email protected]262f8bd2012-03-23 19:30:27152} // namespace shell
153} // namespace ash