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 "ui/views/view_model_utils.h" | 5 #include "ui/views/view_model_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ui/views/view_model.h" | 9 #include "ui/views/view_model.h" |
10 #include "ui/views/view.h" | 10 #include "ui/views/view.h" |
11 | 11 |
12 namespace views { | 12 namespace views { |
13 | 13 |
| 14 namespace { |
| 15 |
| 16 // Used in calculating ideal bounds. |
| 17 int primary_axis_coordinate(ViewModelUtils::Alignment alignment, |
| 18 int x, |
| 19 int y) { |
| 20 return alignment == ViewModelUtils::HORIZONTAL ? x : y; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
14 // static | 25 // static |
15 void ViewModelUtils::SetViewBoundsToIdealBounds(const ViewModel& model) { | 26 void ViewModelUtils::SetViewBoundsToIdealBounds(const ViewModel& model) { |
16 for (int i = 0; i < model.view_size(); ++i) | 27 for (int i = 0; i < model.view_size(); ++i) |
17 model.view_at(i)->SetBoundsRect(model.ideal_bounds(i)); | 28 model.view_at(i)->SetBoundsRect(model.ideal_bounds(i)); |
18 } | 29 } |
19 | 30 |
20 // static | 31 // static |
21 bool ViewModelUtils::IsAtIdealBounds(const ViewModel& model) { | 32 bool ViewModelUtils::IsAtIdealBounds(const ViewModel& model) { |
22 for (int i = 0; i < model.view_size(); ++i) { | 33 for (int i = 0; i < model.view_size(); ++i) { |
23 View* view = model.view_at(i); | 34 View* view = model.view_at(i); |
24 if (view->bounds() != model.ideal_bounds(i)) | 35 if (view->bounds() != model.ideal_bounds(i)) |
25 return false; | 36 return false; |
26 } | 37 } |
27 return true; | 38 return true; |
28 } | 39 } |
29 | 40 |
30 // static | 41 // static |
31 int ViewModelUtils::DetermineMoveIndex(const ViewModel& model, | 42 int ViewModelUtils::DetermineMoveIndex(const ViewModel& model, |
32 View* view, | 43 View* view, |
33 int x) { | 44 Alignment alignment, |
| 45 int x, |
| 46 int y) { |
| 47 int value = primary_axis_coordinate(alignment, x, y); |
34 int current_index = model.GetIndexOfView(view); | 48 int current_index = model.GetIndexOfView(view); |
35 DCHECK_NE(-1, current_index); | 49 DCHECK_NE(-1, current_index); |
36 for (int i = 0; i < current_index; ++i) { | 50 for (int i = 0; i < current_index; ++i) { |
37 int mid_x = model.ideal_bounds(i).x() + model.ideal_bounds(i).width() / 2; | 51 int mid_point = primary_axis_coordinate( |
38 if (x < mid_x) | 52 alignment, |
| 53 model.ideal_bounds(i).x() + model.ideal_bounds(i).width() / 2, |
| 54 model.ideal_bounds(i).y() + model.ideal_bounds(i).height() / 2); |
| 55 if (value < mid_point) |
39 return i; | 56 return i; |
40 } | 57 } |
41 | 58 |
42 if (current_index + 1 == model.view_size()) | 59 if (current_index + 1 == model.view_size()) |
43 return current_index; | 60 return current_index; |
44 | 61 |
45 // For indices after the current index ignore the bounds of the view being | 62 // For indices after the current index ignore the bounds of the view being |
46 // dragged. This keeps the view from bouncing around as moved. | 63 // dragged. This keeps the view from bouncing around as moved. |
47 int delta = model.ideal_bounds(current_index + 1).x() - | 64 int delta = primary_axis_coordinate( |
48 model.ideal_bounds(current_index).x(); | 65 alignment, |
| 66 model.ideal_bounds(current_index + 1).x() - |
| 67 model.ideal_bounds(current_index).x(), |
| 68 model.ideal_bounds(current_index + 1).y() - |
| 69 model.ideal_bounds(current_index).y()); |
49 for (int i = current_index + 1; i < model.view_size(); ++i) { | 70 for (int i = current_index + 1; i < model.view_size(); ++i) { |
50 const gfx::Rect& bounds(model.ideal_bounds(i)); | 71 const gfx::Rect& bounds(model.ideal_bounds(i)); |
51 int mid_x = bounds.x() + bounds.width() / 2 - delta; | 72 int mid_point = primary_axis_coordinate( |
52 if (x < mid_x) | 73 alignment, |
| 74 bounds.x() + bounds.width() / 2 - delta, |
| 75 bounds.y() + bounds.height() / 2 - delta); |
| 76 if (value < mid_point) |
53 return i - 1; | 77 return i - 1; |
54 } | 78 } |
55 return model.view_size() - 1; | 79 return model.view_size() - 1; |
56 } | 80 } |
57 | 81 |
58 } // namespace views | 82 } // namespace views |
OLD | NEW |