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

Side by Side Diff: ui/views/view_model.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: Fix browser_test compile on ChromeOS (missing include). 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
« no previous file with comments | « ui/app_list/views/contents_view.h ('k') | ui/views/view_model.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_H_ 5 #ifndef UI_VIEWS_VIEW_MODEL_H_
6 #define UI_VIEWS_VIEW_MODEL_H_ 6 #define UI_VIEWS_VIEW_MODEL_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "ui/gfx/rect.h" 12 #include "ui/gfx/rect.h"
13 #include "ui/views/views_export.h" 13 #include "ui/views/views_export.h"
14 14
15 namespace views { 15 namespace views {
16 16
17 class View; 17 class View;
18 class ViewModelUtils;
18 19
19 // ViewModel is used to track an 'interesting' set of a views. Often times 20 // Internal implementation of the templated ViewModelT class. Provides
20 // during animations views are removed after a delay, which makes for tricky 21 // non-templated "unsafe" methods for ViewModelT to wrap around. Any methods
21 // coordinate conversion as you have to account for the possibility of the 22 // that allow insertion of or access to a View* should be protected, and have a
22 // indices from the model not lining up with those you expect. This class lets 23 // public method in the ViewModelT subclass that provides type-safe access to
23 // you define the 'interesting' views and operate on those views. 24 // the correct View subclass.
24 class VIEWS_EXPORT ViewModel { 25 class VIEWS_EXPORT ViewModelBase {
25 public: 26 public:
26 ViewModel(); 27 ~ViewModelBase();
27 ~ViewModel();
28
29 // Adds |view| to this model. This does not add |view| to a view hierarchy,
30 // only to this model.
31 void Add(View* view, int index);
32 28
33 // Removes the view at the specified index. This does not actually remove the 29 // Removes the view at the specified index. This does not actually remove the
34 // view from the view hierarchy. 30 // view from the view hierarchy.
35 void Remove(int index); 31 void Remove(int index);
36 32
37 // Moves the view at |index| to |target_index|. |target_index| is in terms 33 // Moves the view at |index| to |target_index|. |target_index| is in terms
38 // of the model *after* the view at |index| is removed. 34 // of the model *after* the view at |index| is removed.
39 void Move(int index, int target_index); 35 void Move(int index, int target_index);
40 36
41 // Variant of Move() that leaves the bounds as is. That is, after invoking 37 // Variant of Move() that leaves the bounds as is. That is, after invoking
42 // this the bounds of the view at |target_index| (and all other indices) are 38 // this the bounds of the view at |target_index| (and all other indices) are
43 // exactly the same as the bounds of the view at |target_index| before 39 // exactly the same as the bounds of the view at |target_index| before
44 // invoking this. 40 // invoking this.
45 void MoveViewOnly(int index, int target_index); 41 void MoveViewOnly(int index, int target_index);
46 42
47 // Returns the number of views. 43 // Returns the number of views.
48 int view_size() const { return static_cast<int>(entries_.size()); } 44 int view_size() const { return static_cast<int>(entries_.size()); }
49 45
50 // Removes and deletes all the views. 46 // Removes and deletes all the views.
51 void Clear(); 47 void Clear();
52 48
53 // Returns the view at the specified index.
54 View* view_at(int index) const {
55 check_index(index);
56 return entries_[index].view;
57 }
58
59 void set_ideal_bounds(int index, const gfx::Rect& bounds) { 49 void set_ideal_bounds(int index, const gfx::Rect& bounds) {
60 check_index(index); 50 check_index(index);
61 entries_[index].ideal_bounds = bounds; 51 entries_[index].ideal_bounds = bounds;
62 } 52 }
63 53
64 const gfx::Rect& ideal_bounds(int index) const { 54 const gfx::Rect& ideal_bounds(int index) const {
65 check_index(index); 55 check_index(index);
66 return entries_[index].ideal_bounds; 56 return entries_[index].ideal_bounds;
67 } 57 }
68 58
69 // Returns the index of the specified view, or -1 if the view isn't in the 59 // Returns the index of the specified view, or -1 if the view isn't in the
70 // model. 60 // model.
71 int GetIndexOfView(const View* view) const; 61 int GetIndexOfView(const View* view) const;
72 62
63 protected:
64 ViewModelBase();
65
66 // Returns the view at the specified index. Note: Most users should use
67 // view_at() in the subclass, to get a view of the correct type. (Do not call
68 // ViewAtBase then static_cast to the desired type.)
69 View* ViewAtBase(int index) const {
70 check_index(index);
71 return entries_[index].view;
72 }
73
74 // Adds |view| to this model. This does not add |view| to a view hierarchy,
75 // only to this model.
76 void AddUnsafe(View* view, int index);
77
73 private: 78 private:
79 // For access to ViewAtBase().
80 friend class ViewModelUtils;
81
74 struct Entry { 82 struct Entry {
75 Entry() : view(NULL) {} 83 Entry() : view(NULL) {}
76 84
77 View* view; 85 View* view;
78 gfx::Rect ideal_bounds; 86 gfx::Rect ideal_bounds;
79 }; 87 };
80 typedef std::vector<Entry> Entries; 88 typedef std::vector<Entry> Entries;
81 89
82 #if !defined(NDEBUG) 90 #if !defined(NDEBUG)
83 void check_index(int index) const { 91 void check_index(int index) const {
84 DCHECK_LT(index, static_cast<int>(entries_.size())); 92 DCHECK_LT(index, static_cast<int>(entries_.size()));
85 DCHECK_GE(index, 0); 93 DCHECK_GE(index, 0);
86 } 94 }
87 #else 95 #else
88 void check_index(int index) const {} 96 void check_index(int index) const {}
89 #endif 97 #endif
90 98
91 Entries entries_; 99 Entries entries_;
92 100
93 DISALLOW_COPY_AND_ASSIGN(ViewModel); 101 DISALLOW_COPY_AND_ASSIGN(ViewModelBase);
94 }; 102 };
95 103
104 // ViewModelT is used to track an 'interesting' set of a views. Often times
105 // during animations views are removed after a delay, which makes for tricky
106 // coordinate conversion as you have to account for the possibility of the
107 // indices from the model not lining up with those you expect. This class lets
108 // you define the 'interesting' views and operate on those views.
109 template <class T>
110 class ViewModelT : public ViewModelBase {
111 public:
112 ViewModelT<T>() {}
113
114 // Adds |view| to this model. This does not add |view| to a view hierarchy,
115 // only to this model.
116 void Add(T* view, int index) { AddUnsafe(view, index); }
117
118 // Returns the view at the specified index.
119 T* view_at(int index) const { return static_cast<T*>(ViewAtBase(index)); }
120
121 private:
122 DISALLOW_COPY_AND_ASSIGN(ViewModelT<T>);
123 };
124
125 // ViewModel is a collection of views with no specfic type. If all views have
126 // the same type, the use of ViewModelT is preferred so that the views can be
127 // retrieved without potentially unsafe downcasts.
128 typedef ViewModelT<View> ViewModel;
129
96 } // namespace views 130 } // namespace views
97 131
98 #endif // UI_VIEWS_VIEW_MODEL_H_ 132 #endif // UI_VIEWS_VIEW_MODEL_H_
OLDNEW
« no previous file with comments | « ui/app_list/views/contents_view.h ('k') | ui/views/view_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698