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

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

Powered by Google App Engine
This is Rietveld 408576698