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" |
| 13 #include "ui/gfx/rect.h" |
10 | 14 |
11 PanelDragController::PanelDragController() | 15 // static |
12 : dragging_panel_(NULL) { | 16 const int PanelDragController::kDetachDockedPanelThreshold = 60; |
| 17 const int PanelDragController::kDockDetachedPanelThreshold = 30; |
| 18 |
| 19 PanelDragController::PanelDragController(PanelManager* panel_manager) |
| 20 : panel_manager_(panel_manager), |
| 21 dragging_panel_(NULL), |
| 22 dragging_panel_original_strip_(NULL) { |
13 } | 23 } |
14 | 24 |
15 PanelDragController::~PanelDragController() { | 25 PanelDragController::~PanelDragController() { |
16 } | 26 } |
17 | 27 |
18 void PanelDragController::StartDragging(Panel* panel, | 28 void PanelDragController::StartDragging(Panel* panel, |
19 const gfx::Point& mouse_location) { | 29 const gfx::Point& mouse_location) { |
20 DCHECK(!dragging_panel_); | 30 DCHECK(!dragging_panel_); |
21 DCHECK(panel->draggable()); | 31 DCHECK(panel->draggable()); |
22 | 32 |
23 last_mouse_location_ = mouse_location; | 33 last_mouse_location_ = mouse_location; |
| 34 offset_from_mouse_location_on_drag_start_ = |
| 35 mouse_location.Subtract(panel->GetBounds().origin()); |
24 | 36 |
25 dragging_panel_ = panel; | 37 dragging_panel_ = panel; |
26 dragging_panel_original_position_ = panel->GetBounds().origin(); | 38 dragging_panel_->SetPreviewMode(true); |
27 | 39 |
28 dragging_panel_->panel_strip()->StartDraggingPanel(panel); | 40 // Keep track of original strip and placement for the case that the drag is |
| 41 // cancelled. |
| 42 dragging_panel_original_strip_ = dragging_panel_->panel_strip(); |
| 43 dragging_panel_original_strip_->SavePanelPlacement(dragging_panel_); |
| 44 |
| 45 dragging_panel_original_strip_->StartDraggingPanelWithinStrip( |
| 46 dragging_panel_); |
29 } | 47 } |
30 | 48 |
31 void PanelDragController::Drag(const gfx::Point& mouse_location) { | 49 void PanelDragController::Drag(const gfx::Point& mouse_location) { |
32 DCHECK(dragging_panel_); | 50 DCHECK(dragging_panel_); |
33 | 51 |
34 dragging_panel_->panel_strip()->DragPanel( | 52 PanelStrip* current_strip = dragging_panel_->panel_strip(); |
| 53 |
| 54 gfx::Rect target_panel_bounds; |
| 55 PanelStrip* target_strip = ComputeDragTargetStrip( |
| 56 mouse_location, &target_panel_bounds); |
| 57 if (target_strip != current_strip) { |
| 58 // End the dragging in old strip. |
| 59 current_strip->EndDraggingPanelWithinStrip(dragging_panel_, true); |
| 60 |
| 61 // Apply new panel position. |
| 62 dragging_panel_->SetPanelBounds(target_panel_bounds); |
| 63 |
| 64 // Move the panel to new strip. |
| 65 panel_manager_->MovePanelToStrip(dragging_panel_, |
| 66 target_strip->type(), |
| 67 PanelStrip::KNOWN_POSITION); |
| 68 |
| 69 // Start the dragging in new strip. |
| 70 target_strip->StartDraggingPanelWithinStrip(dragging_panel_); |
| 71 } else { |
| 72 current_strip->DragPanelWithinStrip( |
35 dragging_panel_, | 73 dragging_panel_, |
36 mouse_location.x() - last_mouse_location_.x(), | 74 mouse_location.x() - last_mouse_location_.x(), |
37 mouse_location.y() - last_mouse_location_.y()); | 75 mouse_location.y() - last_mouse_location_.y()); |
| 76 } |
38 | 77 |
39 last_mouse_location_ = mouse_location; | 78 last_mouse_location_ = mouse_location; |
40 } | 79 } |
41 | 80 |
42 void PanelDragController::EndDragging(bool cancelled) { | 81 void PanelDragController::EndDragging(bool cancelled) { |
43 DCHECK(dragging_panel_); | 82 DCHECK(dragging_panel_); |
44 | 83 |
45 // The code in PanelStrip::EndDraggingPanel might call DragController to find | 84 PanelStrip* current_strip = dragging_panel_->panel_strip(); |
46 // out if the drag has ended. So we need to reset |dragging_panel_| to reflect | 85 if (cancelled) { |
47 // this. | 86 // Abort the drag in current strip. |
48 Panel* panel = dragging_panel_; | 87 current_strip->EndDraggingPanelWithinStrip(dragging_panel_, true); |
| 88 |
| 89 // Restore the dragging panel to its original strip if needed. |
| 90 // Note that the bounds of dragging panel is updated later by calling |
| 91 // RestorePanelToSavedPlacement. |
| 92 if (current_strip != dragging_panel_original_strip_) { |
| 93 PanelStrip::PositioningMask positioning_mask = |
| 94 static_cast<PanelStrip::PositioningMask>( |
| 95 PanelStrip::DEFAULT_POSITION | PanelStrip::DO_NOT_UPDATE_BOUNDS); |
| 96 panel_manager_->MovePanelToStrip( |
| 97 dragging_panel_, |
| 98 dragging_panel_original_strip_->type(), |
| 99 positioning_mask); |
| 100 } |
| 101 |
| 102 // End the preview mode. |
| 103 dragging_panel_->SetPreviewMode(false); |
| 104 |
| 105 // Restore the dragging panel to its original placement. |
| 106 dragging_panel_original_strip_->RestorePanelToSavedPlacement(); |
| 107 } else { |
| 108 // The saved placement is no longer needed. |
| 109 dragging_panel_original_strip_->DiscardSavedPanelPlacement(); |
| 110 |
| 111 // End the preview mode. |
| 112 dragging_panel_->SetPreviewMode(false); |
| 113 |
| 114 // End the drag. This will cause the panel to be moved to its finalized |
| 115 // position. |
| 116 current_strip->EndDraggingPanelWithinStrip(dragging_panel_, false); |
| 117 } |
| 118 |
49 dragging_panel_ = NULL; | 119 dragging_panel_ = NULL; |
50 panel->panel_strip()->EndDraggingPanel(panel, cancelled); | 120 } |
| 121 |
| 122 PanelStrip* PanelDragController::ComputeDragTargetStrip( |
| 123 const gfx::Point& mouse_location, gfx::Rect* new_panel_bounds) const { |
| 124 if (CanDragToDockedStrip(mouse_location, new_panel_bounds)) |
| 125 return panel_manager_->docked_strip(); |
| 126 else if (CanDragToDetachedStrip(mouse_location, new_panel_bounds)) |
| 127 return panel_manager_->detached_strip(); |
| 128 else |
| 129 return dragging_panel_->panel_strip(); |
| 130 } |
| 131 |
| 132 bool PanelDragController::CanDragToDockedStrip( |
| 133 const gfx::Point& mouse_location, |
| 134 gfx::Rect* new_panel_bounds) const { |
| 135 // It has to come from the detached strip. |
| 136 if (dragging_panel_->panel_strip()->type() != PanelStrip::DETACHED) |
| 137 return false; |
| 138 |
| 139 // Compute target panel bounds. The origin is computed based on the fact that |
| 140 // the panel should follow the mouse movement. The size remains unchanged. |
| 141 gfx::Rect target_panel_bounds = dragging_panel_->GetBounds(); |
| 142 target_panel_bounds.set_origin( |
| 143 mouse_location.Subtract(offset_from_mouse_location_on_drag_start_)); |
| 144 |
| 145 // The bottom of the panel should come very close to or fall below the bottom |
| 146 // of the docked area. |
| 147 if (panel_manager_->docked_strip()->display_area().bottom() - |
| 148 target_panel_bounds.bottom() > |
| 149 kDockDetachedPanelThreshold) |
| 150 return false; |
| 151 |
| 152 *new_panel_bounds = target_panel_bounds; |
| 153 return true; |
| 154 } |
| 155 |
| 156 bool PanelDragController::CanDragToDetachedStrip( |
| 157 const gfx::Point& mouse_location, |
| 158 gfx::Rect* new_panel_bounds) const { |
| 159 // It has to come from the docked strip. |
| 160 if (dragging_panel_->panel_strip()->type() != PanelStrip::DOCKED) |
| 161 return false; |
| 162 |
| 163 // The minimized docked panel is not allowed to detach. |
| 164 if (dragging_panel_->IsMinimized()) |
| 165 return false; |
| 166 |
| 167 // Compute target panel bounds. The origin is computed based on the fact that |
| 168 // the panel should follow the mouse movement. The size remains unchanged. |
| 169 gfx::Rect target_panel_bounds = dragging_panel_->GetBounds(); |
| 170 target_panel_bounds.set_origin( |
| 171 mouse_location.Subtract(offset_from_mouse_location_on_drag_start_)); |
| 172 |
| 173 // The panel should be dragged up high enough to pass certain threshold. |
| 174 if (panel_manager_->docked_strip()->display_area().bottom() - |
| 175 target_panel_bounds.bottom() < |
| 176 kDetachDockedPanelThreshold) |
| 177 return false; |
| 178 |
| 179 *new_panel_bounds = target_panel_bounds; |
| 180 return true; |
51 } | 181 } |
52 | 182 |
53 void PanelDragController::OnPanelClosed(Panel* panel) { | 183 void PanelDragController::OnPanelClosed(Panel* panel) { |
54 if (!dragging_panel_) | 184 if (!dragging_panel_) |
55 return; | 185 return; |
56 | 186 |
57 // If the dragging panel is closed, abort the drag. | 187 // If the dragging panel is closed, abort the drag. |
58 if (dragging_panel_ == panel) | 188 if (dragging_panel_ == panel) |
59 EndDragging(false); | 189 EndDragging(false); |
60 } | 190 } |
OLD | NEW |