| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/common/panels/panel_window_resizer.h" | |
| 6 | |
| 7 #include "ash/wm/common/panels/panel_layout_manager.h" | |
| 8 #include "ash/wm/common/shelf/wm_shelf.h" | |
| 9 #include "ash/wm/common/window_parenting_utils.h" | |
| 10 #include "ash/wm/common/window_state.h" | |
| 11 #include "ash/wm/common/wm_lookup.h" | |
| 12 #include "ash/wm/common/wm_root_window_controller.h" | |
| 13 #include "ash/wm/common/wm_shell_window_ids.h" | |
| 14 #include "ash/wm/common/wm_window.h" | |
| 15 #include "ui/base/hit_test.h" | |
| 16 #include "ui/base/ui_base_types.h" | |
| 17 #include "ui/display/display.h" | |
| 18 #include "ui/display/screen.h" | |
| 19 | |
| 20 namespace ash { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const int kPanelSnapToLauncherDistance = 30; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 PanelWindowResizer::~PanelWindowResizer() {} | |
| 29 | |
| 30 // static | |
| 31 PanelWindowResizer* PanelWindowResizer::Create( | |
| 32 WindowResizer* next_window_resizer, | |
| 33 wm::WindowState* window_state) { | |
| 34 return new PanelWindowResizer(next_window_resizer, window_state); | |
| 35 } | |
| 36 | |
| 37 void PanelWindowResizer::Drag(const gfx::Point& location, int event_flags) { | |
| 38 last_location_ = GetTarget()->GetParent()->ConvertPointToScreen(location); | |
| 39 if (!did_move_or_resize_) { | |
| 40 did_move_or_resize_ = true; | |
| 41 StartedDragging(); | |
| 42 } | |
| 43 | |
| 44 // Check if the destination has changed displays. | |
| 45 display::Screen* screen = display::Screen::GetScreen(); | |
| 46 const display::Display dst_display = | |
| 47 screen->GetDisplayNearestPoint(last_location_); | |
| 48 if (dst_display.id() != | |
| 49 panel_container_->GetRootWindow()->GetDisplayNearestWindow().id()) { | |
| 50 // The panel is being dragged to a new display. If the previous container is | |
| 51 // the current parent of the panel it will be informed of the end of drag | |
| 52 // when the panel is reparented, otherwise let the previous container know | |
| 53 // the drag is complete. If we told the panel's parent that the drag was | |
| 54 // complete it would begin positioning the panel. | |
| 55 if (GetTarget()->GetParent() != panel_container_) | |
| 56 PanelLayoutManager::Get(panel_container_)->FinishDragging(); | |
| 57 wm::WmWindow* dst_root = | |
| 58 wm::WmLookup::Get() | |
| 59 ->GetRootWindowControllerWithDisplayId(dst_display.id()) | |
| 60 ->GetWindow(); | |
| 61 panel_container_ = | |
| 62 dst_root->GetChildByShellWindowId(kShellWindowId_PanelContainer); | |
| 63 | |
| 64 // The panel's parent already knows that the drag is in progress for this | |
| 65 // panel. | |
| 66 if (panel_container_ && GetTarget()->GetParent() != panel_container_) | |
| 67 PanelLayoutManager::Get(panel_container_)->StartDragging(GetTarget()); | |
| 68 } | |
| 69 gfx::Point offset; | |
| 70 gfx::Rect bounds(CalculateBoundsForDrag(location)); | |
| 71 if (!(details().bounds_change & WindowResizer::kBoundsChange_Resizes)) { | |
| 72 window_state_->drag_details()->should_attach_to_shelf = | |
| 73 AttachToLauncher(bounds, &offset); | |
| 74 } | |
| 75 gfx::Point modified_location(location.x() + offset.x(), | |
| 76 location.y() + offset.y()); | |
| 77 | |
| 78 base::WeakPtr<PanelWindowResizer> resizer(weak_ptr_factory_.GetWeakPtr()); | |
| 79 next_window_resizer_->Drag(modified_location, event_flags); | |
| 80 if (!resizer) | |
| 81 return; | |
| 82 | |
| 83 if (details().should_attach_to_shelf && | |
| 84 !(details().bounds_change & WindowResizer::kBoundsChange_Resizes)) { | |
| 85 UpdateLauncherPosition(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void PanelWindowResizer::CompleteDrag() { | |
| 90 // The root window can change when dragging into a different screen. | |
| 91 next_window_resizer_->CompleteDrag(); | |
| 92 FinishDragging(); | |
| 93 } | |
| 94 | |
| 95 void PanelWindowResizer::RevertDrag() { | |
| 96 next_window_resizer_->RevertDrag(); | |
| 97 window_state_->drag_details()->should_attach_to_shelf = was_attached_; | |
| 98 FinishDragging(); | |
| 99 } | |
| 100 | |
| 101 PanelWindowResizer::PanelWindowResizer(WindowResizer* next_window_resizer, | |
| 102 wm::WindowState* window_state) | |
| 103 : WindowResizer(window_state), | |
| 104 next_window_resizer_(next_window_resizer), | |
| 105 panel_container_(NULL), | |
| 106 initial_panel_container_(NULL), | |
| 107 did_move_or_resize_(false), | |
| 108 was_attached_(window_state->panel_attached()), | |
| 109 weak_ptr_factory_(this) { | |
| 110 DCHECK(details().is_resizable); | |
| 111 panel_container_ = GetTarget()->GetRootWindow()->GetChildByShellWindowId( | |
| 112 kShellWindowId_PanelContainer); | |
| 113 initial_panel_container_ = panel_container_; | |
| 114 } | |
| 115 | |
| 116 bool PanelWindowResizer::AttachToLauncher(const gfx::Rect& bounds, | |
| 117 gfx::Point* offset) { | |
| 118 bool should_attach = false; | |
| 119 if (panel_container_) { | |
| 120 PanelLayoutManager* panel_layout_manager = | |
| 121 PanelLayoutManager::Get(panel_container_); | |
| 122 gfx::Rect launcher_bounds = GetTarget()->GetParent()->ConvertRectFromScreen( | |
| 123 panel_layout_manager->shelf()->GetWindow()->GetBoundsInScreen()); | |
| 124 switch (panel_layout_manager->shelf()->GetAlignment()) { | |
| 125 case wm::SHELF_ALIGNMENT_BOTTOM: | |
| 126 case wm::SHELF_ALIGNMENT_BOTTOM_LOCKED: | |
| 127 if (bounds.bottom() >= | |
| 128 (launcher_bounds.y() - kPanelSnapToLauncherDistance)) { | |
| 129 should_attach = true; | |
| 130 offset->set_y(launcher_bounds.y() - bounds.height() - bounds.y()); | |
| 131 } | |
| 132 break; | |
| 133 case wm::SHELF_ALIGNMENT_LEFT: | |
| 134 if (bounds.x() <= | |
| 135 (launcher_bounds.right() + kPanelSnapToLauncherDistance)) { | |
| 136 should_attach = true; | |
| 137 offset->set_x(launcher_bounds.right() - bounds.x()); | |
| 138 } | |
| 139 break; | |
| 140 case wm::SHELF_ALIGNMENT_RIGHT: | |
| 141 if (bounds.right() >= | |
| 142 (launcher_bounds.x() - kPanelSnapToLauncherDistance)) { | |
| 143 should_attach = true; | |
| 144 offset->set_x(launcher_bounds.x() - bounds.width() - bounds.x()); | |
| 145 } | |
| 146 break; | |
| 147 } | |
| 148 } | |
| 149 return should_attach; | |
| 150 } | |
| 151 | |
| 152 void PanelWindowResizer::StartedDragging() { | |
| 153 // Tell the panel layout manager that we are dragging this panel before | |
| 154 // attaching it so that it does not get repositioned. | |
| 155 if (panel_container_) | |
| 156 PanelLayoutManager::Get(panel_container_)->StartDragging(GetTarget()); | |
| 157 if (!was_attached_) { | |
| 158 // Attach the panel while dragging placing it in front of other panels. | |
| 159 window_state_->set_panel_attached(true); | |
| 160 // We use root window coordinates to ensure that during the drag the panel | |
| 161 // is reparented to a container in the root window that has that window. | |
| 162 wm::WmWindow* target = GetTarget(); | |
| 163 wm::WmWindow* target_root = target->GetRootWindow(); | |
| 164 wm::WmWindow* old_parent = target->GetParent(); | |
| 165 target->SetParentUsingContext(target_root, | |
| 166 target_root->GetBoundsInScreen()); | |
| 167 wm::ReparentTransientChildrenOfChild(target, old_parent, | |
| 168 target->GetParent()); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void PanelWindowResizer::FinishDragging() { | |
| 173 if (!did_move_or_resize_) | |
| 174 return; | |
| 175 if (window_state_->panel_attached() != details().should_attach_to_shelf) { | |
| 176 window_state_->set_panel_attached(details().should_attach_to_shelf); | |
| 177 // We use last known location to ensure that after the drag the panel | |
| 178 // is reparented to a container in the root window that has that location. | |
| 179 wm::WmWindow* target = GetTarget(); | |
| 180 wm::WmWindow* target_root = target->GetRootWindow(); | |
| 181 wm::WmWindow* old_parent = target->GetParent(); | |
| 182 target->SetParentUsingContext(target_root, | |
| 183 gfx::Rect(last_location_, gfx::Size())); | |
| 184 wm::ReparentTransientChildrenOfChild(target, old_parent, | |
| 185 target->GetParent()); | |
| 186 } | |
| 187 | |
| 188 // If we started the drag in one root window and moved into another root | |
| 189 // but then canceled the drag we may need to inform the original layout | |
| 190 // manager that the drag is finished. | |
| 191 if (initial_panel_container_ != panel_container_) | |
| 192 PanelLayoutManager::Get(initial_panel_container_)->FinishDragging(); | |
| 193 if (panel_container_) | |
| 194 PanelLayoutManager::Get(panel_container_)->FinishDragging(); | |
| 195 } | |
| 196 | |
| 197 void PanelWindowResizer::UpdateLauncherPosition() { | |
| 198 if (panel_container_) { | |
| 199 PanelLayoutManager::Get(panel_container_) | |
| 200 ->shelf() | |
| 201 ->UpdateIconPositionForWindow(GetTarget()); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 } // namespace ash | |
| OLD | NEW |