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

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

Issue 2708613002: Add EventForwarder for routing touch events (Closed)
Patch Set: base::Bind (doesn't compile yet) 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
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"
13 #include "ui/android/event_handler.h"
12 #include "ui/android/ui_android_export.h" 14 #include "ui/android/ui_android_export.h"
13 #include "ui/gfx/geometry/rect_f.h" 15 #include "ui/gfx/geometry/rect_f.h"
14 16
15 class GURL; 17 class GURL;
16 18
17 namespace cc { 19 namespace cc {
18 class Layer; 20 class Layer;
19 } 21 }
20 22
21 namespace ui { 23 namespace ui {
22 24
25 class EventSender;
26 class MotionEventAndroid;
27 class ViewClient;
23 class WindowAndroid; 28 class WindowAndroid;
24 29
25 // A simple container for a UI layer. 30 // A simple container for a UI layer.
26 // At the root of the hierarchy is a WindowAndroid, when attached. 31 // At the root of the hierarchy is a WindowAndroid, when attached.
27 class UI_ANDROID_EXPORT ViewAndroid { 32 class UI_ANDROID_EXPORT ViewAndroid {
Khushal 2017/03/07 04:17:08 May be add a comment about the hit-testing order?
Jinsuk Kim 2017/03/07 05:02:24 Done.
28 public: 33 public:
29 // Stores an anchored view to delete itself at the end of its lifetime 34 // Stores an anchored view to delete itself at the end of its lifetime
30 // automatically. This helps manage the lifecyle without the dependency 35 // automatically. This helps manage the lifecyle without the dependency
31 // on |ViewAndroid|. 36 // on |ViewAndroid|.
32 class ScopedAnchorView { 37 class ScopedAnchorView {
33 public: 38 public:
34 ScopedAnchorView(JNIEnv* env, 39 ScopedAnchorView(JNIEnv* env,
35 const base::android::JavaRef<jobject>& jview, 40 const base::android::JavaRef<jobject>& jview,
36 const base::android::JavaRef<jobject>& jdelegate); 41 const base::android::JavaRef<jobject>& jdelegate);
37 42
(...skipping 11 matching lines...) Expand all
49 private: 54 private:
50 // TODO(jinsukkim): Following weak refs can be cast to strong refs which 55 // 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. 56 // cannot be garbage-collected and leak memory. Rewrite not to use them.
52 // see comments in crrev.com/2103243002. 57 // see comments in crrev.com/2103243002.
53 JavaObjectWeakGlobalRef view_; 58 JavaObjectWeakGlobalRef view_;
54 JavaObjectWeakGlobalRef delegate_; 59 JavaObjectWeakGlobalRef delegate_;
55 60
56 // Default copy/assign disabled by move constructor. 61 // Default copy/assign disabled by move constructor.
57 }; 62 };
58 63
59 // A ViewAndroid may have its own delegate or otherwise will 64 // Abstracts out the dispatcher for various types of events. Used for hit
60 // use the next available parent's delegate. 65 // testing logic.
61 ViewAndroid(const base::android::JavaRef<jobject>& delegate); 66 struct EventSender {
67 virtual bool SendToView(ViewAndroid* view,
68 const MotionEventAndroid& event) const = 0;
69 virtual bool SendToClient(ViewClient* client,
70 const MotionEventAndroid& event) const = 0;
71 };
72
73 // Layout parameters used to set the view's position and size.
74 class LayoutParams {
75 public:
76 static LayoutParams MatchParent() { return {true, 0, 0, 0, 0}; }
boliu 2017/03/06 22:27:45 curious.. did clang not complain about inlining th
Jinsuk Kim 2017/03/07 05:02:24 No it didn't...
77 static LayoutParams Normal(int x, int y, int width, int height) {
78 return {false, x, y, width, height};
79 };
80
81 private:
82 friend class ViewAndroid;
83 LayoutParams(bool match_parent, int x, int y, int width, int height)
84 : match_parent(match_parent),
85 x(x),
86 y(y),
87 width(width),
88 height(height) {}
89
90 const bool match_parent;
boliu 2017/03/06 22:27:45 "_" suffix, or you can keep members public and mak
Jinsuk Kim 2017/03/07 05:02:24 Done.
91 const int x;
92 const int y;
93 const int width;
94 const int 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);
Khushal 2017/03/07 04:17:08 Not for this patch but wanted to ask if this is in
Jinsuk Kim 2017/03/07 05:02:24 Ah yes - for now it is not necessary to do that si
Khushal 2017/03/07 06:32:05 Actually propagating them would be required precis
Jinsuk Kim 2017/03/08 02:16:16 Sorry I meant say 'child VA (RWHVA) is always set
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 // Virtual for testing.
158 virtual float GetDipScale();
159
109 protected: 160 protected:
110 ViewAndroid* parent_; 161 ViewAndroid* parent_;
111 162
112 private: 163 private:
164 friend class EventHandler;
165 friend class EventSendler;
166
167 bool OnTouchEvent(const MotionEventAndroid& event, bool for_touch_handle);
168 bool OnMouseEvent(const MotionEventAndroid& event);
169
113 void RemoveChild(ViewAndroid* child); 170 void RemoveChild(ViewAndroid* child);
114 171
172 bool HitTest(
173 const base::Callback<bool(ViewClient*, const MotionEventAndroid&)>
174 send_to_client,
175 const base::Callback<bool(ViewAndroid*, const MotionEventAndroid&)>
176 send_to_view,
177 const MotionEventAndroid& event);
178
179 static bool SendToClient(ViewClient* client, const MotionEventAndroid& event);
180 static bool SendToView(ViewAndroid* view, const MotionEventAndroid& event);
181
182 bool has_event_handler() const { return !!event_handler_; }
183
184 // Returns true if any node of the tree along the hierarchy (view's children
185 // and parents) already has |EventHandler| attached to it.
186 static bool ViewTreeHasEventHandler(ViewAndroid* view);
187
188 // Returns true if any children node (or self) has |EventHandler|.
189 static bool SubtreeHasEventHandler(ViewAndroid* view);
190
115 // Returns the Java delegate for this view. This is used to delegate work 191 // 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 192 // up to the embedding view (or the embedder that can deal with the
117 // implementation details). 193 // implementation details).
118 const base::android::ScopedJavaLocalRef<jobject> 194 const base::android::ScopedJavaLocalRef<jobject>
119 GetViewAndroidDelegate() const; 195 GetViewAndroidDelegate() const;
120 196
121 std::list<ViewAndroid*> children_; 197 std::list<ViewAndroid*> children_;
122 scoped_refptr<cc::Layer> layer_; 198 scoped_refptr<cc::Layer> layer_;
123 JavaObjectWeakGlobalRef delegate_; 199 JavaObjectWeakGlobalRef delegate_;
124 gfx::Vector2dF content_offset_; // in CSS pixel 200
201 ViewClient* const client_;
202
203 // Basic view layout information. Used to do hit testing deciding whether
204 // the passed events should be processed by the view.
205 gfx::Point origin_; // In parent's coordinate space.
boliu 2017/03/06 22:27:45 move comments here to LayoutParams maybe consider
Jinsuk Kim 2017/03/07 05:02:24 Done. Add copy ctor to |LayoutParams| for this (us
206 gfx::Size size_;
207 bool match_parent_; // Bounds matches that of the parent if true.
208
209 gfx::Vector2dF content_offset_; // in CSS pixel.
210 std::unique_ptr<EventHandler> event_handler_;
125 211
126 DISALLOW_COPY_AND_ASSIGN(ViewAndroid); 212 DISALLOW_COPY_AND_ASSIGN(ViewAndroid);
127 }; 213 };
128 214
129 } // namespace ui 215 } // namespace ui
130 216
131 #endif // UI_ANDROID_VIEW_ANDROID_H_ 217 #endif // UI_ANDROID_VIEW_ANDROID_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698