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/window_tree_host_manager.h"
8 #include "ash/shelf/shelf.h"
9 #include "ash/shelf/shelf_item_delegate_manager.h"
10 #include "ash/shelf/shelf_model.h"
11 #include "ash/shelf/shelf_util.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/shell/window_watcher_shelf_item_delegate.h"
15 #include "ash/shell_window_ids.h"
16 #include "ash/wm/window_util.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/gfx/display.h"
24 class WindowWatcher::WorkspaceWindowWatcher
: public aura::WindowObserver
{
26 explicit WorkspaceWindowWatcher(WindowWatcher
* watcher
) : watcher_(watcher
) {
29 ~WorkspaceWindowWatcher() override
{}
31 void OnWindowAdded(aura::Window
* new_window
) override
{
32 new_window
->AddObserver(watcher_
);
35 void OnWillRemoveWindow(aura::Window
* window
) override
{
36 DCHECK(window
->children().empty());
37 window
->RemoveObserver(watcher_
);
40 void RootWindowAdded(aura::Window
* root
) {
41 aura::Window
* panel_container
=
42 ash::Shell::GetContainer(root
, kShellWindowId_PanelContainer
);
43 panel_container
->AddObserver(watcher_
);
45 aura::Window
* container
=
46 Shelf::ForWindow(root
)->shelf_widget()->window_container();
47 container
->AddObserver(this);
48 for (size_t i
= 0; i
< container
->children().size(); ++i
)
49 container
->children()[i
]->AddObserver(watcher_
);
52 void RootWindowRemoved(aura::Window
* root
) {
53 aura::Window
* panel_container
=
54 ash::Shell::GetContainer(root
, kShellWindowId_PanelContainer
);
55 panel_container
->RemoveObserver(watcher_
);
57 aura::Window
* container
=
58 Shelf::ForWindow(root
)->shelf_widget()->window_container();
59 container
->RemoveObserver(this);
60 for (size_t i
= 0; i
< container
->children().size(); ++i
)
61 container
->children()[i
]->RemoveObserver(watcher_
);
65 WindowWatcher
* watcher_
;
67 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher
);
70 WindowWatcher::WindowWatcher() {
71 workspace_window_watcher_
.reset(new WorkspaceWindowWatcher(this));
72 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
73 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
74 iter
!= root_windows
.end(); ++ iter
) {
75 workspace_window_watcher_
->RootWindowAdded(*iter
);
79 WindowWatcher::~WindowWatcher() {
80 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
81 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
82 iter
!= root_windows
.end(); ++ iter
) {
83 workspace_window_watcher_
->RootWindowRemoved(*iter
);
87 aura::Window
* WindowWatcher::GetWindowByID(ash::ShelfID id
) {
88 IDToWindow::const_iterator i
= id_to_window_
.find(id
);
89 return i
!= id_to_window_
.end() ? i
->second
: NULL
;
92 // aura::WindowObserver overrides:
93 void WindowWatcher::OnWindowAdded(aura::Window
* new_window
) {
94 if (!wm::IsWindowUserPositionable(new_window
))
97 static int image_count
= 0;
98 ShelfModel
* model
= Shell::GetInstance()->shelf_model();
100 item
.type
= new_window
->type() == ui::wm::WINDOW_TYPE_PANEL
101 ? ash::TYPE_APP_PANEL
102 : ash::TYPE_PLATFORM_APP
;
103 ash::ShelfID id
= model
->next_id();
104 id_to_window_
[id
] = new_window
;
106 SkBitmap icon_bitmap
;
107 icon_bitmap
.allocN32Pixels(16, 16);
108 icon_bitmap
.eraseARGB(255,
109 image_count
== 0 ? 255 : 0,
110 image_count
== 1 ? 255 : 0,
111 image_count
== 2 ? 255 : 0);
112 image_count
= (image_count
+ 1) % 3;
113 item
.image
= gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap
, 1.0f
));
117 ShelfItemDelegateManager
* manager
=
118 Shell::GetInstance()->shelf_item_delegate_manager();
119 scoped_ptr
<ShelfItemDelegate
> delegate(
120 new WindowWatcherShelfItemDelegate(id
, this));
121 manager
->SetShelfItemDelegate(id
, delegate
.Pass());
122 SetShelfIDForWindow(id
, new_window
);
125 void WindowWatcher::OnWillRemoveWindow(aura::Window
* window
) {
126 for (IDToWindow::iterator i
= id_to_window_
.begin();
127 i
!= id_to_window_
.end(); ++i
) {
128 if (i
->second
== window
) {
129 ShelfModel
* model
= Shell::GetInstance()->shelf_model();
130 int index
= model
->ItemIndexByID(i
->first
);
131 DCHECK_NE(-1, index
);
132 model
->RemoveItemAt(index
);
133 id_to_window_
.erase(i
);
139 void WindowWatcher::OnDisplayAdded(const gfx::Display
& new_display
) {
140 aura::Window
* root
= Shell::GetInstance()
141 ->window_tree_host_manager()
142 ->GetRootWindowForDisplayId(new_display
.id());
143 workspace_window_watcher_
->RootWindowAdded(root
);
146 void WindowWatcher::OnDisplayRemoved(const gfx::Display
& old_display
) {
147 // All windows in the display has already been removed, so no need to
151 void WindowWatcher::OnDisplayMetricsChanged(const gfx::Display
&, uint32_t) {