[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ash/shell/window_watcher.h" |
| 6 | |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 7 | #include "ash/display/display_controller.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 8 | #include "ash/launcher/launcher.h" |
| 9 | #include "ash/launcher/launcher_model.h" |
| 10 | #include "ash/shell.h" |
[email protected] | 7cf8dd6 | 2012-04-22 05:14:58 | [diff] [blame] | 11 | #include "ash/shell_window_ids.h" |
[email protected] | 88d7112 | 2012-10-18 07:11:01 | [diff] [blame] | 12 | #include "ui/aura/root_window.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 13 | #include "ui/aura/window.h" |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 14 | #include "ui/gfx/display.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 15 | |
| 16 | namespace ash { |
| 17 | namespace shell { |
| 18 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 19 | class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver { |
| 20 | public: |
| 21 | explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | virtual ~WorkspaceWindowWatcher() { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 25 | } |
| 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] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 36 | 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] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 60 | private: |
| 61 | WindowWatcher* watcher_; |
| 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher); |
| 64 | }; |
| 65 | |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 66 | WindowWatcher::WindowWatcher() { |
[email protected] | c96b981 | 2012-10-17 16:04:04 | [diff] [blame] | 67 | workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this)); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 68 | 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | WindowWatcher::~WindowWatcher() { |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 76 | 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | aura::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 | |
| 88 | ash::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: |
| 98 | void WindowWatcher::OnWindowAdded(aura::Window* new_window) { |
[email protected] | 7cf8dd6 | 2012-04-22 05:14:58 | [diff] [blame] | 99 | if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL && |
| 100 | new_window->type() != aura::client::WINDOW_TYPE_PANEL) |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 101 | return; |
| 102 | |
| 103 | static int image_count = 0; |
[email protected] | b913a3a | 2012-12-11 13:07:19 | [diff] [blame] | 104 | ash::LauncherModel* model = Shell::GetInstance()->launcher_model(); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 105 | ash::LauncherItem item; |
[email protected] | 97f29457 | 2012-12-13 09:08:20 | [diff] [blame] | 106 | item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL ? |
| 107 | ash::TYPE_APP_PANEL : ash::TYPE_TABBED; |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 108 | id_to_window_[model->next_id()] = new_window; |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 109 | |
| 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 117 | image_count = (image_count + 1) % 3; |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 118 | item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, |
[email protected] | 0a4cf45 | 2012-11-14 07:28:40 | [diff] [blame] | 119 | ui::SCALE_FACTOR_100P)); |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 120 | |
[email protected] | b33ac376 | 2012-03-24 01:19:52 | [diff] [blame] | 121 | model->Add(item); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void 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] | b913a3a | 2012-12-11 13:07:19 | [diff] [blame] | 128 | ash::LauncherModel* model = Shell::GetInstance()->launcher_model(); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 129 | 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] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame^] | 138 | void WindowWatcher::OnDisplayBoundsChanged(const gfx::Display& display) { |
| 139 | } |
| 140 | |
| 141 | void 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 | |
| 147 | void 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 152 | } // namespace shell |
| 153 | } // namespace ash |