summaryrefslogtreecommitdiffstats
path: root/ash/shell/window_watcher.cc
blob: 05c4f7479f040d03fe5abac03f67783d88c869da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/shell/window_watcher.h"

#include "ash/launcher/launcher.h"
#include "ash/launcher/launcher_model.h"
#include "ash/shell.h"
#include "ui/aura/window.h"

namespace ash {
namespace shell {

WindowWatcher::WindowWatcher()
    : window_(ash::Shell::GetInstance()->launcher()->window_container()) {
  window_->AddObserver(this);
}

WindowWatcher::~WindowWatcher() {
  window_->RemoveObserver(this);
}

aura::Window* WindowWatcher::GetWindowByID(ash::LauncherID id) {
  IDToWindow::const_iterator i = id_to_window_.find(id);
  return i != id_to_window_.end() ? i->second : NULL;
}

ash::LauncherID WindowWatcher::GetIDByWindow(aura::Window* window) const {
  for (IDToWindow::const_iterator i = id_to_window_.begin();
       i != id_to_window_.end(); ++i) {
    if (i->second == window)
      return i->first;
  }
  return 0;  // TODO: add a constant for this.
}

// aura::WindowObserver overrides:
void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
  if (new_window->type() != aura::client::WINDOW_TYPE_NORMAL)
    return;

  static int image_count = 0;
  ash::LauncherModel* model = ash::Shell::GetInstance()->launcher()->model();
  ash::LauncherItem item;
  item.type = ash::TYPE_TABBED;
  id_to_window_[model->next_id()] = new_window;
  item.num_tabs = image_count + 1;
  item.image.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
  item.image.allocPixels();
  item.image.eraseARGB(255,
                       image_count == 0 ? 255 : 0,
                       image_count == 1 ? 255 : 0,
                       image_count == 2 ? 255 : 0);
  image_count = (image_count + 1) % 3;
  model->Add(model->item_count(), item);
}

void WindowWatcher::OnWillRemoveWindow(aura::Window* window) {
  for (IDToWindow::iterator i = id_to_window_.begin();
       i != id_to_window_.end(); ++i) {
    if (i->second == window) {
      ash::LauncherModel* model =
          ash::Shell::GetInstance()->launcher()->model();
      int index = model->ItemIndexByID(i->first);
      DCHECK_NE(-1, index);
      model->RemoveItemAt(index);
      id_to_window_.erase(i);
      break;
    }
  }
}

}  // namespace shell
}  // namespace ash