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
3// found in the LICENSE file.
4
5#ifndef ASH_WM_WORKSPACE_SNAP_SIZER_H_
6#define ASH_WM_WORKSPACE_SNAP_SIZER_H_
7
8#include <vector>
9
10#include "ash/ash_export.h"
11#include "base/basictypes.h"
12#include "base/time/time.h"
13#include "ui/gfx/rect.h"
14
15namespace ash {
16namespace wm {
17class WindowState;
18}
19
20namespace internal {
21
22// SnapSizer is responsible for determining the resulting bounds of a window
23// that is being snapped to the left or right side of the screen.
24// The bounds used in this class are in the container's coordinates.
25class ASH_EXPORT SnapSizer {
26 public:
27  enum Edge {
28    LEFT_EDGE,
29    RIGHT_EDGE
30  };
31
32  enum InputType {
33    TOUCH_MAXIMIZE_BUTTON_INPUT,
34    OTHER_INPUT
35  };
36
37  // Set |input_type| to |TOUCH_MAXIMIZE_BUTTON_INPUT| when called by a touch
38  // operation by the maximize button. This will allow the user to snap resize
39  // the window beginning close to the border.
40  SnapSizer(wm::WindowState* window_state,
41            const gfx::Point& start,
42            Edge edge,
43            InputType input_type);
44  virtual ~SnapSizer();
45
46  // Snaps a window left or right.
47  static void SnapWindow(wm::WindowState* window_state, Edge edge);
48
49  // Snaps |window_| to the target bounds.
50  void SnapWindowToTargetBounds();
51
52  // Updates the target bounds based on a mouse move.
53  void Update(const gfx::Point& location);
54
55  // Bounds to position the window at.
56  const gfx::Rect& target_bounds() const { return target_bounds_; }
57
58  // Returns the appropriate snap bounds (e.g. if a window is already snapped,
59  // then it returns the next snap-bounds).
60  gfx::Rect GetSnapBounds(const gfx::Rect& bounds);
61
62  // Set the snap sizer to the button press default size and prevent resizing.
63  void SelectDefaultSizeAndDisableResize();
64
65  // Returns the target bounds based on the edge and the provided |size_index|.
66  // For unit test purposes this function is not private.
67  gfx::Rect GetTargetBoundsForSize(size_t size_index) const;
68
69  // Returns true when snapping sequence is at its last (docking) step.
70  bool end_of_sequence() const { return end_of_sequence_; }
71
72 private:
73  // Calculates the amount to increment by. This returns one of -1, 0 or 1 and
74  // is intended to by applied to |size_index_|. |x| is the current
75  // x-coordinate, and |reference_x| is used to determine whether to increase
76  // or decrease the position. It's one of |last_adjust_x_| or |last_update_x_|.
77  int CalculateIncrement(int x, int reference_x) const;
78
79  // Changes the bounds. |x| is the current x-coordinate and |delta| the amount
80  // to increase by. |delta| comes from CalculateIncrement() and is applied
81  // to |size_index_|.
82  void ChangeBounds(int x, int delta);
83
84  // Returns the target bounds based on the edge and |size_index_|.
85  gfx::Rect GetTargetBounds() const;
86
87  // Returns true if the specified point is along the edge of the screen.
88  bool AlongEdge(int x) const;
89
90  // WindowState of the window being snapped.
91  wm::WindowState* window_state_;
92
93  const Edge edge_;
94
95  // Current target bounds for the snap.
96  gfx::Rect target_bounds_;
97
98  // Time Update() was last invoked.
99  base::TimeTicks time_last_update_;
100
101  // Index into |kSizes| that dictates the width of the screen the target
102  // bounds should get.
103  int size_index_;
104
105  // Set to true when an attempt is made to increment |size_index_| past
106  // the size of |usable_width_|.
107  bool end_of_sequence_;
108
109  // If set, |size_index_| will get ignored and the single button default
110  // setting will be used instead.
111  bool resize_disabled_;
112
113  // Number of times Update() has been invoked since last ChangeBounds().
114  int num_moves_since_adjust_;
115
116  // X-coordinate the last time ChangeBounds() was invoked.
117  int last_adjust_x_;
118
119  // X-coordinate last supplied to Update().
120  int last_update_x_;
121
122  // Initial x-coordinate.
123  const int start_x_;
124
125  // |TOUCH_MAXIMIZE_BUTTON_INPUT| if the snap sizer was created through a
126  // touch & drag operation of the maximizer button. It changes the behavior of
127  // the drag / resize behavior when the dragging starts close to the border.
128  const InputType input_type_;
129
130  // A list of usable window widths for size. This gets created when the
131  // sizer gets created.
132  const std::vector<int> usable_width_;
133
134  DISALLOW_COPY_AND_ASSIGN(SnapSizer);
135};
136
137}  // namespace internal
138}  // namespace ash
139
140#endif  // ASH_WM_WORKSPACE_SNAP_SIZER_H_
141