summaryrefslogtreecommitdiffstats
path: root/ash/shell/window_watcher.cc
blob: 157a5980ad1575b4b16ca5c695e3c713afd8865e (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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 "ash/shell_window_ids.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"

namespace ash {
namespace shell {

class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
 public:
  explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {
    watcher_->window_->AddObserver(this);
    for (size_t i = 0; i < watcher_->window_->children().size(); ++i)
      watcher_->window_->children()[i]->AddObserver(watcher_);
  }

  virtual ~WorkspaceWindowWatcher() {
    watcher_->window_->RemoveObserver(this);
    for (size_t i = 0; i < watcher_->window_->children().size(); ++i)
      watcher_->window_->children()[i]->RemoveObserver(watcher_);
  }

  virtual void OnWindowAdded(aura::Window* new_window) OVERRIDE {
    new_window->AddObserver(watcher_);
  }

  virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE {
    DCHECK(window->children().empty());
    window->RemoveObserver(watcher_);
  }

 private:
  WindowWatcher* watcher_;

  DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
};

WindowWatcher::WindowWatcher()
    : window_(Launcher::ForPrimaryDisplay()->window_container()),
      panel_container_(ash::Shell::GetContainer(
          window_->GetRootWindow(),
          internal::kShellWindowId_PanelContainer)) {
  workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
  panel_container_->AddObserver(this);
}

WindowWatcher::~WindowWatcher() {
  panel_container_->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 &&
      new_window->type() != aura::client::WINDOW_TYPE_PANEL)
    return;

  static int image_count = 0;
  ash::LauncherModel* model = Launcher::ForPrimaryDisplay()->model();
  ash::LauncherItem item;
  item.type = ash::TYPE_TABBED;
  id_to_window_[model->next_id()] = new_window;

  SkBitmap icon_bitmap;
  icon_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
  icon_bitmap.allocPixels();
  icon_bitmap.eraseARGB(255,
                        image_count == 0 ? 255 : 0,
                        image_count == 1 ? 255 : 0,
                        image_count == 2 ? 255 : 0);
  image_count = (image_count + 1) % 3;
  item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap,
                                                ui::SCALE_FACTOR_100P));

  model->Add(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 = Launcher::ForPrimaryDisplay()->model();
      int index = model->ItemIndexByID(i->first);
      DCHECK_NE(-1, index);
      model->RemoveItemAt(index);
      id_to_window_.erase(i);
      break;
    }
  }
}

}  // namespace shell
}  // namespace ash