OLD | NEW |
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/shelf/shelf_window_watcher.h" | 5 #include "ash/shelf/shelf_window_watcher.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "ash/public/cpp/config.h" | |
11 #include "ash/public/cpp/shell_window_ids.h" | 10 #include "ash/public/cpp/shell_window_ids.h" |
12 #include "ash/public/cpp/window_properties.h" | 11 #include "ash/public/cpp/window_properties.h" |
13 #include "ash/shelf/shelf_constants.h" | 12 #include "ash/shelf/shelf_constants.h" |
14 #include "ash/shelf/shelf_model.h" | 13 #include "ash/shelf/shelf_model.h" |
15 #include "ash/shelf/shelf_window_watcher_item_delegate.h" | 14 #include "ash/shelf/shelf_window_watcher_item_delegate.h" |
16 #include "ash/shell.h" | 15 #include "ash/shell.h" |
17 #include "ash/shell_port.h" | 16 #include "ash/shell_port.h" |
18 #include "ash/wm/window_state.h" | 17 #include "ash/wm/window_state.h" |
19 #include "ash/wm/window_util.h" | 18 #include "ash/wm/window_util.h" |
20 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
21 #include "ui/aura/client/aura_constants.h" | 20 #include "ui/aura/client/aura_constants.h" |
22 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
23 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
24 #include "ui/display/display.h" | 23 #include "ui/display/display.h" |
25 #include "ui/display/screen.h" | 24 #include "ui/display/screen.h" |
26 #include "ui/resources/grit/ui_resources.h" | 25 #include "ui/resources/grit/ui_resources.h" |
27 #include "ui/wm/public/activation_client.h" | 26 #include "ui/wm/public/activation_client.h" |
28 | 27 |
29 namespace ash { | 28 namespace ash { |
30 namespace { | 29 namespace { |
31 | 30 |
32 // Returns the shelf item type, with special temporary behavior for Mash: | 31 // Returns the window's shelf item type property value. |
33 // Mash provides a default shelf item type (TYPE_APP) for non-ignored windows. | |
34 ShelfItemType GetShelfItemType(aura::Window* window) { | 32 ShelfItemType GetShelfItemType(aura::Window* window) { |
35 // TODO(msw): Remove Mash default ShelfItemType assignment. crbug.com/722496 | |
36 if (Shell::GetAshConfig() == Config::MASH && | |
37 window->GetProperty(kShelfItemTypeKey) == TYPE_UNDEFINED && | |
38 !wm::GetWindowState(window)->ignored_by_shelf()) { | |
39 return TYPE_APP; | |
40 } | |
41 return static_cast<ShelfItemType>(window->GetProperty(kShelfItemTypeKey)); | 33 return static_cast<ShelfItemType>(window->GetProperty(kShelfItemTypeKey)); |
42 } | 34 } |
43 | 35 |
44 // Returns the shelf id, with special temporary behavior for Mash: | 36 // Returns the window's shelf id property value. |
45 // Mash provides a default shelf ids for non-ignored windows. | |
46 ShelfID GetShelfID(aura::Window* window) { | 37 ShelfID GetShelfID(aura::Window* window) { |
47 // TODO(msw): Remove Mash default ShelfID assignment. crbug.com/722496 | |
48 if (Shell::GetAshConfig() == Config::MASH && | |
49 !window->GetProperty(kShelfIDKey) && | |
50 !wm::GetWindowState(window)->ignored_by_shelf()) { | |
51 static int id = 0; | |
52 const ash::ShelfID shelf_id(base::IntToString(id++)); | |
53 window->SetProperty(kShelfIDKey, new std::string(shelf_id.Serialize())); | |
54 return shelf_id; | |
55 } | |
56 return ShelfID::Deserialize(window->GetProperty(kShelfIDKey)); | 38 return ShelfID::Deserialize(window->GetProperty(kShelfIDKey)); |
57 } | 39 } |
58 | 40 |
59 // Update the ShelfItem from relevant window properties. | 41 // Update the ShelfItem from relevant window properties. |
60 void UpdateShelfItemForWindow(ShelfItem* item, aura::Window* window) { | 42 void UpdateShelfItemForWindow(ShelfItem* item, aura::Window* window) { |
| 43 DCHECK(item->id.IsNull() || item->id == GetShelfID(window)); |
61 item->id = GetShelfID(window); | 44 item->id = GetShelfID(window); |
62 item->type = GetShelfItemType(window); | 45 item->type = GetShelfItemType(window); |
63 | 46 |
64 item->status = STATUS_RUNNING; | 47 item->status = STATUS_RUNNING; |
65 if (wm::IsActiveWindow(window)) | 48 if (wm::IsActiveWindow(window)) |
66 item->status = STATUS_ACTIVE; | 49 item->status = STATUS_ACTIVE; |
67 else if (window->GetProperty(aura::client::kDrawAttentionKey)) | 50 else if (window->GetProperty(aura::client::kDrawAttentionKey)) |
68 item->status = STATUS_ATTENTION; | 51 item->status = STATUS_ATTENTION; |
69 | 52 |
70 // Prefer app icons over window icons, they're typically larger. | 53 // Prefer app icons over window icons, they're typically larger. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( | 96 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( |
114 ShelfWindowWatcher* window_watcher) | 97 ShelfWindowWatcher* window_watcher) |
115 : window_watcher_(window_watcher) {} | 98 : window_watcher_(window_watcher) {} |
116 | 99 |
117 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} | 100 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} |
118 | 101 |
119 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( | 102 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( |
120 aura::Window* window, | 103 aura::Window* window, |
121 const void* key, | 104 const void* key, |
122 intptr_t old) { | 105 intptr_t old) { |
123 // ShelfIDs should never change except when replacing Mash temporary defaults. | |
124 // TODO(msw): Remove Mash default ShelfID handling. crbug.com/722496 | |
125 if (Shell::GetAshConfig() == Config::MASH && key == kShelfIDKey) { | |
126 ShelfID old_id = ShelfID::Deserialize(reinterpret_cast<std::string*>(old)); | |
127 ShelfID new_id = ShelfID::Deserialize(window->GetProperty(kShelfIDKey)); | |
128 if (old_id != new_id && !old_id.IsNull() && !new_id.IsNull()) { | |
129 // Id changing is not supported; remove the item and it will be re-added. | |
130 window_watcher_->user_windows_with_items_.erase(window); | |
131 const int index = window_watcher_->model_->ItemIndexByID(old_id); | |
132 window_watcher_->model_->RemoveItemAt(index); | |
133 } | |
134 } | |
135 | |
136 if (key == aura::client::kAppIconKey || key == aura::client::kWindowIconKey || | 106 if (key == aura::client::kAppIconKey || key == aura::client::kWindowIconKey || |
137 key == aura::client::kDrawAttentionKey || key == kPanelAttachedKey || | 107 key == aura::client::kDrawAttentionKey || key == kPanelAttachedKey || |
138 key == kShelfItemTypeKey || key == kShelfIDKey) { | 108 key == kShelfItemTypeKey || key == kShelfIDKey) { |
139 window_watcher_->OnUserWindowPropertyChanged(window); | 109 window_watcher_->OnUserWindowPropertyChanged(window); |
140 } | 110 } |
141 } | 111 } |
142 | 112 |
143 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( | 113 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( |
144 aura::Window* window) { | 114 aura::Window* window) { |
145 window_watcher_->OnUserWindowDestroying(window); | 115 window_watcher_->OnUserWindowDestroying(window); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 ShelfWindowWatcher::~ShelfWindowWatcher() { | 149 ShelfWindowWatcher::~ShelfWindowWatcher() { |
180 display::Screen::GetScreen()->RemoveObserver(this); | 150 display::Screen::GetScreen()->RemoveObserver(this); |
181 Shell::Get()->activation_client()->RemoveObserver(this); | 151 Shell::Get()->activation_client()->RemoveObserver(this); |
182 } | 152 } |
183 | 153 |
184 void ShelfWindowWatcher::AddShelfItem(aura::Window* window) { | 154 void ShelfWindowWatcher::AddShelfItem(aura::Window* window) { |
185 user_windows_with_items_.insert(window); | 155 user_windows_with_items_.insert(window); |
186 ShelfItem item; | 156 ShelfItem item; |
187 UpdateShelfItemForWindow(&item, window); | 157 UpdateShelfItemForWindow(&item, window); |
188 | 158 |
189 // ShelfWindowWatcher[ItemDelegate] doesn't support multiple windows per item, | |
190 // but this can happen in Mash (eg. when multiple browser windows are open). | |
191 // Assign a unique launch id in this case to avoid crashing on DCHECKs. | |
192 // TODO(msw): Remove Mash duplicate ShelfID handling. crbug.com/722496 | |
193 if (Shell::GetAshConfig() == Config::MASH && | |
194 model_->ItemIndexByID(item.id) > 0) { | |
195 static int id = 0; | |
196 item.id.launch_id = base::IntToString(id++); | |
197 } | |
198 | |
199 model_->SetShelfItemDelegate( | 159 model_->SetShelfItemDelegate( |
200 item.id, | 160 item.id, |
201 base::MakeUnique<ShelfWindowWatcherItemDelegate>(item.id, window)); | 161 base::MakeUnique<ShelfWindowWatcherItemDelegate>(item.id, window)); |
| 162 |
202 // Panels are inserted on the left so as not to push all existing panels over. | 163 // Panels are inserted on the left so as not to push all existing panels over. |
203 model_->AddAt(item.type == TYPE_APP_PANEL ? 0 : model_->item_count(), item); | 164 model_->AddAt(item.type == TYPE_APP_PANEL ? 0 : model_->item_count(), item); |
204 } | 165 } |
205 | 166 |
206 void ShelfWindowWatcher::RemoveShelfItem(aura::Window* window) { | 167 void ShelfWindowWatcher::RemoveShelfItem(aura::Window* window) { |
207 user_windows_with_items_.erase(window); | 168 user_windows_with_items_.erase(window); |
208 const ShelfID shelf_id = GetShelfID(window); | 169 const ShelfID shelf_id = GetShelfID(window); |
209 DCHECK(!shelf_id.IsNull()); | 170 DCHECK(!shelf_id.IsNull()); |
210 const int index = model_->ItemIndexByID(shelf_id); | 171 const int index = model_->ItemIndexByID(shelf_id); |
211 DCHECK_GE(index, 0); | 172 DCHECK_GE(index, 0); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 } | 255 } |
295 } | 256 } |
296 | 257 |
297 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { | 258 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { |
298 } | 259 } |
299 | 260 |
300 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, | 261 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, |
301 uint32_t) {} | 262 uint32_t) {} |
302 | 263 |
303 } // namespace ash | 264 } // namespace ash |
OLD | NEW |