OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/shell/window_watcher.h" | 5 #include "ash/shell/window_watcher.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "ash/public/cpp/shelf_item.h" | 9 #include "ash/public/cpp/shelf_item.h" |
10 #include "ash/public/cpp/shell_window_ids.h" | 10 #include "ash/public/cpp/shell_window_ids.h" |
| 11 #include "ash/public/cpp/window_properties.h" |
11 #include "ash/shelf/shelf_model.h" | 12 #include "ash/shelf/shelf_model.h" |
12 #include "ash/shelf/shelf_widget.h" | 13 #include "ash/shelf/shelf_widget.h" |
13 #include "ash/shell.h" | 14 #include "ash/shell.h" |
14 #include "ash/shell/window_watcher_shelf_item_delegate.h" | 15 #include "ash/shell/window_watcher_shelf_item_delegate.h" |
15 #include "ash/shell_port.h" | 16 #include "ash/shell_port.h" |
16 #include "ash/wm/window_properties.h" | |
17 #include "ash/wm/window_util.h" | 17 #include "ash/wm/window_util.h" |
18 #include "ash/wm_window.h" | 18 #include "ash/wm_window.h" |
| 19 #include "base/strings/string_number_conversions.h" |
19 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
20 #include "ui/aura/window_event_dispatcher.h" | 21 #include "ui/aura/window_event_dispatcher.h" |
21 #include "ui/display/display.h" | 22 #include "ui/display/display.h" |
22 | 23 |
23 namespace ash { | 24 namespace ash { |
24 namespace shell { | 25 namespace shell { |
25 | 26 |
26 class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver { | 27 class WindowWatcher::WorkspaceWindowWatcher : public aura::WindowObserver { |
27 public: | 28 public: |
28 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {} | 29 explicit WorkspaceWindowWatcher(WindowWatcher* watcher) : watcher_(watcher) {} |
29 | 30 |
30 ~WorkspaceWindowWatcher() override {} | 31 ~WorkspaceWindowWatcher() override {} |
31 | 32 |
32 void OnWindowAdded(aura::Window* new_window) override { | 33 void OnWindowAdded(aura::Window* new_window) override { |
33 new_window->AddObserver(watcher_); | 34 new_window->AddObserver(watcher_); |
34 } | 35 } |
35 | 36 |
36 void OnWillRemoveWindow(aura::Window* window) override { | 37 void OnWillRemoveWindow(aura::Window* window) override { |
37 DCHECK(window->children().empty()); | 38 DCHECK(window->children().empty()); |
38 window->RemoveObserver(watcher_); | 39 window->RemoveObserver(watcher_); |
39 } | 40 } |
40 | 41 |
41 void RootWindowAdded(aura::Window* root) { | 42 void RootWindowAdded(aura::Window* root) { |
42 aura::Window* panel_container = | 43 aura::Window* panel_container = |
43 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer); | 44 Shell::GetContainer(root, kShellWindowId_PanelContainer); |
44 panel_container->AddObserver(watcher_); | 45 panel_container->AddObserver(watcher_); |
45 | 46 |
46 aura::Window* container = | 47 aura::Window* container = |
47 ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer); | 48 Shell::GetContainer(root, kShellWindowId_ShelfContainer); |
48 container->AddObserver(this); | 49 container->AddObserver(this); |
49 for (size_t i = 0; i < container->children().size(); ++i) | 50 for (size_t i = 0; i < container->children().size(); ++i) |
50 container->children()[i]->AddObserver(watcher_); | 51 container->children()[i]->AddObserver(watcher_); |
51 } | 52 } |
52 | 53 |
53 void RootWindowRemoved(aura::Window* root) { | 54 void RootWindowRemoved(aura::Window* root) { |
54 aura::Window* panel_container = | 55 aura::Window* panel_container = |
55 ash::Shell::GetContainer(root, kShellWindowId_PanelContainer); | 56 Shell::GetContainer(root, kShellWindowId_PanelContainer); |
56 panel_container->RemoveObserver(watcher_); | 57 panel_container->RemoveObserver(watcher_); |
57 | 58 |
58 aura::Window* container = | 59 aura::Window* container = |
59 ash::Shell::GetContainer(root, kShellWindowId_ShelfContainer); | 60 Shell::GetContainer(root, kShellWindowId_ShelfContainer); |
60 container->RemoveObserver(this); | 61 container->RemoveObserver(this); |
61 for (size_t i = 0; i < container->children().size(); ++i) | 62 for (size_t i = 0; i < container->children().size(); ++i) |
62 container->children()[i]->RemoveObserver(watcher_); | 63 container->children()[i]->RemoveObserver(watcher_); |
63 } | 64 } |
64 | 65 |
65 private: | 66 private: |
66 WindowWatcher* watcher_; | 67 WindowWatcher* watcher_; |
67 | 68 |
68 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher); | 69 DISALLOW_COPY_AND_ASSIGN(WorkspaceWindowWatcher); |
69 }; | 70 }; |
70 | 71 |
71 WindowWatcher::WindowWatcher() { | 72 WindowWatcher::WindowWatcher() { |
72 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this)); | 73 workspace_window_watcher_.reset(new WorkspaceWindowWatcher(this)); |
73 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | 74 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
74 for (aura::Window::Windows::iterator iter = root_windows.begin(); | 75 for (aura::Window::Windows::iterator iter = root_windows.begin(); |
75 iter != root_windows.end(); ++iter) { | 76 iter != root_windows.end(); ++iter) { |
76 workspace_window_watcher_->RootWindowAdded(*iter); | 77 workspace_window_watcher_->RootWindowAdded(*iter); |
77 } | 78 } |
78 } | 79 } |
79 | 80 |
80 WindowWatcher::~WindowWatcher() { | 81 WindowWatcher::~WindowWatcher() { |
81 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | 82 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
82 for (aura::Window::Windows::iterator iter = root_windows.begin(); | 83 for (aura::Window::Windows::iterator iter = root_windows.begin(); |
83 iter != root_windows.end(); ++iter) { | 84 iter != root_windows.end(); ++iter) { |
84 workspace_window_watcher_->RootWindowRemoved(*iter); | 85 workspace_window_watcher_->RootWindowRemoved(*iter); |
85 } | 86 } |
86 } | 87 } |
87 | 88 |
88 aura::Window* WindowWatcher::GetWindowByID(ash::ShelfID id) { | 89 aura::Window* WindowWatcher::GetWindowByID(const ShelfID& id) { |
89 IDToWindow::const_iterator i = id_to_window_.find(id); | 90 IDToWindow::const_iterator i = id_to_window_.find(id); |
90 return i != id_to_window_.end() ? i->second : NULL; | 91 return i != id_to_window_.end() ? i->second : NULL; |
91 } | 92 } |
92 | 93 |
93 // aura::WindowObserver overrides: | 94 // aura::WindowObserver overrides: |
94 void WindowWatcher::OnWindowAdded(aura::Window* new_window) { | 95 void WindowWatcher::OnWindowAdded(aura::Window* new_window) { |
95 if (!wm::IsWindowUserPositionable(new_window)) | 96 if (!wm::IsWindowUserPositionable(new_window)) |
96 return; | 97 return; |
97 | 98 |
98 static int image_count = 0; | 99 static int image_count = 0; |
99 ShelfModel* model = Shell::Get()->shelf_model(); | 100 ShelfModel* model = Shell::Get()->shelf_model(); |
100 ShelfItem item; | 101 ShelfItem item; |
101 item.type = new_window->type() == ui::wm::WINDOW_TYPE_PANEL | 102 item.type = new_window->type() == ui::wm::WINDOW_TYPE_PANEL ? TYPE_APP_PANEL |
102 ? ash::TYPE_APP_PANEL | 103 : TYPE_APP; |
103 : ash::TYPE_APP; | 104 static int shelf_id = 0; |
104 ash::ShelfID id = model->next_id(); | 105 item.id = ShelfID(base::IntToString(shelf_id++)); |
105 id_to_window_[id] = new_window; | 106 id_to_window_[item.id] = new_window; |
106 | 107 |
107 SkBitmap icon_bitmap; | 108 SkBitmap icon_bitmap; |
108 icon_bitmap.allocN32Pixels(16, 16); | 109 icon_bitmap.allocN32Pixels(16, 16); |
109 icon_bitmap.eraseARGB(255, image_count == 0 ? 255 : 0, | 110 icon_bitmap.eraseARGB(255, image_count == 0 ? 255 : 0, |
110 image_count == 1 ? 255 : 0, image_count == 2 ? 255 : 0); | 111 image_count == 1 ? 255 : 0, image_count == 2 ? 255 : 0); |
111 image_count = (image_count + 1) % 3; | 112 image_count = (image_count + 1) % 3; |
112 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f)); | 113 item.image = gfx::ImageSkia(gfx::ImageSkiaRep(icon_bitmap, 1.0f)); |
113 item.title = new_window->GetTitle(); | 114 item.title = new_window->GetTitle(); |
114 | 115 |
115 model->Add(item); | 116 model->Add(item); |
116 | 117 |
117 model->SetShelfItemDelegate( | 118 model->SetShelfItemDelegate( |
118 id, base::MakeUnique<WindowWatcherShelfItemDelegate>(id, this)); | 119 item.id, base::MakeUnique<WindowWatcherShelfItemDelegate>(item.id, this)); |
119 new_window->SetProperty(kShelfIDKey, id); | 120 new_window->SetProperty(kShelfIDKey, new ShelfID(item.id)); |
120 } | 121 } |
121 | 122 |
122 void WindowWatcher::OnWillRemoveWindow(aura::Window* window) { | 123 void WindowWatcher::OnWillRemoveWindow(aura::Window* window) { |
123 for (IDToWindow::iterator i = id_to_window_.begin(); i != id_to_window_.end(); | 124 for (IDToWindow::iterator i = id_to_window_.begin(); i != id_to_window_.end(); |
124 ++i) { | 125 ++i) { |
125 if (i->second == window) { | 126 if (i->second == window) { |
126 ShelfModel* model = Shell::Get()->shelf_model(); | 127 ShelfModel* model = Shell::Get()->shelf_model(); |
127 int index = model->ItemIndexByID(i->first); | 128 int index = model->ItemIndexByID(i->first); |
128 DCHECK_NE(-1, index); | 129 DCHECK_NE(-1, index); |
129 model->RemoveItemAt(index); | 130 model->RemoveItemAt(index); |
(...skipping 12 matching lines...) Expand all Loading... |
142 void WindowWatcher::OnDisplayRemoved(const display::Display& old_display) { | 143 void WindowWatcher::OnDisplayRemoved(const display::Display& old_display) { |
143 // All windows in the display has already been removed, so no need to | 144 // All windows in the display has already been removed, so no need to |
144 // remove observers. | 145 // remove observers. |
145 } | 146 } |
146 | 147 |
147 void WindowWatcher::OnDisplayMetricsChanged(const display::Display&, uint32_t) { | 148 void WindowWatcher::OnDisplayMetricsChanged(const display::Display&, uint32_t) { |
148 } | 149 } |
149 | 150 |
150 } // namespace shell | 151 } // namespace shell |
151 } // namespace ash | 152 } // namespace ash |
OLD | NEW |