Home | History | Annotate | Download | only in app_list
      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 UI_APP_LIST_APP_LIST_ITEM_H_
      6 #define UI_APP_LIST_APP_LIST_ITEM_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/observer_list.h"
     12 #include "sync/api/string_ordinal.h"
     13 #include "ui/app_list/app_list_export.h"
     14 #include "ui/gfx/image/image_skia.h"
     15 
     16 class FastShowPickler;
     17 
     18 namespace ui {
     19 class MenuModel;
     20 }
     21 
     22 namespace app_list {
     23 
     24 class AppListItemList;
     25 class AppListItemListTest;
     26 class AppListItemObserver;
     27 class AppListModel;
     28 
     29 // AppListItem provides icon and title to be shown in a AppListItemView
     30 // and action to be executed when the AppListItemView is activated.
     31 class APP_LIST_EXPORT AppListItem {
     32  public:
     33   explicit AppListItem(const std::string& id);
     34   virtual ~AppListItem();
     35 
     36   void SetIcon(const gfx::ImageSkia& icon, bool has_shadow);
     37   const gfx::ImageSkia& icon() const { return icon_; }
     38   bool has_shadow() const { return has_shadow_; }
     39 
     40   const std::string& GetDisplayName() const {
     41     return short_name_.empty() ? name_ : short_name_;
     42   }
     43 
     44   const std::string& name() const { return name_; }
     45   // Should only be used in tests; otheriwse use GetDisplayName().
     46   const std::string& short_name() const { return short_name_; }
     47 
     48   void SetHighlighted(bool highlighted);
     49   bool highlighted() const { return highlighted_; }
     50 
     51   void SetIsInstalling(bool is_installing);
     52   bool is_installing() const { return is_installing_; }
     53 
     54   void SetPercentDownloaded(int percent_downloaded);
     55   int percent_downloaded() const { return percent_downloaded_; }
     56 
     57   bool IsInFolder() const { return !folder_id_.empty(); }
     58 
     59   const std::string& id() const { return id_; }
     60   const std::string& folder_id() const { return folder_id_; }
     61   const syncer::StringOrdinal& position() const { return position_; }
     62 
     63   void AddObserver(AppListItemObserver* observer);
     64   void RemoveObserver(AppListItemObserver* observer);
     65 
     66   // Activates (opens) the item. Does nothing by default.
     67   virtual void Activate(int event_flags);
     68 
     69   // Returns a static const char* identifier for the subclass (defaults to "").
     70   // Pointers can be compared for quick type checking.
     71   virtual const char* GetItemType() const;
     72 
     73   // Returns the context menu model for this item, or NULL if there is currently
     74   // no menu for the item (e.g. during install).
     75   // Note the returned menu model is owned by this item.
     76   virtual ui::MenuModel* GetContextMenuModel();
     77 
     78   // Returns the item matching |id| contained in this item (e.g. if the item is
     79   // a folder), or NULL if the item was not found or this is not a container.
     80   virtual AppListItem* FindChildItem(const std::string& id);
     81 
     82   // Returns the number of child items if it has any (e.g. is a folder) or 0.
     83   virtual size_t ChildItemCount() const;
     84 
     85   // Called when the extension preference changed. Used by ExtensionAppItem
     86   // to update icon overlays.
     87   virtual void OnExtensionPreferenceChanged();
     88 
     89   // Utility functions for sync integration tests.
     90   virtual bool CompareForTest(const AppListItem* other) const;
     91   virtual std::string ToDebugString() const;
     92 
     93  protected:
     94   friend class ::FastShowPickler;
     95   friend class AppListItemList;
     96   friend class AppListItemListTest;
     97   friend class AppListModel;
     98 
     99   // These should only be called by AppListModel or in tests so that name
    100   // changes trigger update notifications.
    101 
    102   // Sets the full name of the item. Clears any shortened name.
    103   void SetName(const std::string& name);
    104 
    105   // Sets the full name and an optional shortened name of the item (e.g. to use
    106   // if the full name is too long to fit in a view).
    107   void SetNameAndShortName(const std::string& name,
    108                            const std::string& short_name);
    109 
    110   void set_position(const syncer::StringOrdinal& new_position) {
    111     DCHECK(new_position.IsValid());
    112     position_ = new_position;
    113   }
    114 
    115   void set_folder_id(const std::string& folder_id) { folder_id_ = folder_id; }
    116 
    117  private:
    118   friend class AppListModelTest;
    119 
    120   const std::string id_;
    121   std::string folder_id_;  // Id of containing folder; empty if top level item.
    122   syncer::StringOrdinal position_;
    123   gfx::ImageSkia icon_;
    124   bool has_shadow_;
    125 
    126   // The full name of an item. Used for display if |short_name_| is empty.
    127   std::string name_;
    128 
    129   // A shortened name for the item, used for display.
    130   std::string short_name_;
    131 
    132   bool highlighted_;
    133   bool is_installing_;
    134   int percent_downloaded_;
    135 
    136   ObserverList<AppListItemObserver> observers_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(AppListItem);
    139 };
    140 
    141 }  // namespace app_list
    142 
    143 #endif  // UI_APP_LIST_APP_LIST_ITEM_H_
    144