blob: 70f38a6cb891fb6ce3064a02eea27752401e2fc7 [file] [log] [blame]
[email protected]262f8bd2012-03-23 19:30:271// 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
dchengcbf0d9d2015-12-27 22:49:237#include <utility>
8
msw127e6972017-03-17 06:53:389#include "ash/public/cpp/shelf_item.h"
msw109806d2017-06-02 20:11:5710#include "ash/public/cpp/shelf_model.h"
jamescook8800b8232016-10-19 12:46:2711#include "ash/public/cpp/shell_window_ids.h"
msw84b8a5f2017-05-05 00:13:3612#include "ash/public/cpp/window_properties.h"
James Cookb0bf8e82017-04-09 17:01:4413#include "ash/shelf/shelf_widget.h"
[email protected]262f8bd2012-03-23 19:30:2714#include "ash/shell.h"
[email protected]c25ff502013-12-04 13:52:4815#include "ash/shell/window_watcher_shelf_item_delegate.h"
bruthigb7056f62015-06-04 21:04:5316#include "ash/wm/window_util.h"
Scott Violet82ea8462017-07-13 22:39:5617#include "base/memory/ptr_util.h"
msw84b8a5f2017-05-05 00:13:3618#include "base/strings/string_number_conversions.h"
[email protected]262f8bd2012-03-23 19:30:2719#include "ui/aura/window.h"
[email protected]262f8bd2012-03-23 19:30:2720
21namespace ash {
22namespace shell {
23
[email protected]95058572012-08-20 14:57:2924class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
25 public:
jamescookb8dcef522016-06-25 14:42:5526 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {}
[email protected]95058572012-08-20 14:57:2927
dchengf5963512014-10-28 01:14:1328 ~WorkspaceWindowWatcher() override {}
[email protected]95058572012-08-20 14:57:2929
dchengf5963512014-10-28 01:14:1330 void OnWindowAdded(aura::Window* new_window) override {
[email protected]95058572012-08-20 14:57:2931 new_window->AddObserver(watcher_);
32 }
33
dchengf5963512014-10-28 01:14:1334 void OnWillRemoveWindow(aura::Window* window) override {
[email protected]95058572012-08-20 14:57:2935 DCHECK(window->children().empty());
36 window->RemoveObserver(watcher_);
37 }
38
[email protected]bf9cdb362013-10-25 19:22:4539 void RootWindowAdded(aura::Window* root) {
[email protected]093b8d642014-04-03 20:59:2840 aura::Window* panel_container =
msw84b8a5f2017-05-05 00:13:3641 Shell::GetContainer(root, kShellWindowId_PanelContainer);
[email protected]7a890e442013-01-23 00:48:2042 panel_container->AddObserver(watcher_);
43
[email protected]478c6c32013-03-09 02:50:5844 aura::Window* container =
msw84b8a5f2017-05-05 00:13:3645 Shell::GetContainer(root, kShellWindowId_ShelfContainer);
[email protected]7a890e442013-01-23 00:48:2046 container->AddObserver(this);
47 for (size_t i = 0; i < container->children().size(); ++i)
48 container->children()[i]->AddObserver(watcher_);
49 }
50
[email protected]41baaed2013-11-09 04:18:2651 void RootWindowRemoved(aura::Window* root) {
[email protected]093b8d642014-04-03 20:59:2852 aura::Window* panel_container =
msw84b8a5f2017-05-05 00:13:3653 Shell::GetContainer(root, kShellWindowId_PanelContainer);
[email protected]7a890e442013-01-23 00:48:2054 panel_container->RemoveObserver(watcher_);
55
[email protected]478c6c32013-03-09 02:50:5856 aura::Window* container =
msw84b8a5f2017-05-05 00:13:3657 Shell::GetContainer(root, kShellWindowId_ShelfContainer);
[email protected]7a890e442013-01-23 00:48:2058 container->RemoveObserver(this);
59 for (size_t i = 0; i < container->children().size(); ++i)
60 container->children()[i]->RemoveObserver(watcher_);
61 }
62
[email protected]95058572012-08-20 14:57:2963 private:
64 WindowWatcher* watcher_;
65
66 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
67};
68
[email protected]7a890e442013-01-23 00:48:2069WindowWatcher::WindowWatcher() {
Scott Violet82ea8462017-07-13 22:39:5670 Shell::Get()->AddShellObserver(this);
71 workspace_window_watcher_ = base::MakeUnique<WorkspaceWindowWatcher>(this);
[email protected]c9390bd2013-11-08 20:33:1372 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
73 for (aura::Window::Windows::iterator iter = root_windows.begin();
jamescookb8dcef522016-06-25 14:42:5574 iter != root_windows.end(); ++iter) {
[email protected]7a890e442013-01-23 00:48:2075 workspace_window_watcher_->RootWindowAdded(*iter);
76 }
[email protected]262f8bd2012-03-23 19:30:2777}
78
79WindowWatcher::~WindowWatcher() {
[email protected]c9390bd2013-11-08 20:33:1380 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
81 for (aura::Window::Windows::iterator iter = root_windows.begin();
jamescookb8dcef522016-06-25 14:42:5582 iter != root_windows.end(); ++iter) {
[email protected]41baaed2013-11-09 04:18:2683 workspace_window_watcher_->RootWindowRemoved(*iter);
[email protected]7a890e442013-01-23 00:48:2084 }
Scott Violet82ea8462017-07-13 22:39:5685 Shell::Get()->RemoveShellObserver(this);
[email protected]262f8bd2012-03-23 19:30:2786}
87
msw84b8a5f2017-05-05 00:13:3688aura::Window* WindowWatcher::GetWindowByID(const ShelfID& id) {
[email protected]262f8bd2012-03-23 19:30:2789 IDToWindow::const_iterator i = id_to_window_.find(id);
90 return i != id_to_window_.end() ? i->second : NULL;
91}
92
[email protected]262f8bd2012-03-23 19:30:2793// aura::WindowObserver overrides:
94void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
bruthigb7056f62015-06-04 21:04:5395 if (!wm::IsWindowUserPositionable(new_window))
[email protected]262f8bd2012-03-23 19:30:2796 return;
97
98 static int image_count = 0;
sky5ad143a2017-03-22 04:31:2399 ShelfModel* model = Shell::Get()->shelf_model();
[email protected]eec0971422014-02-03 23:51:27100 ShelfItem item;
Thiago Farina627fba952017-05-24 21:15:26101 item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL
102 ? TYPE_APP_PANEL
103 : TYPE_APP;
msw84b8a5f2017-05-05 00:13:36104 static int shelf_id = 0;
105 item.id = ShelfID(base::IntToString(shelf_id++));
106 id_to_window_[item.id] = new_window;
[email protected]630a4602012-07-17 01:30:16107
108 SkBitmap icon_bitmap;
[email protected]f47d8ca2014-07-02 15:56:30109 icon_bitmap.allocN32Pixels(16, 16);
jamescookb8dcef522016-06-25 14:42:55110 icon_bitmap.eraseARGB(255, image_count == 0 ? 255 : 0,
111 image_count == 1 ? 255 : 0, image_count == 2 ? 255 : 0);
[email protected]262f8bd2012-03-23 19:30:27112 image_count = (image_count + 1) % 3;
[email protected]50b66262013-09-24 03:25:48113 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f));
msw072dc5472017-01-11 15:16:21114 item.title = new_window->GetTitle();
[email protected]630a4602012-07-17 01:30:16115
[email protected]b33ac3762012-03-24 01:19:52116 model->Add(item);
[email protected]6777f632013-10-15 02:38:41117
msw77414f102017-03-10 21:58:06118 model->SetShelfItemDelegate(
msw84b8a5f2017-05-05 00:13:36119 item.id, base::MakeUnique<WindowWatcherShelfItemDelegate>(item.id, this));
msw6958e7f2017-05-15 22:55:06120 new_window->SetProperty(kShelfIDKey, new std::string(item.id.Serialize()));
[email protected]262f8bd2012-03-23 19:30:27121}
122
123void WindowWatcher::OnWillRemoveWindow(aura::Window* window) {
jamescookb8dcef522016-06-25 14:42:55124 for (IDToWindow::iterator i = id_to_window_.begin(); i != id_to_window_.end();
125 ++i) {
[email protected]262f8bd2012-03-23 19:30:27126 if (i->second == window) {
sky5ad143a2017-03-22 04:31:23127 ShelfModel* model = Shell::Get()->shelf_model();
[email protected]262f8bd2012-03-23 19:30:27128 int index = model->ItemIndexByID(i->first);
129 DCHECK_NE(-1, index);
130 model->RemoveItemAt(index);
131 id_to_window_.erase(i);
132 break;
133 }
134 }
135}
136
Scott Violet82ea8462017-07-13 22:39:56137void WindowWatcher::OnRootWindowAdded(aura::Window* root_window) {
138 workspace_window_watcher_->RootWindowAdded(root_window);
[email protected]0c5703d2014-05-22 01:26:01139}
140
[email protected]262f8bd2012-03-23 19:30:27141} // namespace shell
142} // namespace ash