OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/mus/shelf_delegate_mus.h" | 5 #include "ash/common/shelf/shelf_controller.h" |
6 | |
7 #include <memory> | |
8 | 6 |
9 #include "ash/common/shelf/shelf_item_delegate.h" | 7 #include "ash/common/shelf/shelf_item_delegate.h" |
10 #include "ash/common/shelf/shelf_menu_model.h" | 8 #include "ash/common/shelf/shelf_menu_model.h" |
11 #include "ash/common/shelf/shelf_model.h" | |
12 #include "ash/common/shelf/shelf_widget.h" | |
13 #include "ash/common/shelf/wm_shelf.h" | 9 #include "ash/common/shelf/wm_shelf.h" |
14 #include "ash/common/wm_lookup.h" | 10 #include "ash/common/wm_lookup.h" |
15 #include "ash/common/wm_root_window_controller.h" | 11 #include "ash/common/wm_root_window_controller.h" |
16 #include "ash/common/wm_shell.h" | 12 #include "ash/common/wm_shell.h" |
17 #include "ash/common/wm_window.h" | 13 #include "ash/common/wm_window.h" |
18 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
19 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
20 #include "ui/display/display.h" | 16 #include "ui/display/display.h" |
21 #include "ui/display/screen.h" | 17 #include "ui/display/screen.h" |
22 #include "ui/gfx/image/image_skia.h" | 18 #include "ui/gfx/image/image_skia.h" |
23 #include "ui/resources/grit/ui_resources.h" | 19 #include "ui/resources/grit/ui_resources.h" |
24 | 20 |
25 namespace ash { | 21 namespace ash { |
26 | 22 |
27 namespace { | 23 namespace { |
28 | 24 |
29 // A ShelfItemDelegate used for pinned items. | 25 // A ShelfItemDelegate used for pinned items in mash. |
30 // TODO(mash): Support open user windows, etc. | 26 // TODO(mash): Support open windows, cooperate with ShelfWindowWatcher. |
31 class ShelfItemDelegateMus : public ShelfItemDelegate { | 27 class ShelfItemDelegateMus : public ShelfItemDelegate { |
32 public: | 28 public: |
33 ShelfItemDelegateMus() {} | 29 ShelfItemDelegateMus() {} |
34 ~ShelfItemDelegateMus() override {} | 30 ~ShelfItemDelegateMus() override {} |
35 | 31 |
36 void SetDelegate(mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | 32 void SetDelegate(mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { |
37 delegate_.Bind(std::move(delegate)); | 33 delegate_.Bind(std::move(delegate)); |
38 } | 34 } |
39 | 35 |
40 bool pinned() const { return pinned_; } | 36 bool pinned() const { return pinned_; } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 void Close() override { NOTIMPLEMENTED(); } | 124 void Close() override { NOTIMPLEMENTED(); } |
129 | 125 |
130 mojom::ShelfItemDelegateAssociatedPtr delegate_; | 126 mojom::ShelfItemDelegateAssociatedPtr delegate_; |
131 bool pinned_ = false; | 127 bool pinned_ = false; |
132 std::map<uint32_t, base::string16> window_id_to_title_; | 128 std::map<uint32_t, base::string16> window_id_to_title_; |
133 base::string16 title_; | 129 base::string16 title_; |
134 | 130 |
135 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); | 131 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); |
136 }; | 132 }; |
137 | 133 |
| 134 // Returns the ShelfItemDelegateMus instance for the given |shelf_id|. |
138 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { | 135 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { |
139 return static_cast<ShelfItemDelegateMus*>( | 136 return static_cast<ShelfItemDelegateMus*>( |
140 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); | 137 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); |
141 } | 138 } |
142 | 139 |
143 // Returns an icon image from an SkBitmap, or the default shelf icon | 140 // Returns an icon image from an SkBitmap, or the default shelf icon image if |
144 // image if the bitmap is empty. Assumes the bitmap is a 1x icon. | 141 // the bitmap is empty. Assumes the bitmap is a 1x icon. |
145 // TODO(jamescook): Support other scale factors. | 142 // TODO(jamescook): Support other scale factors. |
146 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { | 143 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { |
147 gfx::ImageSkia icon_image; | 144 gfx::ImageSkia icon_image; |
148 if (!bitmap.isNull()) { | 145 if (!bitmap.isNull()) { |
149 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 146 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
150 } else { | 147 } else { |
151 // Use default icon. | 148 // Use default icon. |
152 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 149 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
153 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); | 150 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); |
154 } | 151 } |
155 return icon_image; | 152 return icon_image; |
156 } | 153 } |
157 | 154 |
| 155 // Returns the WmShelf instance for the display with the given |display_id|. |
| 156 WmShelf* GetShelfForDisplay(int64_t display_id) { |
| 157 // The controller may be null for invalid ids or for displays being removed. |
| 158 WmRootWindowController* root_window_controller = |
| 159 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
| 160 return root_window_controller ? root_window_controller->GetShelf() : nullptr; |
| 161 } |
| 162 |
158 } // namespace | 163 } // namespace |
159 | 164 |
160 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {} | 165 ShelfController::ShelfController() {} |
161 | 166 |
162 ShelfDelegateMus::~ShelfDelegateMus() {} | 167 ShelfController::~ShelfController() {} |
163 | 168 |
164 /////////////////////////////////////////////////////////////////////////////// | 169 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) { |
165 // ShelfDelegate: | 170 bindings_.AddBinding(this, std::move(request)); |
| 171 } |
166 | 172 |
167 void ShelfDelegateMus::OnShelfCreated(WmShelf* shelf) { | 173 void ShelfController::NotifyShelfCreated(WmShelf* shelf) { |
168 // Notify observers, Chrome will set alignment and auto-hide from prefs. | 174 // Notify observers, Chrome will set alignment and auto-hide from prefs. |
169 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 175 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
170 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { | 176 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { |
171 observer->OnShelfCreated(display_id); | 177 observer->OnShelfCreated(display_id); |
172 }); | 178 }); |
173 } | 179 } |
174 | 180 |
175 void ShelfDelegateMus::OnShelfDestroyed(WmShelf* shelf) {} | 181 void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) { |
176 | |
177 void ShelfDelegateMus::OnShelfAlignmentChanged(WmShelf* shelf) { | |
178 ShelfAlignment alignment = shelf->alignment(); | 182 ShelfAlignment alignment = shelf->alignment(); |
179 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 183 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
180 observers_.ForAllPtrs( | 184 observers_.ForAllPtrs( |
181 [alignment, display_id](mojom::ShelfObserver* observer) { | 185 [alignment, display_id](mojom::ShelfObserver* observer) { |
182 observer->OnAlignmentChanged(alignment, display_id); | 186 observer->OnAlignmentChanged(alignment, display_id); |
183 }); | 187 }); |
184 } | 188 } |
185 | 189 |
186 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(WmShelf* shelf) { | 190 void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) { |
187 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); | 191 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); |
188 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); | 192 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id(); |
189 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { | 193 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { |
190 observer->OnAutoHideBehaviorChanged(behavior, display_id); | 194 observer->OnAutoHideBehaviorChanged(behavior, display_id); |
191 }); | 195 }); |
192 } | 196 } |
193 | 197 |
194 void ShelfDelegateMus::OnShelfAutoHideStateChanged(WmShelf* shelf) {} | 198 void ShelfController::AddObserver( |
195 | |
196 void ShelfDelegateMus::OnShelfVisibilityStateChanged(WmShelf* shelf) {} | |
197 | |
198 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { | |
199 if (app_id_to_shelf_id_.count(app_id)) | |
200 return app_id_to_shelf_id_[app_id]; | |
201 return 0; | |
202 } | |
203 | |
204 ShelfID ShelfDelegateMus::GetShelfIDForAppIDAndLaunchID( | |
205 const std::string& app_id, | |
206 const std::string& launch_id) { | |
207 return ShelfDelegateMus::GetShelfIDForAppID(app_id); | |
208 } | |
209 | |
210 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { | |
211 return shelf_id_to_app_id_.count(id) != 0; | |
212 } | |
213 | |
214 const std::string& ShelfDelegateMus::GetAppIDForShelfID(ShelfID id) { | |
215 if (shelf_id_to_app_id_.count(id)) | |
216 return shelf_id_to_app_id_[id]; | |
217 return base::EmptyString(); | |
218 } | |
219 | |
220 void ShelfDelegateMus::PinAppWithID(const std::string& app_id) { | |
221 NOTIMPLEMENTED(); | |
222 } | |
223 | |
224 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { | |
225 NOTIMPLEMENTED(); | |
226 return false; | |
227 } | |
228 | |
229 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { | |
230 NOTIMPLEMENTED(); | |
231 } | |
232 | |
233 /////////////////////////////////////////////////////////////////////////////// | |
234 // mojom::ShelfController: | |
235 | |
236 void ShelfDelegateMus::AddObserver( | |
237 mojom::ShelfObserverAssociatedPtrInfo observer) { | 199 mojom::ShelfObserverAssociatedPtrInfo observer) { |
238 mojom::ShelfObserverAssociatedPtr observer_ptr; | 200 mojom::ShelfObserverAssociatedPtr observer_ptr; |
239 observer_ptr.Bind(std::move(observer)); | 201 observer_ptr.Bind(std::move(observer)); |
240 // Notify the observer of the current state. | |
241 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { | |
242 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(display.id()); | |
243 WmShelf* shelf = root->GetRootWindowController()->GetShelf(); | |
244 observer_ptr->OnAlignmentChanged(shelf->alignment(), display.id()); | |
245 observer_ptr->OnAutoHideBehaviorChanged(shelf->auto_hide_behavior(), | |
246 display.id()); | |
247 } | |
248 observers_.AddPtr(std::move(observer_ptr)); | 202 observers_.AddPtr(std::move(observer_ptr)); |
249 } | 203 } |
250 | 204 |
251 void ShelfDelegateMus::SetAlignment(ShelfAlignment alignment, | 205 void ShelfController::SetAlignment(ShelfAlignment alignment, |
252 int64_t display_id) { | 206 int64_t display_id) { |
253 WmRootWindowController* root_window_controller = | 207 if (!ash::WmShelf::CanChangeShelfAlignment()) |
254 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); | 208 return; |
255 // The controller may be null for invalid ids or for displays being removed. | 209 |
256 if (root_window_controller && root_window_controller->HasShelf()) | 210 WmShelf* shelf = GetShelfForDisplay(display_id); |
257 root_window_controller->GetShelf()->SetAlignment(alignment); | 211 // TODO(jamescook): The initialization check should not be necessary, but |
| 212 // otherwise this wrongly tries to set the alignment on a secondary display |
| 213 // during login before the ShelfLockingManager and ShelfView are created. |
| 214 if (shelf && shelf->IsShelfInitialized()) |
| 215 shelf->SetAlignment(alignment); |
258 } | 216 } |
259 | 217 |
260 void ShelfDelegateMus::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, | 218 void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
261 int64_t display_id) { | 219 int64_t display_id) { |
262 WmRootWindowController* root_window_controller = | 220 WmShelf* shelf = GetShelfForDisplay(display_id); |
263 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); | 221 // TODO(jamescook): The initialization check should not be necessary, but |
264 // The controller may be null for invalid ids or for displays being removed. | 222 // otherwise this wrongly tries to set auto-hide state on a secondary display |
265 if (root_window_controller && root_window_controller->HasShelf()) | 223 // during login before the ShelfView is created. |
266 root_window_controller->GetShelf()->SetAutoHideBehavior(auto_hide); | 224 if (shelf && shelf->IsShelfInitialized()) |
| 225 shelf->SetAutoHideBehavior(auto_hide); |
267 } | 226 } |
268 | 227 |
269 void ShelfDelegateMus::PinItem( | 228 void ShelfController::PinItem( |
270 mojom::ShelfItemPtr item, | 229 mojom::ShelfItemPtr item, |
271 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | 230 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { |
272 if (app_id_to_shelf_id_.count(item->app_id)) { | 231 if (app_id_to_shelf_id_.count(item->app_id)) { |
273 ShelfID shelf_id = app_id_to_shelf_id_[item->app_id]; | 232 ShelfID shelf_id = app_id_to_shelf_id_[item->app_id]; |
274 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); | 233 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); |
275 item_delegate->SetDelegate(std::move(delegate)); | 234 item_delegate->SetDelegate(std::move(delegate)); |
276 item_delegate->set_pinned(true); | 235 item_delegate->set_pinned(true); |
277 return; | 236 return; |
278 } | 237 } |
279 | 238 |
280 ShelfID shelf_id = model_->next_id(); | 239 ShelfID shelf_id = model_.next_id(); |
281 app_id_to_shelf_id_.insert(std::make_pair(item->app_id, shelf_id)); | 240 app_id_to_shelf_id_.insert(std::make_pair(item->app_id, shelf_id)); |
282 shelf_id_to_app_id_.insert(std::make_pair(shelf_id, item->app_id)); | 241 shelf_id_to_app_id_.insert(std::make_pair(shelf_id, item->app_id)); |
283 | 242 |
284 ShelfItem shelf_item; | 243 ShelfItem shelf_item; |
285 shelf_item.type = TYPE_APP_SHORTCUT; | 244 shelf_item.type = TYPE_APP_SHORTCUT; |
286 shelf_item.status = STATUS_CLOSED; | 245 shelf_item.status = STATUS_CLOSED; |
287 shelf_item.image = GetShelfIconFromBitmap(item->image); | 246 shelf_item.image = GetShelfIconFromBitmap(item->image); |
288 model_->Add(shelf_item); | 247 model_.Add(shelf_item); |
289 | 248 |
290 std::unique_ptr<ShelfItemDelegateMus> item_delegate( | 249 std::unique_ptr<ShelfItemDelegateMus> item_delegate( |
291 new ShelfItemDelegateMus()); | 250 new ShelfItemDelegateMus()); |
292 item_delegate->SetDelegate(std::move(delegate)); | 251 item_delegate->SetDelegate(std::move(delegate)); |
293 item_delegate->set_pinned(true); | 252 item_delegate->set_pinned(true); |
294 item_delegate->set_title(base::UTF8ToUTF16(item->app_title)); | 253 item_delegate->set_title(base::UTF8ToUTF16(item->app_title)); |
295 model_->SetShelfItemDelegate(shelf_id, std::move(item_delegate)); | 254 model_.SetShelfItemDelegate(shelf_id, std::move(item_delegate)); |
296 } | 255 } |
297 | 256 |
298 void ShelfDelegateMus::UnpinItem(const std::string& app_id) { | 257 void ShelfController::UnpinItem(const std::string& app_id) { |
299 if (!app_id_to_shelf_id_.count(app_id)) | 258 if (!app_id_to_shelf_id_.count(app_id)) |
300 return; | 259 return; |
| 260 |
301 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; | 261 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
302 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); | 262 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); |
303 DCHECK(item_delegate->pinned()); | 263 DCHECK(item_delegate->pinned()); |
304 item_delegate->set_pinned(false); | 264 item_delegate->set_pinned(false); |
305 if (item_delegate->window_id_to_title().empty()) { | 265 if (item_delegate->window_id_to_title().empty()) { |
306 model_->RemoveItemAt(model_->ItemIndexByID(shelf_id)); | 266 model_.RemoveItemAt(model_.ItemIndexByID(shelf_id)); |
307 app_id_to_shelf_id_.erase(app_id); | 267 app_id_to_shelf_id_.erase(app_id); |
308 shelf_id_to_app_id_.erase(shelf_id); | 268 shelf_id_to_app_id_.erase(shelf_id); |
309 } | 269 } |
310 } | 270 } |
311 | 271 |
312 void ShelfDelegateMus::SetItemImage(const std::string& app_id, | 272 void ShelfController::SetItemImage(const std::string& app_id, |
313 const SkBitmap& image) { | 273 const SkBitmap& image) { |
314 if (!app_id_to_shelf_id_.count(app_id)) | 274 if (!app_id_to_shelf_id_.count(app_id)) |
315 return; | 275 return; |
316 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; | 276 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
317 int index = model_->ItemIndexByID(shelf_id); | 277 int index = model_.ItemIndexByID(shelf_id); |
318 DCHECK_GE(index, 0); | 278 DCHECK_GE(index, 0); |
319 ShelfItem item = *model_->ItemByID(shelf_id); | 279 ShelfItem item = *model_.ItemByID(shelf_id); |
320 item.image = GetShelfIconFromBitmap(image); | 280 item.image = GetShelfIconFromBitmap(image); |
321 model_->Set(index, item); | 281 model_.Set(index, item); |
322 } | 282 } |
323 | 283 |
324 } // namespace ash | 284 } // namespace ash |
OLD | NEW |