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

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

Issue 2708613002: Add EventForwarder for routing touch events (Closed)
Patch Set: EventForwarder... 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 EventForwarder;
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) = default;
82
83 private:
84 LayoutParams(bool match_parent, int x, int y, int width, int height)
85 : match_parent(match_parent),
86 x(x),
87 y(y),
88 width(width),
89 height(height) {}
90 };
91
92 explicit ViewAndroid(ViewClient* view_client);
62 93
63 ViewAndroid(); 94 ViewAndroid();
64 virtual ~ViewAndroid(); 95 virtual ~ViewAndroid();
65 96
66 // The content offset is in CSS pixels, and is used to translate 97 // The content offset is in CSS pixels, and is used to translate
67 // snapshots to the correct part of the view. 98 // snapshots to the correct part of the view.
68 void set_content_offset(const gfx::Vector2dF& content_offset) { 99 void set_content_offset(const gfx::Vector2dF& content_offset) {
69 content_offset_ = content_offset; 100 content_offset_ = content_offset;
70 } 101 }
71 102
72 gfx::Vector2dF content_offset() const { 103 gfx::Vector2dF content_offset() const {
73 return content_offset_; 104 return content_offset_;
74 } 105 }
75 106
76 // Returns the window at the root of this hierarchy, or |null| 107 // Returns the window at the root of this hierarchy, or |null|
77 // if disconnected. 108 // if disconnected.
78 virtual WindowAndroid* GetWindowAndroid() const; 109 virtual WindowAndroid* GetWindowAndroid() const;
79 110
80 // Used to return and set the layer for this view. May be |null|. 111 // Used to return and set the layer for this view. May be |null|.
81 cc::Layer* GetLayer() const; 112 cc::Layer* GetLayer() const;
82 void SetLayer(scoped_refptr<cc::Layer> layer); 113 void SetLayer(scoped_refptr<cc::Layer> layer);
83 114
84 void SetDelegate(const base::android::JavaRef<jobject>& delegate); 115 void SetDelegate(const base::android::JavaRef<jobject>& delegate);
85 116
86 // Adds this view as a child of another view. 117 // Gets (creates one if not present) Java object of the EventForwarder
118 // for a view tree in the view hierarchy including this node.
119 // Only one instance per the view tree is allowed.
120 base::android::ScopedJavaLocalRef<jobject> GetEventForwarder();
121
122 // Adds a child to this view.
87 void AddChild(ViewAndroid* child); 123 void AddChild(ViewAndroid* child);
88 124
125 // Moves the give child ViewAndroid to the front of the list so that it can be
126 // the first responder of events.
127 void MoveToFront(ViewAndroid* child);
128
89 // Detaches this view from its parent. 129 // Detaches this view from its parent.
90 void RemoveFromParent(); 130 void RemoveFromParent();
91 131
132 // Sets the layout relative to parent. Used to do hit testing against events.
133 void SetLayout(LayoutParams params);
134
92 bool StartDragAndDrop(const base::android::JavaRef<jstring>& jtext, 135 bool StartDragAndDrop(const base::android::JavaRef<jstring>& jtext,
93 const base::android::JavaRef<jobject>& jimage); 136 const base::android::JavaRef<jobject>& jimage);
94 137
95 void OnBackgroundColorChanged(unsigned int color); 138 void OnBackgroundColorChanged(unsigned int color);
96 void OnTopControlsChanged(float top_controls_offset, 139 void OnTopControlsChanged(float top_controls_offset,
97 float top_content_offset); 140 float top_content_offset);
98 void OnBottomControlsChanged(float bottom_controls_offset, 141 void OnBottomControlsChanged(float bottom_controls_offset,
99 float bottom_content_offset); 142 float bottom_content_offset);
100 void StartContentIntent(const GURL& content_url, bool is_main_frame); 143 void StartContentIntent(const GURL& content_url, bool is_main_frame);
101 144
102 ScopedAnchorView AcquireAnchorView(); 145 ScopedAnchorView AcquireAnchorView();
103 void SetAnchorRect(const base::android::JavaRef<jobject>& anchor, 146 void SetAnchorRect(const base::android::JavaRef<jobject>& anchor,
104 const gfx::RectF& bounds); 147 const gfx::RectF& bounds);
105 148
106 // This may return null. 149 // This may return null.
107 base::android::ScopedJavaLocalRef<jobject> GetContainerView(); 150 base::android::ScopedJavaLocalRef<jobject> GetContainerView();
108 151
152 float GetDipScale();
153
109 protected: 154 protected:
110 ViewAndroid* parent_; 155 ViewAndroid* parent_;
111 156
112 private: 157 private:
158 friend class EventForwarder;
159 friend class ViewAndroidBoundsTest;
160
161 using ViewClientCallback =
162 const base::Callback<bool(ViewClient*, const MotionEventAndroid&)>;
163
164 bool OnTouchEvent(const MotionEventAndroid& event, bool for_touch_handle);
165 bool OnMouseEvent(const MotionEventAndroid& event);
166
113 void RemoveChild(ViewAndroid* child); 167 void RemoveChild(ViewAndroid* child);
114 168
169 bool HitTest(ViewClientCallback send_to_client,
170 const MotionEventAndroid& event);
171
172 static bool SendTouchEventToClient(bool for_touch_handle,
173 ViewClient* client,
174 const MotionEventAndroid& event);
175 static bool SendMouseEventToClient(ViewClient* client,
176 const MotionEventAndroid& event);
177
178 bool has_event_forwarder() const { return !!event_forwarder_; }
179
180 // Returns true if any node of the tree along the hierarchy (view's children
181 // and parents) already has |EventForwarder| attached to it.
182 static bool ViewTreeHasEventForwarder(ViewAndroid* view);
183
184 // Returns true if any children node (or self) has |EventForwarder|.
185 static bool SubtreeHasEventForwarder(ViewAndroid* view);
186
115 // Returns the Java delegate for this view. This is used to delegate work 187 // 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 188 // up to the embedding view (or the embedder that can deal with the
117 // implementation details). 189 // implementation details).
118 const base::android::ScopedJavaLocalRef<jobject> 190 const base::android::ScopedJavaLocalRef<jobject>
119 GetViewAndroidDelegate() const; 191 GetViewAndroidDelegate() const;
120 192
121 std::list<ViewAndroid*> children_; 193 std::list<ViewAndroid*> children_;
122 scoped_refptr<cc::Layer> layer_; 194 scoped_refptr<cc::Layer> layer_;
123 JavaObjectWeakGlobalRef delegate_; 195 JavaObjectWeakGlobalRef delegate_;
124 gfx::Vector2dF content_offset_; // in CSS pixel 196
197 ViewClient* const client_;
198
199 // Basic view layout information. Used to do hit testing deciding whether
200 // the passed events should be processed by the view.
201 LayoutParams layout_params_;
202
203 gfx::Vector2dF content_offset_; // in CSS pixel.
204 std::unique_ptr<EventForwarder> event_forwarder_;
125 205
126 DISALLOW_COPY_AND_ASSIGN(ViewAndroid); 206 DISALLOW_COPY_AND_ASSIGN(ViewAndroid);
127 }; 207 };
128 208
129 } // namespace ui 209 } // namespace ui
130 210
131 #endif // UI_ANDROID_VIEW_ANDROID_H_ 211 #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