OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/common/shelf/shelf_window_watcher.h" | 5 #include "ash/common/shelf/shelf_window_watcher.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "ash/common/shelf/shelf_constants.h" | 10 #include "ash/common/shelf/shelf_constants.h" |
11 #include "ash/common/shelf/shelf_model.h" | 11 #include "ash/common/shelf/shelf_model.h" |
12 #include "ash/common/shelf/shelf_window_watcher_item_delegate.h" | 12 #include "ash/common/shelf/shelf_window_watcher_item_delegate.h" |
13 #include "ash/common/wm/window_state.h" | 13 #include "ash/common/wm/window_state.h" |
14 #include "ash/common/wm_shell.h" | 14 #include "ash/common/wm_shell.h" |
15 #include "ash/common/wm_window.h" | 15 #include "ash/common/wm_window.h" |
16 #include "ash/common/wm_window_property.h" | 16 #include "ash/common/wm_window_property.h" |
17 #include "ash/public/cpp/shell_window_ids.h" | 17 #include "ash/public/cpp/shell_window_ids.h" |
18 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/display/display.h" | 19 #include "ui/display/display.h" |
20 #include "ui/display/screen.h" | 20 #include "ui/display/screen.h" |
21 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
22 #include "ui/resources/grit/ui_resources.h" | |
22 | 23 |
23 namespace ash { | 24 namespace ash { |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Update the ShelfItem from relevant window properties. | 27 // Update the ShelfItem from relevant window properties. |
27 void UpdateShelfItemForWindow(ShelfItem* item, WmWindow* window) { | 28 void UpdateShelfItemForWindow(ShelfItem* item, WmWindow* window) { |
28 item->type = static_cast<ShelfItemType>( | 29 item->type = static_cast<ShelfItemType>( |
29 window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE)); | 30 window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE)); |
30 const int icon = | 31 |
31 window->GetIntProperty(WmWindowProperty::SHELF_ICON_RESOURCE_ID); | 32 // Prefer app icons over window icons, they're typically larger. |
33 gfx::ImageSkia image = window->GetAppIcon(); | |
34 if (image.isNull()) | |
35 image = window->GetWindowIcon(); | |
36 if (!image.isNull()) { | |
37 item->image = image; | |
38 return; | |
39 } | |
40 | |
41 int icon = window->GetIntProperty(WmWindowProperty::SHELF_ICON_RESOURCE_ID); | |
32 if (icon != kInvalidImageResourceID) | 42 if (icon != kInvalidImageResourceID) |
33 item->image = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon); | 43 item->image = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon); |
34 } | 44 } |
35 | 45 |
36 // Returns true if |window| has a ShelfItem added by ShelfWindowWatcher. | 46 // Returns true if |window| has a ShelfItem added by ShelfWindowWatcher. |
37 bool HasShelfItemForWindow(WmWindow* window) { | 47 bool HasShelfItemForWindow(WmWindow* window) { |
38 return window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) != | 48 return window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) != |
39 TYPE_UNDEFINED && | 49 TYPE_UNDEFINED && |
40 window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID; | 50 window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID; |
41 } | 51 } |
42 | 52 |
43 } // namespace | 53 } // namespace |
44 | 54 |
45 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( | 55 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( |
46 ShelfWindowWatcher* window_watcher) | 56 ShelfWindowWatcher* window_watcher) |
47 : window_watcher_(window_watcher) {} | 57 : window_watcher_(window_watcher) {} |
48 | 58 |
49 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {} | 59 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {} |
50 | 60 |
51 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowTreeChanged( | 61 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowTreeChanged( |
52 WmWindow* window, | 62 WmWindow* window, |
53 const TreeChangeParams& params) { | 63 const TreeChangeParams& params) { |
54 if (!params.old_parent && params.new_parent && | 64 if (!params.old_parent && params.new_parent && |
55 params.new_parent->GetShellWindowId() == | 65 (params.new_parent->GetShellWindowId() == |
56 kShellWindowId_DefaultContainer) { | 66 kShellWindowId_DefaultContainer || |
67 params.new_parent->GetShellWindowId() == | |
68 kShellWindowId_PanelContainer)) { | |
57 // A new window was created in the default container. | 69 // A new window was created in the default container. |
James Cook
2016/11/02 17:53:43
nit: update comment
msw
2016/11/10 21:07:46
Done.
| |
58 window_watcher_->OnUserWindowAdded(params.target); | 70 window_watcher_->OnUserWindowAdded(params.target); |
59 } | 71 } |
60 } | 72 } |
61 | 73 |
62 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowDestroying( | 74 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowDestroying( |
63 WmWindow* window) { | 75 WmWindow* window) { |
64 window_watcher_->OnContainerWindowDestroying(window); | 76 window_watcher_->OnContainerWindowDestroying(window); |
65 } | 77 } |
66 | 78 |
67 //////////////////////////////////////////////////////////////////////////////// | 79 //////////////////////////////////////////////////////////////////////////////// |
68 | 80 |
69 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( | 81 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( |
70 ShelfWindowWatcher* window_watcher) | 82 ShelfWindowWatcher* window_watcher) |
71 : window_watcher_(window_watcher) {} | 83 : window_watcher_(window_watcher) {} |
72 | 84 |
73 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} | 85 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} |
74 | 86 |
75 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( | 87 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( |
76 WmWindow* window, | 88 WmWindow* window, |
77 WmWindowProperty property) { | 89 WmWindowProperty property) { |
78 if (property == WmWindowProperty::SHELF_ITEM_TYPE || | 90 if (property == WmWindowProperty::APP_ICON || |
79 property == WmWindowProperty::SHELF_ICON_RESOURCE_ID) { | 91 property == WmWindowProperty::SHELF_ITEM_TYPE || |
92 property == WmWindowProperty::SHELF_ICON_RESOURCE_ID || | |
93 property == WmWindowProperty::WINDOW_ICON) { | |
80 window_watcher_->OnUserWindowPropertyChanged(window); | 94 window_watcher_->OnUserWindowPropertyChanged(window); |
81 } | 95 } |
82 } | 96 } |
83 | 97 |
84 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( | 98 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( |
85 WmWindow* window) { | 99 WmWindow* window) { |
86 window_watcher_->OnUserWindowDestroying(window); | 100 window_watcher_->OnUserWindowDestroying(window); |
87 } | 101 } |
88 | 102 |
89 //////////////////////////////////////////////////////////////////////////////// | 103 //////////////////////////////////////////////////////////////////////////////// |
90 | 104 |
91 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) | 105 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) |
92 : model_(model), | 106 : model_(model), |
93 container_window_observer_(this), | 107 container_window_observer_(this), |
94 user_window_observer_(this), | 108 user_window_observer_(this), |
95 observed_container_windows_(&container_window_observer_), | 109 observed_container_windows_(&container_window_observer_), |
96 observed_user_windows_(&user_window_observer_) { | 110 observed_user_windows_(&user_window_observer_) { |
97 WmShell::Get()->AddActivationObserver(this); | 111 WmShell::Get()->AddActivationObserver(this); |
98 for (WmWindow* root : WmShell::Get()->GetAllRootWindows()) { | 112 for (WmWindow* root : WmShell::Get()->GetAllRootWindows()) { |
99 observed_container_windows_.Add( | 113 observed_container_windows_.Add( |
100 root->GetChildByShellWindowId(kShellWindowId_DefaultContainer)); | 114 root->GetChildByShellWindowId(kShellWindowId_DefaultContainer)); |
115 observed_container_windows_.Add( | |
116 root->GetChildByShellWindowId(kShellWindowId_PanelContainer)); | |
101 } | 117 } |
102 | 118 |
103 display::Screen::GetScreen()->AddObserver(this); | 119 display::Screen::GetScreen()->AddObserver(this); |
104 } | 120 } |
105 | 121 |
106 ShelfWindowWatcher::~ShelfWindowWatcher() { | 122 ShelfWindowWatcher::~ShelfWindowWatcher() { |
107 display::Screen::GetScreen()->RemoveObserver(this); | 123 display::Screen::GetScreen()->RemoveObserver(this); |
108 WmShell::Get()->RemoveActivationObserver(this); | 124 WmShell::Get()->RemoveActivationObserver(this); |
109 } | 125 } |
110 | 126 |
111 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) { | 127 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) { |
128 managed_user_windows_.insert(window); | |
112 ShelfItem item; | 129 ShelfItem item; |
113 ShelfID id = model_->next_id(); | 130 ShelfID id = model_->next_id(); |
114 item.status = window->IsActive() ? STATUS_ACTIVE : STATUS_RUNNING; | 131 item.status = window->IsActive() ? STATUS_ACTIVE : STATUS_RUNNING; |
115 UpdateShelfItemForWindow(&item, window); | 132 UpdateShelfItemForWindow(&item, window); |
116 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); | 133 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); |
117 std::unique_ptr<ShelfItemDelegate> item_delegate( | 134 std::unique_ptr<ShelfItemDelegate> item_delegate( |
118 new ShelfWindowWatcherItemDelegate(window)); | 135 new ShelfWindowWatcherItemDelegate(id, window)); |
119 model_->SetShelfItemDelegate(id, std::move(item_delegate)); | 136 model_->SetShelfItemDelegate(id, std::move(item_delegate)); |
120 model_->Add(item); | 137 model_->Add(item); |
121 } | 138 } |
122 | 139 |
123 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) { | 140 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) { |
141 managed_user_windows_.erase(window); | |
124 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); | 142 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); |
125 DCHECK_NE(shelf_id, kInvalidShelfID); | 143 DCHECK_NE(shelf_id, kInvalidShelfID); |
126 int index = model_->ItemIndexByID(shelf_id); | 144 int index = model_->ItemIndexByID(shelf_id); |
127 DCHECK_GE(index, 0); | 145 DCHECK_GE(index, 0); |
128 model_->RemoveItemAt(index); | 146 model_->RemoveItemAt(index); |
129 window->SetIntProperty(WmWindowProperty::SHELF_ID, kInvalidShelfID); | 147 window->SetIntProperty(WmWindowProperty::SHELF_ID, kInvalidShelfID); |
130 } | 148 } |
131 | 149 |
132 void ShelfWindowWatcher::OnContainerWindowDestroying(WmWindow* container) { | 150 void ShelfWindowWatcher::OnContainerWindowDestroying(WmWindow* container) { |
133 observed_container_windows_.Remove(container); | 151 observed_container_windows_.Remove(container); |
(...skipping 29 matching lines...) Expand all Loading... | |
163 AddShelfItem(window); | 181 AddShelfItem(window); |
164 } | 182 } |
165 } | 183 } |
166 | 184 |
167 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) { | 185 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) { |
168 if (observed_user_windows_.IsObserving(window)) | 186 if (observed_user_windows_.IsObserving(window)) |
169 observed_user_windows_.Remove(window); | 187 observed_user_windows_.Remove(window); |
170 | 188 |
171 if (HasShelfItemForWindow(window)) | 189 if (HasShelfItemForWindow(window)) |
172 RemoveShelfItem(window); | 190 RemoveShelfItem(window); |
173 } | 191 } |
James Cook
2016/11/02 17:53:43
Maybe DCHECK that window is not in managed_user_wi
msw
2016/11/10 21:07:46
Done.
| |
174 | 192 |
175 void ShelfWindowWatcher::OnUserWindowPropertyChanged(WmWindow* window) { | 193 void ShelfWindowWatcher::OnUserWindowPropertyChanged(WmWindow* window) { |
176 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) == | 194 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) == |
177 TYPE_UNDEFINED) { | 195 TYPE_UNDEFINED) { |
178 // Removes ShelfItem for |window| when it has a ShelfItem. | 196 // Remove |window|'s ShelfItem if it was added by this ShelfWindowWatcher. |
179 if (window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID) | 197 if (window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID && |
198 managed_user_windows_.count(window) > 0) { | |
180 RemoveShelfItem(window); | 199 RemoveShelfItem(window); |
200 } | |
181 return; | 201 return; |
182 } | 202 } |
183 | 203 |
184 // Update an existing ShelfItem for |window| when a property has changed. | 204 // Update an existing ShelfItem for |window| when a property has changed. |
185 if (HasShelfItemForWindow(window)) { | 205 if (HasShelfItemForWindow(window)) { |
186 int index = GetShelfItemIndexForWindow(window); | 206 int index = GetShelfItemIndexForWindow(window); |
187 DCHECK_GE(index, 0); | 207 DCHECK_GE(index, 0); |
188 ShelfItem item = model_->items()[index]; | 208 ShelfItem item = model_->items()[index]; |
189 UpdateShelfItemForWindow(&item, window); | 209 UpdateShelfItemForWindow(&item, window); |
190 model_->Set(index, item); | 210 model_->Set(index, item); |
(...skipping 10 matching lines...) Expand all Loading... | |
201 UpdateShelfItemStatus(gained_active, true); | 221 UpdateShelfItemStatus(gained_active, true); |
202 if (lost_active && HasShelfItemForWindow(lost_active)) | 222 if (lost_active && HasShelfItemForWindow(lost_active)) |
203 UpdateShelfItemStatus(lost_active, false); | 223 UpdateShelfItemStatus(lost_active, false); |
204 } | 224 } |
205 | 225 |
206 void ShelfWindowWatcher::OnDisplayAdded(const display::Display& new_display) { | 226 void ShelfWindowWatcher::OnDisplayAdded(const display::Display& new_display) { |
207 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(new_display.id()); | 227 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(new_display.id()); |
208 | 228 |
209 // When the primary root window's display get removed, the existing root | 229 // When the primary root window's display get removed, the existing root |
210 // window is taken over by the new display and the observer is already set. | 230 // window is taken over by the new display and the observer is already set. |
211 WmWindow* container = | 231 WmWindow* default_container = |
212 root->GetChildByShellWindowId(kShellWindowId_DefaultContainer); | 232 root->GetChildByShellWindowId(kShellWindowId_DefaultContainer); |
213 if (!observed_container_windows_.IsObserving(container)) | 233 if (!observed_container_windows_.IsObserving(default_container)) |
214 observed_container_windows_.Add(container); | 234 observed_container_windows_.Add(default_container); |
235 WmWindow* panel_container = | |
236 root->GetChildByShellWindowId(kShellWindowId_PanelContainer); | |
237 if (!observed_container_windows_.IsObserving(panel_container)) | |
238 observed_container_windows_.Add(panel_container); | |
215 } | 239 } |
216 | 240 |
217 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { | 241 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { |
218 } | 242 } |
219 | 243 |
220 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, | 244 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, |
221 uint32_t) {} | 245 uint32_t) {} |
222 | 246 |
223 } // namespace ash | 247 } // namespace ash |
OLD | NEW |