1 // Copyright 2014 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_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
8 #include "base/i18n/rtl.h"
9 #include "content/public/browser/render_widget_host.h"
10 #include "ui/gfx/geometry/rect.h"
11 #include "ui/gfx/geometry/rect_f.h"
12 #include "ui/gfx/native_widget_types.h"
15 struct NativeWebKeyboardEvent
;
26 // Class that controls common functionality for Autofill style popups. Can
27 // determine the correct location of a popup of a desired size and can register
28 // a handler to key press events.
29 class PopupControllerCommon
{
31 PopupControllerCommon(const gfx::RectF
& element_bounds
,
32 base::i18n::TextDirection text_direction
,
33 gfx::NativeView container_view
,
34 content::WebContents
* web_contents
);
35 virtual ~PopupControllerCommon();
37 const gfx::RectF
& element_bounds() const { return element_bounds_
; }
38 bool is_rtl() const { return text_direction_
== base::i18n::RIGHT_TO_LEFT
; }
39 gfx::NativeView
container_view() { return container_view_
; }
40 content::WebContents
* web_contents() { return web_contents_
; }
42 // Returns the enclosing rectangle for |element_bounds_|.
43 const gfx::Rect
RoundedElementBounds() const;
45 // Returns the bounds that the popup should be placed at, given the desired
46 // width and height. By default this places the popup below |element_bounds|
47 // but it will be placed above if there isn't enough space.
48 gfx::Rect
GetPopupBounds(int desired_width
, int desired_height
) const;
50 // Callback used to register with RenderViewHost. This can only be set once,
51 // or else a callback may be registered that will not be removed
52 // (crbug.com/338070). Call will crash if callback is already set.
53 void SetKeyPressCallback(content::RenderWidgetHost::KeyPressEventCallback
);
55 // Register listener for key press events with the current RenderViewHost
56 // associated with |web_contents_|. If callback has already been registered,
57 // this has no effect.
58 void RegisterKeyPressCallback();
60 // Remove previously registered callback, assuming that the current
61 // RenderViewHost is the same as when it was originally registered. Safe to
62 // call even if the callback is not currently registered.
63 void RemoveKeyPressCallback();
66 // A helper function to get the display closest to the given point (virtual
68 virtual gfx::Display
GetDisplayNearestPoint(const gfx::Point
& point
) const;
71 std::pair
<int, int> CalculatePopupXAndWidth(
72 const gfx::Display
& left_display
,
73 const gfx::Display
& right_display
,
74 int popup_required_width
) const;
76 // Calculates the height of the popup and the y position of it. These values
77 // will stay on the screen.
78 std::pair
<int, int> CalculatePopupYAndHeight(
79 const gfx::Display
& top_display
,
80 const gfx::Display
& bottom_display
,
81 int popup_required_height
) const;
83 // The bounds of the text element that is the focus of the popup.
84 // These coordinates are in screen space.
85 gfx::RectF element_bounds_
;
87 // The direction of the <input>.
88 base::i18n::TextDirection text_direction_
;
91 gfx::NativeView container_view_
;
93 // The WebContents in which this object should listen for keyboard events
94 // while showing the popup. Can be NULL, in which case this object will not
95 // listen for keyboard events.
96 content::WebContents
* web_contents_
;
98 // The RenderViewHost that this object has registered its keyboard press
100 content::RenderViewHost
* key_press_event_target_
;
102 content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_
;
104 DISALLOW_COPY_AND_ASSIGN(PopupControllerCommon
);
107 } // namespace autofill
109 #endif // CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_