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