forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextSelectionHandle.java
219 lines (187 loc) · 8.4 KB
/
TextSelectionHandle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import org.mozilla.gecko.animation.ViewHelper;
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
import org.mozilla.gecko.gfx.LayerView;
import org.json.JSONObject;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
* Text selection handles enable a user to change position of selected text in
* Gecko's DOM structure.
*
* A text "Selection" or nsISelection object, has start and end positions,
* referred to as Anchor and Focus objects.
*
* If the Anchor and Focus objects are at the same point, it represents a text
* selection Caret, commonly diplayed as a blinking, vertical |.
*
* Anchor and Focus objects each represent a DOM node, and character offset
* from the start of the node. The Anchor always refers to the start of the
* Selection, and the Focus refers to its end.
*
* In LTR languages such as English, the Anchor is to the left of the Focus.
* In RTL languages such as Hebrew, the Anchor is to the right of the Focus.
*
* For multi-line Selections, in both LTR and RTL languages, the Anchor starts
* above the Focus.
*/
class TextSelectionHandle extends ImageView implements View.OnTouchListener {
private static final String LOGTAG = "GeckoTextSelectionHandle";
public enum HandleType { ANCHOR, CARET, FOCUS };
private final HandleType mHandleType;
private final int mWidth;
private final int mHeight;
private final int mShadow;
private float mLeft;
private float mTop;
private boolean mIsRTL;
private PointF mGeckoPoint;
private PointF mTouchStart;
private RelativeLayout.LayoutParams mLayoutParams;
private static final int IMAGE_LEVEL_LTR = 0;
private static final int IMAGE_LEVEL_RTL = 1;
public TextSelectionHandle(Context context, AttributeSet attrs) {
super(context, attrs);
setOnTouchListener(this);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextSelectionHandle);
int handleType = a.getInt(R.styleable.TextSelectionHandle_handleType, 0x01);
a.recycle();
if (handleType == 0x01)
mHandleType = HandleType.ANCHOR;
else if (handleType == 0x02)
mHandleType = HandleType.CARET;
else
mHandleType = HandleType.FOCUS;
mGeckoPoint = new PointF(0.0f, 0.0f);
mTouchStart = new PointF(0.0f, 0.0f);
mWidth = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_width);
mHeight = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_height);
mShadow = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_shadow);
}
private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
mTouchStart.x = event.getX();
mTouchStart.y = event.getY();
break;
}
case MotionEvent.ACTION_UP: {
mTouchStart.x = 0;
mTouchStart.y = 0;
// Reposition handles to line up with ends of selection
JSONObject args = new JSONObject();
try {
args.put("handleType", mHandleType.toString());
} catch (Exception e) {
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Position");
}
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Position", args.toString()));
break;
}
case MotionEvent.ACTION_MOVE: {
move(event.getRawX(), event.getRawY());
break;
}
}
return true;
}
private void move(float newX, float newY) {
LayerView layerView = GeckoAppShell.getLayerView();
// newX and newY are in screen coordinates, but mLeft/mTop are relative
// to the ancestor (which is what LayerView is relative to also). So,
// we need to adjust newX/newY. The |ancestorOrigin| variable computed
// below is the origin of the ancestor relative to the screen coordinates,
// so subtracting that from newY puts newY into the desired coordinate
// space. We also need to include the offset amount of the touch location
// relative to the top left of the handle (mTouchStart).
int[] layerViewPosition = new int[2];
layerView.getLocationOnScreen(layerViewPosition);
float ancestorOrigin = layerViewPosition[1];
mLeft = newX - mTouchStart.x;
mTop = newY - mTouchStart.y - ancestorOrigin;
// Send x coordinate on the right side of the start handle, left side of the end handle.
float layerViewTranslation = layerView.getSurfaceTranslation();
PointF geckoPoint = new PointF(mLeft + adjustLeftForHandle(),
mTop - layerViewTranslation);
geckoPoint = layerView.convertViewPointToLayerPoint(geckoPoint);
JSONObject args = new JSONObject();
try {
args.put("handleType", mHandleType.toString());
args.put("x", (int) geckoPoint.x);
args.put("y", (int) geckoPoint.y);
} catch (Exception e) {
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Move");
}
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Move", args.toString()));
// If we're positioning a cursor, don't move the handle here. Gecko
// will tell us the position of the caret, so we set the handle
// position then. This allows us to lock the handle to wherever the
// caret appears.
if (mHandleType != HandleType.CARET) {
setLayoutPosition();
}
}
void positionFromGecko(int left, int top, boolean rtl) {
LayerView layerView = GeckoAppShell.getLayerView();
if (layerView == null) {
Log.e(LOGTAG, "Can't position handle because layerView is null");
return;
}
mGeckoPoint = new PointF(left, top);
if (mIsRTL != rtl) {
mIsRTL = rtl;
setImageLevel(mIsRTL ? IMAGE_LEVEL_RTL : IMAGE_LEVEL_LTR);
}
ImmutableViewportMetrics metrics = layerView.getViewportMetrics();
repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
}
void repositionWithViewport(float x, float y, float zoom) {
PointF viewPoint = new PointF((mGeckoPoint.x * zoom) - x,
(mGeckoPoint.y * zoom) - y);
mLeft = viewPoint.x - adjustLeftForHandle();
mTop = viewPoint.y + GeckoAppShell.getLayerView().getSurfaceTranslation();
setLayoutPosition();
}
private float adjustLeftForHandle() {
if (mHandleType == HandleType.ANCHOR) {
return mIsRTL ? mShadow : mWidth - mShadow;
} else if (mHandleType == HandleType.CARET) {
return mWidth / 2;
} else {
return mIsRTL ? mWidth - mShadow : mShadow;
}
}
private void setLayoutPosition() {
if (mLayoutParams == null) {
mLayoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
// Set negative right/bottom margins so that the handles can be dragged outside of
// the content area (if they are dragged to the left/top, the dyanmic margins set
// below will take care of that).
mLayoutParams.rightMargin = 0 - mWidth;
mLayoutParams.bottomMargin = 0 - mHeight;
}
mLayoutParams.leftMargin = (int) mLeft;
mLayoutParams.topMargin = (int) mTop;
setLayoutParams(mLayoutParams);
}
}