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