OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/panels/panel_drag_controller.h" | 5 #include "chrome/browser/ui/panels/panel_drag_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/ui/panels/detached_panel_strip.h" | |
9 #include "chrome/browser/ui/panels/docked_panel_strip.h" | |
8 #include "chrome/browser/ui/panels/panel.h" | 10 #include "chrome/browser/ui/panels/panel.h" |
11 #include "chrome/browser/ui/panels/panel_manager.h" | |
9 #include "chrome/browser/ui/panels/panel_strip.h" | 12 #include "chrome/browser/ui/panels/panel_strip.h" |
10 | 13 |
11 PanelDragController::PanelDragController() | 14 // static |
12 : dragging_panel_(NULL) { | 15 const int PanelDragController::kDetachDockedPanelThreshold = 80; |
jennb
2012/03/08 23:41:09
Was there a spec change? I remember these as 60 an
jianli
2012/03/09 21:48:58
Changed to use the values in spec.
| |
16 const int PanelDragController::kDockDetachedPanelThreshold = 10; | |
17 | |
18 PanelDragController::PanelDragController(PanelManager* panel_manager) | |
19 : panel_manager_(panel_manager), | |
20 dragging_panel_(NULL), | |
21 dragging_panel_original_strip_(NULL) { | |
13 } | 22 } |
14 | 23 |
15 PanelDragController::~PanelDragController() { | 24 PanelDragController::~PanelDragController() { |
16 } | 25 } |
17 | 26 |
18 void PanelDragController::StartDragging(Panel* panel, | 27 void PanelDragController::StartDragging(Panel* panel, |
19 const gfx::Point& mouse_location) { | 28 const gfx::Point& mouse_location) { |
20 DCHECK(!dragging_panel_); | 29 DCHECK(!dragging_panel_); |
21 DCHECK(panel->draggable()); | 30 DCHECK(panel->draggable()); |
22 | 31 |
23 last_mouse_location_ = mouse_location; | 32 last_mouse_location_ = mouse_location; |
33 offset_from_mouse_location_on_drag_start_ = | |
34 mouse_location.Subtract(panel->GetBounds().origin()); | |
24 | 35 |
25 dragging_panel_ = panel; | 36 dragging_panel_ = panel; |
26 dragging_panel_original_position_ = panel->GetBounds().origin(); | 37 dragging_panel_->SetPreviewMode(true); |
27 | 38 |
28 dragging_panel_->panel_strip()->StartDraggingPanel(panel); | 39 // Keep track of original strip and placement for the case that the drag is |
40 // cancelled. | |
41 dragging_panel_original_strip_ = dragging_panel_->panel_strip(); | |
42 dragging_panel_original_strip_->SavePanelPlacement(dragging_panel_); | |
43 | |
44 dragging_panel_original_strip_->StartDraggingPanelWithinStrip( | |
45 dragging_panel_); | |
29 } | 46 } |
30 | 47 |
31 void PanelDragController::Drag(const gfx::Point& mouse_location) { | 48 void PanelDragController::Drag(const gfx::Point& mouse_location) { |
32 DCHECK(dragging_panel_); | 49 DCHECK(dragging_panel_); |
33 | 50 |
34 dragging_panel_->panel_strip()->DragPanel( | 51 PanelStrip* current_strip = dragging_panel_->panel_strip(); |
52 | |
53 // We also need to compute target panel position together with target strip. | |
54 // This is because target panel position needs to be computed in special way | |
55 // when drag leaves docked strip and enters detached strip (see comment in | |
jennb
2012/03/08 23:41:09
There's no comment in CanDragToDetachedStrip about
jianli
2012/03/09 21:48:58
Outdated comment removed.
| |
56 // CanDragToDetachedStrip). | |
57 gfx::Point target_panel_position; | |
jennb
2012/03/08 23:41:09
Should have ComputeDragTargetStrip return target_p
jianli
2012/03/09 21:48:58
Done.
| |
58 PanelStrip* target_strip = ComputeDragTagetStrip( | |
59 mouse_location, &target_panel_position); | |
60 if (target_strip != current_strip) { | |
61 // End the dragging in old strip. | |
62 current_strip->EndDraggingPanelWithinStrip(dragging_panel_, true); | |
63 | |
64 // Apply new panel position. | |
65 gfx::Rect new_bounds(dragging_panel_->GetBounds()); | |
66 new_bounds.set_origin(target_panel_position); | |
67 dragging_panel_->SetPanelBounds(new_bounds); | |
68 | |
69 // Move the panel to new strip. | |
70 panel_manager_->MovePanelToStrip(dragging_panel_, target_strip->type()); | |
71 | |
72 // Start the dragging in new strip. | |
73 target_strip->StartDraggingPanelWithinStrip(dragging_panel_); | |
74 } else { | |
75 current_strip->DragPanelWithinStrip( | |
35 dragging_panel_, | 76 dragging_panel_, |
36 mouse_location.x() - last_mouse_location_.x(), | 77 mouse_location.x() - last_mouse_location_.x(), |
37 mouse_location.y() - last_mouse_location_.y()); | 78 mouse_location.y() - last_mouse_location_.y()); |
79 } | |
38 | 80 |
39 last_mouse_location_ = mouse_location; | 81 last_mouse_location_ = mouse_location; |
40 } | 82 } |
41 | 83 |
42 void PanelDragController::EndDragging(bool cancelled) { | 84 void PanelDragController::EndDragging(bool cancelled) { |
43 DCHECK(dragging_panel_); | 85 DCHECK(dragging_panel_); |
44 | 86 |
45 // The code in PanelStrip::EndDraggingPanel might call DragController to find | 87 PanelStrip* current_strip = dragging_panel_->panel_strip(); |
46 // out if the drag has ended. So we need to reset |dragging_panel_| to reflect | 88 if (cancelled) { |
47 // this. | 89 // Abort the drag in current strip. |
48 Panel* panel = dragging_panel_; | 90 current_strip->EndDraggingPanelWithinStrip(dragging_panel_, true); |
91 | |
92 // Restore the dragging panel to its original strip if needed. | |
93 if (current_strip != dragging_panel_original_strip_) { | |
94 panel_manager_->MovePanelToStrip( | |
jennb
2012/03/08 23:41:09
bounds have not been restored yet. docked_strip::A
jianli
2012/03/09 21:48:58
Per discussion, we added a new parameter to MovePa
| |
95 dragging_panel_, dragging_panel_original_strip_->type()); | |
96 } | |
97 | |
98 // We need to clear the preview mode before restoring to the saved placement | |
99 // because otherwise the panel will remain as it is. | |
100 dragging_panel_->SetPreviewMode(false); | |
101 | |
102 // Restore the dragging panel to its original placement. | |
103 dragging_panel_original_strip_->RestorePanelToSavedPlacement(); | |
104 } else { | |
105 // We need to clear the preview mode first because in this mode, the panel | |
jennb
2012/03/08 23:41:09
discard
end preview mode
end dragging
More readab
jianli
2012/03/09 21:48:58
Done.
| |
106 // will remain as it is. | |
107 dragging_panel_->SetPreviewMode(false); | |
108 | |
109 // End the drag. This will cause the panel to be moved to its finalized | |
110 // position. | |
111 current_strip->EndDraggingPanelWithinStrip(dragging_panel_, false); | |
112 | |
113 // The saved placement is not needed when the drag ends. | |
114 dragging_panel_original_strip_->DiscardSavedPanelPlacement(); | |
115 } | |
116 | |
49 dragging_panel_ = NULL; | 117 dragging_panel_ = NULL; |
50 panel->panel_strip()->EndDraggingPanel(panel, cancelled); | 118 } |
119 | |
120 PanelStrip* PanelDragController::ComputeDragTagetStrip( | |
121 const gfx::Point& mouse_location, gfx::Point* new_panel_position) const { | |
122 if (CanDragToDockedStrip(mouse_location, new_panel_position)) | |
123 return panel_manager_->docked_strip(); | |
124 else if (CanDragToDetachedStrip(mouse_location, new_panel_position)) | |
125 return panel_manager_->detached_strip(); | |
126 else | |
127 return dragging_panel_->panel_strip(); | |
128 } | |
129 | |
130 bool PanelDragController::CanDragToDockedStrip( | |
131 const gfx::Point& mouse_location, | |
132 gfx::Point* new_panel_position) const { | |
133 // It has to come from the detached strip. | |
134 if (dragging_panel_->panel_strip()->type() != PanelStrip::DETACHED) | |
135 return false; | |
136 | |
137 // Compute target panel position by assuming panel follows the mouse movement. | |
138 gfx::Point target_panel_position = | |
139 mouse_location.Subtract(offset_from_mouse_location_on_drag_start_); | |
140 | |
141 // The bottom of the panel should come very close to or fall below the bottom | |
142 // of the docked area. | |
143 if (GetOffsetToDockedAreaBottom(target_panel_position) > | |
144 kDockDetachedPanelThreshold) | |
145 return false; | |
146 | |
147 *new_panel_position = target_panel_position; | |
148 return true; | |
149 } | |
150 | |
151 bool PanelDragController::CanDragToDetachedStrip( | |
152 const gfx::Point& mouse_location, | |
153 gfx::Point* new_panel_position) const { | |
154 // It has to come from the docked strip. | |
155 if (dragging_panel_->panel_strip()->type() != PanelStrip::DOCKED) | |
156 return false; | |
157 | |
158 // The minimized docked panel is not allowed to detach. | |
159 if (dragging_panel_->expansion_state() != Panel::EXPANDED) | |
jennb
2012/03/08 23:41:09
if dragging_panel_->IsMinimized()
jianli
2012/03/09 21:48:58
Done.
| |
160 return false; | |
161 | |
162 // Compute target panel position by assuming panel follows the mouse movement. | |
163 gfx::Point target_panel_position = | |
164 mouse_location.Subtract(offset_from_mouse_location_on_drag_start_); | |
165 | |
166 // The panel should be dragged up high enough to pass certain threshold. | |
167 if (GetOffsetToDockedAreaBottom(target_panel_position) < | |
168 kDetachDockedPanelThreshold) | |
169 return false; | |
170 | |
171 *new_panel_position = target_panel_position; | |
172 return true; | |
173 } | |
174 | |
175 int PanelDragController::GetOffsetToDockedAreaBottom( | |
176 const gfx::Point& target_panel_position) const { | |
jennb
2012/03/08 23:41:09
Should make this take a rect. It's confusing to sa
jianli
2012/03/09 21:48:58
Done.
| |
177 int target_panel_bottom = | |
178 target_panel_position.y() + dragging_panel_->GetBounds().height(); | |
179 int docked_area_bottom = | |
180 panel_manager_->docked_strip()->display_area().bottom(); | |
181 return docked_area_bottom - target_panel_bottom; | |
51 } | 182 } |
52 | 183 |
53 void PanelDragController::OnPanelClosed(Panel* panel) { | 184 void PanelDragController::OnPanelClosed(Panel* panel) { |
54 if (!dragging_panel_) | 185 if (!dragging_panel_) |
55 return; | 186 return; |
56 | 187 |
57 // If the dragging panel is closed, abort the drag. | 188 // If the dragging panel is closed, abort the drag. |
58 if (dragging_panel_ == panel) | 189 if (dragging_panel_ == panel) |
59 EndDragging(false); | 190 EndDragging(false); |
60 } | 191 } |
OLD | NEW |