blob: 99ed48b473ea45734e65c5b8c7207b7ad894a1d6 [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
msw5b121132016-09-15 00:24:079#include "ash/aura/wm_window_aura.h"
jamescookec04cc32016-06-07 00:52:4510#include "ash/common/shelf/shelf_model.h"
mswdf64c66b2016-08-23 18:56:3711#include "ash/common/shelf/shelf_widget.h"
jamescookabe9e24c2016-07-20 20:03:5912#include "ash/common/wm_shell.h"
msw5b121132016-09-15 00:24:0713#include "ash/common/wm_window_property.h"
oshimae2818922015-07-28 01:18:5214#include "ash/display/window_tree_host_manager.h"
jamescook8800b8232016-10-19 12:46:2715#include "ash/public/cpp/shell_window_ids.h"
[email protected]262f8bd2012-03-23 19:30:2716#include "ash/shell.h"
[email protected]c25ff502013-12-04 13:52:4817#include "ash/shell/window_watcher_shelf_item_delegate.h"
bruthigb7056f62015-06-04 21:04:5318#include "ash/wm/window_util.h"
[email protected]262f8bd2012-03-23 19:30:2719#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2620#include "ui/aura/window_event_dispatcher.h"
oshimaf84b0da722016-04-27 19:47:1921#include "ui/display/display.h"
[email protected]262f8bd2012-03-23 19:30:2722
23namespace ash {
24namespace shell {
25
[email protected]95058572012-08-20 14:57:2926class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver {
27 public:
jamescookb8dcef522016-06-25 14:42:5528 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {}
[email protected]95058572012-08-20 14:57:2929
dchengf5963512014-10-28 01:14:1330 ~WorkspaceWindowWatcher() override {}
[email protected]95058572012-08-20 14:57:2931
dchengf5963512014-10-28 01:14:1332 void OnWindowAdded(aura::Window* new_window) override {
[email protected]95058572012-08-20 14:57:2933 new_window->AddObserver(watcher_);
34 }
35
dchengf5963512014-10-28 01:14:1336 void OnWillRemoveWindow(aura::Window* window) override {
[email protected]95058572012-08-20 14:57:2937 DCHECK(window->children().empty());
38 window->RemoveObserver(watcher_);
39 }
40
[email protected]bf9cdb362013-10-25 19:22:4541 void RootWindowAdded(aura::Window* root) {
[email protected]093b8d642014-04-03 20:59:2842 aura::Window* panel_container =
43 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer);
[email protected]7a890e442013-01-23 00:48:2044 panel_container->AddObserver(watcher_);
45
[email protected]478c6c32013-03-09 02:50:5846 aura::Window* container =
jamescook6500ad132016-05-27 06:15:5447 ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer);
[email protected]7a890e442013-01-23 00:48:2048 container->AddObserver(this);
49 for (size_t i = 0; i < container->children().size(); ++i)
50 container->children()[i]->AddObserver(watcher_);
51 }
52
[email protected]41baaed2013-11-09 04:18:2653 void RootWindowRemoved(aura::Window* root) {
[email protected]093b8d642014-04-03 20:59:2854 aura::Window* panel_container =
55 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer);
[email protected]7a890e442013-01-23 00:48:2056 panel_container->RemoveObserver(watcher_);
57
[email protected]478c6c32013-03-09 02:50:5858 aura::Window* container =
jamescook6500ad132016-05-27 06:15:5459 ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer);
[email protected]7a890e442013-01-23 00:48:2060 container->RemoveObserver(this);
61 for (size_t i = 0; i < container->children().size(); ++i)
62 container->children()[i]->RemoveObserver(watcher_);
63 }
64
[email protected]95058572012-08-20 14:57:2965 private:
66 WindowWatcher* watcher_;
67
68 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
69};
70
[email protected]7a890e442013-01-23 00:48:2071WindowWatcher::WindowWatcher() {
[email protected]c96b9812012-10-17 16:04:0472 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
[email protected]c9390bd2013-11-08 20:33:1373 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
74 for (aura::Window::Windows::iterator iter = root_windows.begin();
jamescookb8dcef522016-06-25 14:42:5575 iter != root_windows.end(); ++iter) {
[email protected]7a890e442013-01-23 00:48:2076 workspace_window_watcher_->RootWindowAdded(*iter);
77 }
[email protected]262f8bd2012-03-23 19:30:2778}
79
80WindowWatcher::~WindowWatcher() {
[email protected]c9390bd2013-11-08 20:33:1381 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
82 for (aura::Window::Windows::iterator iter = root_windows.begin();
jamescookb8dcef522016-06-25 14:42:5583 iter != root_windows.end(); ++iter) {
[email protected]41baaed2013-11-09 04:18:2684 workspace_window_watcher_->RootWindowRemoved(*iter);
[email protected]7a890e442013-01-23 00:48:2085 }
[email protected]262f8bd2012-03-23 19:30:2786}
87
[email protected]eec0971422014-02-03 23:51:2788aura::Window* WindowWatcher::GetWindowByID(ash::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;
jamescookabe9e24c2016-07-20 20:03:5999 ShelfModel* model = WmShell::Get()->shelf_model();
[email protected]eec0971422014-02-03 23:51:27100 ShelfItem item;
[email protected]5b251f12013-12-19 01:50:05101 item.type = new_window->type() == ui::wm::WINDOW_TYPE_PANEL
102 ? ash::TYPE_APP_PANEL
mswb3fee9f2016-11-29 03:33:51103 : ash::TYPE_APP;
[email protected]eec0971422014-02-03 23:51:27104 ash::ShelfID id = model->next_id();
[email protected]6777f632013-10-15 02:38:41105 id_to_window_[id] = new_window;
[email protected]630a4602012-07-17 01:30:16106
107 SkBitmap icon_bitmap;
[email protected]f47d8ca2014-07-02 15:56:30108 icon_bitmap.allocN32Pixels(16, 16);
jamescookb8dcef522016-06-25 14:42:55109 icon_bitmap.eraseARGB(255, image_count == 0 ? 255 : 0,
110 image_count == 1 ? 255 : 0, image_count == 2 ? 255 : 0);
[email protected]262f8bd2012-03-23 19:30:27111 image_count = (image_count + 1) % 3;
[email protected]50b66262013-09-24 03:25:48112 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f));
msw072dc5472017-01-11 15:16:21113 item.title = new_window->GetTitle();
[email protected]630a4602012-07-17 01:30:16114
[email protected]b33ac3762012-03-24 01:19:52115 model->Add(item);
[email protected]6777f632013-10-15 02:38:41116
dchenga94547472016-04-08 08:41:11117 std::unique_ptr<ShelfItemDelegate> delegate(
[email protected]c25ff502013-12-04 13:52:48118 new WindowWatcherShelfItemDelegate(id, this));
jamescookb5974f22016-07-22 17:59:33119 model->SetShelfItemDelegate(id, std::move(delegate));
msw5b121132016-09-15 00:24:07120 WmWindowAura::Get(new_window)->SetIntProperty(WmWindowProperty::SHELF_ID, id);
[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) {
jamescookabe9e24c2016-07-20 20:03:59127 ShelfModel* model = WmShell::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
oshimaf84b0da722016-04-27 19:47:19137void WindowWatcher::OnDisplayAdded(const display::Display& new_display) {
oshimae2818922015-07-28 01:18:52138 aura::Window* root = Shell::GetInstance()
139 ->window_tree_host_manager()
140 ->GetRootWindowForDisplayId(new_display.id());
[email protected]7a890e442013-01-23 00:48:20141 workspace_window_watcher_->RootWindowAdded(root);
142}
143
oshimaf84b0da722016-04-27 19:47:19144void WindowWatcher::OnDisplayRemoved(const display::Display& old_display) {
[email protected]7a890e442013-01-23 00:48:20145 // All windows in the display has already been removed, so no need to
146 // remove observers.
147}
148
oshimaf84b0da722016-04-27 19:47:19149void WindowWatcher::OnDisplayMetricsChanged(const display::Display&, uint32_t) {
[email protected]0c5703d2014-05-22 01:26:01150}
151
[email protected]262f8bd2012-03-23 19:30:27152} // namespace shell
153} // namespace ash