blob: ea60dbcb868ddff9ebc949310aa9584180b67e5e [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"
[email protected]6777f632013-10-15 02:38:419#include "ash/launcher/launcher_item_delegate_manager.h"
[email protected]262f8bd2012-03-23 19:30:2710#include "ash/launcher/launcher_model.h"
[email protected]478c6c32013-03-09 02:50:5811#include "ash/shelf/shelf_widget.h"
[email protected]262f8bd2012-03-23 19:30:2712#include "ash/shell.h"
[email protected]6777f632013-10-15 02:38:4113#include "ash/shell/window_watcher_launcher_item_delegate.h"
[email protected]7cf8dd62012-04-22 05:14:5814#include "ash/shell_window_ids.h"
[email protected]88d71122012-10-18 07:11:0115#include "ui/aura/root_window.h"
[email protected]262f8bd2012-03-23 19:30:2716#include "ui/aura/window.h"
[email protected]7a890e442013-01-23 00:48:2017#include "ui/gfx/display.h"
[email protected]262f8bd2012-03-23 19:30:2718
19namespace ash {
20namespace shell {
21
[email protected]95058572012-08-20 14:57:2922class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
23 public:
24 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {
[email protected]95058572012-08-20 14:57:2925 }
26
27 virtual ~WorkspaceWindowWatcher() {
[email protected]95058572012-08-20 14:57:2928 }
29
30 virtual void OnWindowAdded(aura::Window* new_window) OVERRIDE {
31 new_window->AddObserver(watcher_);
32 }
33
34 virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE {
35 DCHECK(window->children().empty());
36 window->RemoveObserver(watcher_);
37 }
38
[email protected]bf9cdb362013-10-25 19:22:4539 void RootWindowAdded(aura::Window* root) {
[email protected]7a890e442013-01-23 00:48:2040 aura::Window* panel_container = ash::Shell::GetContainer(
41 root,
42 internal::kShellWindowId_PanelContainer);
43 panel_container->AddObserver(watcher_);
44
[email protected]478c6c32013-03-09 02:50:5845 aura::Window* container =
46 Launcher::ForWindow(root)->shelf_widget()->window_container();
[email protected]7a890e442013-01-23 00:48:2047 container->AddObserver(this);
48 for (size_t i = 0; i < container->children().size(); ++i)
49 container->children()[i]->AddObserver(watcher_);
50 }
51
52 void RootWindowRemoved(aura::RootWindow* root) {
53 aura::Window* panel_container = ash::Shell::GetContainer(
54 root,
55 internal::kShellWindowId_PanelContainer);
56 panel_container->RemoveObserver(watcher_);
57
[email protected]478c6c32013-03-09 02:50:5858 aura::Window* container =
59 Launcher::ForWindow(root)->shelf_widget()->window_container();
[email protected]7a890e442013-01-23 00:48:2060 container->RemoveObserver(this);
61 for (size_t i = 0; i < container->children().size(); ++i)
62 container->children()[i]->RemoveObserver(watcher_);
63 }
64
[email protected]95058572012-08-20 14:57:2965 private:
66 WindowWatcher* watcher_;
67
68 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
69};
70
[email protected]7a890e442013-01-23 00:48:2071WindowWatcher::WindowWatcher() {
[email protected]c96b9812012-10-17 16:04:0472 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
[email protected]c9390bd2013-11-08 20:33:1373 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
74 for (aura::Window::Windows::iterator iter = root_windows.begin();
[email protected]7a890e442013-01-23 00:48:2075 iter != root_windows.end(); ++ iter) {
76 workspace_window_watcher_->RootWindowAdded(*iter);
77 }
[email protected]262f8bd2012-03-23 19:30:2778}
79
80WindowWatcher::~WindowWatcher() {
[email protected]c9390bd2013-11-08 20:33:1381 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
82 for (aura::Window::Windows::iterator iter = root_windows.begin();
[email protected]7a890e442013-01-23 00:48:2083 iter != root_windows.end(); ++ iter) {
[email protected]c9390bd2013-11-08 20:33:1384 workspace_window_watcher_->RootWindowRemoved((*iter)->GetDispatcher());
[email protected]7a890e442013-01-23 00:48:2085 }
[email protected]262f8bd2012-03-23 19:30:2786}
87
88aura::Window* WindowWatcher::GetWindowByID(ash::LauncherID id) {
89 IDToWindow::const_iterator i = id_to_window_.find(id);
90 return i != id_to_window_.end() ? i->second : NULL;
91}
92
93ash::LauncherID WindowWatcher::GetIDByWindow(aura::Window* window) const {
94 for (IDToWindow::const_iterator i = id_to_window_.begin();
95 i != id_to_window_.end(); ++i) {
96 if (i->second == window)
97 return i->first;
98 }
99 return 0; // TODO: add a constant for this.
100}
101
102// aura::WindowObserver overrides:
103void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
[email protected]7cf8dd62012-04-22 05:14:58104 if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL &&
105 new_window->type() != aura::client::WINDOW_TYPE_PANEL)
[email protected]262f8bd2012-03-23 19:30:27106 return;
107
108 static int image_count = 0;
[email protected]b913a3a2012-12-11 13:07:19109 ash::LauncherModel* model = Shell::GetInstance()->launcher_model();
[email protected]262f8bd2012-03-23 19:30:27110 ash::LauncherItem item;
[email protected]97f294572012-12-13 09:08:20111 item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL ?
[email protected]8d181bf2013-08-28 15:34:32112 ash::TYPE_APP_PANEL : ash::TYPE_PLATFORM_APP;
[email protected]6777f632013-10-15 02:38:41113 ash::LauncherID id = model->next_id();
114 id_to_window_[id] = new_window;
[email protected]630a4602012-07-17 01:30:16115
116 SkBitmap icon_bitmap;
117 icon_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
118 icon_bitmap.allocPixels();
119 icon_bitmap.eraseARGB(255,
120 image_count == 0 ? 255 : 0,
121 image_count == 1 ? 255 : 0,
122 image_count == 2 ? 255 : 0);
[email protected]262f8bd2012-03-23 19:30:27123 image_count = (image_count + 1) % 3;
[email protected]50b66262013-09-24 03:25:48124 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f));
[email protected]630a4602012-07-17 01:30:16125
[email protected]b33ac3762012-03-24 01:19:52126 model->Add(item);
[email protected]6777f632013-10-15 02:38:41127
128 ash::LauncherItemDelegateManager* manager =
129 ash::Shell::GetInstance()->launcher_item_delegate_manager();
130 scoped_ptr<LauncherItemDelegate> delegate(
131 new WindowWatcherLauncherItemDelegate(id, this));
132 manager->SetLauncherItemDelegate(id, delegate.Pass());
[email protected]262f8bd2012-03-23 19:30:27133}
134
135void WindowWatcher::OnWillRemoveWindow(aura::Window* window) {
136 for (IDToWindow::iterator i = id_to_window_.begin();
137 i != id_to_window_.end(); ++i) {
138 if (i->second == window) {
[email protected]b913a3a2012-12-11 13:07:19139 ash::LauncherModel* model = Shell::GetInstance()->launcher_model();
[email protected]262f8bd2012-03-23 19:30:27140 int index = model->ItemIndexByID(i->first);
141 DCHECK_NE(-1, index);
142 model->RemoveItemAt(index);
143 id_to_window_.erase(i);
144 break;
145 }
146 }
147}
148
[email protected]7a890e442013-01-23 00:48:20149void WindowWatcher::OnDisplayBoundsChanged(const gfx::Display& display) {
150}
151
152void WindowWatcher::OnDisplayAdded(const gfx::Display& new_display) {
[email protected]bf9cdb362013-10-25 19:22:45153 aura::Window* root = Shell::GetInstance()->display_controller()->
[email protected]7a890e442013-01-23 00:48:20154 GetRootWindowForDisplayId(new_display.id());
155 workspace_window_watcher_->RootWindowAdded(root);
156}
157
158void WindowWatcher::OnDisplayRemoved(const gfx::Display& old_display) {
159 // All windows in the display has already been removed, so no need to
160 // remove observers.
161}
162
[email protected]262f8bd2012-03-23 19:30:27163} // namespace shell
164} // namespace ash