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.

Resolves connectbot#430
  • Loading branch information
morckx committed Dec 23, 2023
1 parent e3e2a89 commit a120ffe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/src/main/java/org/connectbot/service/TerminalBridge.java
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 void setFontSize(float sizeDp) {
if (sizeDp <= 0.0) {
return;
}
Expand Down Expand Up @@ -557,6 +557,13 @@ private void setFontSize(float sizeDp) {
forcedSize = false;
}

/**
* @return current text size in dp
*/
public float getTextSizeDp() {
return getTextSizePx() / displayDensity / systemFontScale;
}

/**
* @return current text size in pixels
*/
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 @@ -35,6 +35,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 @@ -50,6 +51,7 @@ public class TerminalTextViewOverlay extends androidx.appcompat.widget.AppCompat
private String currentSelection = "";
private ActionMode selectionActionMode;
private ClipboardManager clipboard;
private ScaleGestureDetector scaleDetector;

private int oldBufferHeight = 0;
private int oldScrollY = -1;
Expand All @@ -64,6 +66,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 @@ -176,6 +181,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 @@ -386,4 +393,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.getTextSizeDp() * detector.getScaleFactor());
return true;
}

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

0 comments on commit a120ffe

Please sign in to comment.