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; |
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, |
28 const gfx::Point& mouse_location) { | |
19 DCHECK(!dragging_panel_); | 29 DCHECK(!dragging_panel_); |
20 DCHECK(panel->draggable()); | 30 DCHECK(panel->draggable()); |
21 | 31 |
22 dragging_panel_ = panel; | 32 dragging_panel_ = panel; |
23 dragging_panel_original_position_ = panel->GetBounds().origin(); | 33 last_mouse_location_ = mouse_location; |
24 | 34 |
25 dragging_panel_->panel_strip()->StartDraggingPanel(panel); | 35 // Keep track of original strip and placement for the case that the drag is |
36 // cancelled. | |
37 dragging_panel_original_strip_ = dragging_panel_->panel_strip(); | |
38 dragging_panel_original_strip_->SavePanelPlacement(dragging_panel_); | |
39 | |
40 if (dragging_panel_original_strip_->type() == PanelStrip::DOCKED) | |
41 mouse_location_on_docked_ = last_mouse_location_; | |
42 dragging_panel_original_strip_->StartDraggingPanelLocally(dragging_panel_); | |
26 } | 43 } |
27 | 44 |
28 void PanelDragController::Drag(int delta_x, int delta_y) { | 45 void PanelDragController::Drag(const gfx::Point& mouse_location) { |
29 DCHECK(dragging_panel_); | 46 DCHECK(dragging_panel_); |
30 | 47 |
31 dragging_panel_->panel_strip()->DragPanel(dragging_panel_, delta_x, delta_y); | 48 PanelStrip* current_strip = dragging_panel_->panel_strip(); |
49 | |
50 gfx::Point target_panel_position; | |
jennb
2012/03/03 02:19:33
Add comment explaining why target panel position
jianli
2012/03/07 19:14:31
Done.
| |
51 PanelStrip* target_strip = ComputeDragTagetStrip( | |
52 mouse_location, &target_panel_position); | |
53 if (target_strip != current_strip) { | |
54 current_strip->EndDraggingPanelLocally(dragging_panel_, true); | |
55 current_strip->RemovePanel(dragging_panel_); | |
56 target_strip->AddDraggingPanel(dragging_panel_, target_panel_position); | |
57 if (target_strip->type() == PanelStrip::DOCKED) | |
58 mouse_location_on_docked_ = mouse_location; | |
59 } else { | |
60 current_strip->DragPanelLocally( | |
61 dragging_panel_, | |
62 mouse_location.x() - last_mouse_location_.x(), | |
63 mouse_location.y() - last_mouse_location_.y()); | |
64 } | |
65 | |
66 last_mouse_location_ = mouse_location; | |
32 } | 67 } |
33 | 68 |
34 void PanelDragController::EndDragging(bool cancelled) { | 69 void PanelDragController::EndDragging(bool cancelled) { |
35 DCHECK(dragging_panel_); | 70 DCHECK(dragging_panel_); |
36 | 71 |
37 // The code in PanelStrip::EndDraggingPanel might call DragController to find | 72 PanelStrip* current_strip = dragging_panel_->panel_strip(); |
38 // out if the drag has ended. So we need to reset |dragging_panel_| to reflect | 73 current_strip->EndDraggingPanelLocally(dragging_panel_, cancelled); |
39 // this. | 74 |
40 Panel* panel = dragging_panel_; | 75 if (cancelled) { |
76 // If the dragging panel is not in original strip, put it back. | |
77 if (current_strip != dragging_panel_original_strip_) { | |
78 current_strip->RemovePanel(dragging_panel_); | |
jennb
2012/03/03 02:19:33
Try to go through PanelManager to move panel betwe
jianli
2012/03/07 19:14:31
Done.
| |
79 dragging_panel_->set_panel_strip(dragging_panel_original_strip_); | |
80 dragging_panel_original_strip_->AddPanel(dragging_panel_); | |
jennb
2012/03/03 02:19:33
Add followed by Load - will this appear to the use
jianli
2012/03/07 19:14:31
Fixed per discussion.
| |
81 } | |
82 | |
83 // Restore the dragging panel to its original placement. | |
84 dragging_panel_original_strip_->LoadSavedPanelPlacement(); | |
85 } else { | |
86 // The saved placement is not needed when the drag ends. | |
87 dragging_panel_original_strip_->DiscardSavedPanelPlacement(); | |
88 } | |
89 | |
41 dragging_panel_ = NULL; | 90 dragging_panel_ = NULL; |
42 panel->panel_strip()->EndDraggingPanel(panel, cancelled); | 91 } |
92 | |
93 PanelStrip* PanelDragController::ComputeDragTagetStrip( | |
94 const gfx::Point& mouse_location, gfx::Point* new_panel_position) const { | |
95 if (CanDragToDockedStrip(mouse_location, new_panel_position)) | |
96 return panel_manager_->docked_strip(); | |
97 else if (CanDragToDetachedStrip(mouse_location, new_panel_position)) | |
98 return panel_manager_->detached_strip(); | |
99 else | |
100 return dragging_panel_->panel_strip(); | |
101 } | |
102 | |
103 | |
104 bool PanelDragController::CanDragToDockedStrip( | |
105 const gfx::Point& mouse_location, | |
106 gfx::Point* new_panel_position) const { | |
107 // It has to come from the detached strip. | |
108 if (dragging_panel_->panel_strip()->type() != PanelStrip::DETACHED) | |
109 return false; | |
110 | |
111 // The bottom of the panel should come very close to or fall below the bottom | |
112 // of the docked area. | |
113 int new_panel_bottom = dragging_panel_->GetBounds().bottom() + | |
114 mouse_location.y() - last_mouse_location_.y(); | |
115 int docked_area_bottom = | |
116 panel_manager_->docked_strip()->display_area().bottom(); | |
117 if (docked_area_bottom - new_panel_bottom > kDockDetachedPanelThreshold) | |
118 return false; | |
119 | |
120 *new_panel_position = dragging_panel_->GetBounds().origin(); | |
121 new_panel_position->Offset( | |
122 mouse_location.x() - last_mouse_location_.x(), | |
123 mouse_location.y() - last_mouse_location_.y()); | |
124 return true; | |
125 } | |
126 | |
127 bool PanelDragController::CanDragToDetachedStrip( | |
128 const gfx::Point& mouse_location, | |
129 gfx::Point* new_panel_position) const { | |
130 // It has to come from the docked strip. | |
131 if (dragging_panel_->panel_strip()->type() != PanelStrip::DOCKED) | |
132 return false; | |
133 | |
134 // The minimized docked panel is not allowed to detach. | |
135 if (dragging_panel_->expansion_state() != Panel::EXPANDED) | |
136 return false; | |
137 | |
138 // The panel should be dragged up higher enough to pass certain threshold. | |
jennb
2012/03/03 02:19:33
high enough
jianli
2012/03/07 19:14:31
Done.
| |
139 int delta = mouse_location.y() - mouse_location_on_docked_.y(); | |
jennb
2012/03/03 02:19:33
if (mouse_location.y() >= mouse_location_on_docked
jianli
2012/03/07 19:14:31
Done.
| |
140 if (-delta < kDetachDockedPanelThreshold) | |
141 return false; | |
142 | |
143 *new_panel_position = dragging_panel_->GetBounds().origin(); | |
144 new_panel_position->Offset( | |
jennb
2012/03/03 02:19:33
I don't understand the commenting in these lines.
jianli
2012/03/07 19:14:31
Comment updated.
| |
145 // The x coordinate is updated when the mouse drags in the docked strip. | |
146 mouse_location.x() - last_mouse_location_.x(), | |
147 // The y coordinate is intact when the mouse drags in the docked strip. | |
148 mouse_location.y() - mouse_location_on_docked_.y()); | |
149 return true; | |
43 } | 150 } |
44 | 151 |
45 void PanelDragController::OnPanelClosed(Panel* panel) { | 152 void PanelDragController::OnPanelClosed(Panel* panel) { |
46 if (!dragging_panel_) | 153 if (!dragging_panel_) |
47 return; | 154 return; |
48 | 155 |
49 // If the dragging panel is closed, abort the drag. | 156 // If the dragging panel is closed, abort the drag. |
50 if (dragging_panel_ == panel) | 157 if (dragging_panel_ == panel) |
51 EndDragging(false); | 158 EndDragging(false); |
52 } | 159 } |
OLD | NEW |