Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / ash / shell / window_watcher.cc
blob990cef1e949d1a200da5bcff02065b771875412b
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/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 "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/gfx/display.h"
20 namespace ash {
21 namespace shell {
23 class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
24 public:
25 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {
28 ~WorkspaceWindowWatcher() override {}
30 void OnWindowAdded(aura::Window* new_window) override {
31 new_window->AddObserver(watcher_);
34 void OnWillRemoveWindow(aura::Window* window) override {
35 DCHECK(window->children().empty());
36 window->RemoveObserver(watcher_);
39 void RootWindowAdded(aura::Window* root) {
40 aura::Window* panel_container =
41 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer);
42 panel_container->AddObserver(watcher_);
44 aura::Window* container =
45 Shelf::ForWindow(root)->shelf_widget()->window_container();
46 container->AddObserver(this);
47 for (size_t i = 0; i < container->children().size(); ++i)
48 container->children()[i]->AddObserver(watcher_);
51 void RootWindowRemoved(aura::Window* root) {
52 aura::Window* panel_container =
53 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer);
54 panel_container->RemoveObserver(watcher_);
56 aura::Window* container =
57 Shelf::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_);
63 private:
64 WindowWatcher* watcher_;
66 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
69 WindowWatcher::WindowWatcher() {
70 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
71 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
72 for (aura::Window::Windows::iterator iter = root_windows.begin();
73 iter != root_windows.end(); ++ iter) {
74 workspace_window_watcher_->RootWindowAdded(*iter);
78 WindowWatcher::~WindowWatcher() {
79 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
80 for (aura::Window::Windows::iterator iter = root_windows.begin();
81 iter != root_windows.end(); ++ iter) {
82 workspace_window_watcher_->RootWindowRemoved(*iter);
86 aura::Window* WindowWatcher::GetWindowByID(ash::ShelfID id) {
87 IDToWindow::const_iterator i = id_to_window_.find(id);
88 return i != id_to_window_.end() ? i->second : NULL;
91 // aura::WindowObserver overrides:
92 void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
93 if (new_window->type() != ui::wm::WINDOW_TYPE_NORMAL &&
94 new_window->type() != ui::wm::WINDOW_TYPE_PANEL)
95 return;
97 static int image_count = 0;
98 ShelfModel* model = Shell::GetInstance()->shelf_model();
99 ShelfItem item;
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));
115 model->Add(item);
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);
134 break;
139 void WindowWatcher::OnDisplayAdded(const gfx::Display& new_display) {
140 aura::Window* root = Shell::GetInstance()->display_controller()->
141 GetRootWindowForDisplayId(new_display.id());
142 workspace_window_watcher_->RootWindowAdded(root);
145 void WindowWatcher::OnDisplayRemoved(const gfx::Display& old_display) {
146 // All windows in the display has already been removed, so no need to
147 // remove observers.
150 void WindowWatcher::OnDisplayMetricsChanged(const gfx::Display&, uint32_t) {
153 } // namespace shell
154 } // namespace ash