Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: ui/android/view_android.h

Issue 2708613002: Add EventForwarder for routing touch events (Closed)
Patch Set: comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/android/ui_android_jni_registrar.cc ('k') | ui/android/view_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 UI_ANDROID_VIEW_ANDROID_H_ 5 #ifndef UI_ANDROID_VIEW_ANDROID_H_
6 #define UI_ANDROID_VIEW_ANDROID_H_ 6 #define UI_ANDROID_VIEW_ANDROID_H_
7 7
8 #include <list> 8 #include <list>
9 9
10 #include "base/android/jni_weak_ref.h" 10 #include "base/android/jni_weak_ref.h"
11 #include "base/bind.h"
11 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
12 #include "ui/android/ui_android_export.h" 13 #include "ui/android/ui_android_export.h"
13 #include "ui/gfx/geometry/rect_f.h" 14 #include "ui/gfx/geometry/rect_f.h"
14 15
15 class GURL; 16 class GURL;
16 17
17 namespace cc { 18 namespace cc {
18 class Layer; 19 class Layer;
19 } 20 }
20 21
21 namespace ui { 22 namespace ui {
22 23 class EventHandler;
24 class MotionEventAndroid;
25 class ViewClient;
23 class WindowAndroid; 26 class WindowAndroid;
24 27
25 // A simple container for a UI layer. 28 // A simple container for a UI layer.
26 // At the root of the hierarchy is a WindowAndroid, when attached. 29 // At the root of the hierarchy is a WindowAndroid, when attached.
30 // Dispatches input/view events coming from Java layer. Hit testing against
31 // those events is implemented so that the |ViewClient| will be invoked
32 // when the event got hit on the area defined by |layout_params_|.
33 // Hit testing is done in the order of parent -> child, and from top
34 // of the stack to back among siblings.
27 class UI_ANDROID_EXPORT ViewAndroid { 35 class UI_ANDROID_EXPORT ViewAndroid {
28 public: 36 public:
29 // Stores an anchored view to delete itself at the end of its lifetime 37 // Stores an anchored view to delete itself at the end of its lifetime
30 // automatically. This helps manage the lifecyle without the dependency 38 // automatically. This helps manage the lifecyle without the dependency
31 // on |ViewAndroid|. 39 // on |ViewAndroid|.
32 class ScopedAnchorView { 40 class ScopedAnchorView {
33 public: 41 public:
34 ScopedAnchorView(JNIEnv* env, 42 ScopedAnchorView(JNIEnv* env,
35 const base::android::JavaRef<jobject>& jview, 43 const base::android::JavaRef<jobject>& jview,
36 const base::android::JavaRef<jobject>& jdelegate); 44 const base::android::JavaRef<jobject>& jdelegate);
(...skipping 12 matching lines...) Expand all
49 private: 57 private:
50 // TODO(jinsukkim): Following weak refs can be cast to strong refs which 58 // TODO(jinsukkim): Following weak refs can be cast to strong refs which
51 // cannot be garbage-collected and leak memory. Rewrite not to use them. 59 // cannot be garbage-collected and leak memory. Rewrite not to use them.
52 // see comments in crrev.com/2103243002. 60 // see comments in crrev.com/2103243002.
53 JavaObjectWeakGlobalRef view_; 61 JavaObjectWeakGlobalRef view_;
54 JavaObjectWeakGlobalRef delegate_; 62 JavaObjectWeakGlobalRef delegate_;
55 63
56 // Default copy/assign disabled by move constructor. 64 // Default copy/assign disabled by move constructor.
57 }; 65 };
58 66
59 // A ViewAndroid may have its own delegate or otherwise will 67 // Layout parameters used to set the view's position and size.
60 // use the next available parent's delegate. 68 // Position is in parent's coordinate space.
61 ViewAndroid(const base::android::JavaRef<jobject>& delegate); 69 struct LayoutParams {
70 static LayoutParams MatchParent() { return {true, 0, 0, 0, 0}; }
71 static LayoutParams Normal(int x, int y, int width, int height) {
72 return {false, x, y, width, height};
73 };
74
75 bool match_parent; // Bounds matches that of the parent if true.
76 int x;
77 int y;
78 int width;
79 int height;
80
81 LayoutParams(const LayoutParams& p)
Khushal 2017/03/07 06:32:05 LayoutParams(const LayoutParams& p) = default; wil
Jinsuk Kim 2017/03/08 02:16:16 nice. Done.
82 : match_parent(p.match_parent),
83 x(p.x),
84 y(p.y),
85 width(p.width),
86 height(p.height) {}
87
88 private:
89 LayoutParams(bool match_parent, int x, int y, int width, int height)
90 : match_parent(match_parent),
91 x(x),
92 y(y),
93 width(width),
94 height(height) {}
95 };
96
97 explicit ViewAndroid(ViewClient* view_client);
62 98
63 ViewAndroid(); 99 ViewAndroid();
64 virtual ~ViewAndroid(); 100 virtual ~ViewAndroid();
65 101
66 // The content offset is in CSS pixels, and is used to translate 102 // The content offset is in CSS pixels, and is used to translate
67 // snapshots to the correct part of the view. 103 // snapshots to the correct part of the view.
68 void set_content_offset(const gfx::Vector2dF& content_offset) { 104 void set_content_offset(const gfx::Vector2dF& content_offset) {
69 content_offset_ = content_offset; 105 content_offset_ = content_offset;
70 } 106 }
71 107
72 gfx::Vector2dF content_offset() const { 108 gfx::Vector2dF content_offset() const {
73 return content_offset_; 109 return content_offset_;
74 } 110 }
75 111
76 // Returns the window at the root of this hierarchy, or |null| 112 // Returns the window at the root of this hierarchy, or |null|
77 // if disconnected. 113 // if disconnected.
78 virtual WindowAndroid* GetWindowAndroid() const; 114 virtual WindowAndroid* GetWindowAndroid() const;
79 115
80 // Used to return and set the layer for this view. May be |null|. 116 // Used to return and set the layer for this view. May be |null|.
81 cc::Layer* GetLayer() const; 117 cc::Layer* GetLayer() const;
82 void SetLayer(scoped_refptr<cc::Layer> layer); 118 void SetLayer(scoped_refptr<cc::Layer> layer);
83 119
84 void SetDelegate(const base::android::JavaRef<jobject>& delegate); 120 void SetDelegate(const base::android::JavaRef<jobject>& delegate);
85 121
86 // Adds this view as a child of another view. 122 // Gets (creates one if not present) Java object of the EventHandler
123 // for a view tree in the view hierarchy including this node.
124 // Only one instance per the view tree is allowed.
125 base::android::ScopedJavaLocalRef<jobject> GetEventHandler();
126
127 // Adds a child to this view.
87 void AddChild(ViewAndroid* child); 128 void AddChild(ViewAndroid* child);
88 129
130 // Moves the give child ViewAndroid to the front of the list so that it can be
131 // the first responder of events.
132 void MoveToFront(ViewAndroid* child);
133
89 // Detaches this view from its parent. 134 // Detaches this view from its parent.
90 void RemoveFromParent(); 135 void RemoveFromParent();
91 136
137 // Sets the layout relative to parent. Used to do hit testing against events.
138 void SetLayout(LayoutParams params);
139
92 bool StartDragAndDrop(const base::android::JavaRef<jstring>& jtext, 140 bool StartDragAndDrop(const base::android::JavaRef<jstring>& jtext,
93 const base::android::JavaRef<jobject>& jimage); 141 const base::android::JavaRef<jobject>& jimage);
94 142
95 void OnBackgroundColorChanged(unsigned int color); 143 void OnBackgroundColorChanged(unsigned int color);
96 void OnTopControlsChanged(float top_controls_offset, 144 void OnTopControlsChanged(float top_controls_offset,
97 float top_content_offset); 145 float top_content_offset);
98 void OnBottomControlsChanged(float bottom_controls_offset, 146 void OnBottomControlsChanged(float bottom_controls_offset,
99 float bottom_content_offset); 147 float bottom_content_offset);
100 void StartContentIntent(const GURL& content_url, bool is_main_frame); 148 void StartContentIntent(const GURL& content_url, bool is_main_frame);
101 149
102 ScopedAnchorView AcquireAnchorView(); 150 ScopedAnchorView AcquireAnchorView();
103 void SetAnchorRect(const base::android::JavaRef<jobject>& anchor, 151 void SetAnchorRect(const base::android::JavaRef<jobject>& anchor,
104 const gfx::RectF& bounds); 152 const gfx::RectF& bounds);
105 153
106 // This may return null. 154 // This may return null.
107 base::android::ScopedJavaLocalRef<jobject> GetContainerView(); 155 base::android::ScopedJavaLocalRef<jobject> GetContainerView();
108 156
157 float GetDipScale();
158
109 protected: 159 protected:
110 ViewAndroid* parent_; 160 ViewAndroid* parent_;
111 161
112 private: 162 private:
163 friend class EventHandler;
164 friend class ViewAndroidBoundsTest;
165
166 bool OnTouchEvent(const MotionEventAndroid& event, bool for_touch_handle);
167 bool OnMouseEvent(const MotionEventAndroid& event);
168
113 void RemoveChild(ViewAndroid* child); 169 void RemoveChild(ViewAndroid* child);
114 170
171 bool HitTest(const base::Callback<
172 bool(ViewClient*, const MotionEventAndroid&)> send_to_client,
173 const MotionEventAndroid& event);
174
175 static bool SendTouchEventToClient(bool for_touch_handle,
176 ViewClient* client,
177 const MotionEventAndroid& event);
178 static bool SendMouseEventToClient(ViewClient* client,
179 const MotionEventAndroid& event);
180
181 bool has_event_handler() const { return !!event_handler_; }
182
183 // Returns true if any node of the tree along the hierarchy (view's children
184 // and parents) already has |EventHandler| attached to it.
185 static bool ViewTreeHasEventHandler(ViewAndroid* view);
186
187 // Returns true if any children node (or self) has |EventHandler|.
188 static bool SubtreeHasEventHandler(ViewAndroid* view);
189
115 // Returns the Java delegate for this view. This is used to delegate work 190 // Returns the Java delegate for this view. This is used to delegate work
116 // up to the embedding view (or the embedder that can deal with the 191 // up to the embedding view (or the embedder that can deal with the
117 // implementation details). 192 // implementation details).
118 const base::android::ScopedJavaLocalRef<jobject> 193 const base::android::ScopedJavaLocalRef<jobject>
119 GetViewAndroidDelegate() const; 194 GetViewAndroidDelegate() const;
120 195
121 std::list<ViewAndroid*> children_; 196 std::list<ViewAndroid*> children_;
122 scoped_refptr<cc::Layer> layer_; 197 scoped_refptr<cc::Layer> layer_;
123 JavaObjectWeakGlobalRef delegate_; 198 JavaObjectWeakGlobalRef delegate_;
124 gfx::Vector2dF content_offset_; // in CSS pixel 199
200 ViewClient* const client_;
201
202 // Basic view layout information. Used to do hit testing deciding whether
203 // the passed events should be processed by the view.
204 LayoutParams layout_params_;
205
206 gfx::Vector2dF content_offset_; // in CSS pixel.
207 std::unique_ptr<EventHandler> event_handler_;
125 208
126 DISALLOW_COPY_AND_ASSIGN(ViewAndroid); 209 DISALLOW_COPY_AND_ASSIGN(ViewAndroid);
127 }; 210 };
128 211
129 } // namespace ui 212 } // namespace ui
130 213
131 #endif // UI_ANDROID_VIEW_ANDROID_H_ 214 #endif // UI_ANDROID_VIEW_ANDROID_H_
OLDNEW
« no previous file with comments | « ui/android/ui_android_jni_registrar.cc ('k') | ui/android/view_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698