[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 | 127e697 | 2017-03-17 06:53:38 | [diff] [blame] | 9 | #include "ash/public/cpp/shelf_item.h" |
msw | 109806d | 2017-06-02 20:11:57 | [diff] [blame] | 10 | #include "ash/public/cpp/shelf_model.h" |
jamescook | 8800b823 | 2016-10-19 12:46:27 | [diff] [blame] | 11 | #include "ash/public/cpp/shell_window_ids.h" |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 12 | #include "ash/public/cpp/window_properties.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 13 | #include "ash/shelf/shelf_widget.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 14 | #include "ash/shell.h" |
[email protected] | c25ff50 | 2013-12-04 13:52:48 | [diff] [blame] | 15 | #include "ash/shell/window_watcher_shelf_item_delegate.h" |
bruthig | b7056f6 | 2015-06-04 21:04:53 | [diff] [blame] | 16 | #include "ash/wm/window_util.h" |
Scott Violet | 82ea846 | 2017-07-13 22:39:56 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 19 | #include "ui/aura/window.h" |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 20 | |
| 21 | namespace ash { |
| 22 | namespace shell { |
| 23 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 24 | class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver { |
| 25 | public: |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 26 | explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {} |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 27 | |
dcheng | f596351 | 2014-10-28 01:14:13 | [diff] [blame] | 28 | ~WorkspaceWindowWatcher() override {} |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 29 | |
dcheng | f596351 | 2014-10-28 01:14:13 | [diff] [blame] | 30 | void OnWindowAdded(aura::Window* new_window) override { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 31 | new_window->AddObserver(watcher_); |
| 32 | } |
| 33 | |
dcheng | f596351 | 2014-10-28 01:14:13 | [diff] [blame] | 34 | void OnWillRemoveWindow(aura::Window* window) override { |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 35 | DCHECK(window->children().empty()); |
| 36 | window->RemoveObserver(watcher_); |
| 37 | } |
| 38 | |
[email protected] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 39 | void RootWindowAdded(aura::Window* root) { |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 40 | aura::Window* panel_container = |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 41 | Shell::GetContainer(root, kShellWindowId_PanelContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 42 | panel_container->AddObserver(watcher_); |
| 43 | |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 44 | aura::Window* container = |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 45 | Shell::GetContainer(root, kShellWindowId_ShelfContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 46 | container->AddObserver(this); |
| 47 | for (size_t i = 0; i < container->children().size(); ++i) |
| 48 | container->children()[i]->AddObserver(watcher_); |
| 49 | } |
| 50 | |
[email protected] | 41baaed | 2013-11-09 04:18:26 | [diff] [blame] | 51 | void RootWindowRemoved(aura::Window* root) { |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 52 | aura::Window* panel_container = |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 53 | Shell::GetContainer(root, kShellWindowId_PanelContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 54 | panel_container->RemoveObserver(watcher_); |
| 55 | |
[email protected] | 478c6c3 | 2013-03-09 02:50:58 | [diff] [blame] | 56 | aura::Window* container = |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 57 | Shell::GetContainer(root, kShellWindowId_ShelfContainer); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 58 | container->RemoveObserver(this); |
| 59 | for (size_t i = 0; i < container->children().size(); ++i) |
| 60 | container->children()[i]->RemoveObserver(watcher_); |
| 61 | } |
| 62 | |
[email protected] | 9505857 | 2012-08-20 14:57:29 | [diff] [blame] | 63 | private: |
| 64 | WindowWatcher* watcher_; |
| 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher); |
| 67 | }; |
| 68 | |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 69 | WindowWatcher::WindowWatcher() { |
Scott Violet | 82ea846 | 2017-07-13 22:39:56 | [diff] [blame] | 70 | Shell::Get()->AddShellObserver(this); |
| 71 | workspace_window_watcher_ = base::MakeUnique<WorkspaceWindowWatcher>(this); |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 72 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 73 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 74 | iter != root_windows.end(); ++iter) { |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 75 | workspace_window_watcher_->RootWindowAdded(*iter); |
| 76 | } |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | WindowWatcher::~WindowWatcher() { |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 80 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 81 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 82 | iter != root_windows.end(); ++iter) { |
[email protected] | 41baaed | 2013-11-09 04:18:26 | [diff] [blame] | 83 | workspace_window_watcher_->RootWindowRemoved(*iter); |
[email protected] | 7a890e44 | 2013-01-23 00:48:20 | [diff] [blame] | 84 | } |
Scott Violet | 82ea846 | 2017-07-13 22:39:56 | [diff] [blame] | 85 | Shell::Get()->RemoveShellObserver(this); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 86 | } |
| 87 | |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 88 | aura::Window* WindowWatcher::GetWindowByID(const 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; |
sky | 5ad143a | 2017-03-22 04:31:23 | [diff] [blame] | 99 | ShelfModel* model = Shell::Get()->shelf_model(); |
[email protected] | eec097142 | 2014-02-03 23:51:27 | [diff] [blame] | 100 | ShelfItem item; |
Thiago Farina | 627fba95 | 2017-05-24 21:15:26 | [diff] [blame] | 101 | item.type = new_window->type() == aura::client::WINDOW_TYPE_PANEL |
| 102 | ? TYPE_APP_PANEL |
| 103 | : TYPE_APP; |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 104 | static int shelf_id = 0; |
| 105 | item.id = ShelfID(base::IntToString(shelf_id++)); |
| 106 | id_to_window_[item.id] = new_window; |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 107 | |
| 108 | SkBitmap icon_bitmap; |
[email protected] | f47d8ca | 2014-07-02 15:56:30 | [diff] [blame] | 109 | icon_bitmap.allocN32Pixels(16, 16); |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 110 | icon_bitmap.eraseARGB(255, image_count == 0 ? 255 : 0, |
| 111 | image_count == 1 ? 255 : 0, image_count == 2 ? 255 : 0); |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 112 | image_count = (image_count + 1) % 3; |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 113 | item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f)); |
msw | 072dc547 | 2017-01-11 15:16:21 | [diff] [blame] | 114 | item.title = new_window->GetTitle(); |
[email protected] | 630a460 | 2012-07-17 01:30:16 | [diff] [blame] | 115 | |
[email protected] | b33ac376 | 2012-03-24 01:19:52 | [diff] [blame] | 116 | model->Add(item); |
[email protected] | 6777f63 | 2013-10-15 02:38:41 | [diff] [blame] | 117 | |
msw | 77414f10 | 2017-03-10 21:58:06 | [diff] [blame] | 118 | model->SetShelfItemDelegate( |
msw | 84b8a5f | 2017-05-05 00:13:36 | [diff] [blame] | 119 | item.id, base::MakeUnique<WindowWatcherShelfItemDelegate>(item.id, this)); |
msw | 6958e7f | 2017-05-15 22:55:06 | [diff] [blame] | 120 | new_window->SetProperty(kShelfIDKey, new std::string(item.id.Serialize())); |
[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) { |
sky | 5ad143a | 2017-03-22 04:31:23 | [diff] [blame] | 127 | ShelfModel* model = Shell::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 | |
Scott Violet | 82ea846 | 2017-07-13 22:39:56 | [diff] [blame] | 137 | void WindowWatcher::OnRootWindowAdded(aura::Window* root_window) { |
| 138 | workspace_window_watcher_->RootWindowAdded(root_window); |
[email protected] | 0c5703d | 2014-05-22 01:26:01 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 262f8bd | 2012-03-23 19:30:27 | [diff] [blame] | 141 | } // namespace shell |
| 142 | } // namespace ash |