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