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 CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_RESULT_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_RESULT_VIEW_H_
10 #include "base/gtest_prod_util.h"
11 #include "components/omnibox/browser/autocomplete_match.h"
12 #include "components/omnibox/browser/suggestion_answer.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/gfx/animation/animation_delegate.h"
15 #include "ui/gfx/animation/slide_animation.h"
16 #include "ui/gfx/font_list.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/views/controls/image_view.h"
19 #include "ui/views/view.h"
21 class LocationBarView
;
22 class OmniboxPopupContentsView
;
29 class OmniboxResultView
: public views::View
,
30 private gfx::AnimationDelegate
{
32 // Keep these ordered from least dominant (normal) to most dominant
34 enum ResultViewState
{
50 OmniboxResultView(OmniboxPopupContentsView
* model
,
52 LocationBarView
* location_bar_view
,
53 const gfx::FontList
& font_list
);
54 ~OmniboxResultView() override
;
56 SkColor
GetColor(ResultViewState state
, ColorKind kind
) const;
58 // Updates the match used to paint the contents of this result view. We copy
59 // the match so that we can continue to paint the last result even after the
61 void SetMatch(const AutocompleteMatch
& match
);
63 void ShowKeyword(bool show_keyword
);
68 gfx::Size
GetPreferredSize() const override
;
70 ResultViewState
GetState() const;
72 // Returns the height of the text portion of the result view. In the base
73 // class, this is the height of one line of text.
74 virtual int GetTextHeight() const;
76 // Returns the display width required for the match contents.
77 int GetMatchContentsWidth() const;
79 // Stores the image in a local data member and schedules a repaint.
80 void SetAnswerImage(const gfx::ImageSkia
& image
);
83 // Paints the given |match| using the RenderText instances |contents| and
84 // |description| at offset |x| in the bounds of this view.
85 virtual void PaintMatch(const AutocompleteMatch
& match
,
86 gfx::RenderText
* contents
,
87 gfx::RenderText
* description
,
91 // Draws given |render_text| on |canvas| at given location (|x|, |y|).
92 // |contents| indicates whether the |render_text| is for the match contents
93 // (rather than the separator or the description). Additional properties from
94 // |match| are used to render Infinite suggestions correctly. If |max_width|
95 // is a non-negative number, the text will be elided to fit within
96 // |max_width|. Returns the x position to the right of the string.
97 int DrawRenderText(const AutocompleteMatch
& match
,
98 gfx::RenderText
* render_text
,
103 int max_width
) const;
105 // Creates a RenderText with given |text| and rendering defaults.
106 scoped_ptr
<gfx::RenderText
> CreateRenderText(
107 const base::string16
& text
) const;
109 // Creates a RenderText with default rendering for the given |text|. The
110 // |classifications| and |force_dim| are used to style the text.
111 scoped_ptr
<gfx::RenderText
> CreateClassifiedRenderText(
112 const base::string16
& text
,
113 const ACMatchClassifications
& classifications
,
114 bool force_dim
) const;
116 const gfx::Rect
& text_bounds() const { return text_bounds_
; }
120 const char* GetClassName() const override
;
122 gfx::ImageSkia
GetIcon() const;
123 const gfx::ImageSkia
* GetKeywordIcon() const;
125 // Whether to render only the keyword match. Returns true if |match_| has an
126 // associated keyword match that has been animated so close to the start that
127 // the keyword match will hide even the icon of the regular match.
128 bool ShowOnlyKeywordMatch() const;
130 // Resets all RenderTexts for contents and description of the |match_| and its
131 // associated keyword match.
132 void ResetRenderTexts() const;
134 // Initializes |contents_rendertext_| if it is NULL.
135 void InitContentsRenderTextIfNecessary() const;
138 void Layout() override
;
139 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
140 void OnPaint(gfx::Canvas
* canvas
) override
;
142 // gfx::AnimationDelegate:
143 void AnimationProgressed(const gfx::Animation
* animation
) override
;
145 // Returns the offset at which the contents of the |match| should be displayed
146 // within the text bounds. The directionality of UI and match contents is used
147 // to determine the offset relative to the correct edge.
148 int GetDisplayOffset(const AutocompleteMatch
& match
,
150 bool is_match_contents_rtl
) const;
152 int GetAnswerLineHeight() const;
153 int GetContentLineHeight() const;
155 // Creates a RenderText with text and styling from the image line.
156 scoped_ptr
<gfx::RenderText
> CreateAnswerLine(
157 const SuggestionAnswer::ImageLine
& line
,
158 gfx::FontList font_list
);
160 // Adds |text| to |destination|. |text_type| is an index into the
161 // kTextStyles constant defined in the .cc file and is used to style the text,
162 // including setting the font size, color, and baseline style. See the
163 // TextStyle struct in the .cc file for more.
164 void AppendAnswerText(gfx::RenderText
* destination
,
165 const base::string16
& text
,
168 // AppendAnswerText will break up the |text| into bold and non-bold pieces
169 // and pass each to this helper with the correct |is_bold| value.
170 void AppendAnswerTextHelper(gfx::RenderText
* destination
,
171 const base::string16
& text
,
175 // Returns the necessary margin, if any, at the start and end of the view.
176 // This allows us to keep the icon and text in the view aligned with the
177 // location bar contents. For a left-to-right language, StartMargin()
178 // and EndMargin() correspond to the left and right margins, respectively.
179 int StartMargin() const;
180 int EndMargin() const;
182 static int default_icon_size_
;
184 // This row's model and model index.
185 OmniboxPopupContentsView
* model_
;
188 LocationBarView
* location_bar_view_
;
190 const gfx::FontList font_list_
;
193 // A context used for mirroring regions.
194 class MirroringContext
;
195 scoped_ptr
<MirroringContext
> mirroring_context_
;
197 AutocompleteMatch match_
;
199 gfx::Rect text_bounds_
;
200 gfx::Rect icon_bounds_
;
202 gfx::Rect keyword_text_bounds_
;
203 scoped_ptr
<views::ImageView
> keyword_icon_
;
205 scoped_ptr
<gfx::SlideAnimation
> animation_
;
207 // If the answer has an icon, cache the image.
208 gfx::ImageSkia answer_image_
;
210 // We preserve these RenderTexts so that we won't recreate them on every call
211 // to GetMatchContentsWidth() or OnPaint().
212 mutable scoped_ptr
<gfx::RenderText
> contents_rendertext_
;
213 mutable scoped_ptr
<gfx::RenderText
> description_rendertext_
;
214 mutable scoped_ptr
<gfx::RenderText
> separator_rendertext_
;
215 mutable scoped_ptr
<gfx::RenderText
> keyword_contents_rendertext_
;
216 mutable scoped_ptr
<gfx::RenderText
> keyword_description_rendertext_
;
218 mutable int separator_width_
;
220 DISALLOW_COPY_AND_ASSIGN(OmniboxResultView
);
223 #endif // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_RESULT_VIEW_H_