| 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" |
| 10 #include "ash/public/cpp/shelf_model.h" | 11 #include "ash/public/cpp/shelf_model.h" |
| 11 #include "ash/public/cpp/shell_window_ids.h" | 12 #include "ash/public/cpp/shell_window_ids.h" |
| 12 #include "ash/public/cpp/window_properties.h" | 13 #include "ash/public/cpp/window_properties.h" |
| 13 #include "ash/shelf/shelf_constants.h" | 14 #include "ash/shelf/shelf_constants.h" |
| 14 #include "ash/shelf/shelf_window_watcher_item_delegate.h" | 15 #include "ash/shelf/shelf_window_watcher_item_delegate.h" |
| 15 #include "ash/shell.h" | 16 #include "ash/shell.h" |
| 16 #include "ash/shell_port.h" | 17 #include "ash/shell_port.h" |
| 17 #include "ash/wm/window_state.h" | 18 #include "ash/wm/window_state.h" |
| 18 #include "ash/wm/window_util.h" | 19 #include "ash/wm/window_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_util.h" |
| 20 #include "ui/aura/client/aura_constants.h" | 21 #include "ui/aura/client/aura_constants.h" |
| 21 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/display/display.h" | 24 #include "ui/display/display.h" |
| 24 #include "ui/display/screen.h" | 25 #include "ui/display/screen.h" |
| 25 #include "ui/resources/grit/ui_resources.h" | 26 #include "ui/resources/grit/ui_resources.h" |
| 27 #include "ui/wm/core/transient_window_controller.h" |
| 26 #include "ui/wm/public/activation_client.h" | 28 #include "ui/wm/public/activation_client.h" |
| 27 | 29 |
| 28 namespace ash { | 30 namespace ash { |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 // Returns the window's shelf item type property value. | 33 // Returns the window's shelf item type property value. |
| 34 // Mash provides an initial default shelf item type for untyped windows. |
| 35 // TODO(msw): Extend this Mash behavior to all Ash configs. |
| 32 ShelfItemType GetShelfItemType(aura::Window* window) { | 36 ShelfItemType GetShelfItemType(aura::Window* window) { |
| 37 if (Shell::GetAshConfig() == Config::MASH && |
| 38 window->GetProperty(kShelfItemTypeKey) == TYPE_UNDEFINED && |
| 39 !wm::GetWindowState(window)->ignored_by_shelf()) { |
| 40 return TYPE_DIALOG; |
| 41 } |
| 33 return static_cast<ShelfItemType>(window->GetProperty(kShelfItemTypeKey)); | 42 return static_cast<ShelfItemType>(window->GetProperty(kShelfItemTypeKey)); |
| 34 } | 43 } |
| 35 | 44 |
| 36 // Returns the window's shelf id property value. | 45 // Returns the window's shelf id property value, or provides a default value. |
| 46 // Mash provides an initial default shelf id for unidentified windows. |
| 47 // TODO(msw): Extend this Mash behavior to all Ash configs. |
| 37 ShelfID GetShelfID(aura::Window* window) { | 48 ShelfID GetShelfID(aura::Window* window) { |
| 49 if (Shell::GetAshConfig() == Config::MASH && |
| 50 !window->GetProperty(kShelfIDKey) && |
| 51 !wm::GetWindowState(window)->ignored_by_shelf()) { |
| 52 static int id = 0; |
| 53 const ash::ShelfID shelf_id("ShelfWindowWatcher" + std::to_string(id++)); |
| 54 window->SetProperty(kShelfIDKey, new std::string(shelf_id.Serialize())); |
| 55 return shelf_id; |
| 56 } |
| 38 return ShelfID::Deserialize(window->GetProperty(kShelfIDKey)); | 57 return ShelfID::Deserialize(window->GetProperty(kShelfIDKey)); |
| 39 } | 58 } |
| 40 | 59 |
| 41 // Update the ShelfItem from relevant window properties. | 60 // Update the ShelfItem from relevant window properties. |
| 42 void UpdateShelfItemForWindow(ShelfItem* item, aura::Window* window) { | 61 void UpdateShelfItemForWindow(ShelfItem* item, aura::Window* window) { |
| 43 DCHECK(item->id.IsNull() || item->id == GetShelfID(window)); | 62 DCHECK(item->id.IsNull() || item->id == GetShelfID(window)); |
| 44 item->id = GetShelfID(window); | 63 item->id = GetShelfID(window); |
| 45 item->type = GetShelfItemType(window); | 64 item->type = GetShelfItemType(window); |
| 65 item->title = window->GetTitle(); |
| 46 | 66 |
| 47 item->status = STATUS_RUNNING; | 67 item->status = STATUS_RUNNING; |
| 48 if (wm::IsActiveWindow(window)) | 68 if (wm::IsActiveWindow(window)) |
| 49 item->status = STATUS_ACTIVE; | 69 item->status = STATUS_ACTIVE; |
| 50 else if (window->GetProperty(aura::client::kDrawAttentionKey)) | 70 else if (window->GetProperty(aura::client::kDrawAttentionKey)) |
| 51 item->status = STATUS_ATTENTION; | 71 item->status = STATUS_ATTENTION; |
| 52 | 72 |
| 53 // Prefer app icons over window icons, they're typically larger. | 73 // Prefer app icons over window icons, they're typically larger. |
| 54 gfx::ImageSkia* image = window->GetProperty(aura::client::kAppIconKey); | 74 gfx::ImageSkia* image = window->GetProperty(aura::client::kAppIconKey); |
| 55 if (!image || image->isNull()) | 75 if (!image || image->isNull()) |
| 56 image = window->GetProperty(aura::client::kWindowIconKey); | 76 image = window->GetProperty(aura::client::kWindowIconKey); |
| 57 if (!image || image->isNull()) { | 77 if (!image || image->isNull()) { |
| 58 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 78 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 59 item->image = rb.GetImageNamed(IDR_DEFAULT_FAVICON_32).AsImageSkia(); | 79 item->image = rb.GetImageNamed(IDR_DEFAULT_FAVICON_32).AsImageSkia(); |
| 60 } else { | 80 } else { |
| 61 item->image = *image; | 81 item->image = *image; |
| 62 } | 82 } |
| 63 | 83 |
| 64 item->title = window->GetTitle(); | |
| 65 | |
| 66 // Do not show tooltips for visible attached app panel windows. | 84 // Do not show tooltips for visible attached app panel windows. |
| 67 item->shows_tooltip = item->type != TYPE_APP_PANEL || !window->IsVisible() || | 85 item->shows_tooltip = item->type != TYPE_APP_PANEL || !window->IsVisible() || |
| 68 !window->GetProperty(kPanelAttachedKey); | 86 !window->GetProperty(kPanelAttachedKey); |
| 69 } | 87 } |
| 70 | 88 |
| 71 } // namespace | 89 } // namespace |
| 72 | 90 |
| 73 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( | 91 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( |
| 74 ShelfWindowWatcher* window_watcher) | 92 ShelfWindowWatcher* window_watcher) |
| 75 : window_watcher_(window_watcher) {} | 93 : window_watcher_(window_watcher) {} |
| (...skipping 20 matching lines...) Expand all Loading... |
| 96 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( | 114 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( |
| 97 ShelfWindowWatcher* window_watcher) | 115 ShelfWindowWatcher* window_watcher) |
| 98 : window_watcher_(window_watcher) {} | 116 : window_watcher_(window_watcher) {} |
| 99 | 117 |
| 100 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} | 118 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} |
| 101 | 119 |
| 102 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( | 120 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( |
| 103 aura::Window* window, | 121 aura::Window* window, |
| 104 const void* key, | 122 const void* key, |
| 105 intptr_t old) { | 123 intptr_t old) { |
| 124 // ShelfIDs should never change except when replacing Mash temporary defaults. |
| 125 // TODO(msw): Extend this Mash behavior to all Ash configs. |
| 126 if (Shell::GetAshConfig() == Config::MASH && key == kShelfIDKey) { |
| 127 ShelfID old_id = ShelfID::Deserialize(reinterpret_cast<std::string*>(old)); |
| 128 ShelfID new_id = ShelfID::Deserialize(window->GetProperty(kShelfIDKey)); |
| 129 if (old_id != new_id && !old_id.IsNull() && !new_id.IsNull() && |
| 130 window_watcher_->model_->ItemIndexByID(old_id) >= 0) { |
| 131 // Id changing is not supported; remove the item and it will be re-added. |
| 132 window_watcher_->user_windows_with_items_.erase(window); |
| 133 const int index = window_watcher_->model_->ItemIndexByID(old_id); |
| 134 window_watcher_->model_->RemoveItemAt(index); |
| 135 } |
| 136 } |
| 137 |
| 106 if (key == aura::client::kAppIconKey || key == aura::client::kWindowIconKey || | 138 if (key == aura::client::kAppIconKey || key == aura::client::kWindowIconKey || |
| 107 key == aura::client::kDrawAttentionKey || key == kPanelAttachedKey || | 139 key == aura::client::kDrawAttentionKey || key == kPanelAttachedKey || |
| 108 key == kShelfItemTypeKey || key == kShelfIDKey) { | 140 key == kShelfItemTypeKey || key == kShelfIDKey) { |
| 109 window_watcher_->OnUserWindowPropertyChanged(window); | 141 window_watcher_->OnUserWindowPropertyChanged(window); |
| 110 } | 142 } |
| 111 } | 143 } |
| 112 | 144 |
| 113 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( | 145 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( |
| 114 aura::Window* window) { | 146 aura::Window* window) { |
| 115 window_watcher_->OnUserWindowDestroying(window); | 147 window_watcher_->OnUserWindowDestroying(window); |
| 116 } | 148 } |
| 117 | 149 |
| 118 void ShelfWindowWatcher::UserWindowObserver::OnWindowVisibilityChanged( | 150 void ShelfWindowWatcher::UserWindowObserver::OnWindowVisibilityChanged( |
| 119 aura::Window* window, | 151 aura::Window* window, |
| 120 bool visible) { | 152 bool visible) { |
| 121 // OnWindowVisibilityChanged() is called for descendants too. We only care | 153 // This is also called for descendants; check that the window is observed. |
| 122 // about changes to the visibility of windows we know about. | 154 if (window_watcher_->observed_user_windows_.IsObserving(window)) |
| 123 if (!window_watcher_->observed_user_windows_.IsObserving(window)) | 155 window_watcher_->OnUserWindowPropertyChanged(window); |
| 124 return; | |
| 125 | |
| 126 // The tooltip behavior for panel windows depends on the panel visibility. | |
| 127 window_watcher_->OnUserWindowPropertyChanged(window); | |
| 128 } | 156 } |
| 129 | 157 |
| 130 void ShelfWindowWatcher::UserWindowObserver::OnWindowTitleChanged( | 158 void ShelfWindowWatcher::UserWindowObserver::OnWindowTitleChanged( |
| 131 aura::Window* window) { | 159 aura::Window* window) { |
| 132 window_watcher_->OnUserWindowPropertyChanged(window); | 160 window_watcher_->OnUserWindowPropertyChanged(window); |
| 133 } | 161 } |
| 134 | 162 |
| 135 //////////////////////////////////////////////////////////////////////////////// | 163 //////////////////////////////////////////////////////////////////////////////// |
| 136 | 164 |
| 137 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) | 165 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 DCHECK(!shelf_id.IsNull()); | 198 DCHECK(!shelf_id.IsNull()); |
| 171 const int index = model_->ItemIndexByID(shelf_id); | 199 const int index = model_->ItemIndexByID(shelf_id); |
| 172 DCHECK_GE(index, 0); | 200 DCHECK_GE(index, 0); |
| 173 model_->RemoveItemAt(index); | 201 model_->RemoveItemAt(index); |
| 174 } | 202 } |
| 175 | 203 |
| 176 void ShelfWindowWatcher::OnContainerWindowDestroying(aura::Window* container) { | 204 void ShelfWindowWatcher::OnContainerWindowDestroying(aura::Window* container) { |
| 177 observed_container_windows_.Remove(container); | 205 observed_container_windows_.Remove(container); |
| 178 } | 206 } |
| 179 | 207 |
| 180 int ShelfWindowWatcher::GetShelfItemIndexForWindow(aura::Window* window) const { | |
| 181 const ShelfID shelf_id = GetShelfID(window); | |
| 182 return shelf_id.IsNull() ? -1 : model_->ItemIndexByID(shelf_id); | |
| 183 } | |
| 184 | |
| 185 void ShelfWindowWatcher::OnUserWindowAdded(aura::Window* window) { | 208 void ShelfWindowWatcher::OnUserWindowAdded(aura::Window* window) { |
| 186 // The window may already be tracked from a prior display or parent container. | 209 // The window may already be tracked from a prior display or parent container. |
| 187 if (observed_user_windows_.IsObserving(window)) | 210 if (observed_user_windows_.IsObserving(window)) |
| 188 return; | 211 return; |
| 189 | 212 |
| 190 observed_user_windows_.Add(window); | 213 observed_user_windows_.Add(window); |
| 191 | 214 |
| 192 // Add, update, or remove a ShelfItem for |window|, as needed. | 215 // Add, update, or remove a ShelfItem for |window|, as needed. |
| 193 OnUserWindowPropertyChanged(window); | 216 OnUserWindowPropertyChanged(window); |
| 194 } | 217 } |
| 195 | 218 |
| 196 void ShelfWindowWatcher::OnUserWindowDestroying(aura::Window* window) { | 219 void ShelfWindowWatcher::OnUserWindowDestroying(aura::Window* window) { |
| 197 if (observed_user_windows_.IsObserving(window)) | 220 if (observed_user_windows_.IsObserving(window)) |
| 198 observed_user_windows_.Remove(window); | 221 observed_user_windows_.Remove(window); |
| 199 | 222 |
| 200 if (user_windows_with_items_.count(window) > 0) | 223 if (user_windows_with_items_.count(window) > 0) |
| 201 RemoveShelfItem(window); | 224 RemoveShelfItem(window); |
| 202 DCHECK_EQ(0u, user_windows_with_items_.count(window)); | 225 DCHECK_EQ(0u, user_windows_with_items_.count(window)); |
| 203 } | 226 } |
| 204 | 227 |
| 205 void ShelfWindowWatcher::OnUserWindowPropertyChanged(aura::Window* window) { | 228 void ShelfWindowWatcher::OnUserWindowPropertyChanged(aura::Window* window) { |
| 206 if (GetShelfItemType(window) == TYPE_UNDEFINED || | 229 if (GetShelfItemType(window) == TYPE_UNDEFINED || |
| 207 GetShelfID(window).IsNull()) { | 230 GetShelfID(window).IsNull() || |
| 208 // Remove |window|'s ShelfItem if it was added by this ShelfWindowWatcher. | 231 ::wm::TransientWindowController::Get()->GetTransientParent(window)) { |
| 232 // Remove |window|'s ShelfItem if it was added by ShelfWindowWatcher. |
| 209 if (user_windows_with_items_.count(window) > 0) | 233 if (user_windows_with_items_.count(window) > 0) |
| 210 RemoveShelfItem(window); | 234 RemoveShelfItem(window); |
| 211 return; | 235 return; |
| 212 } | 236 } |
| 213 | 237 |
| 214 // Update an existing ShelfItem for |window| when a property has changed. | 238 // Update an existing ShelfWindowWatcher item when a window property changes. |
| 215 int index = GetShelfItemIndexForWindow(window); | 239 int index = model_->ItemIndexByID(GetShelfID(window)); |
| 216 if (index > 0) { | 240 if (index > 0 && user_windows_with_items_.count(window) > 0) { |
| 217 ShelfItem item = model_->items()[index]; | 241 ShelfItem item = model_->items()[index]; |
| 218 UpdateShelfItemForWindow(&item, window); | 242 UpdateShelfItemForWindow(&item, window); |
| 219 model_->Set(index, item); | 243 model_->Set(index, item); |
| 220 return; | 244 return; |
| 221 } | 245 } |
| 222 | 246 |
| 223 // Creates a new ShelfItem for |window|. | 247 // Create a new ShelfWindowWatcher item for |window|. |
| 224 AddShelfItem(window); | 248 if (index < 0) |
| 249 AddShelfItem(window); |
| 225 } | 250 } |
| 226 | 251 |
| 227 void ShelfWindowWatcher::OnWindowActivated(ActivationReason reason, | 252 void ShelfWindowWatcher::OnWindowActivated(ActivationReason reason, |
| 228 aura::Window* gained_active, | 253 aura::Window* gained_active, |
| 229 aura::Window* lost_active) { | 254 aura::Window* lost_active) { |
| 230 if (gained_active && user_windows_with_items_.count(gained_active) > 0) | 255 if (gained_active && user_windows_with_items_.count(gained_active) > 0) |
| 231 OnUserWindowPropertyChanged(gained_active); | 256 OnUserWindowPropertyChanged(gained_active); |
| 232 if (lost_active && user_windows_with_items_.count(lost_active) > 0) | 257 if (lost_active && user_windows_with_items_.count(lost_active) > 0) |
| 233 OnUserWindowPropertyChanged(lost_active); | 258 OnUserWindowPropertyChanged(lost_active); |
| 234 } | 259 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 255 } | 280 } |
| 256 } | 281 } |
| 257 | 282 |
| 258 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { | 283 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { |
| 259 } | 284 } |
| 260 | 285 |
| 261 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, | 286 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, |
| 262 uint32_t) {} | 287 uint32_t) {} |
| 263 | 288 |
| 264 } // namespace ash | 289 } // namespace ash |
| OLD | NEW |