| 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/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/wm/window_state.h" | 13 #include "ash/common/wm/window_state.h" |
| 14 #include "ash/common/wm_shell.h" | 14 #include "ash/common/wm_shell.h" |
| 15 #include "ash/common/wm_window.h" | 15 #include "ash/common/wm_window.h" |
| 16 #include "ash/common/wm_window_property.h" | |
| 17 #include "ash/public/cpp/shell_window_ids.h" | 16 #include "ash/public/cpp/shell_window_ids.h" |
| 17 #include "ash/public/cpp/window_properties.h" |
| 18 #include "ash/shell.h" |
| 19 #include "ash/wm/window_properties.h" |
| 20 #include "ash/wm/window_state_aura.h" |
| 21 #include "ash/wm/window_util.h" |
| 22 #include "ui/aura/client/aura_constants.h" |
| 23 #include "ui/aura/env.h" |
| 24 #include "ui/aura/window.h" |
| 18 #include "ui/display/display.h" | 25 #include "ui/display/display.h" |
| 19 #include "ui/display/screen.h" | 26 #include "ui/display/screen.h" |
| 27 #include "ui/wm/public/activation_client.h" |
| 20 | 28 |
| 21 namespace ash { | 29 namespace ash { |
| 22 namespace { | 30 namespace { |
| 23 | 31 |
| 32 // Returns the shelf item type, with special temporary behavior for Mash: |
| 33 // Mash provides a default shelf item type (TYPE_APP) for non-ignored windows. |
| 34 ShelfItemType GetShelfItemType(aura::Window* window) { |
| 35 if (aura::Env::GetInstance()->mode() == aura::Env::Mode::LOCAL || |
| 36 window->GetProperty(kShelfItemTypeKey) != TYPE_UNDEFINED) { |
| 37 return static_cast<ShelfItemType>(window->GetProperty(kShelfItemTypeKey)); |
| 38 } |
| 39 return wm::GetWindowState(window)->ignored_by_shelf() ? TYPE_UNDEFINED |
| 40 : TYPE_APP; |
| 41 } |
| 42 |
| 24 // Update the ShelfItem from relevant window properties. | 43 // Update the ShelfItem from relevant window properties. |
| 25 void UpdateShelfItemForWindow(ShelfItem* item, WmWindow* window) { | 44 void UpdateShelfItemForWindow(ShelfItem* item, aura::Window* window) { |
| 26 item->type = static_cast<ShelfItemType>( | 45 item->type = GetShelfItemType(window); |
| 27 window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE)); | |
| 28 | 46 |
| 29 item->status = STATUS_RUNNING; | 47 item->status = STATUS_RUNNING; |
| 30 if (window->IsActive()) | 48 if (wm::IsActiveWindow(window)) |
| 31 item->status = STATUS_ACTIVE; | 49 item->status = STATUS_ACTIVE; |
| 32 else if (window->GetBoolProperty(WmWindowProperty::DRAW_ATTENTION)) | 50 else if (window->GetProperty(aura::client::kDrawAttentionKey)) |
| 33 item->status = STATUS_ATTENTION; | 51 item->status = STATUS_ATTENTION; |
| 34 | 52 |
| 35 item->app_id = window->GetStringProperty(WmWindowProperty::APP_ID); | 53 const std::string* app_id = window->GetProperty(aura::client::kAppIdKey); |
| 54 item->app_id = app_id ? *app_id : std::string(); |
| 36 | 55 |
| 37 // Prefer app icons over window icons, they're typically larger. | 56 // Prefer app icons over window icons, they're typically larger. |
| 38 item->image = window->GetAppIcon(); | 57 gfx::ImageSkia* image = window->GetProperty(aura::client::kAppIconKey); |
| 39 if (item->image.isNull()) | 58 if (!image || image->isNull()) |
| 40 item->image = window->GetWindowIcon(); | 59 image = window->GetProperty(aura::client::kWindowIconKey); |
| 60 item->image = image ? *image : gfx::ImageSkia(); |
| 41 | 61 |
| 42 item->title = window->GetTitle(); | 62 item->title = window->GetTitle(); |
| 43 | 63 |
| 44 // Do not show tooltips for visible attached app panel windows. | 64 // Do not show tooltips for visible attached app panel windows. |
| 45 item->shows_tooltip = | 65 item->shows_tooltip = item->type != TYPE_APP_PANEL || !window->IsVisible() || |
| 46 item->type != TYPE_APP_PANEL || !window->IsVisible() || | 66 !window->GetProperty(kPanelAttachedKey); |
| 47 !window->GetBoolProperty(WmWindowProperty::PANEL_ATTACHED); | |
| 48 } | 67 } |
| 49 | 68 |
| 50 } // namespace | 69 } // namespace |
| 51 | 70 |
| 52 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( | 71 ShelfWindowWatcher::ContainerWindowObserver::ContainerWindowObserver( |
| 53 ShelfWindowWatcher* window_watcher) | 72 ShelfWindowWatcher* window_watcher) |
| 54 : window_watcher_(window_watcher) {} | 73 : window_watcher_(window_watcher) {} |
| 55 | 74 |
| 56 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {} | 75 ShelfWindowWatcher::ContainerWindowObserver::~ContainerWindowObserver() {} |
| 57 | 76 |
| 58 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowTreeChanged( | 77 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowHierarchyChanged( |
| 59 WmWindow* window, | 78 const HierarchyChangeParams& params) { |
| 60 const TreeChangeParams& params) { | |
| 61 if (!params.old_parent && params.new_parent && | 79 if (!params.old_parent && params.new_parent && |
| 62 (params.new_parent->GetShellWindowId() == | 80 (params.new_parent->id() == kShellWindowId_DefaultContainer || |
| 63 kShellWindowId_DefaultContainer || | 81 params.new_parent->id() == kShellWindowId_PanelContainer)) { |
| 64 params.new_parent->GetShellWindowId() == | |
| 65 kShellWindowId_PanelContainer)) { | |
| 66 // A new window was created in the default container or the panel container. | 82 // A new window was created in the default container or the panel container. |
| 67 window_watcher_->OnUserWindowAdded(params.target); | 83 window_watcher_->OnUserWindowAdded(params.target); |
| 68 } | 84 } |
| 69 } | 85 } |
| 70 | 86 |
| 71 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowDestroying( | 87 void ShelfWindowWatcher::ContainerWindowObserver::OnWindowDestroying( |
| 72 WmWindow* window) { | 88 aura::Window* window) { |
| 73 window_watcher_->OnContainerWindowDestroying(window); | 89 window_watcher_->OnContainerWindowDestroying(window); |
| 74 } | 90 } |
| 75 | 91 |
| 76 //////////////////////////////////////////////////////////////////////////////// | 92 //////////////////////////////////////////////////////////////////////////////// |
| 77 | 93 |
| 78 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( | 94 ShelfWindowWatcher::UserWindowObserver::UserWindowObserver( |
| 79 ShelfWindowWatcher* window_watcher) | 95 ShelfWindowWatcher* window_watcher) |
| 80 : window_watcher_(window_watcher) {} | 96 : window_watcher_(window_watcher) {} |
| 81 | 97 |
| 82 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} | 98 ShelfWindowWatcher::UserWindowObserver::~UserWindowObserver() {} |
| 83 | 99 |
| 84 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( | 100 void ShelfWindowWatcher::UserWindowObserver::OnWindowPropertyChanged( |
| 85 WmWindow* window, | 101 aura::Window* window, |
| 86 WmWindowProperty property) { | 102 const void* key, |
| 87 if (property == WmWindowProperty::APP_ICON || | 103 intptr_t old) { |
| 88 property == WmWindowProperty::APP_ID || | 104 if (key == aura::client::kAppIconKey || key == aura::client::kAppIdKey || |
| 89 property == WmWindowProperty::DRAW_ATTENTION || | 105 key == aura::client::kDrawAttentionKey || |
| 90 property == WmWindowProperty::PANEL_ATTACHED || | 106 key == aura::client::kWindowIconKey || key == kPanelAttachedKey || |
| 91 property == WmWindowProperty::SHELF_ITEM_TYPE || | 107 key == kShelfItemTypeKey) { |
| 92 property == WmWindowProperty::WINDOW_ICON) { | |
| 93 window_watcher_->OnUserWindowPropertyChanged(window); | 108 window_watcher_->OnUserWindowPropertyChanged(window); |
| 94 } | 109 } |
| 95 } | 110 } |
| 96 | 111 |
| 97 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( | 112 void ShelfWindowWatcher::UserWindowObserver::OnWindowDestroying( |
| 98 WmWindow* window) { | 113 aura::Window* window) { |
| 99 window_watcher_->OnUserWindowDestroying(window); | 114 window_watcher_->OnUserWindowDestroying(window); |
| 100 } | 115 } |
| 101 | 116 |
| 102 void ShelfWindowWatcher::UserWindowObserver::OnWindowVisibilityChanged( | 117 void ShelfWindowWatcher::UserWindowObserver::OnWindowVisibilityChanged( |
| 103 WmWindow* window, | 118 aura::Window* window, |
| 104 bool visible) { | 119 bool visible) { |
| 105 // OnWindowVisibilityChanged() is called for descendants too. We only care | 120 // OnWindowVisibilityChanged() is called for descendants too. We only care |
| 106 // about changes to the visibility of windows we know about. | 121 // about changes to the visibility of windows we know about. |
| 107 if (!window_watcher_->observed_user_windows_.IsObserving(window)) | 122 if (!window_watcher_->observed_user_windows_.IsObserving(window)) |
| 108 return; | 123 return; |
| 109 | 124 |
| 110 // The tooltip behavior for panel windows depends on the panel visibility. | 125 // The tooltip behavior for panel windows depends on the panel visibility. |
| 111 window_watcher_->OnUserWindowPropertyChanged(window); | 126 window_watcher_->OnUserWindowPropertyChanged(window); |
| 112 } | 127 } |
| 113 | 128 |
| 114 void ShelfWindowWatcher::UserWindowObserver::OnWindowTitleChanged( | 129 void ShelfWindowWatcher::UserWindowObserver::OnWindowTitleChanged( |
| 115 WmWindow* window) { | 130 aura::Window* window) { |
| 116 window_watcher_->OnUserWindowPropertyChanged(window); | 131 window_watcher_->OnUserWindowPropertyChanged(window); |
| 117 } | 132 } |
| 118 | 133 |
| 119 //////////////////////////////////////////////////////////////////////////////// | 134 //////////////////////////////////////////////////////////////////////////////// |
| 120 | 135 |
| 121 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) | 136 ShelfWindowWatcher::ShelfWindowWatcher(ShelfModel* model) |
| 122 : model_(model), | 137 : model_(model), |
| 123 container_window_observer_(this), | 138 container_window_observer_(this), |
| 124 user_window_observer_(this), | 139 user_window_observer_(this), |
| 125 observed_container_windows_(&container_window_observer_), | 140 observed_container_windows_(&container_window_observer_), |
| 126 observed_user_windows_(&user_window_observer_) { | 141 observed_user_windows_(&user_window_observer_) { |
| 127 WmShell::Get()->AddActivationObserver(this); | 142 Shell::GetInstance()->activation_client()->AddObserver(this); |
| 128 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) | 143 for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) |
| 129 OnDisplayAdded(display); | 144 OnDisplayAdded(display); |
| 130 display::Screen::GetScreen()->AddObserver(this); | 145 display::Screen::GetScreen()->AddObserver(this); |
| 131 } | 146 } |
| 132 | 147 |
| 133 ShelfWindowWatcher::~ShelfWindowWatcher() { | 148 ShelfWindowWatcher::~ShelfWindowWatcher() { |
| 134 display::Screen::GetScreen()->RemoveObserver(this); | 149 display::Screen::GetScreen()->RemoveObserver(this); |
| 135 WmShell::Get()->RemoveActivationObserver(this); | 150 Shell::GetInstance()->activation_client()->RemoveObserver(this); |
| 136 } | 151 } |
| 137 | 152 |
| 138 void ShelfWindowWatcher::AddShelfItem(WmWindow* window) { | 153 void ShelfWindowWatcher::AddShelfItem(aura::Window* window) { |
| 139 user_windows_with_items_.insert(window); | 154 user_windows_with_items_.insert(window); |
| 140 ShelfItem item; | 155 ShelfItem item; |
| 141 ShelfID id = model_->next_id(); | 156 ShelfID id = model_->next_id(); |
| 142 UpdateShelfItemForWindow(&item, window); | 157 UpdateShelfItemForWindow(&item, window); |
| 143 window->SetIntProperty(WmWindowProperty::SHELF_ID, id); | 158 window->SetProperty(kShelfIDKey, id); |
| 144 std::unique_ptr<ShelfItemDelegate> item_delegate( | 159 std::unique_ptr<ShelfItemDelegate> item_delegate( |
| 145 new ShelfWindowWatcherItemDelegate(id, window)); | 160 new ShelfWindowWatcherItemDelegate(id, WmWindow::Get(window))); |
| 146 model_->SetShelfItemDelegate(id, std::move(item_delegate)); | 161 model_->SetShelfItemDelegate(id, std::move(item_delegate)); |
| 147 // Panels are inserted on the left so as not to push all existing panels over. | 162 // Panels are inserted on the left so as not to push all existing panels over. |
| 148 model_->AddAt(item.type == TYPE_APP_PANEL ? 0 : model_->item_count(), item); | 163 model_->AddAt(item.type == TYPE_APP_PANEL ? 0 : model_->item_count(), item); |
| 149 } | 164 } |
| 150 | 165 |
| 151 void ShelfWindowWatcher::RemoveShelfItem(WmWindow* window) { | 166 void ShelfWindowWatcher::RemoveShelfItem(aura::Window* window) { |
| 152 user_windows_with_items_.erase(window); | 167 user_windows_with_items_.erase(window); |
| 153 int shelf_id = window->GetIntProperty(WmWindowProperty::SHELF_ID); | 168 int shelf_id = window->GetProperty(kShelfIDKey); |
| 154 DCHECK_NE(shelf_id, kInvalidShelfID); | 169 DCHECK_NE(shelf_id, kInvalidShelfID); |
| 155 int index = model_->ItemIndexByID(shelf_id); | 170 int index = model_->ItemIndexByID(shelf_id); |
| 156 DCHECK_GE(index, 0); | 171 DCHECK_GE(index, 0); |
| 157 model_->RemoveItemAt(index); | 172 model_->RemoveItemAt(index); |
| 158 window->SetIntProperty(WmWindowProperty::SHELF_ID, kInvalidShelfID); | 173 window->SetProperty(kShelfIDKey, kInvalidShelfID); |
| 159 } | 174 } |
| 160 | 175 |
| 161 void ShelfWindowWatcher::OnContainerWindowDestroying(WmWindow* container) { | 176 void ShelfWindowWatcher::OnContainerWindowDestroying(aura::Window* container) { |
| 162 observed_container_windows_.Remove(container); | 177 observed_container_windows_.Remove(container); |
| 163 } | 178 } |
| 164 | 179 |
| 165 int ShelfWindowWatcher::GetShelfItemIndexForWindow(WmWindow* window) const { | 180 int ShelfWindowWatcher::GetShelfItemIndexForWindow(aura::Window* window) const { |
| 166 return model_->ItemIndexByID( | 181 return model_->ItemIndexByID(window->GetProperty(kShelfIDKey)); |
| 167 window->GetIntProperty(WmWindowProperty::SHELF_ID)); | |
| 168 } | 182 } |
| 169 | 183 |
| 170 void ShelfWindowWatcher::OnUserWindowAdded(WmWindow* window) { | 184 void ShelfWindowWatcher::OnUserWindowAdded(aura::Window* window) { |
| 171 // The window may already be tracked from a prior display or parent container. | 185 // The window may already be tracked from a prior display or parent container. |
| 172 if (observed_user_windows_.IsObserving(window)) | 186 if (observed_user_windows_.IsObserving(window)) |
| 173 return; | 187 return; |
| 174 | 188 |
| 175 observed_user_windows_.Add(window); | 189 observed_user_windows_.Add(window); |
| 176 | 190 |
| 177 // Add, update, or remove a ShelfItem for |window|, as needed. | 191 // Add, update, or remove a ShelfItem for |window|, as needed. |
| 178 OnUserWindowPropertyChanged(window); | 192 OnUserWindowPropertyChanged(window); |
| 179 } | 193 } |
| 180 | 194 |
| 181 void ShelfWindowWatcher::OnUserWindowDestroying(WmWindow* window) { | 195 void ShelfWindowWatcher::OnUserWindowDestroying(aura::Window* window) { |
| 182 if (observed_user_windows_.IsObserving(window)) | 196 if (observed_user_windows_.IsObserving(window)) |
| 183 observed_user_windows_.Remove(window); | 197 observed_user_windows_.Remove(window); |
| 184 | 198 |
| 185 if (user_windows_with_items_.count(window) > 0) | 199 if (user_windows_with_items_.count(window) > 0) |
| 186 RemoveShelfItem(window); | 200 RemoveShelfItem(window); |
| 187 DCHECK_EQ(0u, user_windows_with_items_.count(window)); | 201 DCHECK_EQ(0u, user_windows_with_items_.count(window)); |
| 188 } | 202 } |
| 189 | 203 |
| 190 void ShelfWindowWatcher::OnUserWindowPropertyChanged(WmWindow* window) { | 204 void ShelfWindowWatcher::OnUserWindowPropertyChanged(aura::Window* window) { |
| 191 if (window->GetIntProperty(WmWindowProperty::SHELF_ITEM_TYPE) == | 205 if (GetShelfItemType(window) == TYPE_UNDEFINED) { |
| 192 TYPE_UNDEFINED) { | |
| 193 // Remove |window|'s ShelfItem if it was added by this ShelfWindowWatcher. | 206 // Remove |window|'s ShelfItem if it was added by this ShelfWindowWatcher. |
| 194 if (user_windows_with_items_.count(window) > 0) | 207 if (user_windows_with_items_.count(window) > 0) |
| 195 RemoveShelfItem(window); | 208 RemoveShelfItem(window); |
| 196 return; | 209 return; |
| 197 } | 210 } |
| 198 | 211 |
| 199 // Update an existing ShelfItem for |window| when a property has changed. | 212 // Update an existing ShelfItem for |window| when a property has changed. |
| 200 int index = GetShelfItemIndexForWindow(window); | 213 int index = GetShelfItemIndexForWindow(window); |
| 201 if (index > 0) { | 214 if (index > 0) { |
| 202 ShelfItem item = model_->items()[index]; | 215 ShelfItem item = model_->items()[index]; |
| 203 UpdateShelfItemForWindow(&item, window); | 216 UpdateShelfItemForWindow(&item, window); |
| 204 model_->Set(index, item); | 217 model_->Set(index, item); |
| 205 return; | 218 return; |
| 206 } | 219 } |
| 207 | 220 |
| 208 // Creates a new ShelfItem for |window|. | 221 // Creates a new ShelfItem for |window|. |
| 209 AddShelfItem(window); | 222 AddShelfItem(window); |
| 210 } | 223 } |
| 211 | 224 |
| 212 void ShelfWindowWatcher::OnWindowActivated(WmWindow* gained_active, | 225 void ShelfWindowWatcher::OnWindowActivated(ActivationReason reason, |
| 213 WmWindow* lost_active) { | 226 aura::Window* gained_active, |
| 227 aura::Window* lost_active) { |
| 214 if (gained_active && user_windows_with_items_.count(gained_active) > 0) | 228 if (gained_active && user_windows_with_items_.count(gained_active) > 0) |
| 215 OnUserWindowPropertyChanged(gained_active); | 229 OnUserWindowPropertyChanged(gained_active); |
| 216 if (lost_active && user_windows_with_items_.count(lost_active) > 0) | 230 if (lost_active && user_windows_with_items_.count(lost_active) > 0) |
| 217 OnUserWindowPropertyChanged(lost_active); | 231 OnUserWindowPropertyChanged(lost_active); |
| 218 } | 232 } |
| 219 | 233 |
| 220 void ShelfWindowWatcher::OnDisplayAdded(const display::Display& new_display) { | 234 void ShelfWindowWatcher::OnDisplayAdded(const display::Display& new_display) { |
| 221 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(new_display.id()); | 235 WmWindow* root = WmShell::Get()->GetRootWindowForDisplayId(new_display.id()); |
| 236 aura::Window* aura_root = WmWindow::GetAuraWindow(root); |
| 222 | 237 |
| 223 // When the primary root window's display is removed, the existing root window | 238 // When the primary root window's display is removed, the existing root window |
| 224 // is taken over by the new display, and the observer is already set. | 239 // is taken over by the new display, and the observer is already set. |
| 225 WmWindow* default_container = | 240 aura::Window* default_container = |
| 226 root->GetChildByShellWindowId(kShellWindowId_DefaultContainer); | 241 aura_root->GetChildById(kShellWindowId_DefaultContainer); |
| 227 if (!observed_container_windows_.IsObserving(default_container)) { | 242 if (!observed_container_windows_.IsObserving(default_container)) { |
| 228 for (WmWindow* window : default_container->GetChildren()) | 243 for (aura::Window* window : default_container->children()) |
| 229 OnUserWindowAdded(window); | 244 OnUserWindowAdded(window); |
| 230 observed_container_windows_.Add(default_container); | 245 observed_container_windows_.Add(default_container); |
| 231 } | 246 } |
| 232 WmWindow* panel_container = | 247 aura::Window* panel_container = |
| 233 root->GetChildByShellWindowId(kShellWindowId_PanelContainer); | 248 aura_root->GetChildById(kShellWindowId_PanelContainer); |
| 234 if (!observed_container_windows_.IsObserving(panel_container)) { | 249 if (!observed_container_windows_.IsObserving(panel_container)) { |
| 235 for (WmWindow* window : panel_container->GetChildren()) | 250 for (aura::Window* window : panel_container->children()) |
| 236 OnUserWindowAdded(window); | 251 OnUserWindowAdded(window); |
| 237 observed_container_windows_.Add(panel_container); | 252 observed_container_windows_.Add(panel_container); |
| 238 } | 253 } |
| 239 } | 254 } |
| 240 | 255 |
| 241 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { | 256 void ShelfWindowWatcher::OnDisplayRemoved(const display::Display& old_display) { |
| 242 } | 257 } |
| 243 | 258 |
| 244 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, | 259 void ShelfWindowWatcher::OnDisplayMetricsChanged(const display::Display&, |
| 245 uint32_t) {} | 260 uint32_t) {} |
| 246 | 261 |
| 247 } // namespace ash | 262 } // namespace ash |
| OLD | NEW |