OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/sysui/shelf_delegate_mus.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "ash/common/shelf/shelf.h" | |
10 #include "ash/common/shelf/shelf_item_delegate.h" | |
11 #include "ash/common/shelf/shelf_layout_manager.h" | |
12 #include "ash/common/shelf/shelf_menu_model.h" | |
13 #include "ash/common/shelf/shelf_model.h" | |
14 #include "ash/common/shelf/shelf_types.h" | |
15 #include "ash/common/shelf/shelf_widget.h" | |
16 #include "ash/common/system/status_area_widget.h" | |
17 #include "ash/common/wm_shell.h" | |
18 #include "base/strings/string_util.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" | |
26 #include "ui/gfx/image/image_skia.h" | |
27 #include "ui/resources/grit/ui_resources.h" | |
28 #include "ui/views/mus/window_manager_connection.h" | |
29 | |
30 namespace ash { | |
31 namespace sysui { | |
32 | |
33 namespace { | |
34 | |
35 // A ShelfItemDelegate used for pinned items. | |
36 // TODO(mash): Support open user windows, etc. | |
37 class ShelfItemDelegateMus : public ShelfItemDelegate { | |
38 public: | |
39 ShelfItemDelegateMus() {} | |
40 ~ShelfItemDelegateMus() override {} | |
41 | |
42 void SetDelegate( | |
43 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | |
44 delegate_.Bind(std::move(delegate)); | |
45 } | |
46 | |
47 bool pinned() const { return pinned_; } | |
48 void set_pinned(bool pinned) { pinned_ = pinned; } | |
49 | |
50 void AddWindow(uint32_t id, const base::string16& title) { | |
51 DCHECK(!window_id_to_title_.count(id)); | |
52 window_id_to_title_.insert(std::make_pair(id, title)); | |
53 } | |
54 void RemoveWindow(uint32_t id) { window_id_to_title_.erase(id); } | |
55 void SetWindowTitle(uint32_t id, const base::string16& title) { | |
56 DCHECK(window_id_to_title_.count(id)); | |
57 window_id_to_title_[id] = title; | |
58 } | |
59 const std::map<uint32_t, base::string16>& window_id_to_title() const { | |
60 return window_id_to_title_; | |
61 } | |
62 | |
63 const base::string16& title() { return title_; } | |
64 void set_title(const base::string16& title) { title_ = title; } | |
65 | |
66 private: | |
67 // This application menu model for ShelfItemDelegateMus lists open windows. | |
68 class ShelfMenuModelMus : public ShelfMenuModel, | |
69 public ui::SimpleMenuModel::Delegate { | |
70 public: | |
71 explicit ShelfMenuModelMus(ShelfItemDelegateMus* item_delegate) | |
72 : ShelfMenuModel(this), item_delegate_(item_delegate) { | |
73 AddSeparator(ui::SPACING_SEPARATOR); | |
74 AddItem(0, item_delegate_->GetTitle()); | |
75 AddSeparator(ui::SPACING_SEPARATOR); | |
76 for (const auto& window : item_delegate_->window_id_to_title()) | |
77 AddItem(window.first, window.second); | |
78 AddSeparator(ui::SPACING_SEPARATOR); | |
79 } | |
80 ~ShelfMenuModelMus() override {} | |
81 | |
82 // ShelfMenuModel: | |
83 bool IsCommandActive(int command_id) const override { return false; } | |
84 | |
85 // ui::SimpleMenuModel::Delegate: | |
86 bool IsCommandIdChecked(int command_id) const override { return false; } | |
87 bool IsCommandIdEnabled(int command_id) const override { | |
88 return command_id > 0; | |
89 } | |
90 void ExecuteCommand(int command_id, int event_flags) override { | |
91 NOTIMPLEMENTED(); | |
92 } | |
93 | |
94 private: | |
95 ShelfItemDelegateMus* item_delegate_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelMus); | |
98 }; | |
99 | |
100 // ShelfItemDelegate: | |
101 ShelfItemDelegate::PerformedAction ItemSelected( | |
102 const ui::Event& event) override { | |
103 if (window_id_to_title_.empty()) { | |
104 delegate_->LaunchItem(); | |
105 return kNewWindowCreated; | |
106 } | |
107 if (window_id_to_title_.size() == 1) { | |
108 // TODO(mash): Activate the window and return kExistingWindowActivated. | |
109 NOTIMPLEMENTED(); | |
110 } | |
111 return kNoAction; | |
112 } | |
113 | |
114 base::string16 GetTitle() override { | |
115 return window_id_to_title_.empty() ? title_ | |
116 : window_id_to_title_.begin()->second; | |
117 } | |
118 | |
119 bool CanPin() const override { | |
120 NOTIMPLEMENTED(); | |
121 return true; | |
122 } | |
123 | |
124 ShelfMenuModel* CreateApplicationMenu(int event_flags) override { | |
125 return new ShelfMenuModelMus(this); | |
126 } | |
127 | |
128 bool IsDraggable() override { | |
129 NOTIMPLEMENTED(); | |
130 return false; | |
131 } | |
132 | |
133 bool ShouldShowTooltip() override { return true; } | |
134 | |
135 void Close() override { NOTIMPLEMENTED(); } | |
136 | |
137 mash::shelf::mojom::ShelfItemDelegateAssociatedPtr delegate_; | |
138 bool pinned_ = false; | |
139 std::map<uint32_t, base::string16> window_id_to_title_; | |
140 base::string16 title_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); | |
143 }; | |
144 | |
145 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { | |
146 return static_cast<ShelfItemDelegateMus*>( | |
147 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); | |
148 } | |
149 | |
150 // 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. | |
152 // TODO(jamescook): Support other scale factors. | |
153 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { | |
154 gfx::ImageSkia icon_image; | |
155 if (!bitmap.isNull()) { | |
156 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | |
157 } else { | |
158 // Use default icon. | |
159 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
160 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); | |
161 } | |
162 return icon_image; | |
163 } | |
164 | |
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 | |
173 | |
174 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {} | |
175 | |
176 ShelfDelegateMus::~ShelfDelegateMus() {} | |
177 | |
178 /////////////////////////////////////////////////////////////////////////////// | |
179 // ShelfDelegate: | |
180 | |
181 void ShelfDelegateMus::OnShelfCreated(Shelf* shelf) { | |
182 SetShelfPreferredSizes(shelf); | |
183 } | |
184 | |
185 void ShelfDelegateMus::OnShelfDestroyed(Shelf* shelf) { | |
186 NOTIMPLEMENTED(); | |
187 } | |
188 | |
189 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) { | |
190 SetShelfPreferredSizes(shelf); | |
191 mash::shelf::mojom::Alignment alignment = | |
192 static_cast<mash::shelf::mojom::Alignment>(shelf->alignment()); | |
193 observers_.ForAllPtrs( | |
194 [alignment](mash::shelf::mojom::ShelfObserver* observer) { | |
195 observer->OnAlignmentChanged(alignment); | |
196 }); | |
197 } | |
198 | |
199 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(Shelf* shelf) { | |
200 mash::shelf::mojom::AutoHideBehavior behavior = | |
201 static_cast<mash::shelf::mojom::AutoHideBehavior>( | |
202 shelf->auto_hide_behavior()); | |
203 observers_.ForAllPtrs( | |
204 [behavior](mash::shelf::mojom::ShelfObserver* observer) { | |
205 observer->OnAutoHideBehaviorChanged(behavior); | |
206 }); | |
207 } | |
208 | |
209 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 | |
215 void ShelfDelegateMus::OnShelfVisibilityStateChanged(Shelf* shelf) { | |
216 SetShelfPreferredSizes(shelf); | |
217 } | |
218 | |
219 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { | |
220 if (app_id_to_shelf_id_.count(app_id)) | |
221 return app_id_to_shelf_id_[app_id]; | |
222 return 0; | |
223 } | |
224 | |
225 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { | |
226 return shelf_id_to_app_id_.count(id) != 0; | |
227 } | |
228 | |
229 const std::string& ShelfDelegateMus::GetAppIDForShelfID(ShelfID id) { | |
230 if (shelf_id_to_app_id_.count(id)) | |
231 return shelf_id_to_app_id_[id]; | |
232 return base::EmptyString(); | |
233 } | |
234 | |
235 void ShelfDelegateMus::PinAppWithID(const std::string& app_id) { | |
236 NOTIMPLEMENTED(); | |
237 } | |
238 | |
239 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { | |
240 NOTIMPLEMENTED(); | |
241 return false; | |
242 } | |
243 | |
244 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { | |
245 NOTIMPLEMENTED(); | |
246 } | |
247 | |
248 /////////////////////////////////////////////////////////////////////////////// | |
249 // mash::shelf::mojom::ShelfController: | |
250 | |
251 void ShelfDelegateMus::AddObserver( | |
252 mash::shelf::mojom::ShelfObserverAssociatedPtrInfo observer) { | |
253 mash::shelf::mojom::ShelfObserverAssociatedPtr observer_ptr; | |
254 observer_ptr.Bind(std::move(observer)); | |
255 // Notify the observer of the current state. | |
256 Shelf* shelf = Shelf::ForPrimaryDisplay(); | |
257 observer_ptr->OnAlignmentChanged( | |
258 static_cast<mash::shelf::mojom::Alignment>(shelf->alignment())); | |
259 observer_ptr->OnAutoHideBehaviorChanged( | |
260 static_cast<mash::shelf::mojom::AutoHideBehavior>( | |
261 shelf->auto_hide_behavior())); | |
262 observers_.AddPtr(std::move(observer_ptr)); | |
263 } | |
264 | |
265 void ShelfDelegateMus::SetAlignment(mash::shelf::mojom::Alignment alignment) { | |
266 ShelfAlignment value = static_cast<ShelfAlignment>(alignment); | |
267 Shelf::ForPrimaryDisplay()->SetAlignment(value); | |
268 } | |
269 | |
270 void ShelfDelegateMus::SetAutoHideBehavior( | |
271 mash::shelf::mojom::AutoHideBehavior auto_hide) { | |
272 ShelfAutoHideBehavior value = static_cast<ShelfAutoHideBehavior>(auto_hide); | |
273 Shelf::ForPrimaryDisplay()->SetAutoHideBehavior(value); | |
274 } | |
275 | |
276 void ShelfDelegateMus::PinItem( | |
277 mash::shelf::mojom::ShelfItemPtr item, | |
278 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | |
279 std::string app_id(item->app_id.To<std::string>()); | |
280 if (app_id_to_shelf_id_.count(app_id)) { | |
281 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; | |
282 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); | |
283 item_delegate->SetDelegate(std::move(delegate)); | |
284 item_delegate->set_pinned(true); | |
285 return; | |
286 } | |
287 | |
288 ShelfID shelf_id = model_->next_id(); | |
289 app_id_to_shelf_id_.insert(std::make_pair(app_id, shelf_id)); | |
290 shelf_id_to_app_id_.insert(std::make_pair(shelf_id, app_id)); | |
291 | |
292 ShelfItem shelf_item; | |
293 shelf_item.type = TYPE_APP_SHORTCUT; | |
294 shelf_item.status = STATUS_CLOSED; | |
295 shelf_item.image = GetShelfIconFromBitmap(item->image); | |
296 model_->Add(shelf_item); | |
297 | |
298 std::unique_ptr<ShelfItemDelegateMus> item_delegate( | |
299 new ShelfItemDelegateMus()); | |
300 item_delegate->SetDelegate(std::move(delegate)); | |
301 item_delegate->set_pinned(true); | |
302 item_delegate->set_title(item->app_title.To<base::string16>()); | |
303 model_->SetShelfItemDelegate(shelf_id, std::move(item_delegate)); | |
304 } | |
305 | |
306 void ShelfDelegateMus::UnpinItem(const mojo::String& app_id) { | |
307 if (!app_id_to_shelf_id_.count(app_id.To<std::string>())) | |
308 return; | |
309 ShelfID shelf_id = app_id_to_shelf_id_[app_id.To<std::string>()]; | |
310 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id); | |
311 DCHECK(item_delegate->pinned()); | |
312 item_delegate->set_pinned(false); | |
313 if (item_delegate->window_id_to_title().empty()) { | |
314 model_->RemoveItemAt(model_->ItemIndexByID(shelf_id)); | |
315 app_id_to_shelf_id_.erase(app_id.To<std::string>()); | |
316 shelf_id_to_app_id_.erase(shelf_id); | |
317 } | |
318 } | |
319 | |
320 void ShelfDelegateMus::SetItemImage(const mojo::String& app_id, | |
321 const SkBitmap& image) { | |
322 if (!app_id_to_shelf_id_.count(app_id.To<std::string>())) | |
323 return; | |
324 ShelfID shelf_id = app_id_to_shelf_id_[app_id.To<std::string>()]; | |
325 int index = model_->ItemIndexByID(shelf_id); | |
326 DCHECK_GE(index, 0); | |
327 ShelfItem item = *model_->ItemByID(shelf_id); | |
328 item.image = GetShelfIconFromBitmap(image); | |
329 model_->Set(index, item); | |
330 } | |
331 | |
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 | |
OLD | NEW |