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

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

Issue 2734653002: chromeos: Move files in //ash/common to //ash (Closed)
Patch Set: fix a11y tests, fix docs 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
« no previous file with comments | « ash/common/shelf/shelf_controller.h ('k') | ash/common/shelf/shelf_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/common/shelf/shelf_controller.h"
6
7 #include "ash/common/shelf/shelf_item_delegate.h"
8 #include "ash/common/shelf/wm_shelf.h"
9 #include "ash/common/wm_shell.h"
10 #include "ash/common/wm_window.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/shell.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/display/display.h"
17 #include "ui/display/screen.h"
18 #include "ui/gfx/image/image_skia.h"
19 #include "ui/resources/grit/ui_resources.h"
20
21 namespace ash {
22
23 namespace {
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
100 // the bitmap is empty. Assumes the bitmap is a 1x icon.
101 // TODO(jamescook): Support other scale factors.
102 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) {
103 gfx::ImageSkia icon_image;
104 if (!bitmap.isNull()) {
105 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
106 } else {
107 // Use default icon.
108 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
109 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
110 }
111 return icon_image;
112 }
113
114 // Returns the WmShelf instance for the display with the given |display_id|.
115 WmShelf* GetShelfForDisplay(int64_t display_id) {
116 // The controller may be null for invalid ids or for displays being removed.
117 RootWindowController* root_window_controller =
118 Shell::GetRootWindowControllerWithDisplayId(display_id);
119 return root_window_controller ? root_window_controller->GetShelf() : nullptr;
120 }
121
122 } // namespace
123
124 ShelfController::ShelfController() {}
125
126 ShelfController::~ShelfController() {}
127
128 void ShelfController::BindRequest(mojom::ShelfControllerRequest request) {
129 bindings_.AddBinding(this, std::move(request));
130 }
131
132 void ShelfController::NotifyShelfCreated(WmShelf* shelf) {
133 // Notify observers, Chrome will set alignment and auto-hide from prefs.
134 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
135 observers_.ForAllPtrs([display_id](mojom::ShelfObserver* observer) {
136 observer->OnShelfCreated(display_id);
137 });
138 }
139
140 void ShelfController::NotifyShelfAlignmentChanged(WmShelf* shelf) {
141 ShelfAlignment alignment = shelf->alignment();
142 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
143 observers_.ForAllPtrs(
144 [alignment, display_id](mojom::ShelfObserver* observer) {
145 observer->OnAlignmentChanged(alignment, display_id);
146 });
147 }
148
149 void ShelfController::NotifyShelfAutoHideBehaviorChanged(WmShelf* shelf) {
150 ShelfAutoHideBehavior behavior = shelf->auto_hide_behavior();
151 int64_t display_id = shelf->GetWindow()->GetDisplayNearestWindow().id();
152 observers_.ForAllPtrs([behavior, display_id](mojom::ShelfObserver* observer) {
153 observer->OnAutoHideBehaviorChanged(behavior, display_id);
154 });
155 }
156
157 void ShelfController::AddObserver(
158 mojom::ShelfObserverAssociatedPtrInfo observer) {
159 mojom::ShelfObserverAssociatedPtr observer_ptr;
160 observer_ptr.Bind(std::move(observer));
161 observers_.AddPtr(std::move(observer_ptr));
162 }
163
164 void ShelfController::SetAlignment(ShelfAlignment alignment,
165 int64_t display_id) {
166 if (!ash::WmShelf::CanChangeShelfAlignment())
167 return;
168
169 WmShelf* shelf = GetShelfForDisplay(display_id);
170 // TODO(jamescook): The initialization check should not be necessary, but
171 // otherwise this wrongly tries to set the alignment on a secondary display
172 // during login before the ShelfLockingManager and ShelfView are created.
173 if (shelf && shelf->IsShelfInitialized())
174 shelf->SetAlignment(alignment);
175 }
176
177 void ShelfController::SetAutoHideBehavior(ShelfAutoHideBehavior auto_hide,
178 int64_t display_id) {
179 WmShelf* shelf = GetShelfForDisplay(display_id);
180 // TODO(jamescook): The initialization check should not be necessary, but
181 // otherwise this wrongly tries to set auto-hide state on a secondary display
182 // during login before the ShelfView is created.
183 if (shelf && shelf->IsShelfInitialized())
184 shelf->SetAutoHideBehavior(auto_hide);
185 }
186
187 void ShelfController::PinItem(
188 mojom::ShelfItemPtr item,
189 mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
190 if (app_id_to_shelf_id_.count(item->app_id)) {
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 }
216
217 void ShelfController::UnpinItem(const std::string& app_id) {
218 if (!app_id_to_shelf_id_.count(app_id))
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 }
231
232 void ShelfController::SetItemImage(const std::string& app_id,
233 const SkBitmap& image) {
234 if (!app_id_to_shelf_id_.count(app_id))
235 return;
236 ShelfID shelf_id = app_id_to_shelf_id_[app_id];
237 int index = model_.ItemIndexByID(shelf_id);
238 DCHECK_GE(index, 0);
239 ShelfItem item = *model_.ItemByID(shelf_id);
240 item.image = GetShelfIconFromBitmap(image);
241 model_.Set(index, item);
242 }
243
244 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/shelf/shelf_controller.h ('k') | ash/common/shelf/shelf_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698