summaryrefslogtreecommitdiffstats
path: root/chrome/browser/dock_info.cc
blob: d52ddc9fcdd099c7077c4766f5bbc04813ad14c1 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2006-2008 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/dock_info.h"

#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/views/tabs/tab.h"
#else
#include "chrome/browser/gtk/tabs/tab_gtk.h"
#endif

namespace {

// Distance in pixels between the hotspot and when the hint should be shown.
const int kHotSpotDeltaX = 120;
const int kHotSpotDeltaY = 120;

// Size of the popup window.
const int kPopupWidth = 70;
const int kPopupHeight = 70;

}  // namespace

// static
DockInfo::Factory* DockInfo::factory_ = NULL;

// static
bool DockInfo::IsCloseToPoint(const gfx::Point& screen_loc,
                              int x,
                              int y,
                              bool* in_enable_area) {
  int delta_x = abs(x - screen_loc.x());
  int delta_y = abs(y - screen_loc.y());
  *in_enable_area = (delta_x < kPopupWidth / 2 && delta_y < kPopupHeight / 2);
  return *in_enable_area || (delta_x < kHotSpotDeltaX &&
                             delta_y < kHotSpotDeltaY);
}

// static
bool DockInfo::IsCloseToMonitorPoint(const gfx::Point& screen_loc,
                                     int x,
                                     int y,
                                     DockInfo::Type type,
                                     bool* in_enable_area) {
  // Because the monitor relative positions are aligned with the edge of the
  // monitor these need to be handled differently.
  int delta_x = abs(x - screen_loc.x());
  int delta_y = abs(y - screen_loc.y());

  int enable_delta_x = kPopupWidth / 2;
  int enable_delta_y = kPopupHeight / 2;
  int hot_spot_delta_x = kHotSpotDeltaX;
  int hot_spot_delta_y = kHotSpotDeltaY;

  switch (type) {
    case DockInfo::LEFT_HALF:
    case DockInfo::RIGHT_HALF:
      enable_delta_x += enable_delta_x;
      hot_spot_delta_x += hot_spot_delta_x;
      break;


    case DockInfo::MAXIMIZE: {
      // Make the maximize height smaller than the tab height to avoid showing
      // the dock indicator when close to maximized browser.
#if defined(TOOLKIT_VIEWS)
      hot_spot_delta_y = Tab::GetMinimumUnselectedSize().height() - 1;
#else
      hot_spot_delta_y = TabGtk::GetMinimumUnselectedSize().height() - 1;
#endif
      enable_delta_y = hot_spot_delta_y / 2;
      break;
    }
    case DockInfo::BOTTOM_HALF:
      enable_delta_y += enable_delta_y;
      hot_spot_delta_y += hot_spot_delta_y;
      break;

    default:
      NOTREACHED();
      return false;
  }
  *in_enable_area = (delta_x < enable_delta_x && delta_y < enable_delta_y);
  bool result = (*in_enable_area || (delta_x < hot_spot_delta_x &&
                                     delta_y < hot_spot_delta_y));
  if (type != DockInfo::MAXIMIZE)
    return result;

  // Make the hot spot/enable spot for maximized windows the whole top of the
  // monitor.
  int max_delta_y = abs(screen_loc.y() - y);
  *in_enable_area = (*in_enable_area || (max_delta_y < enable_delta_y));
  return *in_enable_area || (max_delta_y < hot_spot_delta_y);
}

// static
int DockInfo::popup_width() {
  return kPopupWidth;
}

// static
int DockInfo::popup_height() {
  return kPopupHeight;
}

bool DockInfo::IsValidForPoint(const gfx::Point& screen_point) {
  if (type_ == NONE)
    return false;

  if (window_) {
    return IsCloseToPoint(screen_point, hot_spot_.x(), hot_spot_.y(),
                          &in_enable_area_);
  }

  return monitor_bounds_.Contains(screen_point) &&
          IsCloseToMonitorPoint(screen_point, hot_spot_.x(),
                                hot_spot_.y(), type_, &in_enable_area_);
}

bool DockInfo::GetNewWindowBounds(gfx::Rect* new_window_bounds,
                                  bool* maximize_new_window) const {
  if (type_ == NONE || !in_enable_area_)
    return false;

  gfx::Rect window_bounds;
  if (window_ && !GetWindowBounds(&window_bounds))
    return false;

  int half_m_width = (monitor_bounds_.right() - monitor_bounds_.x()) / 2;
  int half_m_height = (monitor_bounds_.bottom() - monitor_bounds_.y()) / 2;

  *maximize_new_window = false;

  switch (type_) {
    case LEFT_OF_WINDOW:
      new_window_bounds->SetRect(monitor_bounds_.x(), window_bounds.y(),
                                 half_m_width, window_bounds.height());
      break;

    case RIGHT_OF_WINDOW:
      new_window_bounds->SetRect(monitor_bounds_.x() + half_m_width,
                                 window_bounds.y(), half_m_width,
                                 window_bounds.height());
      break;

    case TOP_OF_WINDOW:
      new_window_bounds->SetRect(window_bounds.x(), monitor_bounds_.y(),
                                 window_bounds.width(), half_m_height);
      break;

    case BOTTOM_OF_WINDOW:
      new_window_bounds->SetRect(window_bounds.x(),
                                 monitor_bounds_.y() + half_m_height,
                                 window_bounds.width(), half_m_height);
      break;

    case LEFT_HALF:
      new_window_bounds->SetRect(monitor_bounds_.x(), monitor_bounds_.y(),
                                 half_m_width, monitor_bounds_.height());
      break;

    case RIGHT_HALF:
      new_window_bounds->SetRect(monitor_bounds_.right() - half_m_width,
          monitor_bounds_.y(), half_m_width, monitor_bounds_.height());
      break;

    case BOTTOM_HALF:
      new_window_bounds->SetRect(monitor_bounds_.x(),
                                 monitor_bounds_.y() + half_m_height,
                                 monitor_bounds_.width(), half_m_height);
      break;

    case MAXIMIZE:
      *maximize_new_window = true;
      break;

    default:
      NOTREACHED();
  }
  return true;
}

void DockInfo::AdjustOtherWindowBounds() const {
  if (!in_enable_area_)
    return;

  gfx::Rect window_bounds;
  if (!window_ || !GetWindowBounds(&window_bounds))
    return;

  gfx::Rect other_window_bounds;
  int half_m_width = (monitor_bounds_.right() - monitor_bounds_.x()) / 2;
  int half_m_height = (monitor_bounds_.bottom() - monitor_bounds_.y()) / 2;

  switch (type_) {
    case LEFT_OF_WINDOW:
      other_window_bounds.SetRect(monitor_bounds_.x() + half_m_width,
                                  window_bounds.y(), half_m_width,
                                  window_bounds.height());
      break;

    case RIGHT_OF_WINDOW:
      other_window_bounds.SetRect(monitor_bounds_.x(), window_bounds.y(),
                                  half_m_width, window_bounds.height());
      break;

    case TOP_OF_WINDOW:
      other_window_bounds.SetRect(window_bounds.x(),
                                  monitor_bounds_.y() + half_m_height,
                                  window_bounds.width(), half_m_height);
      break;

    case BOTTOM_OF_WINDOW:
      other_window_bounds.SetRect(window_bounds.x(), monitor_bounds_.y(),
                                  window_bounds.width(), half_m_height);
      break;

    default:
      return;
  }

  SizeOtherWindowTo(other_window_bounds);
}

gfx::Rect DockInfo::GetPopupRect() const {
  int x = hot_spot_.x() - popup_width() / 2;
  int y = hot_spot_.y() - popup_height() / 2;
  switch (type_) {
    case LEFT_OF_WINDOW:
    case RIGHT_OF_WINDOW:
    case TOP_OF_WINDOW:
    case BOTTOM_OF_WINDOW: {
      // Constrain the popup to the monitor's bounds.
      gfx::Rect ideal_bounds(x, y, popup_width(), popup_height());
      ideal_bounds = ideal_bounds.AdjustToFit(monitor_bounds_);
      return ideal_bounds;
    }
    case DockInfo::MAXIMIZE:
      y += popup_height() / 2;
      break;
    case DockInfo::LEFT_HALF:
      x += popup_width() / 2;
      break;
    case DockInfo::RIGHT_HALF:
      x -= popup_width() / 2;
      break;
    case DockInfo::BOTTOM_HALF:
      y -= popup_height() / 2;
      break;

    default:
      NOTREACHED();
  }
  return gfx::Rect(x, y, popup_width(), popup_height());
}

bool DockInfo::CheckMonitorPoint(const gfx::Point& screen_loc,
                                 int x,
                                 int y,
                                 Type type) {
  if (IsCloseToMonitorPoint(screen_loc, x, y, type, &in_enable_area_)) {
    hot_spot_.SetPoint(x, y);
    type_ = type;
    return true;
  }
  return false;
}