2012-07-19 13:16:44 -07:00
|
|
|
/* 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;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
|
2012-08-20 12:43:53 -07:00
|
|
|
import org.mozilla.gecko.gfx.LayerView;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2012-07-19 13:16:44 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.PointF;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageView;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.widget.RelativeLayout;
|
2012-07-19 13:16:44 -07:00
|
|
|
|
|
|
|
class TextSelectionHandle extends ImageView implements View.OnTouchListener {
|
|
|
|
private static final String LOGTAG = "GeckoTextSelectionHandle";
|
|
|
|
|
2012-09-21 10:56:41 -07:00
|
|
|
private enum HandleType { START, MIDDLE, END };
|
2012-07-19 13:16:44 -07:00
|
|
|
|
|
|
|
private final HandleType mHandleType;
|
|
|
|
private final int mWidth;
|
|
|
|
private final int mHeight;
|
2012-07-23 12:50:43 -07:00
|
|
|
private final int mShadow;
|
2012-07-19 13:16:44 -07:00
|
|
|
|
|
|
|
private int mLeft;
|
|
|
|
private int mTop;
|
2012-10-31 21:49:15 -07:00
|
|
|
private boolean mIsRTL;
|
2012-07-26 07:13:48 -07:00
|
|
|
private PointF mGeckoPoint;
|
2012-07-19 13:16:44 -07:00
|
|
|
private int mTouchStartX;
|
|
|
|
private int mTouchStartY;
|
|
|
|
|
2012-07-19 15:41:21 -07:00
|
|
|
private RelativeLayout.LayoutParams mLayoutParams;
|
|
|
|
|
2012-10-31 21:49:15 -07:00
|
|
|
private static final int IMAGE_LEVEL_LTR = 0;
|
|
|
|
private static final int IMAGE_LEVEL_RTL = 1;
|
|
|
|
|
2012-07-19 13:16:44 -07:00
|
|
|
TextSelectionHandle(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
setOnTouchListener(this);
|
|
|
|
|
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextSelectionHandle);
|
2012-09-21 10:56:41 -07:00
|
|
|
int handleType = a.getInt(R.styleable.TextSelectionHandle_handleType, 0x01);
|
|
|
|
|
|
|
|
if (handleType == 0x01)
|
|
|
|
mHandleType = HandleType.START;
|
|
|
|
else if (handleType == 0x02)
|
|
|
|
mHandleType = HandleType.MIDDLE;
|
|
|
|
else
|
|
|
|
mHandleType = HandleType.END;
|
|
|
|
|
2012-10-31 21:49:15 -07:00
|
|
|
mIsRTL = false;
|
2012-09-21 10:56:41 -07:00
|
|
|
mGeckoPoint = new PointF(0.0f, 0.0f);
|
2012-07-19 13:16:44 -07:00
|
|
|
|
|
|
|
mWidth = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_width);
|
|
|
|
mHeight = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_height);
|
2012-07-23 12:50:43 -07:00
|
|
|
mShadow = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_shadow);
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
switch (event.getActionMasked()) {
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
|
|
|
mTouchStartX = Math.round(event.getX());
|
|
|
|
mTouchStartY = Math.round(event.getY());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MotionEvent.ACTION_UP: {
|
|
|
|
mTouchStartX = 0;
|
|
|
|
mTouchStartY = 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(Math.round(event.getX()), Math.round(event.getY()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void move(int newX, int newY) {
|
|
|
|
mLeft = mLeft + newX - mTouchStartX;
|
|
|
|
mTop = mTop + newY - mTouchStartY;
|
|
|
|
|
2012-08-20 12:43:53 -07:00
|
|
|
LayerView layerView = GeckoApp.mAppContext.getLayerView();
|
|
|
|
if (layerView == null) {
|
|
|
|
Log.e(LOGTAG, "Can't move selection because layerView is null");
|
2012-07-19 13:16:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Send x coordinate on the right side of the start handle, left side of the end handle.
|
2012-10-31 21:49:15 -07:00
|
|
|
float left = (float) mLeft + adjustLeftForHandle();
|
2012-09-21 10:56:41 -07:00
|
|
|
|
2012-07-23 12:50:43 -07:00
|
|
|
PointF geckoPoint = new PointF(left, (float) mTop);
|
2012-08-20 12:43:53 -07:00
|
|
|
geckoPoint = layerView.convertViewPointToLayerPoint(geckoPoint);
|
2012-07-19 13:16:44 -07:00
|
|
|
|
|
|
|
JSONObject args = new JSONObject();
|
|
|
|
try {
|
|
|
|
args.put("handleType", mHandleType.toString());
|
|
|
|
args.put("x", Math.round(geckoPoint.x));
|
|
|
|
args.put("y", Math.round(geckoPoint.y));
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Error building JSON arguments for TextSelection:Move");
|
|
|
|
}
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Move", args.toString()));
|
|
|
|
|
2012-07-19 15:41:21 -07:00
|
|
|
setLayoutPosition();
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 21:49:15 -07:00
|
|
|
void positionFromGecko(int left, int top, boolean rtl) {
|
2012-08-20 12:43:53 -07:00
|
|
|
LayerView layerView = GeckoApp.mAppContext.getLayerView();
|
|
|
|
if (layerView == null) {
|
|
|
|
Log.e(LOGTAG, "Can't position handle because layerView is null");
|
2012-07-19 13:16:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-26 07:13:48 -07:00
|
|
|
mGeckoPoint = new PointF((float) left, (float) top);
|
2012-10-31 21:49:15 -07:00
|
|
|
if (mIsRTL != rtl) {
|
|
|
|
mIsRTL = rtl;
|
|
|
|
setImageLevel(mIsRTL ? IMAGE_LEVEL_RTL : IMAGE_LEVEL_LTR);
|
|
|
|
}
|
|
|
|
|
2012-08-20 12:43:53 -07:00
|
|
|
ImmutableViewportMetrics metrics = layerView.getViewportMetrics();
|
2012-07-26 07:13:48 -07:00
|
|
|
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);
|
|
|
|
|
2012-10-31 21:49:15 -07:00
|
|
|
mLeft = Math.round(viewPoint.x) - (int) adjustLeftForHandle();
|
2012-07-26 07:13:48 -07:00
|
|
|
mTop = Math.round(viewPoint.y);
|
2012-07-19 13:16:44 -07:00
|
|
|
|
2012-07-19 15:41:21 -07:00
|
|
|
setLayoutPosition();
|
|
|
|
}
|
|
|
|
|
2012-10-31 21:49:15 -07:00
|
|
|
private float adjustLeftForHandle() {
|
|
|
|
if (mHandleType.equals(HandleType.START))
|
|
|
|
return mIsRTL ? mShadow : mWidth - mShadow;
|
|
|
|
else if (mHandleType.equals(HandleType.MIDDLE))
|
|
|
|
return (float) ((mWidth - mShadow) / 2);
|
|
|
|
else
|
|
|
|
return mIsRTL ? mWidth - mShadow : mShadow;
|
|
|
|
}
|
|
|
|
|
2012-07-19 15:41:21 -07:00
|
|
|
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 = mLeft;
|
|
|
|
mLayoutParams.topMargin = mTop;
|
|
|
|
setLayoutParams(mLayoutParams);
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
|
|
|
}
|