From 25650b5ef88427ef5696ab7f74bc9da28e73cdc5 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 | 25 +++++++++++++++++-- .../TextSelectionCursorController.java | 15 +++-------- 3 files changed, 28 insertions(+), 16 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 eb62901f20..0a565a9aec 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 void onSingleTapUp(MotionEvent e) { 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 25773840b7..d26ad17e6d 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 boolean isOpaque() { 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 x = (int) (event.getX() / mRenderer.mFontWidth); + int y = (int) ((event.getY() - mRenderer.mFontLineSpacingAndAscent) / mRenderer.mFontLineSpacing); + if (relativeToScroll) { + y += mTopRow; + } + return new int[] { x, y }; + } + /** 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; 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 fe137a181b..253e8deae6 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 void render() { } } - 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))) {