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.
5 #ifndef UI_VIEWS_VIEW_MODEL_H_
6 #define UI_VIEWS_VIEW_MODEL_H_
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/views/views_export.h"
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
{
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
);
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
);
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
41 void MoveViewOnly(int index
, int target_index
);
43 // Returns the number of views.
44 int view_size() const { return static_cast<int>(entries_
.size()); }
46 // Removes and deletes all the views.
49 void set_ideal_bounds(int index
, const gfx::Rect
& bounds
) {
51 entries_
[index
].ideal_bounds
= bounds
;
54 const gfx::Rect
& ideal_bounds(int index
) const {
56 return entries_
[index
].ideal_bounds
;
59 // Returns the index of the specified view, or -1 if the view isn't in the
61 int GetIndexOfView(const View
* view
) const;
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 {
71 return entries_
[index
].view
;
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
);
79 // For access to ViewAtBase().
80 friend class ViewModelUtils
;
83 Entry() : view(NULL
) {}
86 gfx::Rect ideal_bounds
;
88 typedef std::vector
<Entry
> Entries
;
91 void check_index(int index
) const {
92 DCHECK_LT(index
, static_cast<int>(entries_
.size()));
96 void check_index(int index
) const {}
101 DISALLOW_COPY_AND_ASSIGN(ViewModelBase
);
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.
110 class ViewModelT
: public ViewModelBase
{
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
); }
118 // Returns the view at the specified index.
119 T
* view_at(int index
) const { return static_cast<T
*>(ViewAtBase(index
)); }
122 DISALLOW_COPY_AND_ASSIGN(ViewModelT
<T
>);
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
;
132 #endif // UI_VIEWS_VIEW_MODEL_H_