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/sysui/shelf_delegate_mus.h" | 5 #include "ash/mus/shelf_delegate_mus.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "ash/common/shelf/shelf_item_delegate.h" | 9 #include "ash/common/shelf/shelf_item_delegate.h" |
10 #include "ash/common/shelf/shelf_menu_model.h" | 10 #include "ash/common/shelf/shelf_menu_model.h" |
11 #include "ash/common/shelf/shelf_model.h" | 11 #include "ash/common/shelf/shelf_model.h" |
12 #include "ash/common/shelf/shelf_types.h" | 12 #include "ash/common/shelf/wm_shelf.h" |
13 #include "ash/common/system/status_area_widget.h" | 13 #include "ash/common/wm_lookup.h" |
| 14 #include "ash/common/wm_root_window_controller.h" |
14 #include "ash/common/wm_shell.h" | 15 #include "ash/common/wm_shell.h" |
| 16 #include "ash/common/wm_window.h" |
15 #include "ash/shelf/shelf.h" | 17 #include "ash/shelf/shelf.h" |
16 #include "ash/shelf/shelf_layout_manager.h" | |
17 #include "ash/shelf/shelf_widget.h" | 18 #include "ash/shelf/shelf_widget.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "mojo/common/common_type_converters.h" | |
20 #include "services/shell/public/cpp/connector.h" | |
21 #include "services/ui/public/cpp/property_type_converters.h" | |
22 #include "services/ui/public/cpp/window.h" | |
23 #include "services/ui/public/cpp/window_property.h" | |
24 #include "ui/aura/mus/mus_util.h" | |
25 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/display/display.h" |
| 22 #include "ui/display/screen.h" |
26 #include "ui/gfx/image/image_skia.h" | 23 #include "ui/gfx/image/image_skia.h" |
27 #include "ui/resources/grit/ui_resources.h" | 24 #include "ui/resources/grit/ui_resources.h" |
28 #include "ui/views/mus/window_manager_connection.h" | |
29 | 25 |
30 namespace ash { | 26 namespace ash { |
31 namespace sysui { | |
32 | 27 |
33 namespace { | 28 namespace { |
34 | 29 |
35 // A ShelfItemDelegate used for pinned items. | 30 // A ShelfItemDelegate used for pinned items. |
36 // TODO(mash): Support open user windows, etc. | 31 // TODO(mash): Support open user windows, etc. |
37 class ShelfItemDelegateMus : public ShelfItemDelegate { | 32 class ShelfItemDelegateMus : public ShelfItemDelegate { |
38 public: | 33 public: |
39 ShelfItemDelegateMus() {} | 34 ShelfItemDelegateMus() {} |
40 ~ShelfItemDelegateMus() override {} | 35 ~ShelfItemDelegateMus() override {} |
41 | 36 |
42 void SetDelegate( | 37 void SetDelegate(mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { |
43 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | |
44 delegate_.Bind(std::move(delegate)); | 38 delegate_.Bind(std::move(delegate)); |
45 } | 39 } |
46 | 40 |
47 bool pinned() const { return pinned_; } | 41 bool pinned() const { return pinned_; } |
48 void set_pinned(bool pinned) { pinned_ = pinned; } | 42 void set_pinned(bool pinned) { pinned_ = pinned; } |
49 | 43 |
50 void AddWindow(uint32_t id, const base::string16& title) { | 44 void AddWindow(uint32_t id, const base::string16& title) { |
51 DCHECK(!window_id_to_title_.count(id)); | 45 DCHECK(!window_id_to_title_.count(id)); |
52 window_id_to_title_.insert(std::make_pair(id, title)); | 46 window_id_to_title_.insert(std::make_pair(id, title)); |
53 } | 47 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 121 |
128 bool IsDraggable() override { | 122 bool IsDraggable() override { |
129 NOTIMPLEMENTED(); | 123 NOTIMPLEMENTED(); |
130 return false; | 124 return false; |
131 } | 125 } |
132 | 126 |
133 bool ShouldShowTooltip() override { return true; } | 127 bool ShouldShowTooltip() override { return true; } |
134 | 128 |
135 void Close() override { NOTIMPLEMENTED(); } | 129 void Close() override { NOTIMPLEMENTED(); } |
136 | 130 |
137 mash::shelf::mojom::ShelfItemDelegateAssociatedPtr delegate_; | 131 mojom::ShelfItemDelegateAssociatedPtr delegate_; |
138 bool pinned_ = false; | 132 bool pinned_ = false; |
139 std::map<uint32_t, base::string16> window_id_to_title_; | 133 std::map<uint32_t, base::string16> window_id_to_title_; |
140 base::string16 title_; | 134 base::string16 title_; |
141 | 135 |
142 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); | 136 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); |
143 }; | 137 }; |
144 | 138 |
145 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { | 139 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { |
146 return static_cast<ShelfItemDelegateMus*>( | 140 return static_cast<ShelfItemDelegateMus*>( |
147 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); | 141 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); |
148 } | 142 } |
149 | 143 |
150 // Returns an icon image from an SkBitmap, or the default shelf icon | 144 // Returns an icon image from an SkBitmap, or the default shelf icon |
151 // image if the bitmap is empty. Assumes the bitmap is a 1x icon. | 145 // image if the bitmap is empty. Assumes the bitmap is a 1x icon. |
152 // TODO(jamescook): Support other scale factors. | 146 // TODO(jamescook): Support other scale factors. |
153 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { | 147 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { |
154 gfx::ImageSkia icon_image; | 148 gfx::ImageSkia icon_image; |
155 if (!bitmap.isNull()) { | 149 if (!bitmap.isNull()) { |
156 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 150 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
157 } else { | 151 } else { |
158 // Use default icon. | 152 // Use default icon. |
159 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 153 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
160 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); | 154 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); |
161 } | 155 } |
162 return icon_image; | 156 return icon_image; |
163 } | 157 } |
164 | 158 |
165 // Returns an icon image from a serialized SkBitmap. | |
166 gfx::ImageSkia GetShelfIconFromSerializedBitmap( | |
167 const mojo::Array<uint8_t>& serialized_bitmap) { | |
168 SkBitmap bitmap = mojo::ConvertTo<SkBitmap>(serialized_bitmap.storage()); | |
169 return GetShelfIconFromBitmap(bitmap); | |
170 } | |
171 | |
172 } // namespace | 159 } // namespace |
173 | 160 |
174 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {} | 161 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {} |
175 | 162 |
176 ShelfDelegateMus::~ShelfDelegateMus() {} | 163 ShelfDelegateMus::~ShelfDelegateMus() {} |
177 | 164 |
178 /////////////////////////////////////////////////////////////////////////////// | 165 /////////////////////////////////////////////////////////////////////////////// |
179 // ShelfDelegate: | 166 // ShelfDelegate: |
180 | 167 |
181 void ShelfDelegateMus::OnShelfCreated(Shelf* shelf) { | 168 void ShelfDelegateMus::OnShelfCreated(Shelf* shelf) { |
182 SetShelfPreferredSizes(shelf); | 169 // Notify observers, Chrome will set alignment and auto-hide from prefs. |
| 170 WmWindow* window = WmLookup::Get()->GetWindowForWidget(shelf->shelf_widget()); |
| 171 int display_id = window->GetDisplayNearestWindow().id(); |
| 172 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) { |
| 173 observer->OnShelfCreated(display_id); |
| 174 }); |
183 } | 175 } |
184 | 176 |
185 void ShelfDelegateMus::OnShelfDestroyed(Shelf* shelf) { | 177 void ShelfDelegateMus::OnShelfDestroyed(Shelf* shelf) {} |
186 NOTIMPLEMENTED(); | |
187 } | |
188 | 178 |
189 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) { | 179 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) { |
190 SetShelfPreferredSizes(shelf); | 180 ShelfAlignment alignment = shelf->alignment(); |
191 mash::shelf::mojom::Alignment alignment = | 181 WmWindow* window = WmLookup::Get()->GetWindowForWidget(shelf->shelf_widget()); |
192 static_cast<mash::shelf::mojom::Alignment>(shelf->alignment()); | 182 int display_id = window->GetDisplayNearestWindow().id(); |
193 observers_.ForAllPtrs( | 183 observers_.ForAllPtrs( |
194 [alignment](mash::shelf::mojom::ShelfObserver* observer) { | 184 [alignment, display_id](mojom::ShelfObserver* observer) { |
195 observer->OnAlignmentChanged(alignment); | 185 observer->OnAlignmentChanged(alignment, display_id); |
196 }); | 186 }); |
197 } | 187 } |
198 | 188 |
199 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(Shelf* shelf) { | 189 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(Shelf* shelf) { |
200 mash::shelf::mojom::AutoHideBehavior behavior = | 190 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior(); |
201 static_cast<mash::shelf::mojom::AutoHideBehavior>( | 191 WmWindow* window = WmLookup::Get()->GetWindowForWidget(shelf->shelf_widget()); |
202 shelf->auto_hide_behavior()); | 192 int display_id = window->GetDisplayNearestWindow().id(); |
203 observers_.ForAllPtrs( | 193 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) { |
204 [behavior](mash::shelf::mojom::ShelfObserver* observer) { | 194 observer->OnAutoHideBehaviorChanged(behavior, display_id); |
205 observer->OnAutoHideBehaviorChanged(behavior); | 195 }); |
206 }); | |
207 } | 196 } |
208 | 197 |
209 void ShelfDelegateMus::OnShelfAutoHideStateChanged(Shelf* shelf) { | 198 void ShelfDelegateMus::OnShelfAutoHideStateChanged(Shelf* shelf) {} |
210 // Push the new preferred size to the window manager. For example, when the | |
211 // shelf is auto-hidden it becomes a very short "light bar". | |
212 SetShelfPreferredSizes(shelf); | |
213 } | |
214 | 199 |
215 void ShelfDelegateMus::OnShelfVisibilityStateChanged(Shelf* shelf) { | 200 void ShelfDelegateMus::OnShelfVisibilityStateChanged(Shelf* shelf) {} |
216 SetShelfPreferredSizes(shelf); | |
217 } | |
218 | 201 |
219 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { | 202 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { |
220 if (app_id_to_shelf_id_.count(app_id)) | 203 if (app_id_to_shelf_id_.count(app_id)) |
221 return app_id_to_shelf_id_[app_id]; | 204 return app_id_to_shelf_id_[app_id]; |
222 return 0; | 205 return 0; |
223 } | 206 } |
224 | 207 |
225 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { | 208 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { |
226 return shelf_id_to_app_id_.count(id) != 0; | 209 return shelf_id_to_app_id_.count(id) != 0; |
227 } | 210 } |
(...skipping 11 matching lines...) Expand all Loading... |
239 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { | 222 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { |
240 NOTIMPLEMENTED(); | 223 NOTIMPLEMENTED(); |
241 return false; | 224 return false; |
242 } | 225 } |
243 | 226 |
244 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { | 227 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { |
245 NOTIMPLEMENTED(); | 228 NOTIMPLEMENTED(); |
246 } | 229 } |
247 | 230 |
248 /////////////////////////////////////////////////////////////////////////////// | 231 /////////////////////////////////////////////////////////////////////////////// |
249 // mash::shelf::mojom::ShelfController: | 232 // mojom::ShelfController: |
250 | 233 |
251 void ShelfDelegateMus::AddObserver( | 234 void ShelfDelegateMus::AddObserver( |
252 mash::shelf::mojom::ShelfObserverAssociatedPtrInfo observer) { | 235 mojom::ShelfObserverAssociatedPtrInfo observer) { |
253 mash::shelf::mojom::ShelfObserverAssociatedPtr observer_ptr; | 236 mojom::ShelfObserverAssociatedPtr observer_ptr; |
254 observer_ptr.Bind(std::move(observer)); | 237 observer_ptr.Bind(std::move(observer)); |
255 // Notify the observer of the current state. | 238 // Notify the observer of the current state. |
256 Shelf* shelf = Shelf::ForPrimaryDisplay(); | 239 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) { |
257 observer_ptr->OnAlignmentChanged( | 240 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(display.id()); |
258 static_cast<mash::shelf::mojom::Alignment>(shelf->alignment())); | 241 WmShelf* shelf = root->GetRootWindowController()->GetShelf(); |
259 observer_ptr->OnAutoHideBehaviorChanged( | 242 observer_ptr->OnAlignmentChanged(shelf->GetAlignment(), display.id()); |
260 static_cast<mash::shelf::mojom::AutoHideBehavior>( | 243 observer_ptr->OnAutoHideBehaviorChanged(shelf->GetAutoHideBehavior(), |
261 shelf->auto_hide_behavior())); | 244 display.id()); |
| 245 } |
262 observers_.AddPtr(std::move(observer_ptr)); | 246 observers_.AddPtr(std::move(observer_ptr)); |
263 } | 247 } |
264 | 248 |
265 void ShelfDelegateMus::SetAlignment(mash::shelf::mojom::Alignment alignment) { | 249 void ShelfDelegateMus::SetAlignment(ShelfAlignment alignment, |
266 ShelfAlignment value = static_cast<ShelfAlignment>(alignment); | 250 int64_t display_id) { |
267 Shelf::ForPrimaryDisplay()->SetAlignment(value); | 251 WmRootWindowController* root_window_controller = |
| 252 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
| 253 root_window_controller->GetShelf()->SetAlignment(alignment); |
268 } | 254 } |
269 | 255 |
270 void ShelfDelegateMus::SetAutoHideBehavior( | 256 void ShelfDelegateMus::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide, |
271 mash::shelf::mojom::AutoHideBehavior auto_hide) { | 257 int64_t display_id) { |
272 ShelfAutoHideBehavior value = static_cast<ShelfAutoHideBehavior>(auto_hide); | 258 WmRootWindowController* root_window_controller = |
273 Shelf::ForPrimaryDisplay()->SetAutoHideBehavior(value); | 259 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id); |
| 260 root_window_controller->GetShelf()->SetAutoHideBehavior(auto_hide); |
274 } | 261 } |
275 | 262 |
276 void ShelfDelegateMus::PinItem( | 263 void ShelfDelegateMus::PinItem( |
277 mash::shelf::mojom::ShelfItemPtr item, | 264 mojom::ShelfItemPtr item, |
278 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | 265 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { |
279 std::string app_id(item->app_id.To<std::string>()); | 266 if (app_id_to_shelf_id_.count(item->app_id)) { |
280 if (app_id_to_shelf_id_.count(app_id)) { | 267 ShelfID shelf_id = app_id_to_shelf_id_[item->app_id]; |
281 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; | |
282 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); | 268 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); |
283 item_delegate->SetDelegate(std::move(delegate)); | 269 item_delegate->SetDelegate(std::move(delegate)); |
284 item_delegate->set_pinned(true); | 270 item_delegate->set_pinned(true); |
285 return; | 271 return; |
286 } | 272 } |
287 | 273 |
288 ShelfID shelf_id = model_->next_id(); | 274 ShelfID shelf_id = model_->next_id(); |
289 app_id_to_shelf_id_.insert(std::make_pair(app_id, shelf_id)); | 275 app_id_to_shelf_id_.insert(std::make_pair(item->app_id, shelf_id)); |
290 shelf_id_to_app_id_.insert(std::make_pair(shelf_id, app_id)); | 276 shelf_id_to_app_id_.insert(std::make_pair(shelf_id, item->app_id)); |
291 | 277 |
292 ShelfItem shelf_item; | 278 ShelfItem shelf_item; |
293 shelf_item.type = TYPE_APP_SHORTCUT; | 279 shelf_item.type = TYPE_APP_SHORTCUT; |
294 shelf_item.status = STATUS_CLOSED; | 280 shelf_item.status = STATUS_CLOSED; |
295 shelf_item.image = GetShelfIconFromBitmap(item->image); | 281 shelf_item.image = GetShelfIconFromBitmap(item->image); |
296 model_->Add(shelf_item); | 282 model_->Add(shelf_item); |
297 | 283 |
298 std::unique_ptr<ShelfItemDelegateMus> item_delegate( | 284 std::unique_ptr<ShelfItemDelegateMus> item_delegate( |
299 new ShelfItemDelegateMus()); | 285 new ShelfItemDelegateMus()); |
300 item_delegate->SetDelegate(std::move(delegate)); | 286 item_delegate->SetDelegate(std::move(delegate)); |
301 item_delegate->set_pinned(true); | 287 item_delegate->set_pinned(true); |
302 item_delegate->set_title(item->app_title.To<base::string16>()); | 288 item_delegate->set_title(base::UTF8ToUTF16(item->app_title)); |
303 model_->SetShelfItemDelegate(shelf_id, std::move(item_delegate)); | 289 model_->SetShelfItemDelegate(shelf_id, std::move(item_delegate)); |
304 } | 290 } |
305 | 291 |
306 void ShelfDelegateMus::UnpinItem(const mojo::String& app_id) { | 292 void ShelfDelegateMus::UnpinItem(const std::string& app_id) { |
307 if (!app_id_to_shelf_id_.count(app_id.To<std::string>())) | 293 if (!app_id_to_shelf_id_.count(app_id)) |
308 return; | 294 return; |
309 ShelfID shelf_id = app_id_to_shelf_id_[app_id.To<std::string>()]; | 295 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
310 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); | 296 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); |
311 DCHECK(item_delegate->pinned()); | 297 DCHECK(item_delegate->pinned()); |
312 item_delegate->set_pinned(false); | 298 item_delegate->set_pinned(false); |
313 if (item_delegate->window_id_to_title().empty()) { | 299 if (item_delegate->window_id_to_title().empty()) { |
314 model_->RemoveItemAt(model_->ItemIndexByID(shelf_id)); | 300 model_->RemoveItemAt(model_->ItemIndexByID(shelf_id)); |
315 app_id_to_shelf_id_.erase(app_id.To<std::string>()); | 301 app_id_to_shelf_id_.erase(app_id); |
316 shelf_id_to_app_id_.erase(shelf_id); | 302 shelf_id_to_app_id_.erase(shelf_id); |
317 } | 303 } |
318 } | 304 } |
319 | 305 |
320 void ShelfDelegateMus::SetItemImage(const mojo::String& app_id, | 306 void ShelfDelegateMus::SetItemImage(const std::string& app_id, |
321 const SkBitmap& image) { | 307 const SkBitmap& image) { |
322 if (!app_id_to_shelf_id_.count(app_id.To<std::string>())) | 308 if (!app_id_to_shelf_id_.count(app_id)) |
323 return; | 309 return; |
324 ShelfID shelf_id = app_id_to_shelf_id_[app_id.To<std::string>()]; | 310 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; |
325 int index = model_->ItemIndexByID(shelf_id); | 311 int index = model_->ItemIndexByID(shelf_id); |
326 DCHECK_GE(index, 0); | 312 DCHECK_GE(index, 0); |
327 ShelfItem item = *model_->ItemByID(shelf_id); | 313 ShelfItem item = *model_->ItemByID(shelf_id); |
328 item.image = GetShelfIconFromBitmap(image); | 314 item.image = GetShelfIconFromBitmap(image); |
329 model_->Set(index, item); | 315 model_->Set(index, item); |
330 } | 316 } |
331 | 317 |
332 void ShelfDelegateMus::SetShelfPreferredSizes(Shelf* shelf) { | |
333 ShelfWidget* widget = shelf->shelf_widget(); | |
334 ShelfLayoutManager* layout_manager = widget->shelf_layout_manager(); | |
335 ui::Window* window = aura::GetMusWindow(widget->GetNativeWindow()); | |
336 gfx::Size size = layout_manager->GetPreferredSize(); | |
337 window->SetSharedProperty<gfx::Size>( | |
338 ui::mojom::WindowManager::kPreferredSize_Property, size); | |
339 | |
340 StatusAreaWidget* status_widget = widget->status_area_widget(); | |
341 ui::Window* status_window = | |
342 aura::GetMusWindow(status_widget->GetNativeWindow()); | |
343 gfx::Size status_size = status_widget->GetWindowBoundsInScreen().size(); | |
344 status_window->SetSharedProperty<gfx::Size>( | |
345 ui::mojom::WindowManager::kPreferredSize_Property, status_size); | |
346 } | |
347 | |
348 } // namespace sysui | |
349 } // namespace ash | 318 } // namespace ash |
OLD | NEW |