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.
5 #include "ash/shell/window_watcher.h"
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"
20 class WindowWatcher::WorkspaceWindowWatcher
: public aura::WindowObserver
{
22 explicit WorkspaceWindowWatcher(WindowWatcher
* watcher
) : watcher_(watcher
) {
25 virtual ~WorkspaceWindowWatcher() {
28 virtual void OnWindowAdded(aura::Window
* new_window
) OVERRIDE
{
29 new_window
->AddObserver(watcher_
);
32 virtual void OnWillRemoveWindow(aura::Window
* window
) OVERRIDE
{
33 DCHECK(window
->children().empty());
34 window
->RemoveObserver(watcher_
);
37 void RootWindowAdded(aura::RootWindow
* root
) {
38 aura::Window
* panel_container
= ash::Shell::GetContainer(
40 internal::kShellWindowId_PanelContainer
);
41 panel_container
->AddObserver(watcher_
);
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_
);
50 void RootWindowRemoved(aura::RootWindow
* root
) {
51 aura::Window
* panel_container
= ash::Shell::GetContainer(
53 internal::kShellWindowId_PanelContainer
);
54 panel_container
->RemoveObserver(watcher_
);
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_
);
64 WindowWatcher
* watcher_
;
66 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher
);
69 WindowWatcher::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
);
78 WindowWatcher::~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
);
86 aura::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
;
91 ash::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
)
97 return 0; // TODO: add a constant for this.
100 // aura::WindowObserver overrides:
101 void 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
)
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
;
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
));
127 void 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
);
141 void WindowWatcher::OnDisplayBoundsChanged(const gfx::Display
& display
) {
144 void 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
);
150 void WindowWatcher::OnDisplayRemoved(const gfx::Display
& old_display
) {
151 // All windows in the display has already been removed, so no need to