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

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

Issue 2357143004: mash: Support ShelfWindowWatcher via ShelfItem properties. (Closed)
Patch Set: Address comments. 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/common/shelf/shelf_window_watcher.h" 5 #include "ash/common/shelf/shelf_window_watcher.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "ash/common/shelf/shelf_constants.h" 10 #include "ash/common/shelf/shelf_constants.h"
11 #include "ash/common/shelf/shelf_model.h" 11 #include "ash/common/shelf/shelf_model.h"
12 #include "ash/common/shelf/shelf_window_watcher_item_delegate.h" 12 #include "ash/common/shelf/shelf_window_watcher_item_delegate.h"
13 #include "ash/common/shell_window_ids.h" 13 #include "ash/common/shell_window_ids.h"
14 #include "ash/common/wm/window_state.h" 14 #include "ash/common/wm/window_state.h"
15 #include "ash/common/wm_shell.h" 15 #include "ash/common/wm_shell.h"
16 #include "ash/common/wm_window.h" 16 #include "ash/common/wm_window.h"
17 #include "ash/common/wm_window_property.h" 17 #include "ash/common/wm_window_property.h"
18 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
19 #include "ui/display/display.h" 19 #include "ui/display/display.h"
20 #include "ui/display/screen.h" 20 #include "ui/display/screen.h"
21 #include "ui/gfx/image/image_skia.h" 21 #include "ui/gfx/image/image_skia.h"
22 22
23 namespace ash { 23 namespace ash {
24 namespace { 24 namespace {
25 25
26 // Sets ShelfItem property by using the value of |details|. 26 // Update the ShelfItem from relevant window properties.
27 void SetShelfItemDetailsForShelfItem(ash::ShelfItem* item, 27 void UpdateShelfItemForWindow(ShelfItem* item, WmWindow* window) {
28 const ash::ShelfItemDetails& details) { 28 item->type = static_cast<ShelfItemType>(
29 item->type = details.type; 29 window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE));
30 if (details.image_resource_id != ash::kInvalidImageResourceID) { 30 const int icon =
31 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 31 window->GetIntProperty(WmWindowProperty::SHELF_ICON_RESOURCE_ID);
32 item->image = *rb.GetImageSkiaNamed(details.image_resource_id); 32 if (icon != kInvalidImageResourceID)
33 } 33 item->image = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon);
34 } 34 }
35 35
36 // Returns true if |window| has a ShelfItem added by ShelfWindowWatcher. 36 // Returns true if |window| has a ShelfItem added by ShelfWindowWatcher.
37 bool HasShelfItemForWindow(WmWindow* window) { 37 bool HasShelfItemForWindow(WmWindow* window) {
38 return window->GetShelfItemDetails() && 38 return window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) !=
39 TYPE_UNDEFINED &&
39 window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID; 40 window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID;
40 } 41 }
41 42
42 } // namespace 43 } // namespace
43 44
44 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( 45 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver(
45 ShelfWindowWatcher* window_watcher) 46 ShelfWindowWatcher* window_watcher)
46 : window_watcher_(window_watcher) {} 47 : window_watcher_(window_watcher) {}
47 48
48 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {} 49 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {}
(...skipping 18 matching lines...) Expand all
67 68
68 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( 69 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver(
69 ShelfWindowWatcher* window_watcher) 70 ShelfWindowWatcher* window_watcher)
70 : window_watcher_(window_watcher) {} 71 : window_watcher_(window_watcher) {}
71 72
72 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} 73 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {}
73 74
74 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( 75 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged(
75 WmWindow* window, 76 WmWindow* window,
76 WmWindowProperty property) { 77 WmWindowProperty property) {
77 if (property == WmWindowProperty::SHELF_ITEM_DETAILS) 78 if (property == WmWindowProperty::SHELF_ITEM_TYPE ||
78 window_watcher_->OnUserWindowShelfItemDetailsChanged(window); 79 property == WmWindowProperty::SHELF_ICON_RESOURCE_ID) {
80 window_watcher_->OnUserWindowPropertyChanged(window);
81 }
79 } 82 }
80 83
81 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( 84 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying(
82 WmWindow* window) { 85 WmWindow* window) {
83 window_watcher_->OnUserWindowDestroying(window); 86 window_watcher_->OnUserWindowDestroying(window);
84 } 87 }
85 88
86 //////////////////////////////////////////////////////////////////////////////// 89 ////////////////////////////////////////////////////////////////////////////////
87 90
88 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) 91 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model)
(...skipping 10 matching lines...) Expand all
99 102
100 display::Screen::GetScreen()->AddObserver(this); 103 display::Screen::GetScreen()->AddObserver(this);
101 } 104 }
102 105
103 ShelfWindowWatcher::~ShelfWindowWatcher() { 106 ShelfWindowWatcher::~ShelfWindowWatcher() {
104 display::Screen::GetScreen()->RemoveObserver(this); 107 display::Screen::GetScreen()->RemoveObserver(this);
105 WmShell::Get()->RemoveActivationObserver(this); 108 WmShell::Get()->RemoveActivationObserver(this);
106 } 109 }
107 110
108 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) { 111 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) {
109 const ShelfItemDetails* item_details = window->GetShelfItemDetails();
110 ShelfItem item; 112 ShelfItem item;
111 ShelfID id = model_->next_id(); 113 ShelfID id = model_->next_id();
112 item.status = window->IsActive() ? STATUS_ACTIVE : STATUS_RUNNING; 114 item.status = window->IsActive() ? STATUS_ACTIVE : STATUS_RUNNING;
113 SetShelfItemDetailsForShelfItem(&item, *item_details); 115 UpdateShelfItemForWindow(&item, window);
114 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); 116 window->SetIntProperty(WmWindowProperty::SHELF_ID, id);
115 std::unique_ptr<ShelfItemDelegate> item_delegate( 117 std::unique_ptr<ShelfItemDelegate> item_delegate(
116 new ShelfWindowWatcherItemDelegate(window)); 118 new ShelfWindowWatcherItemDelegate(window));
117 model_->SetShelfItemDelegate(id, std::move(item_delegate)); 119 model_->SetShelfItemDelegate(id, std::move(item_delegate));
118 model_->Add(item); 120 model_->Add(item);
119 } 121 }
120 122
121 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) { 123 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) {
122 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); 124 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID);
123 DCHECK_NE(shelf_id, kInvalidShelfID); 125 DCHECK_NE(shelf_id, kInvalidShelfID);
(...skipping 17 matching lines...) Expand all
141 model_->Set(index, item); 143 model_->Set(index, item);
142 } 144 }
143 145
144 int ShelfWindowWatcher::GetShelfItemIndexForWindow(WmWindow* window) const { 146 int ShelfWindowWatcher::GetShelfItemIndexForWindow(WmWindow* window) const {
145 return model_->ItemIndexByID( 147 return model_->ItemIndexByID(
146 window->GetIntProperty(WmWindowProperty::SHELF_ID)); 148 window->GetIntProperty(WmWindowProperty::SHELF_ID));
147 } 149 }
148 150
149 void ShelfWindowWatcher::OnUserWindowAdded(WmWindow* window) { 151 void ShelfWindowWatcher::OnUserWindowAdded(WmWindow* window) {
150 // The window may already be tracked from when it was added to a different 152 // The window may already be tracked from when it was added to a different
151 // display or because an existing window added ShelfItemDetails to itself. 153 // display or because an existing window added its shelf item properties.
152 if (observed_user_windows_.IsObserving(window)) 154 if (observed_user_windows_.IsObserving(window))
153 return; 155 return;
154 156
155 observed_user_windows_.Add(window); 157 observed_user_windows_.Add(window);
156 158
157 // Add ShelfItem if |window| already has a ShelfItemDetails when it is 159 // Add a ShelfItem if |window| has a valid ShelfItemType on creation.
158 // created. 160 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) !=
159 if (window->GetShelfItemDetails() && 161 TYPE_UNDEFINED &&
160 window->GetIntProperty(WmWindowProperty::SHELF_ID) == kInvalidShelfID) { 162 window->GetIntProperty(WmWindowProperty::SHELF_ID) == kInvalidShelfID) {
161 AddShelfItem(window); 163 AddShelfItem(window);
162 } 164 }
163 } 165 }
164 166
165 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) { 167 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) {
166 if (observed_user_windows_.IsObserving(window)) 168 if (observed_user_windows_.IsObserving(window))
167 observed_user_windows_.Remove(window); 169 observed_user_windows_.Remove(window);
168 170
169 if (HasShelfItemForWindow(window)) 171 if (HasShelfItemForWindow(window))
170 RemoveShelfItem(window); 172 RemoveShelfItem(window);
171 } 173 }
172 174
173 void ShelfWindowWatcher::OnUserWindowShelfItemDetailsChanged(WmWindow* window) { 175 void ShelfWindowWatcher::OnUserWindowPropertyChanged(WmWindow* window) {
174 if (!window->GetShelfItemDetails()) { 176 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) ==
177 TYPE_UNDEFINED) {
175 // Removes ShelfItem for |window| when it has a ShelfItem. 178 // Removes ShelfItem for |window| when it has a ShelfItem.
176 if (window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID) 179 if (window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID)
177 RemoveShelfItem(window); 180 RemoveShelfItem(window);
178 return; 181 return;
179 } 182 }
180 183
181 // When ShelfItemDetails is changed, update ShelfItem. 184 // Update an existing ShelfItem for |window| when a property has changed.
182 if (HasShelfItemForWindow(window)) { 185 if (HasShelfItemForWindow(window)) {
183 int index = GetShelfItemIndexForWindow(window); 186 int index = GetShelfItemIndexForWindow(window);
184 DCHECK_GE(index, 0); 187 DCHECK_GE(index, 0);
185 ShelfItem item = model_->items()[index]; 188 ShelfItem item = model_->items()[index];
186 const ShelfItemDetails* details = window->GetShelfItemDetails(); 189 UpdateShelfItemForWindow(&item, window);
187 SetShelfItemDetailsForShelfItem(&item, *details);
188 model_->Set(index, item); 190 model_->Set(index, item);
189 return; 191 return;
190 } 192 }
191 193
192 // Creates a new ShelfItem for |window|. 194 // Creates a new ShelfItem for |window|.
193 AddShelfItem(window); 195 AddShelfItem(window);
194 } 196 }
195 197
196 void ShelfWindowWatcher::OnWindowActivated(WmWindow* gained_active, 198 void ShelfWindowWatcher::OnWindowActivated(WmWindow* gained_active,
197 WmWindow* lost_active) { 199 WmWindow* lost_active) {
(...skipping 14 matching lines...) Expand all
212 observed_container_windows_.Add(container); 214 observed_container_windows_.Add(container);
213 } 215 }
214 216
215 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { 217 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) {
216 } 218 }
217 219
218 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, 220 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&,
219 uint32_t) {} 221 uint32_t) {}
220 222
221 } // namespace ash 223 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/shelf/shelf_window_watcher.h ('k') | ash/common/shelf/shelf_window_watcher_item_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698