Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit 497c32a

Browse files
committed
Modify text selection algorithm to work better:
+ Use drag to select rather than two taps. + Text selection is now a spring-loaded mode: It only lasts for one drag session. + Add a verical offset so you can see what you're selecting.
1 parent 4c8e467 commit 497c32a

File tree

2 files changed

+62
-59
lines changed

2 files changed

+62
-59
lines changed

res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
<string name="edit_text">Edit text</string>
2626
<string name="select_text">Select text</string>
27-
<string name="select_text_done">Copy to clipboard</string>
2827
<string name="copy_all">Copy all</string>
2928
<string name="paste">Paste</string>
3029

src/jackpal/androidterm/Term.java

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public void onCreate(Bundle icicle) {
188188

189189
mEmulatorView = (EmulatorView) findViewById(EMULATOR_VIEW);
190190

191+
DisplayMetrics metrics = new DisplayMetrics();
192+
getWindowManager().getDefaultDisplay().getMetrics(metrics);
193+
mEmulatorView.setScaledDensity(metrics.scaledDensity);
194+
191195
startListening();
192196

193197
mKeyListener = new TermKeyListener();
@@ -463,7 +467,7 @@ public void onCreateContextMenu(ContextMenu menu, View v,
463467
ContextMenuInfo menuInfo) {
464468
super.onCreateContextMenu(menu, v, menuInfo);
465469
menu.setHeaderTitle(R.string.edit_text);
466-
menu.add(0, SELECT_TEXT_ID, 0, !mEmulatorView.getSelectingText() ? R.string.select_text : R.string.select_text_done);
470+
menu.add(0, SELECT_TEXT_ID, 0, R.string.select_text);
467471
menu.add(0, COPY_ALL_ID, 0, R.string.copy_all);
468472
menu.add(0, PASTE_ID, 0, R.string.paste);
469473
if (!canPaste()) {
@@ -475,9 +479,6 @@ public void onCreateContextMenu(ContextMenu menu, View v,
475479
public boolean onContextItemSelected(MenuItem item) {
476480
switch (item.getItemId()) {
477481
case SELECT_TEXT_ID:
478-
if (mEmulatorView.getSelectingText()) {
479-
doCopySelectedText();
480-
}
481482
mEmulatorView.toggleSelectingText();
482483
return true;
483484
case COPY_ALL_ID:
@@ -531,17 +532,7 @@ private void doCopyAll() {
531532
clip.setText(mEmulatorView.getTranscriptText().trim());
532533
}
533534

534-
private void doCopySelectedText() {
535-
ClipboardManager clip = (ClipboardManager)
536-
getSystemService(Context.CLIPBOARD_SERVICE);
537-
clip.setText(mEmulatorView.getSelectedText().trim());
538-
}
539-
540535
private void doPaste() {
541-
if (mEmulatorView.getSelectingText()) {
542-
doCopySelectedText();
543-
}
544-
545536
ClipboardManager clip = (ClipboardManager)
546537
getSystemService(Context.CLIPBOARD_SERVICE);
547538
CharSequence paste = clip.getText();
@@ -2715,14 +2706,15 @@ class EmulatorView extends View implements GestureDetector.OnGestureListener {
27152706

27162707
private boolean mIsSelectingText = false;
27172708

2709+
2710+
private float mScaledDensity;
2711+
private static final int SELECT_TEXT_OFFSET_Y = -40;
2712+
private int mSelXAnchor = -1;
2713+
private int mSelYAnchor = -1;
27182714
private int mSelX1 = -1;
27192715
private int mSelY1 = -1;
27202716
private int mSelX2 = -1;
27212717
private int mSelY2 = -1;
2722-
private int mSelX1Old = -1;
2723-
private int mSelY1Old = -1;
2724-
private int mSelX2Old = -1;
2725-
private int mSelY2Old = -1;
27262718

27272719
/**
27282720
* Used to poll if the view has changed size. Wish there was a better way to do this.
@@ -2781,6 +2773,10 @@ public EmulatorView(Context context) {
27812773
commonConstructor();
27822774
}
27832775

2776+
public void setScaledDensity(float scaledDensity) {
2777+
mScaledDensity = scaledDensity;
2778+
}
2779+
27842780
public void onResume() {
27852781
updateSize(false);
27862782
mHandler.postDelayed(mCheckSize, SCREEN_CHECK_PERIOD);
@@ -3015,8 +3011,7 @@ public void append(byte[] buffer, int base, int length) {
30153011
int rowShift = mEmulator.getScrollCounter();
30163012
mSelY1 -= rowShift;
30173013
mSelY2 -= rowShift;
3018-
mSelY1Old -= rowShift;
3019-
mSelY2Old -= rowShift;
3014+
mSelYAnchor -= rowShift;
30203015
}
30213016
mEmulator.clearScrollCounter();
30223017
ensureCursorVisible();
@@ -3080,15 +3075,6 @@ public boolean onSingleTapUp(MotionEvent e) {
30803075
}
30813076

30823077
public void onLongPress(MotionEvent e) {
3083-
if ( mIsSelectingText ) {
3084-
if ( mSelX1 == mSelX2 && mSelY1 == mSelY2 ) {
3085-
mSelX1 = mSelX1Old;
3086-
mSelY1 = mSelY1Old;
3087-
mSelX2 = mSelX2Old;
3088-
mSelY2 = mSelY2Old;
3089-
invalidate();
3090-
}
3091-
}
30923078
showContextMenu();
30933079
}
30943080

@@ -3135,37 +3121,59 @@ public void onShowPress(MotionEvent e) {
31353121

31363122
public boolean onDown(MotionEvent e) {
31373123
mScrollRemainder = 0.0f;
3138-
if ( mIsSelectingText ) {
3139-
if ( mSelX1 == mSelX2 && mSelY1 == mSelY2 && mSelX1 != -1 ) {
3140-
mSelX2 = (int)(e.getX() / mCharacterWidth);
3141-
mSelY2 = (int)(e.getY() / mCharacterHeight) + mTopRow;
3142-
int minx = Math.min(mSelX1, mSelX2);
3143-
int maxx = Math.max(mSelX1, mSelX2);
3144-
int miny = Math.min(mSelY1, mSelY2);
3145-
int maxy = Math.max(mSelY1, mSelY2);
3146-
mSelX1 = minx;
3147-
mSelY1 = miny;
3148-
mSelX2 = maxx;
3149-
mSelY2 = maxy;
3150-
} else {
3151-
mSelX1Old = mSelX1;
3152-
mSelY1Old = mSelY1;
3153-
mSelX2Old = mSelX2;
3154-
mSelY2Old = mSelY2;
3155-
mSelX1 = (int)(e.getX() / mCharacterWidth);
3156-
mSelY1 = (int)(e.getY() / mCharacterHeight) + mTopRow;
3157-
mSelX2 = mSelX1;
3158-
mSelY2 = mSelY1;
3159-
}
3160-
invalidate();
3161-
}
31623124
return true;
31633125
}
31643126

31653127
// End GestureDetector.OnGestureListener methods
31663128

31673129
@Override public boolean onTouchEvent(MotionEvent ev) {
3168-
return mGestureDetector.onTouchEvent(ev);
3130+
if (mIsSelectingText) {
3131+
return onTouchEventWhileSelectingText(ev);
3132+
} else {
3133+
return mGestureDetector.onTouchEvent(ev);
3134+
}
3135+
}
3136+
3137+
private boolean onTouchEventWhileSelectingText(MotionEvent ev) {
3138+
int action = ev.getAction();
3139+
int cx = (int)(ev.getX() / mCharacterWidth);
3140+
int cy = Math.max(0,
3141+
(int)((ev.getY() + SELECT_TEXT_OFFSET_Y * mScaledDensity)
3142+
/ mCharacterHeight) + mTopRow);
3143+
switch (action) {
3144+
case MotionEvent.ACTION_DOWN:
3145+
mSelXAnchor = cx;
3146+
mSelYAnchor = cy;
3147+
mSelX1 = cx;
3148+
mSelY1 = cy;
3149+
mSelX2 = mSelX1;
3150+
mSelY2 = mSelY1;
3151+
break;
3152+
case MotionEvent.ACTION_MOVE:
3153+
case MotionEvent.ACTION_UP:
3154+
int minx = Math.min(mSelXAnchor, cx);
3155+
int maxx = Math.max(mSelXAnchor, cx);
3156+
int miny = Math.min(mSelYAnchor, cy);
3157+
int maxy = Math.max(mSelYAnchor, cy);
3158+
mSelX1 = minx;
3159+
mSelY1 = miny;
3160+
mSelX2 = maxx;
3161+
mSelY2 = maxy;
3162+
if (action == MotionEvent.ACTION_UP) {
3163+
ClipboardManager clip = (ClipboardManager)
3164+
getContext().getApplicationContext()
3165+
.getSystemService(Context.CLIPBOARD_SERVICE);
3166+
clip.setText(getSelectedText().trim());
3167+
toggleSelectingText();
3168+
}
3169+
invalidate();
3170+
break;
3171+
default:
3172+
toggleSelectingText();
3173+
invalidate();
3174+
break;
3175+
}
3176+
return true;
31693177
}
31703178

31713179
@Override
@@ -3378,10 +3386,6 @@ public void toggleSelectingText() {
33783386
mSelY1 = -1;
33793387
mSelX2 = -1;
33803388
mSelY2 = -1;
3381-
mSelX1Old = -1;
3382-
mSelY1Old = -1;
3383-
mSelX2Old = -1;
3384-
mSelY2Old = -1;
33853389
}
33863390
}
33873391

0 commit comments

Comments
 (0)