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