summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/panels/docked_panel_drag_handler.cc
blob: 8d4384e355a01e77fb8dddcaf322baad876eda8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/panels/docked_panel_drag_handler.h"

#include "chrome/browser/ui/panels/docked_panel_collection.h"
#include "chrome/browser/ui/panels/panel.h"
#include "chrome/browser/ui/panels/panel_manager.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"

// static
void DockedPanelDragHandler::HandleDrag(Panel* panel,
                                        const gfx::Point& target_position) {
  DCHECK_EQ(PanelCollection::DOCKED, panel->collection()->type());

  DockedPanelCollection* collection =
      static_cast<DockedPanelCollection*>(panel->collection());

  // Moves this panel to the dragging position.
  // Note that we still allow the panel to be moved vertically until it gets
  // aligned to the bottom area.
  gfx::Rect new_bounds(panel->GetBounds());
  new_bounds.set_x(target_position.x());
  int delta_x = new_bounds.x() - panel->GetBounds().x();
  int bottom = collection->GetBottomPositionForExpansionState(
      panel->expansion_state());
  if (new_bounds.bottom() != bottom) {
    new_bounds.set_y(target_position.y());
    if (new_bounds.bottom() > bottom)
      new_bounds.set_y(bottom - new_bounds.height());
  }
  panel->SetPanelBoundsInstantly(new_bounds);

  if (delta_x) {
    // Checks and processes other affected panels.
    if (delta_x > 0)
      DragRight(panel);
    else
      DragLeft(panel);

    // Layout refresh will automatically recompute the bounds of all affected
    // panels due to their position changes.
    collection->RefreshLayout();
  }
}

// static
void DockedPanelDragHandler::DragLeft(Panel* panel) {
  DockedPanelCollection* collection =
      static_cast<DockedPanelCollection*>(panel->collection());

  // This is the left corner of the dragging panel. We use it to check against
  // all the panels on its left.
  int dragging_panel_left_boundary = panel->GetBounds().x();

  // Checks the panels to the left of the dragging panel.
  DockedPanelCollection::Panels::iterator dragging_panel_iterator =
      find(collection->panels_.begin(), collection->panels_.end(), panel);
  DockedPanelCollection::Panels::iterator current_panel_iterator =
      dragging_panel_iterator;
  ++current_panel_iterator;
  for (; current_panel_iterator != collection->panels_.end();
       ++current_panel_iterator) {
    Panel* current_panel = *current_panel_iterator;

    // Can we swap dragging panel with its left panel? The criterion is that
    // the left corner of dragging panel should pass the middle position of
    // its left panel.
    if (dragging_panel_left_boundary > current_panel->GetBounds().x() +
            current_panel->GetBounds().width() / 2)
      break;

    // Swaps the contents and makes |dragging_panel_iterator| refers to the new
    // position.
    *dragging_panel_iterator = current_panel;
    *current_panel_iterator = panel;
    dragging_panel_iterator = current_panel_iterator;
  }
}

// static
void DockedPanelDragHandler::DragRight(Panel* panel) {
  DockedPanelCollection* collection =
      static_cast<DockedPanelCollection*>(panel->collection());

  // This is the right corner of the dragging panel. We use it to check against
  // all the panels on its right.
  int dragging_panel_right_boundary = panel->GetBounds().x() +
      panel->GetBounds().width() - 1;

  // Checks the panels to the right of the dragging panel.
  DockedPanelCollection::Panels::iterator dragging_panel_iterator =
      find(collection->panels_.begin(), collection->panels_.end(), panel);
  DockedPanelCollection::Panels::iterator current_panel_iterator =
      dragging_panel_iterator;
  while (current_panel_iterator != collection->panels_.begin()) {
    current_panel_iterator--;
    Panel* current_panel = *current_panel_iterator;

    // Can we swap dragging panel with its right panel? The criterion is that
    // the left corner of dragging panel should pass the middle position of
    // its right panel.
    if (dragging_panel_right_boundary < current_panel->GetBounds().x() +
            current_panel->GetBounds().width() / 2)
      break;

    // Swaps the contents and makes |dragging_panel_iterator| refers to the new
    // position.
    *dragging_panel_iterator = current_panel;
    *current_panel_iterator = panel;
    dragging_panel_iterator = current_panel_iterator;
  }
}