Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: ash/mus/shelf_delegate_mus.cc

Issue 2259153002: mash: Port ash_sysui ShelfDelegateMus impl to mojo:ash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and rebase. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/mus/shelf_delegate_mus.h ('k') | ash/mus/shelf_layout_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.h"
10 #include "ash/common/shelf/shelf_item_delegate.h" 9 #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" 10 #include "ash/common/shelf/shelf_menu_model.h"
13 #include "ash/common/shelf/shelf_model.h" 11 #include "ash/common/shelf/shelf_model.h"
14 #include "ash/common/shelf/shelf_types.h"
15 #include "ash/common/shelf/shelf_widget.h" 12 #include "ash/common/shelf/shelf_widget.h"
16 #include "ash/common/system/status_area_widget.h" 13 #include "ash/common/shelf/wm_shelf.h"
14 #include "ash/common/wm_lookup.h"
15 #include "ash/common/wm_root_window_controller.h"
17 #include "ash/common/wm_shell.h" 16 #include "ash/common/wm_shell.h"
18 #include "base/strings/string_util.h" 17 #include "ash/common/wm_window.h"
19 #include "mojo/common/common_type_converters.h" 18 #include "base/strings/utf_string_conversions.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" 19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/display/display.h"
21 #include "ui/display/screen.h"
26 #include "ui/gfx/image/image_skia.h" 22 #include "ui/gfx/image/image_skia.h"
27 #include "ui/resources/grit/ui_resources.h" 23 #include "ui/resources/grit/ui_resources.h"
28 #include "ui/views/mus/window_manager_connection.h"
29 24
30 namespace ash { 25 namespace ash {
31 namespace sysui {
32 26
33 namespace { 27 namespace {
34 28
35 // A ShelfItemDelegate used for pinned items. 29 // A ShelfItemDelegate used for pinned items.
36 // TODO(mash): Support open user windows, etc. 30 // TODO(mash): Support open user windows, etc.
37 class ShelfItemDelegateMus : public ShelfItemDelegate { 31 class ShelfItemDelegateMus : public ShelfItemDelegate {
38 public: 32 public:
39 ShelfItemDelegateMus() {} 33 ShelfItemDelegateMus() {}
40 ~ShelfItemDelegateMus() override {} 34 ~ShelfItemDelegateMus() override {}
41 35
42 void SetDelegate( 36 void SetDelegate(mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
43 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
44 delegate_.Bind(std::move(delegate)); 37 delegate_.Bind(std::move(delegate));
45 } 38 }
46 39
47 bool pinned() const { return pinned_; } 40 bool pinned() const { return pinned_; }
48 void set_pinned(bool pinned) { pinned_ = pinned; } 41 void set_pinned(bool pinned) { pinned_ = pinned; }
49 42
50 void AddWindow(uint32_t id, const base::string16& title) { 43 void AddWindow(uint32_t id, const base::string16& title) {
51 DCHECK(!window_id_to_title_.count(id)); 44 DCHECK(!window_id_to_title_.count(id));
52 window_id_to_title_.insert(std::make_pair(id, title)); 45 window_id_to_title_.insert(std::make_pair(id, title));
53 } 46 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 120
128 bool IsDraggable() override { 121 bool IsDraggable() override {
129 NOTIMPLEMENTED(); 122 NOTIMPLEMENTED();
130 return false; 123 return false;
131 } 124 }
132 125
133 bool ShouldShowTooltip() override { return true; } 126 bool ShouldShowTooltip() override { return true; }
134 127
135 void Close() override { NOTIMPLEMENTED(); } 128 void Close() override { NOTIMPLEMENTED(); }
136 129
137 mash::shelf::mojom::ShelfItemDelegateAssociatedPtr delegate_; 130 mojom::ShelfItemDelegateAssociatedPtr delegate_;
138 bool pinned_ = false; 131 bool pinned_ = false;
139 std::map<uint32_t, base::string16> window_id_to_title_; 132 std::map<uint32_t, base::string16> window_id_to_title_;
140 base::string16 title_; 133 base::string16 title_;
141 134
142 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); 135 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus);
143 }; 136 };
144 137
145 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) { 138 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) {
146 return static_cast<ShelfItemDelegateMus*>( 139 return static_cast<ShelfItemDelegateMus*>(
147 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id)); 140 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id));
148 } 141 }
149 142
150 // Returns an icon image from an SkBitmap, or the default shelf icon 143 // 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. 144 // image if the bitmap is empty. Assumes the bitmap is a 1x icon.
152 // TODO(jamescook): Support other scale factors. 145 // TODO(jamescook): Support other scale factors.
153 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { 146 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) {
154 gfx::ImageSkia icon_image; 147 gfx::ImageSkia icon_image;
155 if (!bitmap.isNull()) { 148 if (!bitmap.isNull()) {
156 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 149 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
157 } else { 150 } else {
158 // Use default icon. 151 // Use default icon.
159 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 152 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
160 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); 153 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
161 } 154 }
162 return icon_image; 155 return icon_image;
163 } 156 }
164 157
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 158 } // namespace
173 159
174 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {} 160 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {}
175 161
176 ShelfDelegateMus::~ShelfDelegateMus() {} 162 ShelfDelegateMus::~ShelfDelegateMus() {}
177 163
178 /////////////////////////////////////////////////////////////////////////////// 164 ///////////////////////////////////////////////////////////////////////////////
179 // ShelfDelegate: 165 // ShelfDelegate:
180 166
181 void ShelfDelegateMus::OnShelfCreated(Shelf* shelf) { 167 void ShelfDelegateMus::OnShelfCreated(WmShelf* shelf) {
182 SetShelfPreferredSizes(shelf); 168 // Notify observers, Chrome will set alignment and auto-hide from prefs.
169 int display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
170 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) {
171 observer->OnShelfCreated(display_id);
172 });
183 } 173 }
184 174
185 void ShelfDelegateMus::OnShelfDestroyed(Shelf* shelf) { 175 void ShelfDelegateMus::OnShelfDestroyed(WmShelf* shelf) {}
186 NOTIMPLEMENTED();
187 }
188 176
189 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) { 177 void ShelfDelegateMus::OnShelfAlignmentChanged(WmShelf* shelf) {
190 SetShelfPreferredSizes(shelf); 178 ShelfAlignment alignment = shelf->alignment();
191 mash::shelf::mojom::Alignment alignment = 179 int display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
192 static_cast<mash::shelf::mojom::Alignment>(shelf->alignment());
193 observers_.ForAllPtrs( 180 observers_.ForAllPtrs(
194 [alignment](mash::shelf::mojom::ShelfObserver* observer) { 181 [alignment, display_id](mojom::ShelfObserver* observer) {
195 observer->OnAlignmentChanged(alignment); 182 observer->OnAlignmentChanged(alignment, display_id);
196 }); 183 });
197 } 184 }
198 185
199 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(Shelf* shelf) { 186 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(WmShelf* shelf) {
200 mash::shelf::mojom::AutoHideBehavior behavior = 187 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior();
201 static_cast<mash::shelf::mojom::AutoHideBehavior>( 188 int display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
202 shelf->auto_hide_behavior()); 189 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) {
203 observers_.ForAllPtrs( 190 observer->OnAutoHideBehaviorChanged(behavior, display_id);
204 [behavior](mash::shelf::mojom::ShelfObserver* observer) { 191 });
205 observer->OnAutoHideBehaviorChanged(behavior);
206 });
207 } 192 }
208 193
209 void ShelfDelegateMus::OnShelfAutoHideStateChanged(Shelf* shelf) { 194 void ShelfDelegateMus::OnShelfAutoHideStateChanged(WmShelf* 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 195
215 void ShelfDelegateMus::OnShelfVisibilityStateChanged(Shelf* shelf) { 196 void ShelfDelegateMus::OnShelfVisibilityStateChanged(WmShelf* shelf) {}
216 SetShelfPreferredSizes(shelf);
217 }
218 197
219 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { 198 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) {
220 if (app_id_to_shelf_id_.count(app_id)) 199 if (app_id_to_shelf_id_.count(app_id))
221 return app_id_to_shelf_id_[app_id]; 200 return app_id_to_shelf_id_[app_id];
222 return 0; 201 return 0;
223 } 202 }
224 203
225 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { 204 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const {
226 return shelf_id_to_app_id_.count(id) != 0; 205 return shelf_id_to_app_id_.count(id) != 0;
227 } 206 }
(...skipping 11 matching lines...) Expand all
239 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { 218 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) {
240 NOTIMPLEMENTED(); 219 NOTIMPLEMENTED();
241 return false; 220 return false;
242 } 221 }
243 222
244 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { 223 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) {
245 NOTIMPLEMENTED(); 224 NOTIMPLEMENTED();
246 } 225 }
247 226
248 /////////////////////////////////////////////////////////////////////////////// 227 ///////////////////////////////////////////////////////////////////////////////
249 // mash::shelf::mojom::ShelfController: 228 // mojom::ShelfController:
250 229
251 void ShelfDelegateMus::AddObserver( 230 void ShelfDelegateMus::AddObserver(
252 mash::shelf::mojom::ShelfObserverAssociatedPtrInfo observer) { 231 mojom::ShelfObserverAssociatedPtrInfo observer) {
253 mash::shelf::mojom::ShelfObserverAssociatedPtr observer_ptr; 232 mojom::ShelfObserverAssociatedPtr observer_ptr;
254 observer_ptr.Bind(std::move(observer)); 233 observer_ptr.Bind(std::move(observer));
255 // Notify the observer of the current state. 234 // Notify the observer of the current state.
256 Shelf* shelf = Shelf::ForPrimaryDisplay(); 235 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
257 observer_ptr->OnAlignmentChanged( 236 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(display.id());
258 static_cast<mash::shelf::mojom::Alignment>(shelf->alignment())); 237 WmShelf* shelf = root->GetRootWindowController()->GetShelf();
259 observer_ptr->OnAutoHideBehaviorChanged( 238 observer_ptr->OnAlignmentChanged(shelf->alignment(), display.id());
260 static_cast<mash::shelf::mojom::AutoHideBehavior>( 239 observer_ptr->OnAutoHideBehaviorChanged(shelf->auto_hide_behavior(),
261 shelf->auto_hide_behavior())); 240 display.id());
241 }
262 observers_.AddPtr(std::move(observer_ptr)); 242 observers_.AddPtr(std::move(observer_ptr));
263 } 243 }
264 244
265 void ShelfDelegateMus::SetAlignment(mash::shelf::mojom::Alignment alignment) { 245 void ShelfDelegateMus::SetAlignment(ShelfAlignment alignment,
266 ShelfAlignment value = static_cast<ShelfAlignment>(alignment); 246 int64_t display_id) {
267 Shelf::ForPrimaryDisplay()->SetAlignment(value); 247 WmRootWindowController* root_window_controller =
248 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id);
249 // The controller may be null for invalid ids or for displays being removed.
250 if (root_window_controller && root_window_controller->HasShelf())
251 root_window_controller->GetShelf()->SetAlignment(alignment);
268 } 252 }
269 253
270 void ShelfDelegateMus::SetAutoHideBehavior( 254 void ShelfDelegateMus::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide,
271 mash::shelf::mojom::AutoHideBehavior auto_hide) { 255 int64_t display_id) {
272 ShelfAutoHideBehavior value = static_cast<ShelfAutoHideBehavior>(auto_hide); 256 WmRootWindowController* root_window_controller =
273 Shelf::ForPrimaryDisplay()->SetAutoHideBehavior(value); 257 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id);
258 // The controller may be null for invalid ids or for displays being removed.
259 if (root_window_controller && root_window_controller->HasShelf())
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
OLDNEW
« no previous file with comments | « ash/mus/shelf_delegate_mus.h ('k') | ash/mus/shelf_layout_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698