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

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

Issue 851853002: It is time. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Trying to reup because the last upload failed. Created 5 years, 11 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/views/view_constants_aura.cc ('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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef UI_VIEWS_VIEW_MODEL_H_
6 #define UI_VIEWS_VIEW_MODEL_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/views/views_export.h"
14
15 namespace views {
16
17 class View;
18 class ViewModelUtils;
19
20 // Internal implementation of the templated ViewModelT class. Provides
21 // non-templated "unsafe" methods for ViewModelT to wrap around. Any methods
22 // that allow insertion of or access to a View* should be protected, and have a
23 // public method in the ViewModelT subclass that provides type-safe access to
24 // the correct View subclass.
25 class VIEWS_EXPORT ViewModelBase {
26 public:
27 ~ViewModelBase();
28
29 // Removes the view at the specified index. This does not actually remove the
30 // view from the view hierarchy.
31 void Remove(int index);
32
33 // Moves the view at |index| to |target_index|. |target_index| is in terms
34 // of the model *after* the view at |index| is removed.
35 void Move(int index, int target_index);
36
37 // Variant of Move() that leaves the bounds as is. That is, after invoking
38 // this the bounds of the view at |target_index| (and all other indices) are
39 // exactly the same as the bounds of the view at |target_index| before
40 // invoking this.
41 void MoveViewOnly(int index, int target_index);
42
43 // Returns the number of views.
44 int view_size() const { return static_cast<int>(entries_.size()); }
45
46 // Removes and deletes all the views.
47 void Clear();
48
49 void set_ideal_bounds(int index, const gfx::Rect& bounds) {
50 check_index(index);
51 entries_[index].ideal_bounds = bounds;
52 }
53
54 const gfx::Rect& ideal_bounds(int index) const {
55 check_index(index);
56 return entries_[index].ideal_bounds;
57 }
58
59 // Returns the index of the specified view, or -1 if the view isn't in the
60 // model.
61 int GetIndexOfView(const View* view) const;
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
78 private:
79 // For access to ViewAtBase().
80 friend class ViewModelUtils;
81
82 struct Entry {
83 Entry() : view(NULL) {}
84
85 View* view;
86 gfx::Rect ideal_bounds;
87 };
88 typedef std::vector<Entry> Entries;
89
90 #if !defined(NDEBUG)
91 void check_index(int index) const {
92 DCHECK_LT(index, static_cast<int>(entries_.size()));
93 DCHECK_GE(index, 0);
94 }
95 #else
96 void check_index(int index) const {}
97 #endif
98
99 Entries entries_;
100
101 DISALLOW_COPY_AND_ASSIGN(ViewModelBase);
102 };
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
130 } // namespace views
131
132 #endif // UI_VIEWS_VIEW_MODEL_H_
OLDNEW
« no previous file with comments | « ui/views/view_constants_aura.cc ('k') | ui/views/view_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698