From 54bb83de41385b839138ac8d8b6edd75e1b75cab Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Tue, 14 Sep 2021 21:06:16 +0200 Subject: [PATCH] Fix calculation of row number for selection and URL clicking When calculating the row that is clicked, for mouse tracking mFontLineSpacingAndAscent was taken into account, but for selection and URL clicking it wasn't. This adds a common function for calculating the column and row which does take it into account and use that for all three. I'm not quite sure why it's necessary to subtract mFontLineSpacingAndAscent, but with this calculation the click location matches the line that is acted on for me with both touch and mouse and on different font sizes. It also removes the offset for finger the selection/url used because I don't think it's common for apps on Android to have such an offset, and because the mouse tracking did not use such an offset. --- .../terminal/TermuxTerminalViewClient.java | 4 +-- .../java/com/termux/view/TerminalView.java | 27 ++++++++++++++++--- .../TextSelectionCursorController.java | 15 +++-------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index eb62901f..0a565a9a 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -177,8 +177,8 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { TerminalEmulator term = mActivity.getCurrentSession().getEmulator(); if (mActivity.getProperties().shouldOpenTerminalTranscriptURLOnClick()) { - int[] xAndY = mActivity.getTerminalView().getTextSelectionCursorController().getXAndYFromEvent(e); - String wordAtTap = term.getScreen().getWordAtLocation(xAndY[0], xAndY[1]); + int[] columnAndRow = mActivity.getTerminalView().getColumnAndRow(e, true); + String wordAtTap = term.getScreen().getWordAtLocation(columnAndRow[0], columnAndRow[1]); LinkedHashSet urlSet = UrlUtils.extractUrls(wordAtTap); if (!urlSet.isEmpty()) { diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index 25773840..2b56d66e 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -466,10 +466,31 @@ public final class TerminalView extends View { return true; } + /** + * Get the zero indexed column and row of the terminal view for the + * position of the event. + * + * @param event The event with the position to get the column and row for. + * @param relativeToScroll If true the column number will take the scroll + * position into account. E.g. if scrolled 3 lines up and the event + * position is in the top left, column will be -3 if relativeToScroll is + * true and 0 if relativeToScroll is false. + * @return Array with the column and row. + */ + public int[] getColumnAndRow(MotionEvent event, boolean relativeToScroll) { + int column = (int) (event.getX() / mRenderer.mFontWidth); + int row = (int) ((event.getY() - mRenderer.mFontLineSpacingAndAscent) / mRenderer.mFontLineSpacing); + if (relativeToScroll) { + row += mTopRow; + } + return new int[] { column, row }; + } + /** Send a single mouse event code to the terminal. */ void sendMouseEventCode(MotionEvent e, int button, boolean pressed) { - int x = (int) (e.getX() / mRenderer.mFontWidth) + 1; - int y = (int) ((e.getY() - mRenderer.mFontLineSpacingAndAscent) / mRenderer.mFontLineSpacing) + 1; + int[] columnAndRow = getColumnAndRow(e, false); + int x = columnAndRow[0] + 1; + int y = columnAndRow[1] + 1; if (pressed && (button == TerminalEmulator.MOUSE_WHEELDOWN_BUTTON || button == TerminalEmulator.MOUSE_WHEELUP_BUTTON)) { if (mMouseStartDownTime == e.getDownTime()) { x = mMouseScrollStartX; @@ -1129,7 +1150,7 @@ public final class TerminalView extends View { /** * Define functions required for text selection and its handles. */ - public TextSelectionCursorController getTextSelectionCursorController() { + TextSelectionCursorController getTextSelectionCursorController() { if (mTextSelectionCursorController == null) { mTextSelectionCursorController = new TextSelectionCursorController(this); diff --git a/terminal-view/src/main/java/com/termux/view/textselection/TextSelectionCursorController.java b/terminal-view/src/main/java/com/termux/view/textselection/TextSelectionCursorController.java index fe137a18..253e8dea 100644 --- a/terminal-view/src/main/java/com/termux/view/textselection/TextSelectionCursorController.java +++ b/terminal-view/src/main/java/com/termux/view/textselection/TextSelectionCursorController.java @@ -88,19 +88,10 @@ public class TextSelectionCursorController implements CursorController { } } - public int[] getXAndYFromEvent(MotionEvent event) { - int cx = (int) (event.getX() / terminalView.mRenderer.getFontWidth()); - final boolean eventFromMouse = event.isFromSource(InputDevice.SOURCE_MOUSE); - // Offset for finger: - final int SELECT_TEXT_OFFSET_Y = eventFromMouse ? 0 : -40; - int cy = (int) ((event.getY() + SELECT_TEXT_OFFSET_Y) / terminalView.mRenderer.getFontLineSpacing()) + terminalView.getTopRow(); - return new int[] { cx, cy }; - } - public void setInitialTextSelectionPosition(MotionEvent event) { - int[] xAndY = getXAndYFromEvent(event); - mSelX1 = mSelX2 = xAndY[0]; - mSelY1 = mSelY2 = xAndY[1]; + int[] columnAndRow = terminalView.getColumnAndRow(event, true); + mSelX1 = mSelX2 = columnAndRow[0]; + mSelY1 = mSelY2 = columnAndRow[1]; TerminalBuffer screen = terminalView.mEmulator.getScreen(); if (!" ".equals(screen.getSelectedText(mSelX1, mSelY1, mSelX1, mSelY1))) {