| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/wm/tablet_mode/tablet_mode_window_manager.h" | |
| 6 | |
| 7 #include "ash/ash_switches.h" | |
| 8 #include "ash/public/cpp/shell_window_ids.h" | |
| 9 #include "ash/root_window_controller.h" | |
| 10 #include "ash/session/session_state_delegate.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/shell_port.h" | |
| 13 #include "ash/wm/mru_window_tracker.h" | |
| 14 #include "ash/wm/overview/window_selector_controller.h" | |
| 15 #include "ash/wm/tablet_mode/tablet_mode_backdrop_delegate_impl.h" | |
| 16 #include "ash/wm/tablet_mode/tablet_mode_event_handler.h" | |
| 17 #include "ash/wm/tablet_mode/tablet_mode_window_state.h" | |
| 18 #include "ash/wm/window_state.h" | |
| 19 #include "ash/wm/wm_event.h" | |
| 20 #include "ash/wm/workspace_controller.h" | |
| 21 #include "base/command_line.h" | |
| 22 #include "base/memory/ptr_util.h" | |
| 23 #include "base/stl_util.h" | |
| 24 #include "ui/aura/client/aura_constants.h" | |
| 25 #include "ui/display/screen.h" | |
| 26 | |
| 27 namespace ash { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // Exits overview mode if it is currently active. | |
| 32 void CancelOverview() { | |
| 33 WindowSelectorController* controller = | |
| 34 Shell::Get()->window_selector_controller(); | |
| 35 if (controller->IsSelecting()) | |
| 36 controller->OnSelectionEnded(); | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 TabletModeWindowManager::~TabletModeWindowManager() { | |
| 42 // Overview mode needs to be ended before exiting tablet mode to prevent | |
| 43 // transforming windows which are currently in | |
| 44 // overview: http://crbug.com/366605 | |
| 45 CancelOverview(); | |
| 46 for (aura::Window* window : added_windows_) | |
| 47 window->RemoveObserver(this); | |
| 48 added_windows_.clear(); | |
| 49 Shell::Get()->RemoveShellObserver(this); | |
| 50 display::Screen::GetScreen()->RemoveObserver(this); | |
| 51 EnableBackdropBehindTopWindowOnEachDisplay(false); | |
| 52 RemoveWindowCreationObservers(); | |
| 53 RestoreAllWindows(); | |
| 54 } | |
| 55 | |
| 56 int TabletModeWindowManager::GetNumberOfManagedWindows() { | |
| 57 return window_state_map_.size(); | |
| 58 } | |
| 59 | |
| 60 void TabletModeWindowManager::AddWindow(aura::Window* window) { | |
| 61 // Only add the window if it is a direct dependent of a container window | |
| 62 // and not yet tracked. | |
| 63 if (!ShouldHandleWindow(window) || | |
| 64 base::ContainsKey(window_state_map_, window) || | |
| 65 !IsContainerWindow(window->parent())) { | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 MaximizeAndTrackWindow(window); | |
| 70 } | |
| 71 | |
| 72 void TabletModeWindowManager::WindowStateDestroyed(aura::Window* window) { | |
| 73 // At this time ForgetWindow() should already have been called. If not, | |
| 74 // someone else must have replaced the "window manager's state object". | |
| 75 DCHECK(!window->HasObserver(this)); | |
| 76 | |
| 77 auto it = window_state_map_.find(window); | |
| 78 DCHECK(it != window_state_map_.end()); | |
| 79 window_state_map_.erase(it); | |
| 80 } | |
| 81 | |
| 82 void TabletModeWindowManager::OnOverviewModeStarting() { | |
| 83 SetDeferBoundsUpdates(true); | |
| 84 } | |
| 85 | |
| 86 void TabletModeWindowManager::OnOverviewModeEnded() { | |
| 87 SetDeferBoundsUpdates(false); | |
| 88 } | |
| 89 | |
| 90 void TabletModeWindowManager::OnWindowDestroying(aura::Window* window) { | |
| 91 if (IsContainerWindow(window)) { | |
| 92 // container window can be removed on display destruction. | |
| 93 window->RemoveObserver(this); | |
| 94 observed_container_windows_.erase(window); | |
| 95 } else if (base::ContainsValue(added_windows_, window)) { | |
| 96 // Added window was destroyed before being shown. | |
| 97 added_windows_.erase(window); | |
| 98 window->RemoveObserver(this); | |
| 99 } else { | |
| 100 // If a known window gets destroyed we need to remove all knowledge about | |
| 101 // it. | |
| 102 ForgetWindow(window); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void TabletModeWindowManager::OnWindowHierarchyChanged( | |
| 107 const HierarchyChangeParams& params) { | |
| 108 // A window can get removed and then re-added by a drag and drop operation. | |
| 109 if (params.new_parent && IsContainerWindow(params.new_parent) && | |
| 110 !base::ContainsKey(window_state_map_, params.target)) { | |
| 111 // Don't register the window if the window is invisible. Instead, | |
| 112 // wait until it becomes visible because the client may update the | |
| 113 // flag to control if the window should be added. | |
| 114 if (!params.target->IsVisible()) { | |
| 115 if (!base::ContainsValue(added_windows_, params.target)) { | |
| 116 added_windows_.insert(params.target); | |
| 117 params.target->AddObserver(this); | |
| 118 } | |
| 119 return; | |
| 120 } | |
| 121 MaximizeAndTrackWindow(params.target); | |
| 122 // When the state got added, the "WM_EVENT_ADDED_TO_WORKSPACE" event got | |
| 123 // already sent and we have to notify our state again. | |
| 124 if (base::ContainsKey(window_state_map_, params.target)) { | |
| 125 wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE); | |
| 126 wm::GetWindowState(params.target)->OnWMEvent(&event); | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 void TabletModeWindowManager::OnWindowPropertyChanged(aura::Window* window, | |
| 132 const void* key, | |
| 133 intptr_t old) { | |
| 134 // Stop managing |window| if the always-on-top property is added. | |
| 135 if (key == aura::client::kAlwaysOnTopKey && | |
| 136 window->GetProperty(aura::client::kAlwaysOnTopKey)) { | |
| 137 ForgetWindow(window); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 void TabletModeWindowManager::OnWindowBoundsChanged( | |
| 142 aura::Window* window, | |
| 143 const gfx::Rect& old_bounds, | |
| 144 const gfx::Rect& new_bounds) { | |
| 145 if (!IsContainerWindow(window)) | |
| 146 return; | |
| 147 // Reposition all non maximizeable windows. | |
| 148 for (auto& pair : window_state_map_) | |
| 149 pair.second->UpdateWindowPosition(wm::GetWindowState(pair.first)); | |
| 150 } | |
| 151 | |
| 152 void TabletModeWindowManager::OnWindowVisibilityChanged(aura::Window* window, | |
| 153 bool visible) { | |
| 154 // Skip if it's already managed. | |
| 155 if (base::ContainsKey(window_state_map_, window)) | |
| 156 return; | |
| 157 | |
| 158 if (IsContainerWindow(window->parent()) && | |
| 159 base::ContainsValue(added_windows_, window) && visible) { | |
| 160 added_windows_.erase(window); | |
| 161 window->RemoveObserver(this); | |
| 162 MaximizeAndTrackWindow(window); | |
| 163 // When the state got added, the "WM_EVENT_ADDED_TO_WORKSPACE" event got | |
| 164 // already sent and we have to notify our state again. | |
| 165 if (base::ContainsKey(window_state_map_, window)) { | |
| 166 wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE); | |
| 167 wm::GetWindowState(window)->OnWMEvent(&event); | |
| 168 } | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void TabletModeWindowManager::OnDisplayAdded(const display::Display& display) { | |
| 173 DisplayConfigurationChanged(); | |
| 174 } | |
| 175 | |
| 176 void TabletModeWindowManager::OnDisplayRemoved( | |
| 177 const display::Display& display) { | |
| 178 DisplayConfigurationChanged(); | |
| 179 } | |
| 180 | |
| 181 void TabletModeWindowManager::OnDisplayMetricsChanged(const display::Display&, | |
| 182 uint32_t) { | |
| 183 // Nothing to do here. | |
| 184 } | |
| 185 | |
| 186 void TabletModeWindowManager::SetIgnoreWmEventsForExit() { | |
| 187 for (auto& pair : window_state_map_) { | |
| 188 pair.second->set_ignore_wm_events(true); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 TabletModeWindowManager::TabletModeWindowManager() { | |
| 193 // The overview mode needs to be ended before the tablet mode is started. To | |
| 194 // guarantee the proper order, it will be turned off from here. | |
| 195 CancelOverview(); | |
| 196 | |
| 197 MaximizeAllWindows(); | |
| 198 AddWindowCreationObservers(); | |
| 199 EnableBackdropBehindTopWindowOnEachDisplay(true); | |
| 200 display::Screen::GetScreen()->AddObserver(this); | |
| 201 Shell::Get()->AddShellObserver(this); | |
| 202 event_handler_ = ShellPort::Get()->CreateTabletModeEventHandler(); | |
| 203 } | |
| 204 | |
| 205 void TabletModeWindowManager::MaximizeAllWindows() { | |
| 206 MruWindowTracker::WindowList windows = | |
| 207 Shell::Get()->mru_window_tracker()->BuildWindowListIgnoreModal(); | |
| 208 // Add all existing MRU windows. | |
| 209 for (auto* window : windows) | |
| 210 MaximizeAndTrackWindow(window); | |
| 211 } | |
| 212 | |
| 213 void TabletModeWindowManager::RestoreAllWindows() { | |
| 214 while (window_state_map_.size()) | |
| 215 ForgetWindow(window_state_map_.begin()->first); | |
| 216 } | |
| 217 | |
| 218 void TabletModeWindowManager::SetDeferBoundsUpdates(bool defer_bounds_updates) { | |
| 219 for (auto& pair : window_state_map_) | |
| 220 pair.second->SetDeferBoundsUpdates(defer_bounds_updates); | |
| 221 } | |
| 222 | |
| 223 void TabletModeWindowManager::MaximizeAndTrackWindow(aura::Window* window) { | |
| 224 if (!ShouldHandleWindow(window)) | |
| 225 return; | |
| 226 | |
| 227 DCHECK(!base::ContainsKey(window_state_map_, window)); | |
| 228 window->AddObserver(this); | |
| 229 | |
| 230 // We create and remember a tablet mode state which will attach itself to | |
| 231 // the provided state object. | |
| 232 window_state_map_[window] = new TabletModeWindowState(window, this); | |
| 233 } | |
| 234 | |
| 235 void TabletModeWindowManager::ForgetWindow(aura::Window* window) { | |
| 236 WindowToState::iterator it = window_state_map_.find(window); | |
| 237 | |
| 238 // The following DCHECK could fail if our window state object was destroyed | |
| 239 // earlier by someone else. However - at this point there is no other client | |
| 240 // which replaces the state object and therefore this should not happen. | |
| 241 DCHECK(it != window_state_map_.end()); | |
| 242 window->RemoveObserver(this); | |
| 243 | |
| 244 // By telling the state object to revert, it will switch back the old | |
| 245 // State object and destroy itself, calling WindowStateDestroyed(). | |
| 246 it->second->LeaveTabletMode(wm::GetWindowState(it->first)); | |
| 247 DCHECK(!base::ContainsKey(window_state_map_, window)); | |
| 248 } | |
| 249 | |
| 250 bool TabletModeWindowManager::ShouldHandleWindow(aura::Window* window) { | |
| 251 DCHECK(window); | |
| 252 | |
| 253 // Windows with the always-on-top property should be free-floating and thus | |
| 254 // not managed by us. | |
| 255 if (window->GetProperty(aura::client::kAlwaysOnTopKey)) | |
| 256 return false; | |
| 257 | |
| 258 // If the changing bounds in the maximized/fullscreen is allowed, then | |
| 259 // let the client manage it even in tablet mode. | |
| 260 if (wm::GetWindowState(window)->allow_set_bounds_direct()) | |
| 261 return false; | |
| 262 | |
| 263 return window->type() == aura::client::WINDOW_TYPE_NORMAL; | |
| 264 } | |
| 265 | |
| 266 void TabletModeWindowManager::AddWindowCreationObservers() { | |
| 267 DCHECK(observed_container_windows_.empty()); | |
| 268 // Observe window activations/creations in the default containers on all root | |
| 269 // windows. | |
| 270 for (aura::Window* root : Shell::GetAllRootWindows()) { | |
| 271 aura::Window* default_container = | |
| 272 root->GetChildById(kShellWindowId_DefaultContainer); | |
| 273 DCHECK(!base::ContainsKey(observed_container_windows_, default_container)); | |
| 274 default_container->AddObserver(this); | |
| 275 observed_container_windows_.insert(default_container); | |
| 276 } | |
| 277 } | |
| 278 | |
| 279 void TabletModeWindowManager::RemoveWindowCreationObservers() { | |
| 280 for (aura::Window* window : observed_container_windows_) | |
| 281 window->RemoveObserver(this); | |
| 282 observed_container_windows_.clear(); | |
| 283 } | |
| 284 | |
| 285 void TabletModeWindowManager::DisplayConfigurationChanged() { | |
| 286 EnableBackdropBehindTopWindowOnEachDisplay(false); | |
| 287 RemoveWindowCreationObservers(); | |
| 288 AddWindowCreationObservers(); | |
| 289 EnableBackdropBehindTopWindowOnEachDisplay(true); | |
| 290 } | |
| 291 | |
| 292 bool TabletModeWindowManager::IsContainerWindow(aura::Window* window) { | |
| 293 return base::ContainsKey(observed_container_windows_, window); | |
| 294 } | |
| 295 | |
| 296 void TabletModeWindowManager::EnableBackdropBehindTopWindowOnEachDisplay( | |
| 297 bool enable) { | |
| 298 // Inform the WorkspaceLayoutManager that we want to show a backdrop behind | |
| 299 // the topmost window of its container. | |
| 300 for (auto* controller : Shell::GetAllRootWindowControllers()) { | |
| 301 controller->workspace_controller()->SetBackdropDelegate( | |
| 302 enable ? base::MakeUnique<TabletModeBackdropDelegateImpl>() : nullptr); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 } // namespace ash | |
| OLD | NEW |