diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2015-01-30 20:59:05 +0900 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2015-02-09 08:12:07 +0100 |
commit | 72e7e945d09d028cedabaf7f80d5ec96b833e76e (patch) | |
tree | 1ef0f6e71301f9aa72a3edbfecb8beaf83fe2d50 /android | |
parent | c9893aa85887c108431c59d29feeeea8bef5f4ef (diff) |
android: TextSelection improvements
Change-Id: I485976edda5e2bb41381ad3a90b8d2139ca7d5f4
Diffstat (limited to 'android')
6 files changed, 94 insertions, 133 deletions
diff --git a/android/experimental/LOAndroid3/res/drawable/handle_end_level.xml b/android/experimental/LOAndroid3/res/drawable/handle_end_level.xml new file mode 100644 index 000000000000..40a512b3800a --- /dev/null +++ b/android/experimental/LOAndroid3/res/drawable/handle_end_level.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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/. --> + +<level-list xmlns:android="http://schemas.android.com/apk/res/android"> + + <!-- LTR --> + <item android:maxLevel="0" android:drawable="@drawable/handle_end"/> + + <!-- RTL --> + <item android:maxLevel="1" android:drawable="@drawable/handle_start"/> + +</level-list> diff --git a/android/experimental/LOAndroid3/res/drawable/handle_start_level.xml b/android/experimental/LOAndroid3/res/drawable/handle_start_level.xml new file mode 100644 index 000000000000..22945138d765 --- /dev/null +++ b/android/experimental/LOAndroid3/res/drawable/handle_start_level.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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/. --> + +<level-list xmlns:android="http://schemas.android.com/apk/res/android"> + + <!-- LTR --> + <item android:maxLevel="0" android:drawable="@drawable/handle_start"/> + + <!-- RTL --> + <item android:maxLevel="1" android:drawable="@drawable/handle_end"/> + +</level-list> diff --git a/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml b/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml index 1f0d8165729a..c876b288bc3d 100644 --- a/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml +++ b/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml @@ -9,7 +9,7 @@ <org.mozilla.gecko.TextSelectionHandle android:id="@+id/start_handle" android:layout_width="@dimen/text_selection_handle_width" android:layout_height="@dimen/text_selection_handle_height" - android:src="@drawable/handle_start" + android:src="@drawable/handle_start_level" android:visibility="gone" gecko:handleType="start"/> @@ -23,7 +23,7 @@ <org.mozilla.gecko.TextSelectionHandle android:id="@+id/end_handle" android:layout_width="@dimen/text_selection_handle_width" android:layout_height="@dimen/text_selection_handle_height" - android:src="@drawable/handle_end" + android:src="@drawable/handle_end_level" android:visibility="gone" gecko:handleType="end"/> </merge> diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java index 0bf9f1fad3fc..f416b4505904 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java @@ -11,6 +11,7 @@ import org.libreoffice.kit.Document; import org.libreoffice.kit.LibreOfficeKit; import org.libreoffice.kit.Office; import org.mozilla.gecko.TextSelection; +import org.mozilla.gecko.TextSelectionHandle; import org.mozilla.gecko.gfx.BufferedCairoImage; import org.mozilla.gecko.gfx.CairoImage; import org.mozilla.gecko.gfx.GeckoLayerClient; @@ -390,9 +391,10 @@ public class LOKitTileProvider implements TileProvider, Document.MessageCallback Log.i(LOGTAG, "Invalidate visible cursor: " + payload); RectF rect = convertCallbackMessageStringToRectF(payload); if (rect != null) { + RectF underSelection = new RectF(rect.centerX(), rect.bottom, rect.centerX(), rect.bottom); TextSelection textSelection = LibreOfficeMainActivity.mAppContext.getTextSelection(); - textSelection.positionHandle("MIDDLE", new RectF(rect.right,rect.bottom, rect.right, rect.bottom)); - textSelection.showHandle("MIDDLE"); + textSelection.positionHandle(TextSelectionHandle.HandleType.MIDDLE, underSelection); + textSelection.showHandle(TextSelectionHandle.HandleType.MIDDLE); } break; } diff --git a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelection.java b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelection.java index f09e93a9dbdb..241ce14704eb 100644 --- a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelection.java +++ b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelection.java @@ -16,6 +16,8 @@ import org.mozilla.gecko.gfx.Layer; import org.mozilla.gecko.gfx.LayerView; import org.mozilla.gecko.util.FloatUtils; +import static org.mozilla.gecko.TextSelectionHandle.HandleType.*; + public class TextSelection extends Layer { private static final String LOGTAG = "GeckoTextSelection"; @@ -43,84 +45,6 @@ public class TextSelection extends Layer { void destroy() { } - public void handleMessage(String event, JSONObject message) { - try { - if (event.equals("TextSelection:ShowHandles")) { - final JSONArray handles = message.getJSONArray("handles"); - LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { - public void run() { - try { - for (int i=0; i < handles.length(); i++) { - String handle = handles.getString(i); - - if (handle.equals("START")) - mStartHandle.setVisibility(View.VISIBLE); - else if (handle.equals("MIDDLE")) - mMiddleHandle.setVisibility(View.VISIBLE); - else - mEndHandle.setVisibility(View.VISIBLE); - } - - mViewLeft = 0.0f; - mViewTop = 0.0f; - mViewZoom = 0.0f; - LayerView layerView = LOKitShell.getLayerView(); - if (layerView != null) { - layerView.addLayer(TextSelection.this); - } - } catch(Exception e) {} - } - }); - } else if (event.equals("TextSelection:HideHandles")) { - final JSONArray handles = message.getJSONArray("handles"); - LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { - public void run() { - try { - LayerView layerView = LOKitShell.getLayerView(); - if (layerView != null) { - layerView.removeLayer(TextSelection.this); - } - - for (int i=0; i < handles.length(); i++) { - String handle = handles.getString(i); - if (handle.equals("START")) - mStartHandle.setVisibility(View.GONE); - else if (handle.equals("MIDDLE")) - mMiddleHandle.setVisibility(View.GONE); - else - mEndHandle.setVisibility(View.GONE); - } - - } catch(Exception e) {} - } - }); - } else if (event.equals("TextSelection:PositionHandles")) { - final JSONArray positions = message.getJSONArray("positions"); - LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { - public void run() { - try { - for (int i=0; i < positions.length(); i++) { - JSONObject position = positions.getJSONObject(i); - String handle = position.getString("handle"); - int left = position.getInt("left"); - int top = position.getInt("top"); - - if (handle.equals("START")) - mStartHandle.positionFromGecko(left, top); - else if (handle.equals("MIDDLE")) - mMiddleHandle.positionFromGecko(left, top); - else - mEndHandle.positionFromGecko(left, top); - } - } catch (Exception e) { } - } - }); - } - } catch (Exception e) { - Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e); - } - } - @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 @@ -144,47 +68,49 @@ public class TextSelection extends Layer { }); } - public void showHandle(final String handleType) { + public void showHandle(final TextSelectionHandle.HandleType handleType) { LOKitShell.getMainHandler().post(new Runnable() { public void run() { - try { - TextSelectionHandle handle; - if (handleType.equals("START")) - handle = mStartHandle; - else if (handleType.equals("MIDDLE")) - handle = mMiddleHandle; - else - handle = mEndHandle; - - handle.setVisibility(View.VISIBLE); - - mViewLeft = 0.0f; - mViewTop = 0.0f; - mViewZoom = 0.0f; - LayerView layerView = LOKitShell.getLayerView(); - if (layerView != null) { - layerView.addLayer(TextSelection.this); - } - } catch (Exception e) { + TextSelectionHandle handle = getHandle(handleType); + + handle.setVisibility(View.VISIBLE); + + mViewLeft = 0.0f; + mViewTop = 0.0f; + mViewZoom = 0.0f; + LayerView layerView = LOKitShell.getLayerView(); + if (layerView != null) { + layerView.addLayer(TextSelection.this); } } }); } - public void positionHandle(final String handleType, final RectF position) { + private TextSelectionHandle getHandle(TextSelectionHandle.HandleType handleType) { + if (handleType == START) { + return mStartHandle; + } else if (handleType == MIDDLE) { + return mMiddleHandle; + } else { + return mEndHandle; + } + } + + public void hideHandle(final TextSelectionHandle.HandleType handleType) { + LOKitShell.getMainHandler().post(new Runnable() { + public void run() { + TextSelectionHandle handle = getHandle(handleType); + handle.setVisibility(View.GONE); + } + }); + } + + + public void positionHandle(final TextSelectionHandle.HandleType handleType, final RectF position) { LOKitShell.getMainHandler().post(new Runnable() { public void run() { - try { - TextSelectionHandle handle; - if (handleType.equals("START")) - handle = mStartHandle; - else if (handleType.equals("MIDDLE")) - handle = mMiddleHandle; - else - handle = mEndHandle; - - handle.positionFromGecko((int) position.left, (int) position.top); - } catch (Exception e) { } + TextSelectionHandle handle = getHandle(handleType); + handle.positionFromGecko((int) position.left, (int) position.top, false); } }); } diff --git a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelectionHandle.java b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelectionHandle.java index 92ca9d42a114..b554465eef46 100644 --- a/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelectionHandle.java +++ b/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/TextSelectionHandle.java @@ -24,7 +24,7 @@ import android.widget.RelativeLayout; public class TextSelectionHandle extends ImageView implements View.OnTouchListener { private static final String LOGTAG = "GeckoTextSelectionHandle"; - private enum HandleType { START, MIDDLE, END }; + public enum HandleType { START, MIDDLE, END }; private final HandleType mHandleType; private final int mWidth; @@ -33,12 +33,16 @@ public class TextSelectionHandle extends ImageView implements View.OnTouchListen private int mLeft; private int mTop; + private boolean mIsRTL; private PointF mGeckoPoint; private int mTouchStartX; private int mTouchStartY; 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); @@ -53,6 +57,7 @@ public class TextSelectionHandle extends ImageView implements View.OnTouchListen else mHandleType = HandleType.END; + mIsRTL = false; mGeckoPoint = new PointF(0.0f, 0.0f); mWidth = getResources().getDimensionPixelSize(R.dimen.text_selection_handle_width); @@ -99,13 +104,7 @@ public class TextSelectionHandle extends ImageView implements View.OnTouchListen return; } // Send x coordinate on the right side of the start handle, left side of the end handle. - float left = (float) mLeft; - if (mHandleType.equals(HandleType.START)) - left += mWidth - mShadow; - else if (mHandleType.equals(HandleType.MIDDLE)) - left += (float) ((mWidth - mShadow) / 2); - else - left += mShadow; + float left = (float) mLeft + adjustLeftForHandle(); PointF geckoPoint = new PointF(left, (float) mTop); geckoPoint = layerView.getLayerClient().convertViewPointToLayerPoint(geckoPoint); @@ -123,8 +122,7 @@ public class TextSelectionHandle extends ImageView implements View.OnTouchListen setLayoutPosition(); } - void positionFromGecko(int left, int top) { - Log.i(LOGTAG, "positionFromGecko: " + left + " " + top); + void positionFromGecko(int left, int top, boolean rtl) { LayerView layerView = LOKitShell.getLayerView(); if (layerView == null) { Log.e(LOGTAG, "Can't position handle because layerView is null"); @@ -132,27 +130,34 @@ public class TextSelectionHandle extends ImageView implements View.OnTouchListen } mGeckoPoint = new PointF((float) left, (float) 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 = Math.round(viewPoint.x); - if (mHandleType.equals(HandleType.START)) - mLeft -= mWidth - mShadow; - else if (mHandleType.equals(HandleType.MIDDLE)) - mLeft -= (float) ((mWidth - mShadow) / 2); - else - mLeft -= mShadow; + (mGeckoPoint.y * zoom) - y); + mLeft = Math.round(viewPoint.x) - (int) adjustLeftForHandle(); mTop = Math.round(viewPoint.y); setLayoutPosition(); } + 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; + } + private void setLayoutPosition() { if (mLayoutParams == null) { mLayoutParams = (RelativeLayout.LayoutParams) getLayoutParams(); |