OLD | NEW |
---|---|
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; } | |
sky
2016/04/07 22:02:46
Document when this changes (maybe it should DCHECK
msw
2016/04/08 21:20:48
Moved back to ctor. (this is super temporary; we n
| |
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(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(Shelf* shelf) { |
130 NOTIMPLEMENTED(); | 140 NOTIMPLEMENTED(); |
131 } | 141 } |
132 | 142 |
133 void ShelfDelegateMus::OnShelfAlignmentChanged(Shelf* shelf) { | 143 void ShelfDelegateMus::OnShelfAlignmentChanged(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(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 Loading... | |
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::ShelfObserverAssociatedPtrInfo observer) { | |
188 mash::shelf::mojom::ShelfObserverAssociatedPtr observer_ptr; | |
189 observer_ptr.Bind(std::move(observer)); | |
190 // Notify the observer of the current state. | |
191 Shelf* shelf = Shelf::ForPrimaryDisplay(); | |
sky
2016/04/07 22:02:46
Do we support shelfs for different displays? If we
msw
2016/04/08 21:20:48
I asked Robert "Does mash support some test way to
sky
2016/04/08 23:08:24
Mus itself supports multiple displays. But I'm fin
| |
192 observer_ptr->OnAlignmentChanged( | |
193 static_cast<mash::shelf::mojom::Alignment>(shelf->GetAlignment())); | |
194 observer_ptr->OnAutoHideBehaviorChanged( | |
195 static_cast<mash::shelf::mojom::AutoHideBehavior>( | |
196 shelf->auto_hide_behavior())); | |
197 observers_.AddInterfacePtr(std::move(observer_ptr)); | |
198 } | |
199 | |
200 void ShelfDelegateMus::SetAlignment(mash::shelf::mojom::Alignment alignment) { | |
201 ShelfAlignment value = static_cast<ShelfAlignment>(alignment); | |
202 Shell::GetInstance()->SetShelfAlignment(value, Shell::GetPrimaryRootWindow()); | |
203 } | |
204 | |
205 void ShelfDelegateMus::SetAutoHideBehavior( | |
206 mash::shelf::mojom::AutoHideBehavior auto_hide) { | |
207 ShelfAutoHideBehavior value = static_cast<ShelfAutoHideBehavior>(auto_hide); | |
208 Shell::GetInstance()->SetShelfAutoHideBehavior(value, | |
209 Shell::GetPrimaryRootWindow()); | |
210 } | |
211 | |
212 void ShelfDelegateMus::AddItem( | |
213 mash::shelf::mojom::ShelfItemPtr item, | |
214 mash::shelf::mojom::ShelfItemDelegateAssociatedPtrInfo delegate) { | |
215 ShelfItem shelf_item; | |
216 shelf_item.type = TYPE_APP_SHORTCUT; | |
217 shelf_item.status = STATUS_CLOSED; | |
218 shelf_item.image = GetShelfIconFromBitmap(item->image.To<SkBitmap>()); | |
219 | |
220 std::string item_id(item->id.To<std::string>()); | |
sky
2016/04/07 22:02:46
Ignore request if item_id is in app_id_to_shelf_id
msw
2016/04/08 21:20:48
Done.
| |
221 ShelfID shelf_id = model_->next_id(); | |
222 app_id_to_shelf_id_.insert(std::make_pair(item_id, shelf_id)); | |
223 model_->Add(shelf_item); | |
224 | |
225 scoped_ptr<ShelfItemDelegateMus> item_delegate(new ShelfItemDelegateMus( | |
226 item->title.To<base::string16>(), user_window_controller_.get())); | |
227 Shell::GetInstance()->shelf_item_delegate_manager()->SetShelfItemDelegate( | |
228 shelf_id, std::move(item_delegate)); | |
229 } | |
230 | |
231 void ShelfDelegateMus::RemoveItem(const mojo::String& id) { | |
232 std::string item_id(id.To<std::string>()); | |
233 DCHECK(app_id_to_shelf_id_.count(item_id)); | |
234 model_->RemoveItemAt(model_->ItemIndexByID(app_id_to_shelf_id_[item_id])); | |
235 } | |
236 | |
169 void ShelfDelegateMus::OnUserWindowObserverAdded( | 237 void ShelfDelegateMus::OnUserWindowObserverAdded( |
170 mojo::Array<mash::wm::mojom::UserWindowPtr> user_windows) { | 238 mojo::Array<mash::wm::mojom::UserWindowPtr> user_windows) { |
171 for (size_t i = 0; i < user_windows.size(); ++i) | 239 for (size_t i = 0; i < user_windows.size(); ++i) |
172 OnUserWindowAdded(std::move(user_windows[i])); | 240 OnUserWindowAdded(std::move(user_windows[i])); |
173 } | 241 } |
174 | 242 |
175 void ShelfDelegateMus::OnUserWindowAdded( | 243 void ShelfDelegateMus::OnUserWindowAdded( |
176 mash::wm::mojom::UserWindowPtr user_window) { | 244 mash::wm::mojom::UserWindowPtr user_window) { |
177 ShelfItem item; | 245 ShelfItem item; |
178 item.type = TYPE_PLATFORM_APP; | 246 item.type = TYPE_PLATFORM_APP; |
179 item.status = user_window->window_has_focus ? STATUS_ACTIVE : STATUS_RUNNING; | 247 item.status = user_window->window_has_focus ? STATUS_ACTIVE : STATUS_RUNNING; |
180 item.image = GetShelfIconFromBitmap(user_window->window_app_icon); | 248 item.image = GetShelfIconFromSerializedBitmap(user_window->window_app_icon); |
181 | 249 |
182 ShelfID shelf_id = model_->next_id(); | 250 ShelfID shelf_id = model_->next_id(); |
183 window_id_to_shelf_id_.insert( | 251 window_id_to_shelf_id_.insert( |
184 std::make_pair(user_window->window_id, shelf_id)); | 252 std::make_pair(user_window->window_id, shelf_id)); |
185 model_->Add(item); | 253 model_->Add(item); |
186 | 254 |
187 ShelfItemDelegateManager* manager = | 255 scoped_ptr<ShelfItemDelegateMus> item_delegate( |
188 Shell::GetInstance()->shelf_item_delegate_manager(); | 256 new ShelfItemDelegateMus(user_window->window_title.To<base::string16>(), |
189 scoped_ptr<ShelfItemDelegate> delegate(new ShelfItemDelegateMus( | 257 user_window_controller_.get())); |
190 user_window->window_id, user_window->window_title.To<base::string16>(), | 258 item_delegate->set_window_id(user_window->window_id); |
191 user_window_controller_.get())); | 259 Shell::GetInstance()->shelf_item_delegate_manager()->SetShelfItemDelegate( |
192 manager->SetShelfItemDelegate(shelf_id, std::move(delegate)); | 260 shelf_id, std::move(item_delegate)); |
193 } | 261 } |
194 | 262 |
195 void ShelfDelegateMus::OnUserWindowRemoved(uint32_t window_id) { | 263 void ShelfDelegateMus::OnUserWindowRemoved(uint32_t window_id) { |
196 DCHECK(window_id_to_shelf_id_.count(window_id)); | 264 DCHECK(window_id_to_shelf_id_.count(window_id)); |
197 model_->RemoveItemAt( | 265 model_->RemoveItemAt( |
198 model_->ItemIndexByID(window_id_to_shelf_id_[window_id])); | 266 model_->ItemIndexByID(window_id_to_shelf_id_[window_id])); |
199 } | 267 } |
200 | 268 |
201 void ShelfDelegateMus::OnUserWindowTitleChanged( | 269 void ShelfDelegateMus::OnUserWindowTitleChanged( |
202 uint32_t window_id, | 270 uint32_t window_id, |
(...skipping 22 matching lines...) Expand all Loading... | |
225 mojo::Array<uint8_t> app_icon) { | 293 mojo::Array<uint8_t> app_icon) { |
226 // Find the shelf ID for this window. | 294 // Find the shelf ID for this window. |
227 DCHECK(window_id_to_shelf_id_.count(window_id)); | 295 DCHECK(window_id_to_shelf_id_.count(window_id)); |
228 ShelfID shelf_id = window_id_to_shelf_id_[window_id]; | 296 ShelfID shelf_id = window_id_to_shelf_id_[window_id]; |
229 DCHECK_GT(shelf_id, 0); | 297 DCHECK_GT(shelf_id, 0); |
230 | 298 |
231 // Update the icon in the ShelfItem. | 299 // Update the icon in the ShelfItem. |
232 int index = model_->ItemIndexByID(shelf_id); | 300 int index = model_->ItemIndexByID(shelf_id); |
233 DCHECK_GE(index, 0); | 301 DCHECK_GE(index, 0); |
234 ShelfItem item = *model_->ItemByID(shelf_id); | 302 ShelfItem item = *model_->ItemByID(shelf_id); |
235 item.image = GetShelfIconFromBitmap(app_icon); | 303 item.image = GetShelfIconFromSerializedBitmap(app_icon); |
236 model_->Set(index, item); | 304 model_->Set(index, item); |
237 } | 305 } |
238 | 306 |
239 void ShelfDelegateMus::OnUserWindowFocusChanged(uint32_t window_id, | 307 void ShelfDelegateMus::OnUserWindowFocusChanged(uint32_t window_id, |
240 bool has_focus) { | 308 bool has_focus) { |
241 DCHECK(window_id_to_shelf_id_.count(window_id)); | 309 DCHECK(window_id_to_shelf_id_.count(window_id)); |
242 ShelfID shelf_id = window_id_to_shelf_id_[window_id]; | 310 ShelfID shelf_id = window_id_to_shelf_id_[window_id]; |
243 int index = model_->ItemIndexByID(shelf_id); | 311 int index = model_->ItemIndexByID(shelf_id); |
244 DCHECK_GE(index, 0); | 312 DCHECK_GE(index, 0); |
245 ShelfItems::const_iterator iter = model_->ItemByID(shelf_id); | 313 ShelfItems::const_iterator iter = model_->ItemByID(shelf_id); |
246 DCHECK(iter != model_->items().end()); | 314 DCHECK(iter != model_->items().end()); |
247 ShelfItem item = *iter; | 315 ShelfItem item = *iter; |
248 item.status = has_focus ? STATUS_ACTIVE : STATUS_RUNNING; | 316 item.status = has_focus ? STATUS_ACTIVE : STATUS_RUNNING; |
249 model_->Set(index, item); | 317 model_->Set(index, item); |
250 } | 318 } |
251 | 319 |
252 } // namespace sysui | 320 } // namespace sysui |
253 } // namespace ash | 321 } // namespace ash |
OLD | NEW |