summaryrefslogtreecommitdiffstats
path: root/ash/wm/workspace/frame_maximize_button.cc
blob: 6b633104da27db51008a35126382a5f2dd25f475 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// 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/workspace/frame_maximize_button.h"

#include "ash/screen_ash.h"
#include "ash/shell.h"
#include "ash/wm/property_util.h"
#include "ash/launcher/launcher.h"
#include "ash/wm/workspace/phantom_window_controller.h"
#include "ash/wm/workspace/snap_sizer.h"
#include "grit/ash_strings.h"
#include "grit/ui_resources.h"
#include "ui/aura/event.h"
#include "ui/aura/event_filter.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/screen.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"

using ash::internal::SnapSizer;

namespace ash {

namespace {

// Delay before forcing an update of the snap location.
const int kUpdateDelayMS = 400;

}

// EscapeEventFilter is installed on the RootWindow to track when the escape key
// is pressed. We use an EventFilter for this as the FrameMaximizeButton
// normally does not get focus.
class FrameMaximizeButton::EscapeEventFilter : public aura::EventFilter {
 public:
  explicit EscapeEventFilter(FrameMaximizeButton* button);
  virtual ~EscapeEventFilter();

  // EventFilter overrides:
  virtual bool PreHandleKeyEvent(aura::Window* target,
                                 aura::KeyEvent* event) OVERRIDE;
  virtual bool PreHandleMouseEvent(aura::Window* target,
                                   aura::MouseEvent* event) OVERRIDE;
  virtual ui::TouchStatus PreHandleTouchEvent(
      aura::Window* target,
      aura::TouchEvent* event) OVERRIDE;
  virtual ui::GestureStatus PreHandleGestureEvent(
      aura::Window* target,
      aura::GestureEvent* event) OVERRIDE;

 private:
  FrameMaximizeButton* button_;

  DISALLOW_COPY_AND_ASSIGN(EscapeEventFilter);
};

FrameMaximizeButton::EscapeEventFilter::EscapeEventFilter(
    FrameMaximizeButton* button)
    : button_(button) {
  Shell::GetInstance()->AddRootWindowEventFilter(this);
}

FrameMaximizeButton::EscapeEventFilter::~EscapeEventFilter() {
  Shell::GetInstance()->RemoveRootWindowEventFilter(this);
}

bool FrameMaximizeButton::EscapeEventFilter::PreHandleKeyEvent(
    aura::Window* target,
    aura::KeyEvent* event) {
  if (event->type() == ui::ET_KEY_PRESSED &&
      event->key_code() == ui::VKEY_ESCAPE) {
    button_->Cancel();
  }
  return false;
}

bool FrameMaximizeButton::EscapeEventFilter::PreHandleMouseEvent(
    aura::Window* target,
    aura::MouseEvent* event) {
  return false;
}

ui::TouchStatus FrameMaximizeButton::EscapeEventFilter::PreHandleTouchEvent(
      aura::Window* target,
      aura::TouchEvent* event) {
  return ui::TOUCH_STATUS_UNKNOWN;
}

ui::GestureStatus FrameMaximizeButton::EscapeEventFilter::PreHandleGestureEvent(
      aura::Window* target,
      aura::GestureEvent* event) {
  return ui::GESTURE_STATUS_UNKNOWN;
}

// FrameMaximizeButton ---------------------------------------------------------

FrameMaximizeButton::FrameMaximizeButton(views::ButtonListener* listener,
                                         views::NonClientFrameView* frame)
    : ImageButton(listener),
      frame_(frame),
      is_snap_enabled_(false),
      is_left_right_enabled_(true),
      is_maximize_enabled_(true),
      exceeded_drag_threshold_(false),
      snap_type_(SNAP_NONE) {
  // TODO(sky): nuke this. It's temporary while we don't have good images.
  SetImageAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
  SetTooltipText(l10n_util::GetStringUTF16(IDS_FRAME_MAXIMIZE_BUTTON_TOOLTIP));
}

FrameMaximizeButton::~FrameMaximizeButton() {
}

bool FrameMaximizeButton::OnMousePressed(const views::MouseEvent& event) {
  is_snap_enabled_ = event.IsLeftMouseButton();
  if (is_snap_enabled_) {
    snap_sizer_.reset(NULL);
    InstallEventFilter();
    snap_type_ = SNAP_NONE;
    press_location_ = event.location();
    exceeded_drag_threshold_ = false;
    update_timer_.Start(
        FROM_HERE,
        base::TimeDelta::FromMilliseconds(kUpdateDelayMS),
        this, &FrameMaximizeButton::UpdateSnapFromCursorScreenPoint);
  }
  ImageButton::OnMousePressed(event);
  return true;
}

void FrameMaximizeButton::OnMouseEntered(const views::MouseEvent& event) {
  ImageButton::OnMouseEntered(event);
}

void FrameMaximizeButton::OnMouseExited(const views::MouseEvent& event) {
  ImageButton::OnMouseExited(event);
}

bool FrameMaximizeButton::OnMouseDragged(const views::MouseEvent& event) {
  if (is_snap_enabled_) {
    int delta_x = event.location().x() - press_location_.x();
    int delta_y = event.location().y() - press_location_.y();
    if (!exceeded_drag_threshold_) {
      exceeded_drag_threshold_ =
          views::View::ExceededDragThreshold(delta_x, delta_y);
    }
    if (exceeded_drag_threshold_)
      UpdateSnap(event.location());
  }
  return ImageButton::OnMouseDragged(event);
}

void FrameMaximizeButton::OnMouseReleased(const views::MouseEvent& event) {
  update_timer_.Stop();
  UninstallEventFilter();
  bool should_snap = is_snap_enabled_;
  is_snap_enabled_ = false;
  if (should_snap && snap_type_ != SNAP_NONE) {
    SetState(BS_NORMAL);
    // SetState will not call SchedulePaint() if state was already set to
    // BS_NORMAL during a drag.
    SchedulePaint();
    phantom_window_.reset();
    Snap();
  } else {
    ImageButton::OnMouseReleased(event);
  }
}

void FrameMaximizeButton::OnMouseCaptureLost() {
  Cancel();
  ImageButton::OnMouseCaptureLost();
}

void FrameMaximizeButton::SetIsLeftRightEnabled(bool e) {
  is_left_right_enabled_ = e;
  int id = is_left_right_enabled_ ?
      IDS_FRAME_MAXIMIZE_BUTTON_TOOLTIP :
      IDS_FRAME_MAXIMIZE_BUTTON_NO_SIDE_SNAP_TOOLTIP;
  SetTooltipText(l10n_util::GetStringUTF16(id));
}

SkBitmap FrameMaximizeButton::GetImageToPaint() {
  if (is_snap_enabled_) {
    int id = 0;
    if (frame_->GetWidget()->IsMaximized()) {
      switch (snap_type_) {
        case SNAP_LEFT:
          id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_LEFT_P;
          break;
        case SNAP_RIGHT:
          id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_RIGHT_P;
          break;
        case SNAP_MAXIMIZE:
        case SNAP_RESTORE:
        case SNAP_NONE:
          id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_P;
          break;
        case SNAP_MINIMIZE:
          id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_MINIMIZE_P;
          break;
        default:
          NOTREACHED();
      }
    } else {
      switch (snap_type_) {
        case SNAP_LEFT:
          id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_LEFT_P;
          break;
        case SNAP_RIGHT:
          id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_RIGHT_P;
          break;
        case SNAP_MAXIMIZE:
        case SNAP_RESTORE:
        case SNAP_NONE:
          id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_P;
          break;
        case SNAP_MINIMIZE:
          id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_MINIMIZE_P;
          break;
        default:
          NOTREACHED();
      }
    }
    return *ResourceBundle::GetSharedInstance().GetImageNamed(id).ToSkBitmap();
  } else if (state() == BS_HOT) {
    return *ResourceBundle::GetSharedInstance().GetImageNamed(
        IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_P).ToSkBitmap();
  }
  return ImageButton::GetImageToPaint();
}

void FrameMaximizeButton::Cancel() {
  UninstallEventFilter();
  is_snap_enabled_ = false;
  phantom_window_.reset();
  snap_sizer_.reset();
  update_timer_.Stop();
  SchedulePaint();
}

void FrameMaximizeButton::InstallEventFilter() {
  if (escape_event_filter_.get())
    return;

  escape_event_filter_.reset(new EscapeEventFilter(this));
}

void FrameMaximizeButton::UninstallEventFilter() {
  escape_event_filter_.reset(NULL);
}

void FrameMaximizeButton::UpdateSnapFromCursorScreenPoint() {
  // If the drag threshold has been exceeded the snap location is up to date.
  if (exceeded_drag_threshold_)
    return;
  exceeded_drag_threshold_ = true;
  gfx::Point cursor_point(gfx::Screen::GetCursorScreenPoint());
  ConvertPointFromScreen(this, &cursor_point);
  UpdateSnap(cursor_point);
}

void FrameMaximizeButton::UpdateSnap(const gfx::Point& location) {
  SnapType type = SnapTypeForLocation(location);
  if (type == snap_type_) {
    if (snap_sizer_.get()) {
      snap_sizer_->Update(LocationForSnapSizer(location));
      phantom_window_->Show(snap_sizer_->target_bounds());
    }
    return;
  }

  snap_type_ = type;
  snap_sizer_.reset();
  SchedulePaint();

  if (snap_type_ == SNAP_NONE) {
    phantom_window_.reset();
    return;
  }

  if (snap_type_ == SNAP_LEFT || snap_type_ == SNAP_RIGHT) {
    SnapSizer::Edge snap_edge = snap_type_ == SNAP_LEFT ?
        SnapSizer::LEFT_EDGE : SnapSizer::RIGHT_EDGE;
    int grid_size = Shell::GetInstance()->GetGridSize();
    snap_sizer_.reset(new SnapSizer(frame_->GetWidget()->GetNativeWindow(),
                                    LocationForSnapSizer(location),
                                    snap_edge, grid_size));
  }
  if (!phantom_window_.get()) {
    phantom_window_.reset(new internal::PhantomWindowController(
                              frame_->GetWidget()->GetNativeWindow()));
  }
  phantom_window_->Show(BoundsForType(snap_type_));
}

bool FrameMaximizeButton::AllowMaximize() const {
  return !frame_->GetWidget()->IsMaximized() && is_maximize_enabled_;
}

FrameMaximizeButton::SnapType FrameMaximizeButton::SnapTypeForLocation(
    const gfx::Point& location) const {
  int delta_x = location.x() - press_location_.x();
  int delta_y = location.y() - press_location_.y();
  if (!views::View::ExceededDragThreshold(delta_x, delta_y))
    return AllowMaximize() ? SNAP_MAXIMIZE : SNAP_RESTORE;
  else if (delta_x < 0 && delta_y > delta_x && delta_y < -delta_x)
    return is_left_right_enabled_ ? SNAP_LEFT : SNAP_NONE;
  else if (delta_x > 0 && delta_y > -delta_x && delta_y < delta_x)
    return is_left_right_enabled_ ? SNAP_RIGHT : SNAP_NONE;
  else if (delta_y > 0)
    return SNAP_MINIMIZE;
  return AllowMaximize() ? SNAP_MAXIMIZE : SNAP_RESTORE;
}

gfx::Rect FrameMaximizeButton::BoundsForType(SnapType type) const {
  aura::Window* window = frame_->GetWidget()->GetNativeWindow();
  switch (type) {
    case SNAP_LEFT:
    case SNAP_RIGHT:
      return snap_sizer_->target_bounds();
    case SNAP_MAXIMIZE:
      return ScreenAsh::GetMaximizedWindowBounds(window);
    case SNAP_MINIMIZE: {
      Launcher* launcher = Shell::GetInstance()->launcher();
      gfx::Rect item_rect(launcher->GetScreenBoundsOfItemIconForWindow(window));
      if (!item_rect.IsEmpty()) {
        // PhantomWindowController insets slightly, outset it so the phantom
        // doesn't appear inset.
        item_rect.Inset(-8, -8);
        return item_rect;
      }
      return launcher->widget()->GetWindowScreenBounds();
    }
    case SNAP_RESTORE: {
      const gfx::Rect* restore = GetRestoreBounds(window);
      return restore ? *restore : frame_->GetWidget()->GetWindowScreenBounds();
    }
    case SNAP_NONE:
      NOTREACHED();
  }
  return gfx::Rect();
}

gfx::Point FrameMaximizeButton::LocationForSnapSizer(
    const gfx::Point& location) const {
  gfx::Point result(location);
  views::View::ConvertPointToScreen(this, &result);
  return result;
}

void FrameMaximizeButton::Snap() {
  switch (snap_type_) {
    case SNAP_LEFT:
    case SNAP_RIGHT:
      if (frame_->GetWidget()->IsMaximized()) {
        ash::SetRestoreBounds(frame_->GetWidget()->GetNativeWindow(),
                              BoundsForType(snap_type_));
        frame_->GetWidget()->Restore();
      } else {
        frame_->GetWidget()->SetBounds(BoundsForType(snap_type_));
      }
      break;
    case SNAP_MAXIMIZE:
      frame_->GetWidget()->Maximize();
      break;
    case SNAP_MINIMIZE:
      frame_->GetWidget()->Minimize();
      break;
    case SNAP_RESTORE:
      frame_->GetWidget()->Restore();
      break;
    case SNAP_NONE:
      NOTREACHED();
  }
}

}  // namespace ash