[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 1 | // 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 | |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
msw | 5b12113 | 2016-09-15 00:24:07 | [diff] [blame] | 9 | #include "ash/aura/wm_window_aura.h" |
jamescook | ec04cc3 | 2016-06-07 00:52:45 | [diff] [blame] | 10 | #include "ash/common/shelf/shelf_model.h" |
msw | df64c66b | 2016-08-23 18:56:37 | [diff] [blame] | 11 | #include "ash/common/shelf/shelf_widget.h" |
jamescook | abe9e24c | 2016-07-20 20:03:59 | [diff] [blame] | 12 | #include "ash/common/wm_shell.h" |
msw | 5b12113 | 2016-09-15 00:24:07 | [diff] [blame] | 13 | #include "ash/common/wm_window_property.h" |
oshima | e281892 | 2015-07-28 01:18:52 | [diff] [blame] | 14 | #include "ash/display/window_tree_host_manager.h" |
jamescook | 8800b823 | 2016-10-19 12:46:27 | [diff] [blame] | 15 | #include "ash/public/cpp/shell_window_ids.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 16 | #include "ash/shell.h" |
[email protected] | c25ff50 | 2013-12-04 13:52:48 | [diff] [blame] | 17 | #include "ash/shell/window_watcher_shelf_item_delegate.h" |
bruthig | b7056f6 | 2015-06-04 21:04:53 | [diff] [blame] | 18 | #include "ash/wm/window_util.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 19 | #include "ui/aura/window.h" |
[email protected] | fcc51c95 | 2014-02-21 21:31:26 | [diff] [blame] | 20 | #include "ui/aura/window_event_dispatcher.h" |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 21 | #include "ui/display/display.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 22 | |
| 23 | namespace ash { |
| 24 | namespace shell { |
| 25 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 26 | class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver { |
| 27 | public: |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 28 | explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {} |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 29 | |
dcheng | f596351 | 2014-10-28 01:14:13 | [diff] [blame] | 30 | ~WorkspaceWindowWatcher() override {} |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 31 | |
dcheng | f596351 | 2014-10-28 01:14:13 | [diff] [blame] | 32 | void OnWindowAdded(aura::Window* new_window) override { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 33 | new_window->AddObserver(watcher_); |
| 34 | } |
| 35 | |
dcheng | f596351 | 2014-10-28 01:14:13 | [diff] [blame] | 36 | void OnWillRemoveWindow(aura::Window* window) override { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 37 | DCHECK(window->children().empty()); |
| 38 | window->RemoveObserver(watcher_); |
| 39 | } |
| 40 | |
[email protected] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 41 | void RootWindowAdded(aura::Window* root) { |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 42 | aura::Window* panel_container = |
| 43 | ash::Shell::GetContainer(root, kShellWindowId_PanelContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 44 | panel_container->AddObserver(watcher_); |
| 45 | |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 46 | aura::Window* container = |
jamescook | 6500ad13 | 2016-05-27 06:15:54 | [diff] [blame] | 47 | ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 48 | container->AddObserver(this); |
| 49 | for (size_t i = 0; i < container->children().size(); ++i) |
| 50 | container->children()[i]->AddObserver(watcher_); |
| 51 | } |
| 52 | |
[email protected] | 41baaed | 2013-11-09 04:18:26 | [diff] [blame] | 53 | void RootWindowRemoved(aura::Window* root) { |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 54 | aura::Window* panel_container = |
| 55 | ash::Shell::GetContainer(root, kShellWindowId_PanelContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 56 | panel_container->RemoveObserver(watcher_); |
| 57 | |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 58 | aura::Window* container = |
jamescook | 6500ad13 | 2016-05-27 06:15:54 | [diff] [blame] | 59 | ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 60 | container->RemoveObserver(this); |
| 61 | for (size_t i = 0; i < container->children().size(); ++i) |
| 62 | container->children()[i]->RemoveObserver(watcher_); |
| 63 | } |
| 64 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 65 | private: |
| 66 | WindowWatcher* watcher_; |
| 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher); |
| 69 | }; |
| 70 | |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 71 | WindowWatcher::WindowWatcher() { |
[email protected] | c96b981 | 2012-10-17 16:04:04 | [diff] [blame] | 72 | workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this)); |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 73 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 74 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 75 | iter != root_windows.end(); ++iter) { |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 76 | workspace_window_watcher_->RootWindowAdded(*iter); |
| 77 | } |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | WindowWatcher::~WindowWatcher() { |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 81 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 82 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 83 | iter != root_windows.end(); ++iter) { |
[email protected] | 41baaed | 2013-11-09 04:18:26 | [diff] [blame] | 84 | workspace_window_watcher_->RootWindowRemoved(*iter); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 85 | } |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | eec097142 | 2014-02-03 23:51:27 | [diff] [blame] | 88 | aura::Window* WindowWatcher::GetWindowByID(ash::ShelfID id) { |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 89 | IDToWindow::const_iterator i = id_to_window_.find(id); |
| 90 | return i != id_to_window_.end() ? i->second : NULL; |
| 91 | } |
| 92 | |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 93 | // aura::WindowObserver overrides: |
| 94 | void WindowWatcher::OnWindowAdded(aura::Window* new_window) { |
bruthig | b7056f6 | 2015-06-04 21:04:53 | [diff] [blame] | 95 | if (!wm::IsWindowUserPositionable(new_window)) |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 96 | return; |
| 97 | |
| 98 | static int image_count = 0; |
jamescook | abe9e24c | 2016-07-20 20:03:59 | [diff] [blame] | 99 | ShelfModel* model = WmShell::Get()->shelf_model(); |
[email protected] | eec097142 | 2014-02-03 23:51:27 | [diff] [blame] | 100 | ShelfItem item; |
[email protected] | 5b251f1 | 2013-12-19 01:50:05 | [diff] [blame] | 101 | item.type = new_window->type() == ui::wm::WINDOW_TYPE_PANEL |
| 102 | ? ash::TYPE_APP_PANEL |
msw | b3fee9f | 2016-11-29 03:33:51 | [diff] [blame] | 103 | : ash::TYPE_APP; |
[email protected] | eec097142 | 2014-02-03 23:51:27 | [diff] [blame] | 104 | ash::ShelfID id = model->next_id(); |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 105 | id_to_window_[id] = new_window; |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 106 | |
| 107 | SkBitmap icon_bitmap; |
[email protected] | f47d8ca | 2014-07-02 15:56:30 | [diff] [blame] | 108 | icon_bitmap.allocN32Pixels(16, 16); |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 109 | icon_bitmap.eraseARGB(255, image_count == 0 ? 255 : 0, |
| 110 | image_count == 1 ? 255 : 0, image_count == 2 ? 255 : 0); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 111 | image_count = (image_count + 1) % 3; |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 112 | item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f)); |
msw | 072dc547 | 2017-01-11 15:16:21 | [diff] [blame^] | 113 | item.title = new_window->GetTitle(); |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 114 | |
[email protected] | b33ac376 | 2012-03-24 01:19:52 | [diff] [blame] | 115 | model->Add(item); |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 116 | |
dcheng | a9454747 | 2016-04-08 08:41:11 | [diff] [blame] | 117 | std::unique_ptr<ShelfItemDelegate> delegate( |
[email protected] | c25ff50 | 2013-12-04 13:52:48 | [diff] [blame] | 118 | new WindowWatcherShelfItemDelegate(id, this)); |
jamescook | b5974f2 | 2016-07-22 17:59:33 | [diff] [blame] | 119 | model->SetShelfItemDelegate(id, std::move(delegate)); |
msw | 5b12113 | 2016-09-15 00:24:07 | [diff] [blame] | 120 | WmWindowAura::Get(new_window)->SetIntProperty(WmWindowProperty::SHELF_ID, id); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void WindowWatcher::OnWillRemoveWindow(aura::Window* window) { |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 124 | for (IDToWindow::iterator i = id_to_window_.begin(); i != id_to_window_.end(); |
| 125 | ++i) { |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 126 | if (i->second == window) { |
jamescook | abe9e24c | 2016-07-20 20:03:59 | [diff] [blame] | 127 | ShelfModel* model = WmShell::Get()->shelf_model(); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 128 | 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 | |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 137 | void WindowWatcher::OnDisplayAdded(const display::Display& new_display) { |
oshima | e281892 | 2015-07-28 01:18:52 | [diff] [blame] | 138 | aura::Window* root = Shell::GetInstance() |
| 139 | ->window_tree_host_manager() |
| 140 | ->GetRootWindowForDisplayId(new_display.id()); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 141 | workspace_window_watcher_->RootWindowAdded(root); |
| 142 | } |
| 143 | |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 144 | void WindowWatcher::OnDisplayRemoved(const display::Display& old_display) { |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 145 | // All windows in the display has already been removed, so no need to |
| 146 | // remove observers. |
| 147 | } |
| 148 | |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 149 | void WindowWatcher::OnDisplayMetricsChanged(const display::Display&, uint32_t) { |
[email protected] | 0c5703d | 2014-05-22 01:26:01 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 152 | } // namespace shell |
| 153 | } // namespace ash |