Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f83e36b authored by Hans Boehm's avatar Hans Boehm
Browse files

Improve copy/paste menu appearance

Bug: 20503008

Correctly provide content position information to the ActionMode so
that menues can be better positioned.

Highlight a result that's about to be copied.

Ensure that the end of the current formula becomes visible when
the paste menu appears.

Change-Id: I318985776e59175b827d5089c0ca4978f3a658cb
parent 017de989
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/formula"
        android:bufferType="spannable"
        android:singleLine="true"
        android:textColor="@color/display_result_text_color" />

+33 −1
Original line number Diff line number Diff line
@@ -20,10 +20,13 @@ import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Rect;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.ActionMode;
@@ -522,11 +525,26 @@ public class CalculatorResult extends AlignedTextView {

    // Copy support:

    private ActionMode.Callback mCopyActionModeCallback = new ActionMode.Callback() {
    private ActionMode.Callback2 mCopyActionModeCallback = new ActionMode.Callback2() {

        private BackgroundColorSpan mHighlightSpan;

        private void highlightResult() {
            final Spannable text = (Spannable) getText();
            mHighlightSpan = new BackgroundColorSpan(getHighlightColor());
            text.setSpan(mHighlightSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        private void unhighlightResult() {
            final Spannable text = (Spannable) getText();
            text.removeSpan(mHighlightSpan);
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.copy, menu);
            highlightResult();
            return true;
        }

@@ -549,8 +567,22 @@ public class CalculatorResult extends AlignedTextView {

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            unhighlightResult();
            mActionMode = null;
        }

        @Override
        public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
            super.onGetContentRect(mode, view, outRect);
            outRect.left += getPaddingLeft();
            outRect.top += getPaddingTop();
            outRect.right -= getPaddingRight();
            outRect.bottom -= getPaddingBottom();
            final int width = (int) Layout.getDesiredWidth(getText(), getPaint());
            if (width < outRect.width()) {
                outRect.left = outRect.right - width;
            }
        }
    };

    public boolean stopActionMode() {
+14 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.text.Layout;
import android.text.TextPaint;
import android.text.method.ScrollingMovementMethod;
@@ -37,7 +38,8 @@ import android.widget.TextView;
 */
public class CalculatorText extends AlignedTextView implements View.OnLongClickListener {

    private final ActionMode.Callback mPasteActionModeCallback = new ActionMode.Callback() {
    private final ActionMode.Callback2 mPasteActionModeCallback = new ActionMode.Callback2() {

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (item.getItemId() == R.id.menu_paste) {
@@ -53,6 +55,7 @@ public class CalculatorText extends AlignedTextView implements View.OnLongClickL
            final ClipboardManager clipboard = (ClipboardManager) getContext()
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipboard.hasPrimaryClip()) {
                bringPointIntoView(length());
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.paste, menu);
                return true;
@@ -70,6 +73,16 @@ public class CalculatorText extends AlignedTextView implements View.OnLongClickL
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
        }

        @Override
        public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
            super.onGetContentRect(mode, view, outRect);
            outRect.top += getTotalPaddingTop();
            outRect.right -= getTotalPaddingRight();
            outRect.bottom -= getTotalPaddingBottom();
            // Encourage menu positioning towards the right, possibly over formula.
            outRect.left = outRect.right;
        }
    };

    // Temporary paint for use in layout methods.