blob: f171c34ec3de3eb4c7e3b1a8579908777f615015 [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
oshimae2818922015-07-28 01:18:529#include "ash/display/window_tree_host_manager.h"
[email protected]864b58552013-12-19 04:19:3810#include "ash/shelf/shelf.h"
[email protected]c25ff502013-12-04 13:52:4811#include "ash/shelf/shelf_item_delegate_manager.h"
[email protected]64936bf2013-11-14 19:48:3612#include "ash/shelf/shelf_model.h"
[email protected]472fc7f2013-11-16 02:35:5813#include "ash/shelf/shelf_util.h"
[email protected]478c6c32013-03-09 02:50:5814#include "ash/shelf/shelf_widget.h"
[email protected]262f8bd2012-03-23 19:30:2715#include "ash/shell.h"
[email protected]c25ff502013-12-04 13:52:4816#include "ash/shell/window_watcher_shelf_item_delegate.h"
[email protected]7cf8dd62012-04-22 05:14:5817#include "ash/shell_window_ids.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:
28 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {
[email protected]95058572012-08-20 14:57:2929 }
30
dchengf5963512014-10-28 01:14:1331 ~WorkspaceWindowWatcher() override {}
[email protected]95058572012-08-20 14:57:2932
dchengf5963512014-10-28 01:14:1333 void OnWindowAdded(aura::Window* new_window) override {
[email protected]95058572012-08-20 14:57:2934 new_window->AddObserver(watcher_);
35 }
36
dchengf5963512014-10-28 01:14:1337 void OnWillRemoveWindow(aura::Window* window) override {
[email protected]95058572012-08-20 14:57:2938 DCHECK(window->children().empty());
39 window->RemoveObserver(watcher_);
40 }
41
[email protected]bf9cdb362013-10-25 19:22:4542 void RootWindowAdded(aura::Window* root) {
[email protected]093b8d642014-04-03 20:59:2843 aura::Window* panel_container =
44 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer);
[email protected]7a890e442013-01-23 00:48:2045 panel_container->AddObserver(watcher_);
46
[email protected]478c6c32013-03-09 02:50:5847 aura::Window* container =
jamescook6500ad132016-05-27 06:15:5448 ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer);
[email protected]7a890e442013-01-23 00:48:2049 container->AddObserver(this);
50 for (size_t i = 0; i < container->children().size(); ++i)
51 container->children()[i]->AddObserver(watcher_);
52 }
53
[email protected]41baaed2013-11-09 04:18:2654 void RootWindowRemoved(aura::Window* root) {
[email protected]093b8d642014-04-03 20:59:2855 aura::Window* panel_container =
56 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer);
[email protected]7a890e442013-01-23 00:48:2057 panel_container->RemoveObserver(watcher_);
58
[email protected]478c6c32013-03-09 02:50:5859 aura::Window* container =
jamescook6500ad132016-05-27 06:15:5460 ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer);
[email protected]7a890e442013-01-23 00:48:2061 container->RemoveObserver(this);
62 for (size_t i = 0; i < container->children().size(); ++i)
63 container->children()[i]->RemoveObserver(watcher_);
64 }
65
[email protected]95058572012-08-20 14:57:2966 private:
67 WindowWatcher* watcher_;
68
69 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher);
70};
71
[email protected]7a890e442013-01-23 00:48:2072WindowWatcher::WindowWatcher() {
[email protected]c96b9812012-10-17 16:04:0473 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this));
[email protected]c9390bd2013-11-08 20:33:1374 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
75 for (aura::Window::Windows::iterator iter = root_windows.begin();
[email protected]7a890e442013-01-23 00:48:2076 iter != root_windows.end(); ++ iter) {
77 workspace_window_watcher_->RootWindowAdded(*iter);
78 }
[email protected]262f8bd2012-03-23 19:30:2779}
80
81WindowWatcher::~WindowWatcher() {
[email protected]c9390bd2013-11-08 20:33:1382 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
83 for (aura::Window::Windows::iterator iter = root_windows.begin();
[email protected]7a890e442013-01-23 00:48:2084 iter != root_windows.end(); ++ iter) {
[email protected]41baaed2013-11-09 04:18:2685 workspace_window_watcher_->RootWindowRemoved(*iter);
[email protected]7a890e442013-01-23 00:48:2086 }
[email protected]262f8bd2012-03-23 19:30:2787}
88
[email protected]eec0971422014-02-03 23:51:2789aura::Window* WindowWatcher::GetWindowByID(ash::ShelfID id) {
[email protected]262f8bd2012-03-23 19:30:2790 IDToWindow::const_iterator i = id_to_window_.find(id);
91 return i != id_to_window_.end() ? i->second : NULL;
92}
93
[email protected]262f8bd2012-03-23 19:30:2794// aura::WindowObserver overrides:
95void WindowWatcher::OnWindowAdded(aura::Window* new_window) {
bruthigb7056f62015-06-04 21:04:5396 if (!wm::IsWindowUserPositionable(new_window))
[email protected]262f8bd2012-03-23 19:30:2797 return;
98
99 static int image_count = 0;
[email protected]64936bf2013-11-14 19:48:36100 ShelfModel* model = Shell::GetInstance()->shelf_model();
[email protected]eec0971422014-02-03 23:51:27101 ShelfItem item;
[email protected]5b251f12013-12-19 01:50:05102 item.type = new_window->type() == ui::wm::WINDOW_TYPE_PANEL
103 ? ash::TYPE_APP_PANEL
104 : ash::TYPE_PLATFORM_APP;
[email protected]eec0971422014-02-03 23:51:27105 ash::ShelfID id = model->next_id();
[email protected]6777f632013-10-15 02:38:41106 id_to_window_[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);
[email protected]630a4602012-07-17 01:30:16110 icon_bitmap.eraseARGB(255,
111 image_count == 0 ? 255 : 0,
112 image_count == 1 ? 255 : 0,
113 image_count == 2 ? 255 : 0);
[email protected]262f8bd2012-03-23 19:30:27114 image_count = (image_count + 1) % 3;
[email protected]50b66262013-09-24 03:25:48115 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f));
[email protected]630a4602012-07-17 01:30:16116
[email protected]b33ac3762012-03-24 01:19:52117 model->Add(item);
[email protected]6777f632013-10-15 02:38:41118
[email protected]c25ff502013-12-04 13:52:48119 ShelfItemDelegateManager* manager =
120 Shell::GetInstance()->shelf_item_delegate_manager();
dchenga94547472016-04-08 08:41:11121 std::unique_ptr<ShelfItemDelegate> delegate(
[email protected]c25ff502013-12-04 13:52:48122 new WindowWatcherShelfItemDelegate(id, this));
dchengcbf0d9d2015-12-27 22:49:23123 manager->SetShelfItemDelegate(id, std::move(delegate));
[email protected]eec0971422014-02-03 23:51:27124 SetShelfIDForWindow(id, new_window);
[email protected]262f8bd2012-03-23 19:30:27125}
126
127void WindowWatcher::OnWillRemoveWindow(aura::Window* window) {
128 for (IDToWindow::iterator i = id_to_window_.begin();
129 i != id_to_window_.end(); ++i) {
130 if (i->second == window) {
[email protected]64936bf2013-11-14 19:48:36131 ShelfModel* model = Shell::GetInstance()->shelf_model();
[email protected]262f8bd2012-03-23 19:30:27132 int index = model->ItemIndexByID(i->first);
133 DCHECK_NE(-1, index);
134 model->RemoveItemAt(index);
135 id_to_window_.erase(i);
136 break;
137 }
138 }
139}
140
oshimaf84b0da722016-04-27 19:47:19141void WindowWatcher::OnDisplayAdded(const display::Display& new_display) {
oshimae2818922015-07-28 01:18:52142 aura::Window* root = Shell::GetInstance()
143 ->window_tree_host_manager()
144 ->GetRootWindowForDisplayId(new_display.id());
[email protected]7a890e442013-01-23 00:48:20145 workspace_window_watcher_->RootWindowAdded(root);
146}
147
oshimaf84b0da722016-04-27 19:47:19148void WindowWatcher::OnDisplayRemoved(const display::Display& old_display) {
[email protected]7a890e442013-01-23 00:48:20149 // All windows in the display has already been removed, so no need to
150 // remove observers.
151}
152
oshimaf84b0da722016-04-27 19:47:19153void WindowWatcher::OnDisplayMetricsChanged(const display::Display&, uint32_t) {
[email protected]0c5703d2014-05-22 01:26:01154}
155
[email protected]262f8bd2012-03-23 19:30:27156} // namespace shell
157} // namespace ash