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 #include "ash/wm/workspace/snap_sizer.h" | 5 #include "ash/wm/workspace/snap_sizer.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "ash/screen_ash.h" | 9 #include "ash/screen_ash.h" |
10 #include "ash/wm/window_resizer.h" | 10 #include "ash/wm/window_resizer.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 if (std::abs(location.x() - last_adjust_x_) >= kPixelsBeforeAdjust || | 59 if (std::abs(location.x() - last_adjust_x_) >= kPixelsBeforeAdjust || |
60 (along_edge && num_moves_since_adjust_ >= kMovesBeforeAdjust)) { | 60 (along_edge && num_moves_since_adjust_ >= kMovesBeforeAdjust)) { |
61 ChangeBounds(location.x(), | 61 ChangeBounds(location.x(), |
62 CalculateIncrement(location.x(), last_adjust_x_)); | 62 CalculateIncrement(location.x(), last_adjust_x_)); |
63 } | 63 } |
64 } | 64 } |
65 last_update_x_ = location.x(); | 65 last_update_x_ = location.x(); |
66 time_last_update_ = base::TimeTicks::Now(); | 66 time_last_update_ = base::TimeTicks::Now(); |
67 } | 67 } |
68 | 68 |
| 69 gfx::Rect SnapSizer::GetSnapBounds(const gfx::Rect& bounds) { |
| 70 size_t current; |
| 71 for (current = 0; current < arraysize(kPercents); ++current) { |
| 72 gfx::Rect target = GetTargetBoundsForPercent(current); |
| 73 if (target == bounds) { |
| 74 ++current; |
| 75 break; |
| 76 } |
| 77 } |
| 78 return GetTargetBoundsForPercent(current % arraysize(kPercents)); |
| 79 } |
| 80 |
69 int SnapSizer::CalculateIncrement(int x, int reference_x) const { | 81 int SnapSizer::CalculateIncrement(int x, int reference_x) const { |
70 if (AlongEdge(x)) | 82 if (AlongEdge(x)) |
71 return 1; | 83 return 1; |
72 if (x == reference_x) | 84 if (x == reference_x) |
73 return 0; | 85 return 0; |
74 if (edge_ == LEFT_EDGE) { | 86 if (edge_ == LEFT_EDGE) { |
75 if (x < reference_x) | 87 if (x < reference_x) |
76 return 1; | 88 return 1; |
77 return -1; | 89 return -1; |
78 } | 90 } |
79 // edge_ == RIGHT_EDGE. | 91 // edge_ == RIGHT_EDGE. |
80 if (x > reference_x) | 92 if (x > reference_x) |
81 return 1; | 93 return 1; |
82 return -1; | 94 return -1; |
83 } | 95 } |
84 | 96 |
85 void SnapSizer::ChangeBounds(int x, int delta) { | 97 void SnapSizer::ChangeBounds(int x, int delta) { |
86 int index = std::min(static_cast<int>(arraysize(kPercents)) - 1, | 98 int index = std::min(static_cast<int>(arraysize(kPercents)) - 1, |
87 std::max(percent_index_ + delta, 0)); | 99 std::max(percent_index_ + delta, 0)); |
88 if (index != percent_index_) { | 100 if (index != percent_index_) { |
89 percent_index_ = index; | 101 percent_index_ = index; |
90 target_bounds_ = GetTargetBounds(); | 102 target_bounds_ = GetTargetBounds(); |
91 } | 103 } |
92 num_moves_since_adjust_ = 0; | 104 num_moves_since_adjust_ = 0; |
93 last_adjust_x_ = x; | 105 last_adjust_x_ = x; |
94 } | 106 } |
95 | 107 |
96 gfx::Rect SnapSizer::GetTargetBounds() const { | 108 gfx::Rect SnapSizer::GetTargetBounds() const { |
| 109 return GetTargetBoundsForPercent(percent_index_); |
| 110 } |
| 111 |
| 112 gfx::Rect SnapSizer::GetTargetBoundsForPercent(int percent_index) const { |
97 gfx::Rect work_area(ScreenAsh::GetUnmaximizedWorkAreaBounds(window_)); | 113 gfx::Rect work_area(ScreenAsh::GetUnmaximizedWorkAreaBounds(window_)); |
98 int y = WindowResizer::AlignToGridRoundUp(work_area.y(), grid_size_); | 114 int y = WindowResizer::AlignToGridRoundUp(work_area.y(), grid_size_); |
99 // We don't align to the bottom of the grid as the launcher may not | 115 // We don't align to the bottom of the grid as the launcher may not |
100 // necessarily align to the grid (happens when auto-hidden). | 116 // necessarily align to the grid (happens when auto-hidden). |
101 int max_y = work_area.bottom(); | 117 int max_y = work_area.bottom(); |
102 int width = static_cast<float>(work_area.width()) * kPercents[percent_index_]; | 118 int width = static_cast<float>(work_area.width()) * kPercents[percent_index]; |
103 if (edge_ == LEFT_EDGE) { | 119 if (edge_ == LEFT_EDGE) { |
104 int x = WindowResizer::AlignToGridRoundUp(work_area.x(), grid_size_); | 120 int x = WindowResizer::AlignToGridRoundUp(work_area.x(), grid_size_); |
105 int mid_x = WindowResizer::AlignToGridRoundUp( | 121 int mid_x = WindowResizer::AlignToGridRoundUp( |
106 work_area.x() + width, grid_size_); | 122 work_area.x() + width, grid_size_); |
107 return gfx::Rect(x, y, mid_x - x, max_y - y); | 123 return gfx::Rect(x, y, mid_x - x, max_y - y); |
108 } | 124 } |
109 int max_x = | 125 int max_x = |
110 WindowResizer::AlignToGridRoundDown(work_area.right(), grid_size_); | 126 WindowResizer::AlignToGridRoundDown(work_area.right(), grid_size_); |
111 int x = WindowResizer::AlignToGridRoundUp(max_x - width, grid_size_); | 127 int x = WindowResizer::AlignToGridRoundUp(max_x - width, grid_size_); |
112 return gfx::Rect(x , y, max_x - x, max_y - y); | 128 return gfx::Rect(x , y, max_x - x, max_y - y); |
113 } | 129 } |
114 | 130 |
115 bool SnapSizer::AlongEdge(int x) const { | 131 bool SnapSizer::AlongEdge(int x) const { |
116 // TODO: need to support multi-monitor. | 132 // TODO: need to support multi-monitor. |
117 gfx::Rect area(gfx::Screen::GetMonitorNearestWindow(window_).bounds()); | 133 gfx::Rect area(gfx::Screen::GetMonitorNearestWindow(window_).bounds()); |
118 return (x <= area.x()) || (x >= area.right() - 1); | 134 return (x <= area.x()) || (x >= area.right() - 1); |
119 } | 135 } |
120 | 136 |
121 } // namespace internal | 137 } // namespace internal |
122 } // namespace ash | 138 } // namespace ash |
OLD | NEW |