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

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

Issue 2391253004: Use mojo Shelf interfaces for both mash and classic ash. (Closed)
Patch Set: Address manifest comment; working on test failures/crashes. Created 4 years, 2 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/mus/shelf_delegate_mus.h" 5 #include "ash/mus/shelf_delegate_mus.h"
6 6
7 #include <memory> 7 #include "ash/common/shelf/shelf_controller.h"
8
9 #include "ash/common/shelf/shelf_item_delegate.h"
10 #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"
14 #include "ash/common/wm_lookup.h"
15 #include "ash/common/wm_root_window_controller.h"
16 #include "ash/common/wm_shell.h" 8 #include "ash/common/wm_shell.h"
17 #include "ash/common/wm_window.h" 9 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/display/display.h"
21 #include "ui/display/screen.h"
22 #include "ui/gfx/image/image_skia.h"
23 #include "ui/resources/grit/ui_resources.h"
24 10
25 namespace ash { 11 namespace ash {
26 12
27 namespace { 13 ShelfDelegateMus::ShelfDelegateMus() {}
28
29 // A ShelfItemDelegate used for pinned items.
30 // TODO(mash): Support open user windows, etc.
31 class ShelfItemDelegateMus : public ShelfItemDelegate {
32 public:
33 ShelfItemDelegateMus() {}
34 ~ShelfItemDelegateMus() override {}
35
36 void SetDelegate(mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
37 delegate_.Bind(std::move(delegate));
38 }
39
40 bool pinned() const { return pinned_; }
41 void set_pinned(bool pinned) { pinned_ = pinned; }
42
43 void AddWindow(uint32_t id, const base::string16& title) {
44 DCHECK(!window_id_to_title_.count(id));
45 window_id_to_title_.insert(std::make_pair(id, title));
46 }
47 void RemoveWindow(uint32_t id) { window_id_to_title_.erase(id); }
48 void SetWindowTitle(uint32_t id, const base::string16& title) {
49 DCHECK(window_id_to_title_.count(id));
50 window_id_to_title_[id] = title;
51 }
52 const std::map<uint32_t, base::string16>& window_id_to_title() const {
53 return window_id_to_title_;
54 }
55
56 const base::string16& title() { return title_; }
57 void set_title(const base::string16& title) { title_ = title; }
58
59 private:
60 // This application menu model for ShelfItemDelegateMus lists open windows.
61 class ShelfMenuModelMus : public ShelfMenuModel,
62 public ui::SimpleMenuModel::Delegate {
63 public:
64 explicit ShelfMenuModelMus(ShelfItemDelegateMus* item_delegate)
65 : ShelfMenuModel(this), item_delegate_(item_delegate) {
66 AddSeparator(ui::SPACING_SEPARATOR);
67 AddItem(0, item_delegate_->GetTitle());
68 AddSeparator(ui::SPACING_SEPARATOR);
69 for (const auto& window : item_delegate_->window_id_to_title())
70 AddItem(window.first, window.second);
71 AddSeparator(ui::SPACING_SEPARATOR);
72 }
73 ~ShelfMenuModelMus() override {}
74
75 // ShelfMenuModel:
76 bool IsCommandActive(int command_id) const override { return false; }
77
78 // ui::SimpleMenuModel::Delegate:
79 bool IsCommandIdChecked(int command_id) const override { return false; }
80 bool IsCommandIdEnabled(int command_id) const override {
81 return command_id > 0;
82 }
83 void ExecuteCommand(int command_id, int event_flags) override {
84 NOTIMPLEMENTED();
85 }
86
87 private:
88 ShelfItemDelegateMus* item_delegate_;
89
90 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelMus);
91 };
92
93 // ShelfItemDelegate:
94 ShelfItemDelegate::PerformedAction ItemSelected(
95 const ui::Event& event) override {
96 if (window_id_to_title_.empty()) {
97 delegate_->LaunchItem();
98 return kNewWindowCreated;
99 }
100 if (window_id_to_title_.size() == 1) {
101 // TODO(mash): Activate the window and return kExistingWindowActivated.
102 NOTIMPLEMENTED();
103 }
104 return kNoAction;
105 }
106
107 base::string16 GetTitle() override {
108 return window_id_to_title_.empty() ? title_
109 : window_id_to_title_.begin()->second;
110 }
111
112 bool CanPin() const override {
113 NOTIMPLEMENTED();
114 return true;
115 }
116
117 ShelfMenuModel* CreateApplicationMenu(int event_flags) override {
118 return new ShelfMenuModelMus(this);
119 }
120
121 bool IsDraggable() override {
122 NOTIMPLEMENTED();
123 return false;
124 }
125
126 bool ShouldShowTooltip() override { return true; }
127
128 void Close() override { NOTIMPLEMENTED(); }
129
130 mojom::ShelfItemDelegateAssociatedPtr delegate_;
131 bool pinned_ = false;
132 std::map<uint32_t, base::string16> window_id_to_title_;
133 base::string16 title_;
134
135 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus);
136 };
137
138 ShelfItemDelegateMus* GetShelfItemDelegate(ShelfID shelf_id) {
139 return static_cast<ShelfItemDelegateMus*>(
140 WmShell::Get()->shelf_model()->GetShelfItemDelegate(shelf_id));
141 }
142
143 // Returns an icon image from an SkBitmap, or the default shelf icon
144 // image if the bitmap is empty. Assumes the bitmap is a 1x icon.
145 // TODO(jamescook): Support other scale factors.
146 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) {
147 gfx::ImageSkia icon_image;
148 if (!bitmap.isNull()) {
149 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
150 } else {
151 // Use default icon.
152 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
153 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
154 }
155 return icon_image;
156 }
157
158 } // namespace
159
160 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) : model_(model) {}
161 14
162 ShelfDelegateMus::~ShelfDelegateMus() {} 15 ShelfDelegateMus::~ShelfDelegateMus() {}
163 16
164 /////////////////////////////////////////////////////////////////////////////// 17 ///////////////////////////////////////////////////////////////////////////////
165 // ShelfDelegate: 18 // ShelfDelegate:
166 19
167 void ShelfDelegateMus::OnShelfCreated(WmShelf* shelf) {
168 // Notify observers, Chrome will set alignment and auto-hide from prefs.
169 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
170 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) {
171 observer->OnShelfCreated(display_id);
172 });
173 }
174
175 void ShelfDelegateMus::OnShelfDestroyed(WmShelf* shelf) {}
176
177 void ShelfDelegateMus::OnShelfAlignmentChanged(WmShelf* shelf) {
178 ShelfAlignment alignment = shelf->alignment();
179 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
180 observers_.ForAllPtrs(
181 [alignment, display_id](mojom::ShelfObserver* observer) {
182 observer->OnAlignmentChanged(alignment, display_id);
183 });
184 }
185
186 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(WmShelf* shelf) {
187 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior();
188 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
189 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) {
190 observer->OnAutoHideBehaviorChanged(behavior, display_id);
191 });
192 }
193
194 void ShelfDelegateMus::OnShelfAutoHideStateChanged(WmShelf* shelf) {}
195
196 void ShelfDelegateMus::OnShelfVisibilityStateChanged(WmShelf* shelf) {}
197
198 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { 20 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) {
199 if (app_id_to_shelf_id_.count(app_id)) 21 if (WmShell::Get()->shelf_controller()->app_id_to_shelf_id().count(app_id))
200 return app_id_to_shelf_id_[app_id]; 22 return WmShell::Get()->shelf_controller()->app_id_to_shelf_id().at(app_id);
201 return 0; 23 return 0;
202 } 24 }
203 25
204 ShelfID ShelfDelegateMus::GetShelfIDForAppIDAndLaunchID( 26 ShelfID ShelfDelegateMus::GetShelfIDForAppIDAndLaunchID(
205 const std::string& app_id, 27 const std::string& app_id,
206 const std::string& launch_id) { 28 const std::string& launch_id) {
207 return ShelfDelegateMus::GetShelfIDForAppID(app_id); 29 return ShelfDelegateMus::GetShelfIDForAppID(app_id);
208 } 30 }
209 31
210 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { 32 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const {
211 return shelf_id_to_app_id_.count(id) != 0; 33 return WmShell::Get()->shelf_controller()->shelf_id_to_app_id().count(id) > 0;
212 } 34 }
213 35
214 const std::string& ShelfDelegateMus::GetAppIDForShelfID(ShelfID id) { 36 const std::string& ShelfDelegateMus::GetAppIDForShelfID(ShelfID id) {
215 if (shelf_id_to_app_id_.count(id)) 37 if (WmShell::Get()->shelf_controller()->shelf_id_to_app_id().count(id))
216 return shelf_id_to_app_id_[id]; 38 return WmShell::Get()->shelf_controller()->shelf_id_to_app_id().at(id);
217 return base::EmptyString(); 39 return base::EmptyString();
218 } 40 }
219 41
220 void ShelfDelegateMus::PinAppWithID(const std::string& app_id) { 42 void ShelfDelegateMus::PinAppWithID(const std::string& app_id) {
221 NOTIMPLEMENTED(); 43 NOTIMPLEMENTED();
222 } 44 }
223 45
224 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { 46 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) {
225 NOTIMPLEMENTED(); 47 NOTIMPLEMENTED();
226 return false; 48 return false;
227 } 49 }
228 50
229 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { 51 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) {
230 NOTIMPLEMENTED(); 52 NOTIMPLEMENTED();
231 } 53 }
232 54
233 ///////////////////////////////////////////////////////////////////////////////
234 // mojom::ShelfController:
235
236 void ShelfDelegateMus::AddObserver(
237 mojom::ShelfObserverAssociatedPtrInfo observer) {
238 mojom::ShelfObserverAssociatedPtr observer_ptr;
239 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));
249 }
250
251 void ShelfDelegateMus::SetAlignment(ShelfAlignment alignment,
252 int64_t display_id) {
253 WmRootWindowController* root_window_controller =
254 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id);
255 // The controller may be null for invalid ids or for displays being removed.
256 if (root_window_controller && root_window_controller->HasShelf())
257 root_window_controller->GetShelf()->SetAlignment(alignment);
258 }
259
260 void ShelfDelegateMus::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide,
261 int64_t display_id) {
262 WmRootWindowController* root_window_controller =
263 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display_id);
264 // The controller may be null for invalid ids or for displays being removed.
265 if (root_window_controller && root_window_controller->HasShelf())
266 root_window_controller->GetShelf()->SetAutoHideBehavior(auto_hide);
267 }
268
269 void ShelfDelegateMus::PinItem(
270 mojom::ShelfItemPtr item,
271 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
272 if (app_id_to_shelf_id_.count(item->app_id)) {
273 ShelfID shelf_id = app_id_to_shelf_id_[item->app_id];
274 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id);
275 item_delegate->SetDelegate(std::move(delegate));
276 item_delegate->set_pinned(true);
277 return;
278 }
279
280 ShelfID shelf_id = model_->next_id();
281 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));
283
284 ShelfItem shelf_item;
285 shelf_item.type = TYPE_APP_SHORTCUT;
286 shelf_item.status = STATUS_CLOSED;
287 shelf_item.image = GetShelfIconFromBitmap(item->image);
288 model_->Add(shelf_item);
289
290 std::unique_ptr<ShelfItemDelegateMus> item_delegate(
291 new ShelfItemDelegateMus());
292 item_delegate->SetDelegate(std::move(delegate));
293 item_delegate->set_pinned(true);
294 item_delegate->set_title(base::UTF8ToUTF16(item->app_title));
295 model_->SetShelfItemDelegate(shelf_id, std::move(item_delegate));
296 }
297
298 void ShelfDelegateMus::UnpinItem(const std::string& app_id) {
299 if (!app_id_to_shelf_id_.count(app_id))
300 return;
301 ShelfID shelf_id = app_id_to_shelf_id_[app_id];
302 ShelfItemDelegateMus* item_delegate = GetShelfItemDelegate(shelf_id);
303 DCHECK(item_delegate->pinned());
304 item_delegate->set_pinned(false);
305 if (item_delegate->window_id_to_title().empty()) {
306 model_->RemoveItemAt(model_->ItemIndexByID(shelf_id));
307 app_id_to_shelf_id_.erase(app_id);
308 shelf_id_to_app_id_.erase(shelf_id);
309 }
310 }
311
312 void ShelfDelegateMus::SetItemImage(const std::string& app_id,
313 const SkBitmap& image) {
314 if (!app_id_to_shelf_id_.count(app_id))
315 return;
316 ShelfID shelf_id = app_id_to_shelf_id_[app_id];
317 int index = model_->ItemIndexByID(shelf_id);
318 DCHECK_GE(index, 0);
319 ShelfItem item = *model_->ItemByID(shelf_id);
320 item.image = GetShelfIconFromBitmap(image);
321 model_->Set(index, item);
322 }
323
324 } // namespace ash 55 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698