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_FOCUS_VIEW_STORAGE_H_
6 #define UI_VIEWS_FOCUS_VIEW_STORAGE_H_
11 #include "base/basictypes.h"
12 #include "ui/views/views_export.h"
14 template <typename T
> struct DefaultSingletonTraits
;
16 // This class is a simple storage place for storing/retrieving views. It is
17 // used for example in the FocusManager to store/restore focused views when the
18 // main window becomes active/inactive.
19 // It automatically removes a view from the storage if the view is removed from
20 // the tree hierarchy or when the view is destroyed, which ever comes first.
22 // To use it, you first need to create a view storage id that can then be used
23 // to store/retrieve views.
28 class VIEWS_EXPORT ViewStorage
{
30 // Returns the global ViewStorage instance.
31 // It is guaranted to be non NULL.
32 static ViewStorage
* GetInstance();
34 // Returns a unique storage id that can be used to store/retrieve views.
35 int CreateStorageID();
37 // Associates |view| with the specified |storage_id|.
38 void StoreView(int storage_id
, View
* view
);
40 // Returns the view associated with |storage_id| if any, NULL otherwise.
41 View
* RetrieveView(int storage_id
);
43 // Removes the view associated with |storage_id| if any.
44 void RemoveView(int storage_id
);
46 // Notifies the ViewStorage that a view was removed from its parent somewhere.
47 void ViewRemoved(View
* removed
);
49 size_t view_count() const { return view_to_ids_
.size(); }
52 friend struct DefaultSingletonTraits
<ViewStorage
>;
57 // Removes the view associated with |storage_id|. If |remove_all_ids| is true,
58 // all other mapping pointing to the same view are removed as well.
59 void EraseView(int storage_id
, bool remove_all_ids
);
61 // Next id for the view storage.
62 int view_storage_next_id_
;
64 // The association id to View used for the view storage.
65 std::map
<int, View
*> id_to_view_
;
67 // Association View to id, used to speed up view notification removal.
68 std::map
<View
*, std::vector
<int>*> view_to_ids_
;
70 DISALLOW_COPY_AND_ASSIGN(ViewStorage
);
75 #endif // UI_VIEWS_FOCUS_VIEW_STORAGE_H_