1 // Copyright 2013 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 ASH_APP_LIST_MODEL_APP_LIST_FOLDER_ITEM_H_
6 #define ASH_APP_LIST_MODEL_APP_LIST_FOLDER_ITEM_H_
7 
8 #include <stddef.h>
9 
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "ash/app_list/model/app_list_item.h"
16 #include "ash/app_list/model/app_list_item_list_observer.h"
17 #include "ash/app_list/model/app_list_item_observer.h"
18 #include "ash/app_list/model/app_list_model_export.h"
19 #include "ash/app_list/model/folder_image.h"
20 #include "ash/public/cpp/app_list/app_list_config_provider.h"
21 #include "ash/public/cpp/app_list/app_list_types.h"
22 #include "base/macros.h"
23 #include "base/scoped_observer.h"
24 
25 namespace gfx {
26 class Rect;
27 }  // namespace gfx
28 
29 namespace ash {
30 
31 class AppListConfig;
32 class AppListItemList;
33 
34 // AppListFolderItem implements the model/controller for folders.
35 class APP_LIST_MODEL_EXPORT AppListFolderItem
36     : public AppListItem,
37       public FolderImageObserver,
38       public AppListConfigProvider::Observer {
39  public:
40   // The folder type affects folder behavior.
41   enum FolderType {
42     // Default folder type.
43     FOLDER_TYPE_NORMAL,
44     // Items can not be moved to/from OEM folders in the UI.
45     FOLDER_TYPE_OEM
46   };
47 
48   static const char kItemType[];
49 
50   explicit AppListFolderItem(const std::string& id);
51   ~AppListFolderItem() override;
52 
53   // Returns the target icon bounds for |item| to fly back to its parent folder
54   // icon in animation UI. If |item| is one of the top item icon, this will
55   // match its corresponding top item icon in the folder icon. Otherwise,
56   // the target icon bounds is centered at the |folder_icon_bounds| with
57   // the same size of the top item icon.
58   // The Rect returned is in the same coordinates of |folder_icon_bounds|.
59   gfx::Rect GetTargetIconRectInFolderForItem(
60       const AppListConfig& app_list_config,
61       AppListItem* item,
62       const gfx::Rect& folder_icon_bounds);
63 
item_list()64   AppListItemList* item_list() { return item_list_.get(); }
item_list()65   const AppListItemList* item_list() const { return item_list_.get(); }
66 
folder_type()67   FolderType folder_type() const { return folder_type_; }
68 
69   // AppListItem overrides:
70   const char* GetItemType() const override;
71   AppListItem* FindChildItem(const std::string& id) override;
72   size_t ChildItemCount() const override;
73 
74   // AppListConfigProvider::Observer override:
75   void OnAppListConfigCreated(AppListConfigType config_type) override;
76 
77   // Persistent folders will be retained even if there is 1 app in them.
78   bool IsPersistent() const;
79   void SetIsPersistent(bool is_persistent);
80 
81   // Returns true if this folder is a candidate for auto-removal (based on its
82   // type and the number of children it has).
83   bool ShouldAutoRemove() const;
84 
85   // Returns an id for a new folder.
86   static std::string GenerateId();
87 
88   // FolderImageObserver overrides:
89   void OnFolderImageUpdated(AppListConfigType config_type) override;
90 
91   // Informs the folder item of an item being dragged, that it may notify its
92   // image.
93   void NotifyOfDraggedItem(AppListItem* dragged_item);
94 
95   FolderImage* GetFolderImageForTesting(AppListConfigType type) const;
96 
97  private:
98   // Creates FolderImages for config types in |config_types| that also exist in
99   // AppListConfigProvider, and adds them to |folder_images_|.
100   // |request_icon_update| - Whether FolderImage::UpdateIcon() should be called
101   //     on the created icon images - this should be set if called outside app
102   //     list model initialization (i.e. outside constructor).
103   void EnsureIconsForAvailableConfigTypes(
104       const std::vector<AppListConfigType>& config_types,
105       bool request_icon_update);
106 
107   // The type of folder; may affect behavior of folder views.
108   const FolderType folder_type_;
109 
110   // List of items in the folder.
111   std::unique_ptr<AppListItemList> item_list_;
112 
113   std::map<AppListConfigType, std::unique_ptr<FolderImage>> folder_images_;
114 
115   // Set when a folder item is being dragged.
116   AppListItem* dragged_item_ = nullptr;
117 
118   ScopedObserver<AppListConfigProvider, AppListConfigProvider::Observer>
119       config_provider_observer_{this};
120 
121   DISALLOW_COPY_AND_ASSIGN(AppListFolderItem);
122 };
123 
124 }  // namespace ash
125 
126 #endif  // ASH_APP_LIST_MODEL_APP_LIST_FOLDER_ITEM_H_
127