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_POPUP_CONTENTS_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_POPUP_CONTENTS_VIEW_H_
8 #include "base/memory/weak_ptr.h"
9 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
10 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
11 #include "ui/base/window_open_disposition.h"
12 #include "ui/gfx/animation/animation_delegate.h"
13 #include "ui/gfx/animation/slide_animation.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/views/view.h"
16 #include "ui/views/view_targeter_delegate.h"
18 struct AutocompleteMatch
;
19 class LocationBarView
;
20 class OmniboxEditModel
;
21 class OmniboxResultView
;
25 // A view representing the contents of the autocomplete popup.
26 class OmniboxPopupContentsView
: public views::View
,
27 public OmniboxPopupView
,
28 public views::ViewTargeterDelegate
,
29 public gfx::AnimationDelegate
{
31 // Factory method for creating the AutocompletePopupView.
32 static OmniboxPopupView
* Create(const gfx::FontList
& font_list
,
33 OmniboxView
* omnibox_view
,
34 OmniboxEditModel
* edit_model
,
35 LocationBarView
* location_bar_view
);
37 // Returns the bounds the popup should be shown at. This is the display bounds
38 // and includes offsets for the dropshadow which this view's border renders.
39 gfx::Rect
GetPopupBounds() const;
41 virtual void LayoutChildren();
44 bool IsOpen() const override
;
45 void InvalidateLine(size_t line
) override
;
46 void UpdatePopupAppearance() override
;
47 gfx::Rect
GetTargetBounds() override
;
48 void PaintUpdatesNow() override
;
49 void OnDragCanceled() override
;
51 // gfx::AnimationDelegate:
52 void AnimationProgressed(const gfx::Animation
* animation
) override
;
55 void Layout() override
;
56 views::View
* GetTooltipHandlerForPoint(const gfx::Point
& point
) override
;
57 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
58 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
59 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
60 void OnMouseCaptureLost() override
;
61 void OnMouseMoved(const ui::MouseEvent
& event
) override
;
62 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
63 void OnMouseExited(const ui::MouseEvent
& event
) override
;
64 void OnGestureEvent(ui::GestureEvent
* event
) override
;
66 bool IsSelectedIndex(size_t index
) const;
67 bool IsHoveredIndex(size_t index
) const;
68 gfx::Image
GetIconIfExtensionMatch(size_t index
) const;
69 bool IsStarredMatch(const AutocompleteMatch
& match
) const;
71 int max_match_contents_width() const {
72 return max_match_contents_width_
;
76 OmniboxPopupContentsView(const gfx::FontList
& font_list
,
77 OmniboxView
* omnibox_view
,
78 OmniboxEditModel
* edit_model
,
79 LocationBarView
* location_bar_view
);
80 ~OmniboxPopupContentsView() override
;
82 LocationBarView
* location_bar_view() { return location_bar_view_
; }
84 virtual void PaintResultViews(gfx::Canvas
* canvas
);
86 // Calculates the height needed to show all the results in the model.
87 virtual int CalculatePopupHeight();
88 virtual OmniboxResultView
* CreateResultView(int model_index
,
89 const gfx::FontList
& font_list
);
92 class AutocompletePopupWidget
;
95 const char* GetClassName() const override
;
96 void OnPaint(gfx::Canvas
* canvas
) override
;
97 // This method should not be triggered directly as we paint our children
98 // in an un-conventional way inside OnPaint. We use a separate canvas to
99 // paint the children. Hence we override this method to a no-op so that
100 // the view hierarchy does not "accidentally" trigger this.
101 void PaintChildren(gfx::Canvas
* canvas
,
102 const views::CullSet
& cull_set
) override
;
104 // views::ViewTargeterDelegate:
105 views::View
* TargetForRect(views::View
* root
, const gfx::Rect
& rect
) override
;
107 // Call immediately after construction.
110 // Returns true if the model has a match at the specified index.
111 bool HasMatchAt(size_t index
) const;
113 // Returns the match at the specified index within the popup model.
114 const AutocompleteMatch
& GetMatchAtIndex(size_t index
) const;
116 // Fill a path for the contents' roundrect. |bounding_rect| is the rect that
118 void MakeContentsPath(gfx::Path
* path
, const gfx::Rect
& bounding_rect
);
120 // Find the index of the match under the given |point|, specified in window
121 // coordinates. Returns OmniboxPopupModel::kNoMatch if there isn't a match at
122 // the specified point.
123 size_t GetIndexForPoint(const gfx::Point
& point
);
125 // Processes a located event (e.g. mouse/gesture) and sets the selection/hover
126 // state of a line in the list.
127 void UpdateLineEvent(const ui::LocatedEvent
& event
,
128 bool should_set_selected_line
);
130 // Opens an entry from the list depending on the event and the selected
132 void OpenSelectedLine(const ui::LocatedEvent
& event
,
133 WindowOpenDisposition disposition
);
135 OmniboxResultView
* result_view_at(size_t i
);
137 scoped_ptr
<OmniboxPopupModel
> model_
;
139 // The popup that contains this view. We create this, but it deletes itself
140 // when its window is destroyed. This is a WeakPtr because it's possible for
141 // the OS to destroy the window and thus delete this object before we're
142 // deleted, or without our knowledge.
143 base::WeakPtr
<AutocompletePopupWidget
> popup_
;
145 // The edit view that invokes us.
146 OmniboxView
* omnibox_view_
;
148 LocationBarView
* location_bar_view_
;
150 // The font list used for result rows, based on the omnibox font list.
151 gfx::FontList font_list_
;
153 // If the user cancels a dragging action (i.e. by pressing ESC), we don't have
154 // a convenient way to release mouse capture. Instead we use this flag to
155 // simply ignore all remaining drag events, and the eventual mouse release
156 // event. Since OnDragCanceled() can be called when we're not dragging, this
157 // flag is reset to false on a mouse pressed event, to make sure we don't
158 // erroneously ignore the next drag.
159 bool ignore_mouse_drag_
;
161 // The popup sizes vertically using an animation when the popup is getting
162 // shorter (not larger, that makes it look "slow").
163 gfx::SlideAnimation size_animation_
;
164 gfx::Rect start_bounds_
;
165 gfx::Rect target_bounds_
;
170 const gfx::ImageSkia
* bottom_shadow_
; // Ptr owned by resource bundle.
172 // Amount of extra padding to add to the popup on the top and bottom.
173 int outside_vertical_padding_
;
175 // When the dropdown is not wide enough while displaying postfix suggestions,
176 // we use the width of widest match contents to shift the suggestions so that
177 // the widest suggestion just reaches the end edge.
178 int max_match_contents_width_
;
180 DISALLOW_COPY_AND_ASSIGN(OmniboxPopupContentsView
);
183 #endif // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_POPUP_CONTENTS_VIEW_H_