[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" |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 9 | #include "ash/launcher/launcher_item_delegate_manager.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 10 | #include "ash/launcher/launcher_model.h" |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 11 | #include "ash/shelf/shelf_widget.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 12 | #include "ash/shell.h" |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 13 | #include "ash/shell/window_watcher_launcher_item_delegate.h" |
[email protected] | 7cf8dd6 | 2012-04-22 05:14:58 | [diff] [blame] | 14 | #include "ash/shell_window_ids.h" |
[email protected] | 88d7112 | 2012-10-18 07:11:01 | [diff] [blame] | 15 | #include "ui/aura/root_window.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 16 | #include "ui/aura/window.h" |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 17 | #include "ui/gfx/display.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 18 | |
| 19 | namespace ash { |
| 20 | namespace shell { |
| 21 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 22 | class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver { |
| 23 | public: |
| 24 | explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | virtual ~WorkspaceWindowWatcher() { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 28 | } |
| 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] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 39 | void RootWindowAdded(aura::Window* root) { |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 40 | aura::Window* panel_container = ash::Shell::GetContainer( |
| 41 | root, |
| 42 | internal::kShellWindowId_PanelContainer); |
| 43 | panel_container->AddObserver(watcher_); |
| 44 | |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 45 | aura::Window* container = |
| 46 | Launcher::ForWindow(root)->shelf_widget()->window_container(); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 47 | container->AddObserver(this); |
| 48 | for (size_t i = 0; i < container->children().size(); ++i) |
| 49 | container->children()[i]->AddObserver(watcher_); |
| 50 | } |
| 51 | |
[email protected] | 41baaed | 2013-11-09 04:18:26 | [diff] [blame^] | 52 | void RootWindowRemoved(aura::Window* root) { |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 53 | aura::Window* panel_container = ash::Shell::GetContainer( |
| 54 | root, |
| 55 | internal::kShellWindowId_PanelContainer); |
| 56 | panel_container->RemoveObserver(watcher_); |
| 57 | |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 58 | aura::Window* container = |
| 59 | Launcher::ForWindow(root)->shelf_widget()->window_container(); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 60 | container->RemoveObserver(this); |
| 61 | for (size_t i = 0; i < container->children().size(); ++i) |
| 62 | container->children()[i]->RemoveObserver(watcher_); |
| 63 | } |
| 64 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 65 | private: |
| 66 | WindowWatcher* watcher_; |
| 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher); |
| 69 | }; |
| 70 | |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 71 | WindowWatcher::WindowWatcher() { |
[email protected] | c96b981 | 2012-10-17 16:04:04 | [diff] [blame] | 72 | workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this)); |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 73 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 74 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 75 | iter != root_windows.end(); ++ iter) { |
| 76 | workspace_window_watcher_->RootWindowAdded(*iter); |
| 77 | } |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | WindowWatcher::~WindowWatcher() { |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 81 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 82 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 83 | iter != root_windows.end(); ++ iter) { |
[email protected] | 41baaed | 2013-11-09 04:18:26 | [diff] [blame^] | 84 | workspace_window_watcher_->RootWindowRemoved(*iter); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 85 | } |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | aura::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 | |
| 93 | ash::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: |
| 103 | void WindowWatcher::OnWindowAdded(aura::Window* new_window) { |
[email protected] | 7cf8dd6 | 2012-04-22 05:14:58 | [diff] [blame] | 104 | if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL && |
| 105 | new_window->type() != aura::client::WINDOW_TYPE_PANEL) |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 106 | return; |
| 107 | |
| 108 | static int image_count = 0; |
[email protected] | b913a3a | 2012-12-11 13:07:19 | [diff] [blame] | 109 | ash::LauncherModel* model = Shell::GetInstance()->launcher_model(); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 110 | ash::LauncherItem item; |
[email protected] | 97f29457 | 2012-12-13 09:08:20 | [diff] [blame] | 111 | item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL ? |
[email protected] | 8d181bf | 2013-08-28 15:34:32 | [diff] [blame] | 112 | ash::TYPE_APP_PANEL : ash::TYPE_PLATFORM_APP; |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 113 | ash::LauncherID id = model->next_id(); |
| 114 | id_to_window_[id] = new_window; |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 115 | |
| 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 123 | image_count = (image_count + 1) % 3; |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 124 | item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f)); |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 125 | |
[email protected] | b33ac376 | 2012-03-24 01:19:52 | [diff] [blame] | 126 | model->Add(item); |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 127 | |
| 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void 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] | b913a3a | 2012-12-11 13:07:19 | [diff] [blame] | 139 | ash::LauncherModel* model = Shell::GetInstance()->launcher_model(); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 140 | 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] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 149 | void WindowWatcher::OnDisplayBoundsChanged(const gfx::Display& display) { |
| 150 | } |
| 151 | |
| 152 | void WindowWatcher::OnDisplayAdded(const gfx::Display& new_display) { |
[email protected] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 153 | aura::Window* root = Shell::GetInstance()->display_controller()-> |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 154 | GetRootWindowForDisplayId(new_display.id()); |
| 155 | workspace_window_watcher_->RootWindowAdded(root); |
| 156 | } |
| 157 | |
| 158 | void 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] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 163 | } // namespace shell |
| 164 | } // namespace ash |