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

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

Issue 2357143004: mash: Support ShelfWindowWatcher via ShelfItem properties. (Closed)
Patch Set: Set Settings WmWindow title with a WmWindowObserver. 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 = window->GetIntProperty(WmWindowProperty::SHELF_ICON_ID);
31 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 31 if (icon != kInvalidImageResourceID)
32 item->image = *rb.GetImageSkiaNamed(details.image_resource_id); 32 item->image = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon);
33 }
34 } 33 }
35 34
36 // Returns true if |window| has a ShelfItem added by ShelfWindowWatcher. 35 // Returns true if |window| has a ShelfItem added by ShelfWindowWatcher.
37 bool HasShelfItemForWindow(WmWindow* window) { 36 bool HasShelfItemForWindow(WmWindow* window) {
38 return window->GetShelfItemDetails() && 37 return window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) !=
38 TYPE_UNDEFINED &&
39 window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID; 39 window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID;
40 } 40 }
41 41
42 } // namespace 42 } // namespace
43 43
44 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( 44 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver(
45 ShelfWindowWatcher* window_watcher) 45 ShelfWindowWatcher* window_watcher)
46 : window_watcher_(window_watcher) {} 46 : window_watcher_(window_watcher) {}
47 47
48 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {} 48 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {}
(...skipping 18 matching lines...) Expand all
67 67
68 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( 68 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver(
69 ShelfWindowWatcher* window_watcher) 69 ShelfWindowWatcher* window_watcher)
70 : window_watcher_(window_watcher) {} 70 : window_watcher_(window_watcher) {}
71 71
72 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} 72 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {}
73 73
74 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( 74 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged(
75 WmWindow* window, 75 WmWindow* window,
76 WmWindowProperty property) { 76 WmWindowProperty property) {
77 if (property == WmWindowProperty::SHELF_ITEM_DETAILS) 77 if (property == WmWindowProperty::SHELF_ITEM_TYPE ||
78 window_watcher_->OnUserWindowShelfItemDetailsChanged(window); 78 property == WmWindowProperty::SHELF_ICON_ID) {
79 window_watcher_->OnUserWindowPropertyChanged(window);
80 }
79 } 81 }
80 82
81 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( 83 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying(
82 WmWindow* window) { 84 WmWindow* window) {
83 window_watcher_->OnUserWindowDestroying(window); 85 window_watcher_->OnUserWindowDestroying(window);
84 } 86 }
85 87
86 //////////////////////////////////////////////////////////////////////////////// 88 ////////////////////////////////////////////////////////////////////////////////
87 89
88 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) 90 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model)
(...skipping 10 matching lines...) Expand all
99 101
100 display::Screen::GetScreen()->AddObserver(this); 102 display::Screen::GetScreen()->AddObserver(this);
101 } 103 }
102 104
103 ShelfWindowWatcher::~ShelfWindowWatcher() { 105 ShelfWindowWatcher::~ShelfWindowWatcher() {
104 display::Screen::GetScreen()->RemoveObserver(this); 106 display::Screen::GetScreen()->RemoveObserver(this);
105 WmShell::Get()->RemoveActivationObserver(this); 107 WmShell::Get()->RemoveActivationObserver(this);
106 } 108 }
107 109
108 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) { 110 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) {
109 const ShelfItemDetails* item_details = window->GetShelfItemDetails();
110 ShelfItem item; 111 ShelfItem item;
111 ShelfID id = model_->next_id(); 112 ShelfID id = model_->next_id();
112 item.status = window->IsActive() ? STATUS_ACTIVE : STATUS_RUNNING; 113 item.status = window->IsActive() ? STATUS_ACTIVE : STATUS_RUNNING;
113 SetShelfItemDetailsForShelfItem(&item, *item_details); 114 UpdateShelfItemForWindow(&item, window);
114 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); 115 window->SetIntProperty(WmWindowProperty::SHELF_ID, id);
115 std::unique_ptr<ShelfItemDelegate> item_delegate( 116 std::unique_ptr<ShelfItemDelegate> item_delegate(
116 new ShelfWindowWatcherItemDelegate(window)); 117 new ShelfWindowWatcherItemDelegate(window));
117 model_->SetShelfItemDelegate(id, std::move(item_delegate)); 118 model_->SetShelfItemDelegate(id, std::move(item_delegate));
118 model_->Add(item); 119 model_->Add(item);
119 } 120 }
120 121
121 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) { 122 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) {
122 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); 123 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID);
123 DCHECK_NE(shelf_id, kInvalidShelfID); 124 DCHECK_NE(shelf_id, kInvalidShelfID);
(...skipping 17 matching lines...) Expand all
141 model_->Set(index, item); 142 model_->Set(index, item);
142 } 143 }
143 144
144 int ShelfWindowWatcher::GetShelfItemIndexForWindow(WmWindow* window) const { 145 int ShelfWindowWatcher::GetShelfItemIndexForWindow(WmWindow* window) const {
145 return model_->ItemIndexByID( 146 return model_->ItemIndexByID(
146 window->GetIntProperty(WmWindowProperty::SHELF_ID)); 147 window->GetIntProperty(WmWindowProperty::SHELF_ID));
147 } 148 }
148 149
149 void ShelfWindowWatcher::OnUserWindowAdded(WmWindow* window) { 150 void ShelfWindowWatcher::OnUserWindowAdded(WmWindow* window) {
150 // The window may already be tracked from when it was added to a different 151 // 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. 152 // display or because an existing window added its shelf item properties.
152 if (observed_user_windows_.IsObserving(window)) 153 if (observed_user_windows_.IsObserving(window))
153 return; 154 return;
154 155
155 observed_user_windows_.Add(window); 156 observed_user_windows_.Add(window);
156 157
157 // Add ShelfItem if |window| already has a ShelfItemDetails when it is 158 // Add a ShelfItem if |window| has a valid ShelfItemType on creation.
158 // created. 159 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) !=
159 if (window->GetShelfItemDetails() && 160 TYPE_UNDEFINED &&
160 window->GetIntProperty(WmWindowProperty::SHELF_ID) == kInvalidShelfID) { 161 window->GetIntProperty(WmWindowProperty::SHELF_ID) == kInvalidShelfID) {
161 AddShelfItem(window); 162 AddShelfItem(window);
162 } 163 }
163 } 164 }
164 165
165 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) { 166 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) {
166 if (observed_user_windows_.IsObserving(window)) 167 if (observed_user_windows_.IsObserving(window))
167 observed_user_windows_.Remove(window); 168 observed_user_windows_.Remove(window);
168 169
169 if (HasShelfItemForWindow(window)) 170 if (HasShelfItemForWindow(window))
170 RemoveShelfItem(window); 171 RemoveShelfItem(window);
171 } 172 }
172 173
173 void ShelfWindowWatcher::OnUserWindowShelfItemDetailsChanged(WmWindow* window) { 174 void ShelfWindowWatcher::OnUserWindowPropertyChanged(WmWindow* window) {
174 if (!window->GetShelfItemDetails()) { 175 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) ==
176 TYPE_UNDEFINED) {
175 // Removes ShelfItem for |window| when it has a ShelfItem. 177 // Removes ShelfItem for |window| when it has a ShelfItem.
176 if (window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID) 178 if (window->GetIntProperty(WmWindowProperty::SHELF_ID) != kInvalidShelfID)
177 RemoveShelfItem(window); 179 RemoveShelfItem(window);
178 return; 180 return;
179 } 181 }
180 182
181 // When ShelfItemDetails is changed, update ShelfItem. 183 // Update an existing ShelfItem for |window| when a property has changed.
182 if (HasShelfItemForWindow(window)) { 184 if (HasShelfItemForWindow(window)) {
183 int index = GetShelfItemIndexForWindow(window); 185 int index = GetShelfItemIndexForWindow(window);
184 DCHECK_GE(index, 0); 186 DCHECK_GE(index, 0);
185 ShelfItem item = model_->items()[index]; 187 ShelfItem item = model_->items()[index];
186 const ShelfItemDetails* details = window->GetShelfItemDetails(); 188 UpdateShelfItemForWindow(&item, window);
187 SetShelfItemDetailsForShelfItem(&item, *details);
188 model_->Set(index, item); 189 model_->Set(index, item);
189 return; 190 return;
190 } 191 }
191 192
192 // Creates a new ShelfItem for |window|. 193 // Creates a new ShelfItem for |window|.
193 AddShelfItem(window); 194 AddShelfItem(window);
194 } 195 }
195 196
196 void ShelfWindowWatcher::OnWindowActivated(WmWindow* gained_active, 197 void ShelfWindowWatcher::OnWindowActivated(WmWindow* gained_active,
197 WmWindow* lost_active) { 198 WmWindow* lost_active) {
(...skipping 14 matching lines...) Expand all
212 observed_container_windows_.Add(container); 213 observed_container_windows_.Add(container);
213 } 214 }
214 215
215 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { 216 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) {
216 } 217 }
217 218
218 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, 219 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&,
219 uint32_t) {} 220 uint32_t) {}
220 221
221 } // namespace ash 222 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698