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/common/wm/panels/panel_window_resizer.h" | |
6 | |
7 #include "ash/common/shelf/wm_shelf.h" | |
8 #include "ash/common/wm/panels/panel_layout_manager.h" | |
9 #include "ash/common/wm/window_parenting_utils.h" | |
10 #include "ash/common/wm/window_state.h" | |
11 #include "ash/common/wm_window.h" | |
12 #include "ash/public/cpp/shell_window_ids.h" | |
13 #include "ash/public/cpp/window_properties.h" | |
14 #include "ash/root_window_controller.h" | |
15 #include "ash/shell.h" | |
16 #include "ui/base/hit_test.h" | |
17 #include "ui/base/ui_base_types.h" | |
18 #include "ui/display/display.h" | |
19 #include "ui/display/screen.h" | |
20 | |
21 namespace ash { | |
22 | |
23 namespace { | |
24 | |
25 const int kPanelSnapToLauncherDistance = 30; | |
26 | |
27 } // namespace | |
28 | |
29 PanelWindowResizer::~PanelWindowResizer() {} | |
30 | |
31 // static | |
32 PanelWindowResizer* PanelWindowResizer::Create( | |
33 WindowResizer* next_window_resizer, | |
34 wm::WindowState* window_state) { | |
35 return new PanelWindowResizer(next_window_resizer, window_state); | |
36 } | |
37 | |
38 void PanelWindowResizer::Drag(const gfx::Point& location, int event_flags) { | |
39 last_location_ = GetTarget()->GetParent()->ConvertPointToScreen(location); | |
40 if (!did_move_or_resize_) { | |
41 did_move_or_resize_ = true; | |
42 StartedDragging(); | |
43 } | |
44 | |
45 // Check if the destination has changed displays. | |
46 display::Screen* screen = display::Screen::GetScreen(); | |
47 const display::Display dst_display = | |
48 screen->GetDisplayNearestPoint(last_location_); | |
49 if (dst_display.id() != | |
50 panel_container_->GetRootWindow()->GetDisplayNearestWindow().id()) { | |
51 // The panel is being dragged to a new display. If the previous container is | |
52 // the current parent of the panel it will be informed of the end of drag | |
53 // when the panel is reparented, otherwise let the previous container know | |
54 // the drag is complete. If we told the panel's parent that the drag was | |
55 // complete it would begin positioning the panel. | |
56 if (GetTarget()->GetParent() != panel_container_) | |
57 PanelLayoutManager::Get(panel_container_)->FinishDragging(); | |
58 WmWindow* dst_root = | |
59 Shell::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_(GetTarget()->aura_window()->GetProperty(kPanelAttachedKey)), | |
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 SHELF_ALIGNMENT_BOTTOM: | |
126 case 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 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 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 WmWindow* target = GetTarget(); | |
160 target->aura_window()->SetProperty(kPanelAttachedKey, true); | |
161 // We use root window coordinates to ensure that during the drag the panel | |
162 // is reparented to a container in the root window that has that window. | |
163 WmWindow* target_root = target->GetRootWindow(); | |
164 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 (GetTarget()->aura_window()->GetProperty(kPanelAttachedKey) != | |
176 details().should_attach_to_shelf) { | |
177 GetTarget()->aura_window()->SetProperty(kPanelAttachedKey, | |
178 details().should_attach_to_shelf); | |
179 // We use last known location to ensure that after the drag the panel | |
180 // is reparented to a container in the root window that has that location. | |
181 WmWindow* target = GetTarget(); | |
182 WmWindow* target_root = target->GetRootWindow(); | |
183 WmWindow* old_parent = target->GetParent(); | |
184 target->SetParentUsingContext(target_root, | |
185 gfx::Rect(last_location_, gfx::Size())); | |
186 wm::ReparentTransientChildrenOfChild(target, old_parent, | |
187 target->GetParent()); | |
188 } | |
189 | |
190 // If we started the drag in one root window and moved into another root | |
191 // but then canceled the drag we may need to inform the original layout | |
192 // manager that the drag is finished. | |
193 if (initial_panel_container_ != panel_container_) | |
194 PanelLayoutManager::Get(initial_panel_container_)->FinishDragging(); | |
195 if (panel_container_) | |
196 PanelLayoutManager::Get(panel_container_)->FinishDragging(); | |
197 } | |
198 | |
199 void PanelWindowResizer::UpdateLauncherPosition() { | |
200 if (panel_container_) { | |
201 PanelLayoutManager::Get(panel_container_) | |
202 ->shelf() | |
203 ->UpdateIconPositionForPanel(GetTarget()); | |
204 } | |
205 } | |
206 | |
207 } // namespace ash | |
OLD | NEW |