Home | History | Annotate | Download | only in views
      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 
     19 // ViewModel is used to track an 'interesting' set of a views. Often times
     20 // during animations views are removed after a delay, which makes for tricky
     21 // coordinate conversion as you have to account for the possibility of the
     22 // indices from the model not lining up with those you expect. This class lets
     23 // you define the 'interesting' views and operate on those views.
     24 class VIEWS_EXPORT ViewModel {
     25  public:
     26   ViewModel();
     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 
     33   // Removes the view at the specified index. This does not actually remove the
     34   // view from the view hierarchy.
     35   void Remove(int index);
     36 
     37   // Moves the view at |index| to |target_index|. |target_index| is in terms
     38   // of the model *after* the view at |index| is removed.
     39   void Move(int index, int target_index);
     40 
     41   // 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
     43   // exactly the same as the bounds of the view at |target_index| before
     44   // invoking this.
     45   void MoveViewOnly(int index, int target_index);
     46 
     47   // Returns the number of views.
     48   int view_size() const { return static_cast<int>(entries_.size()); }
     49 
     50   // Removes and deletes all the views.
     51   void Clear();
     52 
     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) {
     60     check_index(index);
     61     entries_[index].ideal_bounds = bounds;
     62   }
     63 
     64   const gfx::Rect& ideal_bounds(int index) const {
     65     check_index(index);
     66     return entries_[index].ideal_bounds;
     67   }
     68 
     69   // Returns the index of the specified view, or -1 if the view isn't in the
     70   // model.
     71   int GetIndexOfView(const View* view) const;
     72 
     73  private:
     74   struct Entry {
     75     Entry() : view(NULL) {}
     76 
     77     View* view;
     78     gfx::Rect ideal_bounds;
     79   };
     80   typedef std::vector<Entry> Entries;
     81 
     82 #if !defined(NDEBUG)
     83   void check_index(int index) const {
     84     DCHECK_LT(index, static_cast<int>(entries_.size()));
     85     DCHECK_GE(index, 0);
     86   }
     87 #else
     88   void check_index(int index) const {}
     89 #endif
     90 
     91   Entries entries_;
     92 
     93   DISALLOW_COPY_AND_ASSIGN(ViewModel);
     94 };
     95 
     96 }  // namespace views
     97 
     98 #endif  // UI_VIEWS_VIEW_MODEL_H_
     99