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

Side by Side Diff: ash/common/shelf/shelf_controller.cc

Issue 2718563008: mash: Use mojo for ShelfItemDelegate and [app] MenuItem. (Closed)
Patch Set: Cleanup; fix ash_shell compile and a couple tests. Created 3 years, 9 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
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/common/shelf/shelf_controller.h" 5 #include "ash/common/shelf/shelf_controller.h"
6 6
7 #include "ash/common/shelf/shelf_item_delegate.h" 7 #include "ash/common/shelf/shelf_item_delegate.h"
8 #include "ash/common/shelf/wm_shelf.h" 8 #include "ash/common/shelf/wm_shelf.h"
9 #include "ash/common/wm_shell.h" 9 #include "ash/common/wm_shell.h"
10 #include "ash/common/wm_window.h" 10 #include "ash/common/wm_window.h"
11 #include "ash/root_window_controller.h" 11 #include "ash/root_window_controller.h"
12 #include "ash/shell.h" 12 #include "ash/shell.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/base/models/simple_menu_model.h" 14 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/display/display.h" 16 #include "ui/display/display.h"
17 #include "ui/display/screen.h" 17 #include "ui/display/screen.h"
18 #include "ui/gfx/image/image_skia.h" 18 #include "ui/gfx/image/image_skia.h"
19 #include "ui/resources/grit/ui_resources.h" 19 #include "ui/resources/grit/ui_resources.h"
20 20
21 namespace ash { 21 namespace ash {
22 22
23 namespace { 23 namespace {
24 24
25 // A ShelfItemDelegate used for pinned items in mash.
26 // TODO(mash): Support open windows, cooperate with ShelfWindowWatcher.
27 class ShelfItemDelegateMus : public ShelfItemDelegate {
28 public:
29 ShelfItemDelegateMus() {}
30 ~ShelfItemDelegateMus() override {}
31
32 void SetDelegate(mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
33 delegate_.Bind(std::move(delegate));
34 }
35
36 bool pinned() const { return pinned_; }
37 void set_pinned(bool pinned) { pinned_ = pinned; }
38
39 void AddWindow(uint32_t id, const base::string16& title) {
40 DCHECK(!window_id_to_title_.count(id));
41 window_id_to_title_.insert(std::make_pair(id, title));
42 }
43 void RemoveWindow(uint32_t id) { window_id_to_title_.erase(id); }
44 void SetWindowTitle(uint32_t id, const base::string16& title) {
45 DCHECK(window_id_to_title_.count(id));
46 window_id_to_title_[id] = title;
47 }
48 const std::map<uint32_t, base::string16>& window_id_to_title() const {
49 return window_id_to_title_;
50 }
51
52 const base::string16& title() { return title_; }
53 void set_title(const base::string16& title) { title_ = title; }
54
55 private:
56 // ShelfItemDelegate:
57 ShelfAction ItemSelected(ui::EventType event_type,
58 int event_flags,
59 int64_t display_id,
60 ShelfLaunchSource source) override {
61 if (window_id_to_title_.empty()) {
62 delegate_->LaunchItem();
63 return SHELF_ACTION_NEW_WINDOW_CREATED;
64 }
65 if (window_id_to_title_.size() == 1) {
66 // TODO(mash): Activate the window and return
67 // SHELF_ACTION_WINDOW_ACTIVATED.
68 NOTIMPLEMENTED();
69 }
70 return SHELF_ACTION_NONE;
71 }
72
73 ShelfAppMenuItemList GetAppMenuItems(int event_flags) override {
74 // Return an empty item list to avoid showing an application menu.
75 return ShelfAppMenuItemList();
76 }
77
78 void ExecuteCommand(uint32_t command_id, int event_flags) override {
79 // This delegate does not support showing an application menu.
80 NOTIMPLEMENTED();
81 }
82
83 void Close() override { NOTIMPLEMENTED(); }
84
85 mojom::ShelfItemDelegateAssociatedPtr delegate_;
86 bool pinned_ = false;
87 std::map<uint32_t, base::string16> window_id_to_title_;
88 base::string16 title_;
89
90 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus);
91 };
92
93 // Returns the ShelfItemDelegateMus instance for the given |shelf_id|.
94 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) {
95 return static_cast<ShelfItemDelegateMus*>(
96 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id));
97 }
98
99 // Returns an icon image from an SkBitmap, or the default shelf icon image if 25 // Returns an icon image from an SkBitmap, or the default shelf icon image if
100 // the bitmap is empty. Assumes the bitmap is a 1x icon. 26 // the bitmap is empty. Assumes the bitmap is a 1x icon.
101 // TODO(jamescook): Support other scale factors. 27 // TODO(jamescook): Support other scale factors.
102 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) { 28 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) {
103 gfx::ImageSkia icon_image; 29 gfx::ImageSkia icon_image;
104 if (!bitmap.isNull()) { 30 if (!bitmap.isNull()) {
105 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 31 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
106 } else { 32 } else {
107 // Use default icon. 33 // Use default icon.
108 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 34 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 // TODO(jamescook): The initialization check should not be necessary, but 106 // TODO(jamescook): The initialization check should not be necessary, but
181 // otherwise this wrongly tries to set auto-hide state on a secondary display 107 // otherwise this wrongly tries to set auto-hide state on a secondary display
182 // during login before the ShelfView is created. 108 // during login before the ShelfView is created.
183 if (shelf && shelf->IsShelfInitialized()) 109 if (shelf && shelf->IsShelfInitialized())
184 shelf->SetAutoHideBehavior(auto_hide); 110 shelf->SetAutoHideBehavior(auto_hide);
185 } 111 }
186 112
187 void ShelfController::PinItem( 113 void ShelfController::PinItem(
188 mojom::ShelfItemPtr item, 114 mojom::ShelfItemPtr item,
189 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { 115 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
190 if (app_id_to_shelf_id_.count(item->app_id)) { 116 NOTIMPLEMENTED();
191 ShelfID shelf_id = app_id_to_shelf_id_[item->app_id];
192 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id);
193 item_delegate->SetDelegate(std::move(delegate));
194 item_delegate->set_pinned(true);
195 return;
196 }
197
198 ShelfID shelf_id = model_.next_id();
199 app_id_to_shelf_id_.insert(std::make_pair(item->app_id, shelf_id));
200 shelf_id_to_app_id_.insert(std::make_pair(shelf_id, item->app_id));
201
202 ShelfItem shelf_item;
203 shelf_item.type = TYPE_APP_SHORTCUT;
204 shelf_item.status = STATUS_CLOSED;
205 shelf_item.image = GetShelfIconFromBitmap(item->image);
206 shelf_item.title = base::UTF8ToUTF16(item->app_title);
207 model_.Add(shelf_item);
208
209 std::unique_ptr<ShelfItemDelegateMus> item_delegate =
210 base::MakeUnique<ShelfItemDelegateMus>();
211 item_delegate->SetDelegate(std::move(delegate));
212 item_delegate->set_pinned(true);
213 item_delegate->set_title(shelf_item.title);
214 model_.SetShelfItemDelegate(shelf_id, std::move(item_delegate));
215 } 117 }
216 118
217 void ShelfController::UnpinItem(const std::string& app_id) { 119 void ShelfController::UnpinItem(const std::string& app_id) {
218 if (!app_id_to_shelf_id_.count(app_id)) 120 NOTIMPLEMENTED();
219 return;
220
221 ShelfID shelf_id = app_id_to_shelf_id_[app_id];
222 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id);
223 DCHECK(item_delegate->pinned());
224 item_delegate->set_pinned(false);
225 if (item_delegate->window_id_to_title().empty()) {
226 model_.RemoveItemAt(model_.ItemIndexByID(shelf_id));
227 app_id_to_shelf_id_.erase(app_id);
228 shelf_id_to_app_id_.erase(shelf_id);
229 }
230 } 121 }
231 122
232 void ShelfController::SetItemImage(const std::string& app_id, 123 void ShelfController::SetItemImage(const std::string& app_id,
233 const SkBitmap& image) { 124 const SkBitmap& image) {
234 if (!app_id_to_shelf_id_.count(app_id)) 125 if (!app_id_to_shelf_id_.count(app_id))
235 return; 126 return;
236 ShelfID shelf_id = app_id_to_shelf_id_[app_id]; 127 ShelfID shelf_id = app_id_to_shelf_id_[app_id];
237 int index = model_.ItemIndexByID(shelf_id); 128 int index = model_.ItemIndexByID(shelf_id);
238 DCHECK_GE(index, 0); 129 DCHECK_GE(index, 0);
239 ShelfItem item = *model_.ItemByID(shelf_id); 130 ShelfItem item = *model_.ItemByID(shelf_id);
240 item.image = GetShelfIconFromBitmap(image); 131 item.image = GetShelfIconFromBitmap(image);
241 model_.Set(index, item); 132 model_.Set(index, item);
242 } 133 }
243 134
244 } // namespace ash 135 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698