omnibox_result_view.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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 CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_RESULT_VIEW_H_
6#define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_RESULT_VIEW_H_
7
8#include <vector>
9
10#include "base/gtest_prod_util.h"
11#include "chrome/browser/autocomplete/autocomplete_match.h"
12#include "third_party/skia/include/core/SkColor.h"
13#include "ui/gfx/animation/animation_delegate.h"
14#include "ui/gfx/animation/slide_animation.h"
15#include "ui/gfx/font_list.h"
16#include "ui/gfx/rect.h"
17#include "ui/views/controls/image_view.h"
18#include "ui/views/view.h"
19
20class LocationBarView;
21class OmniboxPopupContentsView;
22
23namespace gfx {
24class Canvas;
25class RenderText;
26}
27
28class OmniboxResultView : public views::View,
29                          private gfx::AnimationDelegate {
30 public:
31  // Keep these ordered from least dominant (normal) to most dominant
32  // (selected).
33  enum ResultViewState {
34    NORMAL = 0,
35    HOVERED,
36    SELECTED,
37    NUM_STATES
38  };
39
40  enum ColorKind {
41    BACKGROUND = 0,
42    TEXT,
43    DIMMED_TEXT,
44    URL,
45    DIVIDER,
46    NUM_KINDS
47  };
48
49  OmniboxResultView(OmniboxPopupContentsView* model,
50                    int model_index,
51                    LocationBarView* location_bar_view,
52                    const gfx::FontList& font_list);
53  virtual ~OmniboxResultView();
54
55  SkColor GetColor(ResultViewState state, ColorKind kind) const;
56
57  // Updates the match used to paint the contents of this result view. We copy
58  // the match so that we can continue to paint the last result even after the
59  // model has changed.
60  void SetMatch(const AutocompleteMatch& match);
61
62  void ShowKeyword(bool show_keyword);
63
64  void Invalidate();
65
66  // views::View:
67  virtual gfx::Size GetPreferredSize() OVERRIDE;
68
69  ResultViewState GetState() const;
70
71  // Returns the height of the text portion of the result view. In the base
72  // class, this is the height of one line of text.
73  virtual int GetTextHeight() const;
74
75  // Returns the display width required for the match contents.
76  int GetMatchContentsWidth() const;
77
78 protected:
79  // Paints the given |match| using the RenderText instances |contents| and
80  // |description| at offset |x| in the bounds of this view.
81  virtual void PaintMatch(const AutocompleteMatch& match,
82                          gfx::RenderText* contents,
83                          gfx::RenderText* description,
84                          gfx::Canvas* canvas,
85                          int x) const;
86
87  // Draws given |render_text| on |canvas| at given location (|x|, |y|).
88  // |contents| indicates whether the |render_text| is for the match contents
89  // (rather than the separator or the description).  Additional properties from
90  // |match| are used to render Infinite suggestions correctly.  If |max_width|
91  // is a non-negative number, the text will be elided to fit within
92  // |max_width|.  Returns the x position to the right of the string.
93  int DrawRenderText(const AutocompleteMatch& match,
94                     gfx::RenderText* render_text,
95                     bool contents,
96                     gfx::Canvas* canvas,
97                     int x,
98                     int y,
99                     int max_width) const;
100
101  // Creates a RenderText with given |text| and rendering defaults.
102  scoped_ptr<gfx::RenderText> CreateRenderText(
103      const base::string16& text) const;
104
105  // Creates a RenderText with default rendering for the given |text|. The
106  // |classifications| and |force_dim| are used to style the text.
107  scoped_ptr<gfx::RenderText> CreateClassifiedRenderText(
108      const base::string16& text,
109      const ACMatchClassifications& classifications,
110      bool force_dim) const;
111
112  const gfx::Rect& text_bounds() const { return text_bounds_; }
113
114  void set_edge_item_padding(int value) { edge_item_padding_ = value; }
115  void set_item_padding(int value) { item_padding_ = value; }
116  void set_minimum_text_vertical_padding(int value) {
117    minimum_text_vertical_padding_ = value;
118  }
119
120 private:
121  gfx::ImageSkia GetIcon() const;
122  const gfx::ImageSkia* GetKeywordIcon() const;
123
124  // Whether to render only the keyword match.  Returns true if |match_| has an
125  // associated keyword match that has been animated so close to the start that
126  // the keyword match will hide even the icon of the regular match.
127  bool ShowOnlyKeywordMatch() const;
128
129  // Resets all RenderTexts for contents and description of the |match_| and its
130  // associated keyword match.
131  void ResetRenderTexts() const;
132
133  // Initializes |contents_rendertext_| if it is NULL.
134  void InitContentsRenderTextIfNecessary() const;
135
136  // views::View:
137  virtual void Layout() OVERRIDE;
138  virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
139  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
140
141  // gfx::AnimationDelegate:
142  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
143
144  // Returns the offset at which the contents of the |match| should be displayed
145  // within the text bounds. The directionality of UI and match contents is used
146  // to determine the offset relative to the correct edge.
147  int GetDisplayOffset(const AutocompleteMatch& match,
148                       bool is_ui_rtl,
149                       bool is_match_contents_rtl) const;
150
151  static int default_icon_size_;
152
153  // Default values cached here, may be overridden using the setters above.
154  int edge_item_padding_;
155  int item_padding_;
156  int minimum_text_vertical_padding_;
157
158  // This row's model and model index.
159  OmniboxPopupContentsView* model_;
160  size_t model_index_;
161
162  LocationBarView* location_bar_view_;
163
164  const gfx::FontList font_list_;
165  int font_height_;
166
167  // A context used for mirroring regions.
168  class MirroringContext;
169  scoped_ptr<MirroringContext> mirroring_context_;
170
171  AutocompleteMatch match_;
172
173  gfx::Rect text_bounds_;
174  gfx::Rect icon_bounds_;
175
176  gfx::Rect keyword_text_bounds_;
177  scoped_ptr<views::ImageView> keyword_icon_;
178
179  scoped_ptr<gfx::SlideAnimation> animation_;
180
181  // We preserve these RenderTexts so that we won't recreate them on every call
182  // to GetMatchContentsWidth() or OnPaint().
183  mutable scoped_ptr<gfx::RenderText> contents_rendertext_;
184  mutable scoped_ptr<gfx::RenderText> description_rendertext_;
185  mutable scoped_ptr<gfx::RenderText> separator_rendertext_;
186  mutable scoped_ptr<gfx::RenderText> keyword_contents_rendertext_;
187  mutable scoped_ptr<gfx::RenderText> keyword_description_rendertext_;
188
189  mutable int separator_width_;
190
191  DISALLOW_COPY_AND_ASSIGN(OmniboxResultView);
192};
193
194#endif  // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_RESULT_VIEW_H_
195