Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1219)

Side by Side Diff: ui/views/view_model_utils.h

Issue 598013003: Added views::ViewModelT<T>, a type-safe template version of ViewModel. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@appsgridview-static-casts
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 UI_VIEWS_VIEW_MODEL_UTILS_H_ 5 #ifndef UI_VIEWS_VIEW_MODEL_UTILS_H_
6 #define UI_VIEWS_VIEW_MODEL_UTILS_H_ 6 #define UI_VIEWS_VIEW_MODEL_UTILS_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "ui/views/view.h"
10 #include "ui/views/view_model.h"
9 #include "ui/views/views_export.h" 11 #include "ui/views/views_export.h"
10 12
11 namespace views { 13 namespace views {
12 14
13 class View;
14 class ViewModel;
15
16 class VIEWS_EXPORT ViewModelUtils { 15 class VIEWS_EXPORT ViewModelUtils {
17 public: 16 public:
18 enum Alignment { 17 enum Alignment {
19 HORIZONTAL, 18 HORIZONTAL,
20 VERTICAL 19 VERTICAL
21 }; 20 };
22 21
23 // Sets the bounds of each view to its ideal bounds. 22 // Sets the bounds of each view to its ideal bounds.
24 static void SetViewBoundsToIdealBounds(const ViewModel& model); 23 template <class T>
24 static void SetViewBoundsToIdealBounds(const ViewModel<T>& model);
25 25
26 // Returns true if the Views in |model| are at their ideal bounds. 26 // Returns true if the Views in |model| are at their ideal bounds.
27 static bool IsAtIdealBounds(const ViewModel& model); 27 template <class T>
28 static bool IsAtIdealBounds(const ViewModel<T>& model);
28 29
29 // Returns the index to move |view| to based on a coordinate of |x| and |y|. 30 // Returns the index to move |view| to based on a coordinate of |x| and |y|.
30 static int DetermineMoveIndex(const ViewModel& model, 31 template <class T>
32 static int DetermineMoveIndex(const ViewModel<T>& model,
31 View* view, 33 View* view,
32 Alignment alignment, 34 Alignment alignment,
33 int x, 35 int x,
34 int y); 36 int y);
35 37
36 private: 38 private:
37 DISALLOW_IMPLICIT_CONSTRUCTORS(ViewModelUtils); 39 DISALLOW_IMPLICIT_CONSTRUCTORS(ViewModelUtils);
38 }; 40 };
39 41
42 namespace {
43
44 // Used in calculating ideal bounds.
45 int primary_axis_coordinate(ViewModelUtils::Alignment alignment,
46 int x,
47 int y) {
48 return alignment == ViewModelUtils::HORIZONTAL ? x : y;
49 }
50
51 } // namespace
52
53 // static
54 template <class T>
55 void ViewModelUtils::SetViewBoundsToIdealBounds(const ViewModel<T>& model) {
56 for (int i = 0; i < model.view_size(); ++i)
57 model.view_at(i)->SetBoundsRect(model.ideal_bounds(i));
58 }
59
60 // static
61 template <class T>
62 bool ViewModelUtils::IsAtIdealBounds(const ViewModel<T>& model) {
63 for (int i = 0; i < model.view_size(); ++i) {
64 View* view = model.view_at(i);
65 if (view->bounds() != model.ideal_bounds(i))
66 return false;
67 }
68 return true;
69 }
70
71 // static
72 template <class T>
73 int ViewModelUtils::DetermineMoveIndex(const ViewModel<T>& model,
74 View* view,
75 Alignment alignment,
76 int x,
77 int y) {
78 int value = primary_axis_coordinate(alignment, x, y);
79 int current_index = model.GetIndexOfView(view);
80 DCHECK_NE(-1, current_index);
81 for (int i = 0; i < current_index; ++i) {
82 int mid_point = primary_axis_coordinate(
83 alignment,
84 model.ideal_bounds(i).x() + model.ideal_bounds(i).width() / 2,
85 model.ideal_bounds(i).y() + model.ideal_bounds(i).height() / 2);
86 if (value < mid_point)
87 return i;
88 }
89
90 if (current_index + 1 == model.view_size())
91 return current_index;
92
93 // For indices after the current index ignore the bounds of the view being
94 // dragged. This keeps the view from bouncing around as moved.
95 int delta = primary_axis_coordinate(
96 alignment,
97 model.ideal_bounds(current_index + 1).x() -
98 model.ideal_bounds(current_index).x(),
99 model.ideal_bounds(current_index + 1).y() -
100 model.ideal_bounds(current_index).y());
101 for (int i = current_index + 1; i < model.view_size(); ++i) {
102 const gfx::Rect& bounds(model.ideal_bounds(i));
103 int mid_point = primary_axis_coordinate(
104 alignment,
105 bounds.x() + bounds.width() / 2 - delta,
106 bounds.y() + bounds.height() / 2 - delta);
107 if (value < mid_point)
108 return i - 1;
109 }
110 return model.view_size() - 1;
111 }
112
40 } // namespace views 113 } // namespace views
41 114
42 #endif // UI_VIEWS_VIEW_MODEL_UTILS_H_ 115 #endif // UI_VIEWS_VIEW_MODEL_UTILS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698