Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/wm/window_positioning_utils.h" | 5 #include "ash/common/wm/window_positioning_utils.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/common/wm/system_modal_container_layout_manager.h" | |
| 9 #include "ash/common/wm/window_state.h" | 10 #include "ash/common/wm/window_state.h" |
| 10 #include "ash/common/wm/wm_event.h" | 11 #include "ash/common/wm/wm_event.h" |
| 11 #include "ash/common/wm/wm_screen_util.h" | 12 #include "ash/common/wm/wm_screen_util.h" |
| 13 #include "ash/common/wm_lookup.h" | |
| 14 #include "ash/common/wm_root_window_controller.h" | |
| 15 #include "ash/common/wm_shell.h" | |
| 12 #include "ash/common/wm_window.h" | 16 #include "ash/common/wm_window.h" |
| 17 #include "ash/common/wm_window_tracker.h" | |
| 18 #include "ui/display/display.h" | |
| 13 #include "ui/gfx/geometry/rect.h" | 19 #include "ui/gfx/geometry/rect.h" |
| 14 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
| 15 | 21 |
| 16 namespace ash { | 22 namespace ash { |
| 17 namespace wm { | 23 namespace wm { |
| 18 | 24 |
| 19 namespace { | 25 namespace { |
| 20 | 26 |
| 21 // Returns the default width of a snapped window. | 27 // Returns the default width of a snapped window. |
| 22 int GetDefaultSnappedWindowWidth(WmWindow* window) { | 28 int GetDefaultSnappedWindowWidth(WmWindow* window) { |
| 23 const float kSnappedWidthWorkspaceRatio = 0.5f; | 29 const float kSnappedWidthWorkspaceRatio = 0.5f; |
| 24 | 30 |
| 25 int work_area_width = GetDisplayWorkAreaBoundsInParent(window).width(); | 31 int work_area_width = GetDisplayWorkAreaBoundsInParent(window).width(); |
| 26 int min_width = window->GetMinimumSize().width(); | 32 int min_width = window->GetMinimumSize().width(); |
| 27 int ideal_width = | 33 int ideal_width = |
| 28 static_cast<int>(work_area_width * kSnappedWidthWorkspaceRatio); | 34 static_cast<int>(work_area_width * kSnappedWidthWorkspaceRatio); |
| 29 return std::min(work_area_width, std::max(ideal_width, min_width)); | 35 return std::min(work_area_width, std::max(ideal_width, min_width)); |
| 30 } | 36 } |
| 31 | 37 |
| 38 // Return true if the window or its ancestor has |kStayInSameRootWindowkey| | |
| 39 // property. | |
| 40 bool ShouldStayInSameRootWindow(const WmWindow* window) { | |
| 41 return window && (window->IsLockedToRoot() || | |
| 42 ShouldStayInSameRootWindow(window->GetParent())); | |
| 43 } | |
| 44 | |
| 45 // Move all transient children to |dst_root|, including the ones in | |
| 46 // the child windows and transient children of the transient children. | |
| 47 void MoveAllTransientChildrenToNewRoot(const display::Display& display, | |
| 48 WmWindow* window) { | |
| 49 WmWindow* dst_root = WmLookup::Get() | |
| 50 ->GetRootWindowControllerWithDisplayId(display.id()) | |
| 51 ->GetWindow(); | |
| 52 for (WmWindow* transient_child : window->GetTransientChildren()) { | |
| 53 int container_id = transient_child->GetParent()->GetShellWindowId(); | |
| 54 DCHECK_GE(container_id, 0); | |
| 55 WmWindow* container = dst_root->GetChildByShellWindowId(container_id); | |
| 56 gfx::Rect parent_bounds_in_screen = transient_child->GetBoundsInScreen(); | |
|
James Cook
2016/09/15 16:31:40
Another naming nit/question: Does this have anythi
sky
2016/09/15 17:35:03
You're right. Updated.
| |
| 57 container->AddChild(transient_child); | |
| 58 transient_child->SetBoundsInScreen(parent_bounds_in_screen, display); | |
| 59 | |
| 60 // Transient children may have transient children. | |
| 61 MoveAllTransientChildrenToNewRoot(display, transient_child); | |
| 62 } | |
| 63 // Move transient children of the child windows if any. | |
| 64 for (WmWindow* child : window->GetChildren()) | |
| 65 MoveAllTransientChildrenToNewRoot(display, child); | |
| 66 } | |
| 67 | |
| 32 } // namespace | 68 } // namespace |
| 33 | 69 |
| 34 void AdjustBoundsSmallerThan(const gfx::Size& max_size, gfx::Rect* bounds) { | 70 void AdjustBoundsSmallerThan(const gfx::Size& max_size, gfx::Rect* bounds) { |
| 35 bounds->set_width(std::min(bounds->width(), max_size.width())); | 71 bounds->set_width(std::min(bounds->width(), max_size.width())); |
| 36 bounds->set_height(std::min(bounds->height(), max_size.height())); | 72 bounds->set_height(std::min(bounds->height(), max_size.height())); |
| 37 } | 73 } |
| 38 | 74 |
| 39 void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area, | 75 void AdjustBoundsToEnsureWindowVisibility(const gfx::Rect& visible_area, |
| 40 int min_width, | 76 int min_width, |
| 41 int min_height, | 77 int min_height, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 int width = GetDefaultSnappedWindowWidth(window); | 116 int width = GetDefaultSnappedWindowWidth(window); |
| 81 return gfx::Rect(work_area_in_parent.right() - width, work_area_in_parent.y(), | 117 return gfx::Rect(work_area_in_parent.right() - width, work_area_in_parent.y(), |
| 82 width, work_area_in_parent.height()); | 118 width, work_area_in_parent.height()); |
| 83 } | 119 } |
| 84 | 120 |
| 85 void CenterWindow(WmWindow* window) { | 121 void CenterWindow(WmWindow* window) { |
| 86 WMEvent event(WM_EVENT_CENTER); | 122 WMEvent event(WM_EVENT_CENTER); |
| 87 window->GetWindowState()->OnWMEvent(&event); | 123 window->GetWindowState()->OnWMEvent(&event); |
| 88 } | 124 } |
| 89 | 125 |
| 126 void SetBoundsInScreen(WmWindow* window, | |
| 127 const gfx::Rect& bounds, | |
|
James Cook
2016/09/15 16:31:40
How about |bounds_in_screen|? Coordinate systems h
sky
2016/09/15 17:35:03
Done.
| |
| 128 const display::Display& display) { | |
| 129 DCHECK_NE(display::Display::kInvalidDisplayID, display.id()); | |
| 130 // Don't move a window to other root window if: | |
| 131 // a) the window is a transient window. It moves when its | |
| 132 // transient parent moves. | |
| 133 // b) if the window or its ancestor has IsLockedToRoot(). It's intentionally | |
| 134 // kept in the same root window even if the bounds is outside of the | |
| 135 // display. | |
| 136 if (!window->GetTransientParent() && !ShouldStayInSameRootWindow(window)) { | |
| 137 WmRootWindowController* dst_root_window_controller = | |
| 138 WmLookup::Get()->GetRootWindowControllerWithDisplayId(display.id()); | |
| 139 DCHECK(dst_root_window_controller); | |
| 140 WmWindow* dst_root = dst_root_window_controller->GetWindow(); | |
| 141 DCHECK(dst_root); | |
| 142 WmWindow* dst_container = nullptr; | |
| 143 if (dst_root != window->GetRootWindow()) { | |
| 144 int container_id = window->GetParent()->GetShellWindowId(); | |
| 145 // All containers that uses screen coordinates must have valid window ids. | |
| 146 DCHECK_GE(container_id, 0); | |
| 147 // Don't move modal background. | |
| 148 if (!SystemModalContainerLayoutManager::IsModalBackground(window)) | |
| 149 dst_container = dst_root->GetChildByShellWindowId(container_id); | |
| 150 } | |
| 151 | |
| 152 if (dst_container && window->GetParent() != dst_container) { | |
| 153 WmWindow* focused = WmShell::Get()->GetFocusedWindow(); | |
| 154 WmWindow* active = WmShell::Get()->GetActiveWindow(); | |
| 155 | |
| 156 WmWindowTracker tracker; | |
| 157 if (focused) | |
| 158 tracker.Add(focused); | |
| 159 if (active && focused != active) | |
| 160 tracker.Add(active); | |
| 161 | |
| 162 gfx::Point origin = bounds.origin(); | |
| 163 const gfx::Point display_origin = display.bounds().origin(); | |
| 164 origin.Offset(-display_origin.x(), -display_origin.y()); | |
| 165 gfx::Rect new_bounds = gfx::Rect(origin, bounds.size()); | |
| 166 | |
| 167 // Set new bounds now so that the container's layout manager can adjust | |
| 168 // the bounds if necessary. | |
| 169 window->SetBounds(new_bounds); | |
| 170 | |
| 171 dst_container->AddChild(window); | |
| 172 | |
| 173 MoveAllTransientChildrenToNewRoot(display, window); | |
| 174 | |
| 175 // Restore focused/active window. | |
| 176 if (tracker.Contains(focused)) { | |
| 177 focused->SetFocused(); | |
| 178 WmShell::Get()->set_root_window_for_new_windows( | |
| 179 focused->GetRootWindow()); | |
| 180 } else if (tracker.Contains(active)) { | |
| 181 active->Activate(); | |
| 182 } | |
| 183 // TODO(oshima): We should not have to update the bounds again | |
| 184 // below in theory, but we currently do need as there is a code | |
| 185 // that assumes that the bounds will never be overridden by the | |
| 186 // layout mananger. We should have more explicit control how | |
| 187 // constraints are applied by the layout manager. | |
| 188 } | |
| 189 } | |
| 190 gfx::Point origin(bounds.origin()); | |
| 191 const gfx::Point display_origin = | |
| 192 window->GetDisplayNearestWindow().bounds().origin(); | |
| 193 origin.Offset(-display_origin.x(), -display_origin.y()); | |
| 194 window->SetBounds(gfx::Rect(origin, bounds.size())); | |
| 195 } | |
| 196 | |
| 90 } // namespace wm | 197 } // namespace wm |
| 91 } // namespace ash | 198 } // namespace ash |
| OLD | NEW |