Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 EventHandler |
| 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> GetEventHandler(); | |
| 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 EventHandler; | |
| 159 friend class ViewAndroidBoundsTest; | |
| 160 | |
| 161 bool OnTouchEvent(const MotionEventAndroid& event, bool for_touch_handle); | |
| 162 bool OnMouseEvent(const MotionEventAndroid& event); | |
| 163 | |
| 113 void RemoveChild(ViewAndroid* child); | 164 void RemoveChild(ViewAndroid* child); |
| 114 | 165 |
| 166 bool HitTest(const base::Callback< | |
|
boliu
2017/03/08 02:23:43
nit: alias the callback type so you don't have to
Jinsuk Kim
2017/03/08 04:29:40
Done.
| |
| 167 bool(ViewClient*, const MotionEventAndroid&)> send_to_client, | |
| 168 const MotionEventAndroid& event); | |
| 169 | |
| 170 static bool SendTouchEventToClient(bool for_touch_handle, | |
| 171 ViewClient* client, | |
| 172 const MotionEventAndroid& event); | |
| 173 static bool SendMouseEventToClient(ViewClient* client, | |
| 174 const MotionEventAndroid& event); | |
| 175 | |
| 176 bool has_event_handler() const { return !!event_handler_; } | |
| 177 | |
| 178 // Returns true if any node of the tree along the hierarchy (view's children | |
| 179 // and parents) already has |EventHandler| attached to it. | |
| 180 static bool ViewTreeHasEventHandler(ViewAndroid* view); | |
| 181 | |
| 182 // Returns true if any children node (or self) has |EventHandler|. | |
| 183 static bool SubtreeHasEventHandler(ViewAndroid* view); | |
| 184 | |
| 115 // Returns the Java delegate for this view. This is used to delegate work | 185 // 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 | 186 // up to the embedding view (or the embedder that can deal with the |
| 117 // implementation details). | 187 // implementation details). |
| 118 const base::android::ScopedJavaLocalRef<jobject> | 188 const base::android::ScopedJavaLocalRef<jobject> |
| 119 GetViewAndroidDelegate() const; | 189 GetViewAndroidDelegate() const; |
| 120 | 190 |
| 121 std::list<ViewAndroid*> children_; | 191 std::list<ViewAndroid*> children_; |
| 122 scoped_refptr<cc::Layer> layer_; | 192 scoped_refptr<cc::Layer> layer_; |
| 123 JavaObjectWeakGlobalRef delegate_; | 193 JavaObjectWeakGlobalRef delegate_; |
| 124 gfx::Vector2dF content_offset_; // in CSS pixel | 194 |
| 195 ViewClient* const client_; | |
| 196 | |
| 197 // Basic view layout information. Used to do hit testing deciding whether | |
| 198 // the passed events should be processed by the view. | |
| 199 LayoutParams layout_params_; | |
| 200 | |
| 201 gfx::Vector2dF content_offset_; // in CSS pixel. | |
| 202 std::unique_ptr<EventHandler> event_handler_; | |
| 125 | 203 |
| 126 DISALLOW_COPY_AND_ASSIGN(ViewAndroid); | 204 DISALLOW_COPY_AND_ASSIGN(ViewAndroid); |
| 127 }; | 205 }; |
| 128 | 206 |
| 129 } // namespace ui | 207 } // namespace ui |
| 130 | 208 |
| 131 #endif // UI_ANDROID_VIEW_ANDROID_H_ | 209 #endif // UI_ANDROID_VIEW_ANDROID_H_ |
| OLD | NEW |