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-26 07:13:48 -07:00
|
|
|
import org.mozilla.gecko.gfx.Layer;
|
2012-08-20 12:43:53 -07:00
|
|
|
import org.mozilla.gecko.gfx.LayerView;
|
2012-08-02 18:38:45 -07:00
|
|
|
import org.mozilla.gecko.util.EventDispatcher;
|
2012-07-27 21:57:47 -07:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2012-08-02 17:13:40 -07:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2012-09-21 10:56:41 -07:00
|
|
|
import org.json.JSONArray;
|
2012-12-18 22:57:54 -08:00
|
|
|
import org.json.JSONException;
|
2012-07-19 13:16:44 -07:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
|
2012-07-26 07:13:48 -07:00
|
|
|
class TextSelection extends Layer implements GeckoEventListener {
|
2012-07-19 13:16:44 -07:00
|
|
|
private static final String LOGTAG = "GeckoTextSelection";
|
|
|
|
|
|
|
|
private final TextSelectionHandle mStartHandle;
|
2012-09-21 10:56:41 -07:00
|
|
|
private final TextSelectionHandle mMiddleHandle;
|
2012-07-19 13:16:44 -07:00
|
|
|
private final TextSelectionHandle mEndHandle;
|
2012-08-02 18:38:45 -07:00
|
|
|
private final EventDispatcher mEventDispatcher;
|
2012-07-19 13:16:44 -07:00
|
|
|
|
2012-07-26 07:13:48 -07:00
|
|
|
private float mViewLeft;
|
|
|
|
private float mViewTop;
|
|
|
|
private float mViewZoom;
|
|
|
|
|
2012-12-18 22:57:54 -08:00
|
|
|
private GeckoApp mActivity;
|
|
|
|
|
2012-09-21 10:56:41 -07:00
|
|
|
TextSelection(TextSelectionHandle startHandle,
|
|
|
|
TextSelectionHandle middleHandle,
|
|
|
|
TextSelectionHandle endHandle,
|
2012-12-18 22:57:54 -08:00
|
|
|
EventDispatcher eventDispatcher,
|
|
|
|
GeckoApp activity) {
|
2012-07-19 13:16:44 -07:00
|
|
|
mStartHandle = startHandle;
|
2012-09-21 10:56:41 -07:00
|
|
|
mMiddleHandle = middleHandle;
|
2012-07-19 13:16:44 -07:00
|
|
|
mEndHandle = endHandle;
|
2012-08-02 18:38:45 -07:00
|
|
|
mEventDispatcher = eventDispatcher;
|
2012-12-18 22:57:54 -08:00
|
|
|
mActivity = activity;
|
2012-07-19 13:16:44 -07:00
|
|
|
|
2012-09-21 10:56:41 -07:00
|
|
|
// Only register listeners if we have valid start/middle/end handles
|
|
|
|
if (mStartHandle == null || mMiddleHandle == null || mEndHandle == null) {
|
2012-07-23 15:41:55 -07:00
|
|
|
Log.e(LOGTAG, "Failed to initialize text selection because at least one handle is null");
|
|
|
|
} else {
|
2012-08-02 18:38:45 -07:00
|
|
|
registerEventListener("TextSelection:ShowHandles");
|
|
|
|
registerEventListener("TextSelection:HideHandles");
|
|
|
|
registerEventListener("TextSelection:PositionHandles");
|
2012-07-23 15:41:55 -07:00
|
|
|
}
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
2012-08-02 18:38:45 -07:00
|
|
|
unregisterEventListener("TextSelection:ShowHandles");
|
|
|
|
unregisterEventListener("TextSelection:HideHandles");
|
|
|
|
unregisterEventListener("TextSelection:PositionHandles");
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
|
|
|
|
2012-10-18 10:41:06 -07:00
|
|
|
private TextSelectionHandle getHandle(String name) {
|
|
|
|
if (name.equals("START")) {
|
|
|
|
return mStartHandle;
|
|
|
|
} else if (name.equals("MIDDLE")) {
|
|
|
|
return mMiddleHandle;
|
|
|
|
} else {
|
|
|
|
return mEndHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-18 22:57:54 -08:00
|
|
|
public void handleMessage(final String event, final JSONObject message) {
|
|
|
|
mActivity.runOnUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-18 22:57:54 -08:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
if (event.equals("TextSelection:ShowHandles")) {
|
|
|
|
final JSONArray handles = message.getJSONArray("handles");
|
|
|
|
for (int i=0; i < handles.length(); i++) {
|
|
|
|
String handle = handles.getString(i);
|
|
|
|
getHandle(handle).setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
mViewLeft = 0.0f;
|
|
|
|
mViewTop = 0.0f;
|
|
|
|
mViewZoom = 0.0f;
|
|
|
|
LayerView layerView = mActivity.getLayerView();
|
|
|
|
if (layerView != null) {
|
|
|
|
layerView.addLayer(TextSelection.this);
|
|
|
|
}
|
|
|
|
} else if (event.equals("TextSelection:HideHandles")) {
|
|
|
|
LayerView layerView = mActivity.getLayerView();
|
|
|
|
if (layerView != null) {
|
|
|
|
layerView.removeLayer(TextSelection.this);
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:25:19 -07:00
|
|
|
mStartHandle.setVisibility(View.GONE);
|
|
|
|
mMiddleHandle.setVisibility(View.GONE);
|
|
|
|
mEndHandle.setVisibility(View.GONE);
|
2012-12-18 22:57:54 -08:00
|
|
|
} else if (event.equals("TextSelection:PositionHandles")) {
|
|
|
|
final boolean rtl = message.getBoolean("rtl");
|
|
|
|
final JSONArray positions = message.getJSONArray("positions");
|
|
|
|
for (int i=0; i < positions.length(); i++) {
|
|
|
|
JSONObject position = positions.getJSONObject(i);
|
|
|
|
int left = position.getInt("left");
|
|
|
|
int top = position.getInt("top");
|
|
|
|
|
|
|
|
TextSelectionHandle handle = getHandle(position.getString("handle"));
|
|
|
|
handle.setVisibility(position.getBoolean("hidden") ? View.GONE : View.VISIBLE);
|
|
|
|
handle.positionFromGecko(left, top, rtl);
|
|
|
|
}
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
2012-12-18 22:57:54 -08:00
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "JSON exception", e);
|
|
|
|
}
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
2012-12-18 22:57:54 -08:00
|
|
|
});
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|
2012-07-26 07:13:48 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(final RenderContext context) {
|
|
|
|
// cache the relevant values from the context and bail out if they are the same. we do this
|
|
|
|
// because this draw function gets called a lot (once per compositor frame) and we want to
|
|
|
|
// avoid doing a lot of extra work in cases where it's not needed.
|
2013-04-25 10:47:08 -07:00
|
|
|
final float viewLeft = context.viewport.left - context.offset.x;
|
|
|
|
final float viewTop = context.viewport.top - context.offset.y;
|
|
|
|
final float viewZoom = context.zoomFactor;
|
|
|
|
|
|
|
|
if (FloatUtils.fuzzyEquals(mViewLeft, viewLeft)
|
|
|
|
&& FloatUtils.fuzzyEquals(mViewTop, viewTop)
|
|
|
|
&& FloatUtils.fuzzyEquals(mViewZoom, viewZoom)) {
|
2012-07-26 07:13:48 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-04-25 10:47:08 -07:00
|
|
|
mViewLeft = viewLeft;
|
|
|
|
mViewTop = viewTop;
|
|
|
|
mViewZoom = viewZoom;
|
2012-07-26 07:13:48 -07:00
|
|
|
|
2012-12-18 22:57:54 -08:00
|
|
|
mActivity.runOnUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-07-26 07:13:48 -07:00
|
|
|
public void run() {
|
2013-04-25 10:47:08 -07:00
|
|
|
mStartHandle.repositionWithViewport(viewLeft, viewTop, viewZoom);
|
|
|
|
mMiddleHandle.repositionWithViewport(viewLeft, viewTop, viewZoom);
|
|
|
|
mEndHandle.repositionWithViewport(viewLeft, viewTop, viewZoom);
|
2012-07-26 07:13:48 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-08-02 18:38:45 -07:00
|
|
|
|
|
|
|
private void registerEventListener(String event) {
|
|
|
|
mEventDispatcher.registerEventListener(event, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unregisterEventListener(String event) {
|
|
|
|
mEventDispatcher.unregisterEventListener(event, this);
|
|
|
|
}
|
2012-07-19 13:16:44 -07:00
|
|
|
}
|