Home | History | Annotate | Download | only in app_list
      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_APP_LIST_SEARCH_RESULT_H_
      6 #define UI_APP_LIST_SEARCH_RESULT_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/observer_list.h"
     12 #include "base/strings/string16.h"
     13 #include "ui/app_list/app_list_export.h"
     14 #include "ui/gfx/image/image_skia.h"
     15 #include "ui/gfx/range/range.h"
     16 
     17 namespace ui {
     18 class MenuModel;
     19 }
     20 
     21 namespace app_list {
     22 
     23 class SearchResultObserver;
     24 
     25 // SearchResult consists of an icon, title text and details text. Title and
     26 // details text can have tagged ranges that are displayed differently from
     27 // default style.
     28 class APP_LIST_EXPORT SearchResult {
     29  public:
     30   // How the result should be displayed.
     31   enum DisplayType {
     32     DISPLAY_LIST,
     33     DISPLAY_TILE,
     34   };
     35 
     36   // A tagged range in search result text.
     37   struct APP_LIST_EXPORT Tag {
     38     // Similar to ACMatchClassification::Style, the style values are not
     39     // mutually exclusive.
     40     enum Style {
     41       NONE  = 0,
     42       URL   = 1 << 0,
     43       MATCH = 1 << 1,
     44       DIM   = 1 << 2,
     45     };
     46 
     47     Tag(int styles, size_t start, size_t end)
     48         : styles(styles),
     49           range(start, end) {
     50     }
     51 
     52     int styles;
     53     gfx::Range range;
     54   };
     55   typedef std::vector<Tag> Tags;
     56 
     57   // Data representing an action that can be performed on this search result.
     58   // An action could be represented as an icon set or as a blue button with
     59   // a label. Icon set is chosen if label text is empty. Otherwise, a blue
     60   // button with the label text will be used.
     61   struct APP_LIST_EXPORT Action {
     62     Action(const gfx::ImageSkia& base_image,
     63            const gfx::ImageSkia& hover_image,
     64            const gfx::ImageSkia& pressed_image,
     65            const base::string16& tooltip_text);
     66     Action(const base::string16& label_text,
     67            const base::string16& tooltip_text);
     68     ~Action();
     69 
     70     gfx::ImageSkia base_image;
     71     gfx::ImageSkia hover_image;
     72     gfx::ImageSkia pressed_image;
     73 
     74     base::string16 tooltip_text;
     75     base::string16 label_text;
     76   };
     77   typedef std::vector<Action> Actions;
     78 
     79   SearchResult();
     80   virtual ~SearchResult();
     81 
     82   const gfx::ImageSkia& icon() const { return icon_; }
     83   void SetIcon(const gfx::ImageSkia& icon);
     84 
     85   const base::string16& title() const { return title_; }
     86   void set_title(const base::string16& title) { title_ = title;}
     87 
     88   const Tags& title_tags() const { return title_tags_; }
     89   void set_title_tags(const Tags& tags) { title_tags_ = tags; }
     90 
     91   const base::string16& details() const { return details_; }
     92   void set_details(const base::string16& details) { details_ = details; }
     93 
     94   const Tags& details_tags() const { return details_tags_; }
     95   void set_details_tags(const Tags& tags) { details_tags_ = tags; }
     96 
     97   const std::string& id() const { return id_; }
     98   double relevance() const { return relevance_; }
     99   DisplayType display_type() const { return display_type_; }
    100 
    101   const Actions& actions() const {
    102     return actions_;
    103   }
    104   void SetActions(const Actions& sets);
    105 
    106   bool is_installing() const { return is_installing_; }
    107   void SetIsInstalling(bool is_installing);
    108 
    109   int percent_downloaded() const { return percent_downloaded_; }
    110   void SetPercentDownloaded(int percent_downloaded);
    111 
    112   // Returns the dimension at which this result's icon should be displayed.
    113   int GetPreferredIconDimension() const;
    114 
    115   void NotifyItemInstalled();
    116   void NotifyItemUninstalled();
    117 
    118   void AddObserver(SearchResultObserver* observer);
    119   void RemoveObserver(SearchResultObserver* observer);
    120 
    121   // Opens the result.
    122   virtual void Open(int event_flags);
    123 
    124   // Invokes a custom action on the result.
    125   virtual void InvokeAction(int action_index, int event_flags);
    126 
    127   // Returns the context menu model for this item, or NULL if there is currently
    128   // no menu for the item (e.g. during install).
    129   // Note the returned menu model is owned by this item.
    130   virtual ui::MenuModel* GetContextMenuModel();
    131 
    132  protected:
    133   void set_id(const std::string& id) { id_ = id; }
    134   void set_relevance(double relevance) { relevance_ = relevance; }
    135   void set_display_type(DisplayType display_type) {
    136     display_type_ = display_type;
    137   }
    138 
    139  private:
    140   gfx::ImageSkia icon_;
    141 
    142   base::string16 title_;
    143   Tags title_tags_;
    144 
    145   base::string16 details_;
    146   Tags details_tags_;
    147 
    148   std::string id_;
    149   double relevance_;
    150   DisplayType display_type_;
    151 
    152   Actions actions_;
    153 
    154   bool is_installing_;
    155   int percent_downloaded_;
    156 
    157   ObserverList<SearchResultObserver> observers_;
    158 
    159   DISALLOW_COPY_AND_ASSIGN(SearchResult);
    160 };
    161 
    162 }  // namespace app_list
    163 
    164 #endif  // UI_APP_LIST_SEARCH_RESULT_H_
    165