Skip to content

Commit

Permalink
Enable pinch to change font size
Browse files Browse the repository at this point in the history
Standard way of changing the font size on touch screen devices. It can
be easily and flexibly handled (e.g. with changing lighting
conditions) and doesn't interfere with volume settings nor with
selecting text. Two finger touches are reliably distinguished from one
finger long presses.

Fixes connectbot#430
  • Loading branch information
morckx committed May 31, 2018
1 parent 76b698f commit c09fe25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public synchronized void tryKeyVibrate() {
*
* @param sizeDp Size of font in dp
*/
private void setFontSize(float sizeDp) {
public final void setFontSize(float sizeDp) {
if (sizeDp <= 0.0) {
return;
}
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.view.MotionEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.ScaleGestureDetector;
import android.widget.TextView;
import de.mud.terminal.VDUBuffer;
import de.mud.terminal.vt320;
Expand All @@ -52,6 +53,7 @@ public class TerminalTextViewOverlay extends android.support.v7.widget.AppCompat
private String currentSelection = "";
private ActionMode selectionActionMode;
private ClipboardManager clipboard;
private ScaleGestureDetector scaleDetector;

private int oldBufferHeight = 0;
private int oldScrollY = -1;
Expand All @@ -66,6 +68,9 @@ public TerminalTextViewOverlay(Context context, TerminalView terminalView) {
setTypeface(Typeface.MONOSPACE);
setTextIsSelectable(true);
setCustomSelectionActionModeCallback(new TextSelectionActionModeCallback());

// Enable pinch to zoom
scaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

public void refreshTextFromBuffer() {
Expand Down Expand Up @@ -178,6 +183,8 @@ public void scrollTo(int x, int y) {

@Override
public boolean onTouchEvent(MotionEvent event) {
scaleDetector.onTouchEvent(event);

if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Selection may be beginning. Sync the TextView with the buffer.
refreshTextFromBuffer();
Expand Down Expand Up @@ -393,4 +400,22 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
public void onDestroyActionMode(ActionMode mode) {
}
}

@Override
public boolean bringPointIntoView(int offset) {
return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
terminalView.bridge.setFontSize(terminalView.bridge.getFontSize() * detector.getScaleFactor());
return true;
}

@Override
public void onScaleEnd(ScaleGestureDetector detector) {
refreshTextFromBuffer();
}
}
}

0 comments on commit c09fe25

Please sign in to comment.