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

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

Issue 1839223003: Add basic Chrome interaction with the mash shelf. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and rebase. Created 4 years, 8 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/sysui_application.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/mus/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/shelf/shelf.h" 9 #include "ash/shelf/shelf.h"
10 #include "ash/shelf/shelf_item_delegate.h" 10 #include "ash/shelf/shelf_item_delegate.h"
11 #include "ash/shelf/shelf_item_delegate_manager.h" 11 #include "ash/shelf/shelf_item_delegate_manager.h"
12 #include "ash/shelf/shelf_layout_manager.h" 12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "ash/shelf/shelf_model.h" 13 #include "ash/shelf/shelf_model.h"
14 #include "ash/shelf/shelf_types.h"
14 #include "ash/shelf/shelf_widget.h" 15 #include "ash/shelf/shelf_widget.h"
15 #include "ash/shell.h" 16 #include "ash/shell.h"
16 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
17 #include "components/mus/public/cpp/property_type_converters.h" 18 #include "components/mus/public/cpp/property_type_converters.h"
18 #include "components/mus/public/cpp/window.h" 19 #include "components/mus/public/cpp/window.h"
19 #include "components/mus/public/cpp/window_property.h" 20 #include "components/mus/public/cpp/window_property.h"
20 #include "mojo/common/common_type_converters.h" 21 #include "mojo/common/common_type_converters.h"
21 #include "mojo/shell/public/cpp/connector.h" 22 #include "mojo/shell/public/cpp/connector.h"
23 #include "skia/public/type_converters.h"
22 #include "ui/aura/mus/mus_util.h" 24 #include "ui/aura/mus/mus_util.h"
23 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/image/image_skia.h" 26 #include "ui/gfx/image/image_skia.h"
25 #include "ui/resources/grit/ui_resources.h" 27 #include "ui/resources/grit/ui_resources.h"
26 #include "ui/views/mus/window_manager_connection.h" 28 #include "ui/views/mus/window_manager_connection.h"
27 29
28 using mash::wm::mojom::UserWindowController; 30 using mash::wm::mojom::UserWindowController;
29 31
30 namespace ash { 32 namespace ash {
31 namespace sysui { 33 namespace sysui {
32 34
33 namespace { 35 namespace {
34 36
37 // A ShelfItemDelegate used for pinned items and open user windows.
35 class ShelfItemDelegateMus : public ShelfItemDelegate { 38 class ShelfItemDelegateMus : public ShelfItemDelegate {
36 public: 39 public:
37 ShelfItemDelegateMus(uint32_t window_id, 40 ShelfItemDelegateMus(uint32_t window_id,
38 const base::string16& title, 41 const base::string16& title,
39 UserWindowController* user_window_controller) 42 UserWindowController* user_window_controller)
40 : window_id_(window_id), 43 : window_id_(window_id),
41 title_(title), 44 title_(title),
42 user_window_controller_(user_window_controller) {} 45 user_window_controller_(user_window_controller) {}
43 ~ShelfItemDelegateMus() override {} 46 ~ShelfItemDelegateMus() override {}
44 47
45 void UpdateTitle(const base::string16& new_title) { title_ = new_title; } 48 void UpdateTitle(const base::string16& new_title) { title_ = new_title; }
46 49
47 private: 50 private:
48 // ShelfItemDelegate: 51 // ShelfItemDelegate:
49 ShelfItemDelegate::PerformedAction ItemSelected( 52 ShelfItemDelegate::PerformedAction ItemSelected(
50 const ui::Event& event) override { 53 const ui::Event& event) override {
51 user_window_controller_->FocusUserWindow(window_id_); 54 if (window_id_ != 0) {
52 return kExistingWindowActivated; 55 user_window_controller_->FocusUserWindow(window_id_);
56 return kExistingWindowActivated;
57 }
58 NOTIMPLEMENTED();
59 return kNoAction;
53 } 60 }
54 61
55 base::string16 GetTitle() override { return title_; } 62 base::string16 GetTitle() override { return title_; }
56 63
57 bool CanPin() const override { 64 bool CanPin() const override {
58 NOTIMPLEMENTED(); 65 NOTIMPLEMENTED();
59 return false; 66 return false;
60 } 67 }
61 68
62 ShelfMenuModel* CreateApplicationMenu(int event_flags) override { 69 ShelfMenuModel* CreateApplicationMenu(int event_flags) override {
(...skipping 11 matching lines...) Expand all
74 void Close() override { NOTIMPLEMENTED(); } 81 void Close() override { NOTIMPLEMENTED(); }
75 82
76 // TODO(msw): Support multiple open windows per button. 83 // TODO(msw): Support multiple open windows per button.
77 uint32_t window_id_; 84 uint32_t window_id_;
78 base::string16 title_; 85 base::string16 title_;
79 UserWindowController* user_window_controller_; 86 UserWindowController* user_window_controller_;
80 87
81 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus); 88 DISALLOW_COPY_AND_ASSIGN(ShelfItemDelegateMus);
82 }; 89 };
83 90
84 // Returns an icon image from a serialized SkBitmap, or the default shelf icon 91 // Returns an icon image from an SkBitmap, or the default shelf icon
85 // image if the bitmap is empty. Assumes the bitmap is a 1x icon. 92 // image if the bitmap is empty. Assumes the bitmap is a 1x icon.
86 // TODO(jamescook): Support other scale factors. 93 // TODO(jamescook): Support other scale factors.
87 gfx::ImageSkia GetShelfIconFromBitmap( 94 gfx::ImageSkia GetShelfIconFromBitmap(const SkBitmap& bitmap) {
88 const mojo::Array<uint8_t>& serialized_bitmap) {
89 // Convert the data to an ImageSkia.
90 SkBitmap bitmap = mojo::ConvertTo<SkBitmap>(serialized_bitmap.storage());
91 gfx::ImageSkia icon_image; 95 gfx::ImageSkia icon_image;
92 if (!bitmap.isNull()) { 96 if (!bitmap.isNull()) {
93 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 97 icon_image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
94 } else { 98 } else {
95 // Use default icon. 99 // Use default icon.
96 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 100 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
97 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); 101 icon_image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON);
98 } 102 }
99 return icon_image; 103 return icon_image;
100 } 104 }
101 105
106 // Returns an icon image from a serialized SkBitmap.
107 gfx::ImageSkia GetShelfIconFromSerializedBitmap(
108 const mojo::Array<uint8_t>& serialized_bitmap) {
109 SkBitmap bitmap = mojo::ConvertTo<SkBitmap>(serialized_bitmap.storage());
110 return GetShelfIconFromBitmap(bitmap);
111 }
112
102 } // namespace 113 } // namespace
103 114
104 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model) 115 ShelfDelegateMus::ShelfDelegateMus(ShelfModel* model)
105 : model_(model), binding_(this) { 116 : model_(model), binding_(this) {
106 mojo::Connector* connector = 117 mojo::Connector* connector =
107 views::WindowManagerConnection::Get()->connector(); 118 views::WindowManagerConnection::Get()->connector();
108 connector->ConnectToInterface("mojo:desktop_wm", &user_window_controller_); 119 connector->ConnectToInterface("mojo:desktop_wm", &user_window_controller_);
109 user_window_controller_->AddUserWindowObserver( 120 user_window_controller_->AddUserWindowObserver(
110 binding_.CreateInterfacePtrAndBind()); 121 binding_.CreateInterfacePtrAndBind());
111 } 122 }
112 123
113 ShelfDelegateMus::~ShelfDelegateMus() {} 124 ShelfDelegateMus::~ShelfDelegateMus() {}
114 125
115 void ShelfDelegateMus::OnShelfCreated(Shelf* shelf) { 126 void ShelfDelegateMus::OnShelfCreated(Shelf* shelf) {
116 ash::ShelfWidget* widget = shelf->shelf_widget(); 127 ShelfWidget* widget = shelf->shelf_widget();
117 ash::ShelfLayoutManager* layout_manager = widget->shelf_layout_manager(); 128 ShelfLayoutManager* layout_manager = widget->shelf_layout_manager();
118 mus::Window* window = aura::GetMusWindow(widget->GetNativeWindow()); 129 mus::Window* window = aura::GetMusWindow(widget->GetNativeWindow());
119 gfx::Size size = layout_manager->GetIdealBounds().size(); 130 gfx::Size size = layout_manager->GetIdealBounds().size();
120 window->SetSharedProperty<gfx::Size>( 131 window->SetSharedProperty<gfx::Size>(
121 mus::mojom::WindowManager::kPreferredSize_Property, size); 132 mus::mojom::WindowManager::kPreferredSize_Property, size);
122 133
123 ash::StatusAreaWidget* status_widget = widget->status_area_widget(); 134 StatusAreaWidget* status_widget = widget->status_area_widget();
124 mus::Window* status_window = 135 mus::Window* status_window =
125 aura::GetMusWindow(status_widget->GetNativeWindow()); 136 aura::GetMusWindow(status_widget->GetNativeWindow());
126 gfx::Size status_size = status_widget->GetWindowBoundsInScreen().size(); 137 gfx::Size status_size = status_widget->GetWindowBoundsInScreen().size();
127 status_window->SetSharedProperty<gfx::Size>( 138 status_window->SetSharedProperty<gfx::Size>(
128 mus::mojom::WindowManager::kPreferredSize_Property, status_size); 139 mus::mojom::WindowManager::kPreferredSize_Property, status_size);
129 } 140 }
130 141
131 void ShelfDelegateMus::OnShelfDestroyed(Shelf* shelf) { 142 void ShelfDelegateMus::OnShelfDestroyed(Shelf* shelf) {
132 NOTIMPLEMENTED(); 143 NOTIMPLEMENTED();
133 } 144 }
134 145
135 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) { 146 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) {
136 NOTIMPLEMENTED(); 147 observers_.ForAllPtrs([shelf](mash::shelf::mojom::ShelfObserver* observer) {
148 observer->OnAlignmentChanged(
149 static_cast<mash::shelf::mojom::Alignment>(shelf->GetAlignment()));
150 });
137 } 151 }
138 152
139 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(Shelf* shelf) { 153 void ShelfDelegateMus::OnShelfAutoHideBehaviorChanged(Shelf* shelf) {
140 NOTIMPLEMENTED(); 154 observers_.ForAllPtrs([shelf](mash::shelf::mojom::ShelfObserver* observer) {
155 observer->OnAutoHideBehaviorChanged(
156 static_cast<mash::shelf::mojom::AutoHideBehavior>(
157 shelf->auto_hide_behavior()));
158 });
141 } 159 }
142 160
143 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) { 161 ShelfID ShelfDelegateMus::GetShelfIDForAppID(const std::string& app_id) {
144 NOTIMPLEMENTED(); 162 NOTIMPLEMENTED();
145 return 0; 163 return 0;
146 } 164 }
147 165
148 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const { 166 bool ShelfDelegateMus::HasShelfIDToAppIDMapping(ShelfID id) const {
149 NOTIMPLEMENTED(); 167 NOTIMPLEMENTED();
150 return false; 168 return false;
(...skipping 10 matching lines...) Expand all
161 179
162 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) { 180 bool ShelfDelegateMus::IsAppPinned(const std::string& app_id) {
163 NOTIMPLEMENTED(); 181 NOTIMPLEMENTED();
164 return false; 182 return false;
165 } 183 }
166 184
167 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) { 185 void ShelfDelegateMus::UnpinAppWithID(const std::string& app_id) {
168 NOTIMPLEMENTED(); 186 NOTIMPLEMENTED();
169 } 187 }
170 188
189 void ShelfDelegateMus::AddObserver(
190 mash::shelf::mojom::ShelfObserverAssociatedPtrInfo observer) {
191 mash::shelf::mojom::ShelfObserverAssociatedPtr observer_ptr;
192 observer_ptr.Bind(std::move(observer));
193 // Notify the observer of the current state.
194 Shelf* shelf = Shelf::ForPrimaryDisplay();
195 observer_ptr->OnAlignmentChanged(
196 static_cast<mash::shelf::mojom::Alignment>(shelf->GetAlignment()));
197 observer_ptr->OnAutoHideBehaviorChanged(
198 static_cast<mash::shelf::mojom::AutoHideBehavior>(
199 shelf->auto_hide_behavior()));
200 observers_.AddPtr(std::move(observer_ptr));
201 }
202
203 void ShelfDelegateMus::SetAlignment(mash::shelf::mojom::Alignment alignment) {
204 ShelfAlignment value = static_cast<ShelfAlignment>(alignment);
205 Shell::GetInstance()->SetShelfAlignment(value, Shell::GetPrimaryRootWindow());
206 }
207
208 void ShelfDelegateMus::SetAutoHideBehavior(
209 mash::shelf::mojom::AutoHideBehavior auto_hide) {
210 ShelfAutoHideBehavior value = static_cast<ShelfAutoHideBehavior>(auto_hide);
211 Shell::GetInstance()->SetShelfAutoHideBehavior(value,
212 Shell::GetPrimaryRootWindow());
213 }
214
215 void ShelfDelegateMus::AddItem(
216 mash::shelf::mojom::ShelfItemPtr item,
217 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) {
218 ShelfItem shelf_item;
219 shelf_item.type = TYPE_APP_SHORTCUT;
220 shelf_item.status = STATUS_CLOSED;
221 shelf_item.image = GetShelfIconFromBitmap(item->image.To<SkBitmap>());
222
223 std::string item_id(item->id.To<std::string>());
224 if (app_id_to_shelf_id_.count(item_id))
225 return;
226 ShelfID shelf_id = model_->next_id();
227 app_id_to_shelf_id_.insert(std::make_pair(item_id, shelf_id));
228 model_->Add(shelf_item);
229
230 scoped_ptr<ShelfItemDelegateMus> item_delegate(new ShelfItemDelegateMus(
231 0, item->title.To<base::string16>(), user_window_controller_.get()));
232 Shell::GetInstance()->shelf_item_delegate_manager()->SetShelfItemDelegate(
233 shelf_id, std::move(item_delegate));
234 }
235
236 void ShelfDelegateMus::RemoveItem(const mojo::String& id) {
237 std::string item_id(id.To<std::string>());
238 DCHECK(app_id_to_shelf_id_.count(item_id));
239 model_->RemoveItemAt(model_->ItemIndexByID(app_id_to_shelf_id_[item_id]));
240 }
241
171 void ShelfDelegateMus::OnUserWindowObserverAdded( 242 void ShelfDelegateMus::OnUserWindowObserverAdded(
172 mojo::Array<mash::wm::mojom::UserWindowPtr> user_windows) { 243 mojo::Array<mash::wm::mojom::UserWindowPtr> user_windows) {
173 for (size_t i = 0; i < user_windows.size(); ++i) 244 for (size_t i = 0; i < user_windows.size(); ++i)
174 OnUserWindowAdded(std::move(user_windows[i])); 245 OnUserWindowAdded(std::move(user_windows[i]));
175 } 246 }
176 247
177 void ShelfDelegateMus::OnUserWindowAdded( 248 void ShelfDelegateMus::OnUserWindowAdded(
178 mash::wm::mojom::UserWindowPtr user_window) { 249 mash::wm::mojom::UserWindowPtr user_window) {
179 ShelfItem item; 250 ShelfItem item;
180 item.type = TYPE_PLATFORM_APP; 251 item.type = TYPE_PLATFORM_APP;
181 item.status = user_window->window_has_focus ? STATUS_ACTIVE : STATUS_RUNNING; 252 item.status = user_window->window_has_focus ? STATUS_ACTIVE : STATUS_RUNNING;
182 item.image = GetShelfIconFromBitmap(user_window->window_app_icon); 253 item.image = GetShelfIconFromSerializedBitmap(user_window->window_app_icon);
183 254
184 ShelfID shelf_id = model_->next_id(); 255 ShelfID shelf_id = model_->next_id();
185 window_id_to_shelf_id_.insert( 256 window_id_to_shelf_id_.insert(
186 std::make_pair(user_window->window_id, shelf_id)); 257 std::make_pair(user_window->window_id, shelf_id));
187 model_->Add(item); 258 model_->Add(item);
188 259
189 ShelfItemDelegateManager* manager = 260 std::unique_ptr<ShelfItemDelegate> item_delegate(new ShelfItemDelegateMus(
190 Shell::GetInstance()->shelf_item_delegate_manager();
191 std::unique_ptr<ShelfItemDelegate> delegate(new ShelfItemDelegateMus(
192 user_window->window_id, user_window->window_title.To<base::string16>(), 261 user_window->window_id, user_window->window_title.To<base::string16>(),
193 user_window_controller_.get())); 262 user_window_controller_.get()));
194 manager->SetShelfItemDelegate(shelf_id, std::move(delegate)); 263 Shell::GetInstance()->shelf_item_delegate_manager()->SetShelfItemDelegate(
264 shelf_id, std::move(item_delegate));
195 } 265 }
196 266
197 void ShelfDelegateMus::OnUserWindowRemoved(uint32_t window_id) { 267 void ShelfDelegateMus::OnUserWindowRemoved(uint32_t window_id) {
198 DCHECK(window_id_to_shelf_id_.count(window_id)); 268 DCHECK(window_id_to_shelf_id_.count(window_id));
199 model_->RemoveItemAt( 269 model_->RemoveItemAt(
200 model_->ItemIndexByID(window_id_to_shelf_id_[window_id])); 270 model_->ItemIndexByID(window_id_to_shelf_id_[window_id]));
201 } 271 }
202 272
203 void ShelfDelegateMus::OnUserWindowTitleChanged( 273 void ShelfDelegateMus::OnUserWindowTitleChanged(
204 uint32_t window_id, 274 uint32_t window_id,
(...skipping 22 matching lines...) Expand all
227 mojo::Array<uint8_t> app_icon) { 297 mojo::Array<uint8_t> app_icon) {
228 // Find the shelf ID for this window. 298 // Find the shelf ID for this window.
229 DCHECK(window_id_to_shelf_id_.count(window_id)); 299 DCHECK(window_id_to_shelf_id_.count(window_id));
230 ShelfID shelf_id = window_id_to_shelf_id_[window_id]; 300 ShelfID shelf_id = window_id_to_shelf_id_[window_id];
231 DCHECK_GT(shelf_id, 0); 301 DCHECK_GT(shelf_id, 0);
232 302
233 // Update the icon in the ShelfItem. 303 // Update the icon in the ShelfItem.
234 int index = model_->ItemIndexByID(shelf_id); 304 int index = model_->ItemIndexByID(shelf_id);
235 DCHECK_GE(index, 0); 305 DCHECK_GE(index, 0);
236 ShelfItem item = *model_->ItemByID(shelf_id); 306 ShelfItem item = *model_->ItemByID(shelf_id);
237 item.image = GetShelfIconFromBitmap(app_icon); 307 item.image = GetShelfIconFromSerializedBitmap(app_icon);
238 model_->Set(index, item); 308 model_->Set(index, item);
239 } 309 }
240 310
241 void ShelfDelegateMus::OnUserWindowFocusChanged(uint32_t window_id, 311 void ShelfDelegateMus::OnUserWindowFocusChanged(uint32_t window_id,
242 bool has_focus) { 312 bool has_focus) {
243 DCHECK(window_id_to_shelf_id_.count(window_id)); 313 DCHECK(window_id_to_shelf_id_.count(window_id));
244 ShelfID shelf_id = window_id_to_shelf_id_[window_id]; 314 ShelfID shelf_id = window_id_to_shelf_id_[window_id];
245 int index = model_->ItemIndexByID(shelf_id); 315 int index = model_->ItemIndexByID(shelf_id);
246 DCHECK_GE(index, 0); 316 DCHECK_GE(index, 0);
247 ShelfItems::const_iterator iter = model_->ItemByID(shelf_id); 317 ShelfItems::const_iterator iter = model_->ItemByID(shelf_id);
248 DCHECK(iter != model_->items().end()); 318 DCHECK(iter != model_->items().end());
249 ShelfItem item = *iter; 319 ShelfItem item = *iter;
250 item.status = has_focus ? STATUS_ACTIVE : STATUS_RUNNING; 320 item.status = has_focus ? STATUS_ACTIVE : STATUS_RUNNING;
251 model_->Set(index, item); 321 model_->Set(index, item);
252 } 322 }
253 323
254 } // namespace sysui 324 } // namespace sysui
255 } // namespace ash 325 } // namespace ash
OLDNEW
« no previous file with comments | « ash/mus/shelf_delegate_mus.h ('k') | ash/mus/sysui_application.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698