summaryrefslogtreecommitdiffstats
path: root/chromium/ash/wm/panels/panel_window_resizer.cc
blob: 739544f97d4a391acf2cdf1ad6de3ce4846a33ae (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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 "ash/wm/panels/panel_window_resizer.h"

#include "ash/display/display_controller.h"
#include "ash/launcher/launcher.h"
#include "ash/screen_ash.h"
#include "ash/shelf/shelf_types.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/coordinate_conversion.h"
#include "ash/wm/panels/panel_layout_manager.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/memory/weak_ptr.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/window_tree_client.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/base/hit_test.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/screen.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {
const int kPanelSnapToLauncherDistance = 30;

internal::PanelLayoutManager* GetPanelLayoutManager(
    aura::Window* panel_container) {
  DCHECK(panel_container->id() == internal::kShellWindowId_PanelContainer);
  return static_cast<internal::PanelLayoutManager*>(
      panel_container->layout_manager());
}

}  // namespace

PanelWindowResizer::~PanelWindowResizer() {
}

// static
PanelWindowResizer*
PanelWindowResizer::Create(WindowResizer* next_window_resizer,
                           aura::Window* window,
                           const gfx::Point& location,
                           int window_component,
                           aura::client::WindowMoveSource source) {
  Details details(window, location, window_component, source);
  return details.is_resizable ?
      new PanelWindowResizer(next_window_resizer, details) : NULL;
}

void PanelWindowResizer::Drag(const gfx::Point& location, int event_flags) {
  last_location_ = location;
  wm::ConvertPointToScreen(GetTarget()->parent(), &last_location_);
  if (!did_move_or_resize_) {
    did_move_or_resize_ = true;
    StartedDragging();
  }

  // Check if the destination has changed displays.
  gfx::Screen* screen = Shell::GetScreen();
  const gfx::Display dst_display =
      screen->GetDisplayNearestPoint(last_location_);
  if (dst_display.id() !=
      screen->GetDisplayNearestWindow(panel_container_->GetRootWindow()).id()) {
    // The panel is being dragged to a new display. If the previous container is
    // the current parent of the panel it will be informed of the end of drag
    // when the panel is reparented, otherwise let the previous container know
    // the drag is complete. If we told the panel's parent that the drag was
    // complete it would begin positioning the panel.
    if (GetTarget()->parent() != panel_container_)
      GetPanelLayoutManager(panel_container_)->FinishDragging();
    aura::Window* dst_root = Shell::GetInstance()->display_controller()->
        GetRootWindowForDisplayId(dst_display.id());
    panel_container_ = Shell::GetContainer(
        dst_root, internal::kShellWindowId_PanelContainer);

    // The panel's parent already knows that the drag is in progress for this
    // panel.
    if (panel_container_ && GetTarget()->parent() != panel_container_)
      GetPanelLayoutManager(panel_container_)->StartDragging(GetTarget());
  }
  gfx::Point offset;
  gfx::Rect bounds(CalculateBoundsForDrag(details_, location));
  should_attach_ = AttachToLauncher(bounds, &offset);
  gfx::Point modified_location(location.x() + offset.x(),
                               location.y() + offset.y());

  base::WeakPtr<PanelWindowResizer> resizer(weak_ptr_factory_.GetWeakPtr());
  next_window_resizer_->Drag(modified_location, event_flags);
  if (!resizer)
    return;

  if (should_attach_ &&
      !(details_.bounds_change & WindowResizer::kBoundsChange_Resizes)) {
    UpdateLauncherPosition();
  }
}

void PanelWindowResizer::CompleteDrag(int event_flags) {
  // The root window can change when dragging into a different screen.
  next_window_resizer_->CompleteDrag(event_flags);
  FinishDragging();
}

void PanelWindowResizer::RevertDrag() {
  next_window_resizer_->RevertDrag();
  should_attach_ = was_attached_;
  FinishDragging();
}

aura::Window* PanelWindowResizer::GetTarget() {
  return next_window_resizer_->GetTarget();
}

const gfx::Point& PanelWindowResizer::GetInitialLocation() const {
  return details_.initial_location_in_parent;
}

PanelWindowResizer::PanelWindowResizer(WindowResizer* next_window_resizer,
                                       const Details& details)
    : details_(details),
      next_window_resizer_(next_window_resizer),
      panel_container_(NULL),
      initial_panel_container_(NULL),
      did_move_or_resize_(false),
      was_attached_(wm::GetWindowState(GetTarget())->panel_attached()),
      should_attach_(was_attached_),
      weak_ptr_factory_(this) {
  DCHECK(details_.is_resizable);
  panel_container_ = Shell::GetContainer(
      details.window->GetRootWindow(),
      internal::kShellWindowId_PanelContainer);
  initial_panel_container_ = panel_container_;
}

bool PanelWindowResizer::AttachToLauncher(const gfx::Rect& bounds,
                                          gfx::Point* offset) {
  bool should_attach = false;
  if (panel_container_) {
    internal::PanelLayoutManager* panel_layout_manager =
        GetPanelLayoutManager(panel_container_);
    gfx::Rect launcher_bounds = ScreenAsh::ConvertRectFromScreen(
        GetTarget()->parent(),
        panel_layout_manager->launcher()->
        shelf_widget()->GetWindowBoundsInScreen());
    switch (panel_layout_manager->launcher()->alignment()) {
      case SHELF_ALIGNMENT_BOTTOM:
        if (bounds.bottom() >= (launcher_bounds.y() -
                                kPanelSnapToLauncherDistance)) {
          should_attach = true;
          offset->set_y(launcher_bounds.y() - bounds.height() - bounds.y());
        }
        break;
      case SHELF_ALIGNMENT_LEFT:
        if (bounds.x() <= (launcher_bounds.right() +
                           kPanelSnapToLauncherDistance)) {
          should_attach = true;
          offset->set_x(launcher_bounds.right() - bounds.x());
        }
        break;
      case SHELF_ALIGNMENT_RIGHT:
        if (bounds.right() >= (launcher_bounds.x() -
                               kPanelSnapToLauncherDistance)) {
          should_attach = true;
          offset->set_x(launcher_bounds.x() - bounds.width() - bounds.x());
        }
        break;
      case SHELF_ALIGNMENT_TOP:
        if (bounds.y() <= (launcher_bounds.bottom() +
                           kPanelSnapToLauncherDistance)) {
          should_attach = true;
          offset->set_y(launcher_bounds.bottom() - bounds.y());
        }
        break;
    }
  }
  return should_attach;
}

void PanelWindowResizer::StartedDragging() {
  // Tell the panel layout manager that we are dragging this panel before
  // attaching it so that it does not get repositioned.
  if (panel_container_)
    GetPanelLayoutManager(panel_container_)->StartDragging(GetTarget());
  if (!was_attached_) {
    // Attach the panel while dragging placing it in front of other panels.
    wm::GetWindowState(GetTarget())->set_continue_drag_after_reparent(true);
    wm::GetWindowState(GetTarget())->set_panel_attached(true);
    // We use root window coordinates to ensure that during the drag the panel
    // is reparented to a container in the root window that has that window.
    aura::Window* target = GetTarget();
    aura::Window* target_root = target->GetRootWindow();
    aura::Window* old_parent = target->parent();
    aura::client::ParentWindowWithContext(
        target, target_root, target_root->GetBoundsInScreen());
    wm::ReparentTransientChildrenOfChild(target, old_parent, target->parent());
  }
}

void PanelWindowResizer::FinishDragging() {
  if (!did_move_or_resize_)
    return;
  if (details_.bounds_change & WindowResizer::kBoundsChange_Resizes)
    should_attach_ = was_attached_;
  if (wm::GetWindowState(GetTarget())->panel_attached() != should_attach_) {
    wm::GetWindowState(GetTarget())->set_panel_attached(should_attach_);
    // We use last known location to ensure that after the drag the panel
    // is reparented to a container in the root window that has that location.
    aura::Window* target = GetTarget();
    aura::Window* target_root = target->GetRootWindow();
    aura::Window* old_parent = target->parent();
    aura::client::ParentWindowWithContext(
        target, target_root, gfx::Rect(last_location_, gfx::Size()));
    wm::ReparentTransientChildrenOfChild(target, old_parent, target->parent());
  }

  // If we started the drag in one root window and moved into another root
  // but then canceled the drag we may need to inform the original layout
  // manager that the drag is finished.
  if (initial_panel_container_ != panel_container_)
     GetPanelLayoutManager(initial_panel_container_)->FinishDragging();
  if (panel_container_)
    GetPanelLayoutManager(panel_container_)->FinishDragging();
}

void PanelWindowResizer::UpdateLauncherPosition() {
  if (panel_container_) {
    GetPanelLayoutManager(panel_container_)->launcher()->
        UpdateIconPositionForWindow(GetTarget());
  }
}

}  // namespace aura