[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 | |
| 7 | #include "ash/launcher/launcher.h" |
| 8 | #include "ash/launcher/launcher_model.h" |
| 9 | #include "ash/shell.h" |
| 10 | #include "ui/aura/window.h" |
| 11 | |
| 12 | namespace ash { |
| 13 | namespace shell { |
| 14 | |
| 15 | WindowWatcher::WindowWatcher() |
| 16 | : window_(ash::Shell::GetInstance()->launcher()->window_container()) { |
| 17 | window_->AddObserver(this); |
| 18 | } |
| 19 | |
| 20 | WindowWatcher::~WindowWatcher() { |
| 21 | window_->RemoveObserver(this); |
| 22 | } |
| 23 | |
| 24 | aura::Window* WindowWatcher::GetWindowByID(ash::LauncherID id) { |
| 25 | IDToWindow::const_iterator i = id_to_window_.find(id); |
| 26 | return i != id_to_window_.end() ? i->second : NULL; |
| 27 | } |
| 28 | |
| 29 | ash::LauncherID WindowWatcher::GetIDByWindow(aura::Window* window) const { |
| 30 | for (IDToWindow::const_iterator i = id_to_window_.begin(); |
| 31 | i != id_to_window_.end(); ++i) { |
| 32 | if (i->second == window) |
| 33 | return i->first; |
| 34 | } |
| 35 | return 0; // TODO: add a constant for this. |
| 36 | } |
| 37 | |
| 38 | // aura::WindowObserver overrides: |
| 39 | void WindowWatcher::OnWindowAdded(aura::Window* new_window) { |
| 40 | if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL) |
| 41 | return; |
| 42 | |
| 43 | static int image_count = 0; |
| 44 | ash::LauncherModel* model = ash::Shell::GetInstance()->launcher()->model(); |
| 45 | ash::LauncherItem item; |
| 46 | item.type = ash::TYPE_TABBED; |
| 47 | id_to_window_[model->next_id()] = new_window; |
| 48 | item.num_tabs = image_count + 1; |
| 49 | item.image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
| 50 | item.image.allocPixels(); |
| 51 | item.image.eraseARGB(255, |
| 52 | image_count == 0 ? 255 : 0, |
| 53 | image_count == 1 ? 255 : 0, |
| 54 | image_count == 2 ? 255 : 0); |
| 55 | image_count = (image_count + 1) % 3; |
| 56 | model->Add(model->item_count(), item); |
| 57 | } |
| 58 | |
| 59 | void WindowWatcher::OnWillRemoveWindow(aura::Window* window) { |
| 60 | for (IDToWindow::iterator i = id_to_window_.begin(); |
| 61 | i != id_to_window_.end(); ++i) { |
| 62 | if (i->second == window) { |
| 63 | ash::LauncherModel* model = |
| 64 | ash::Shell::GetInstance()->launcher()->model(); |
| 65 | int index = model->ItemIndexByID(i->first); |
| 66 | DCHECK_NE(-1, index); |
| 67 | model->RemoveItemAt(index); |
| 68 | id_to_window_.erase(i); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } // namespace shell |
| 75 | } // namespace ash |