Skip to content

Commit 54bb83d

Browse files
committed
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.
1 parent 1a5a66d commit 54bb83d

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ public void onSingleTapUp(MotionEvent e) {
177177
TerminalEmulator term = mActivity.getCurrentSession().getEmulator();
178178

179179
if (mActivity.getProperties().shouldOpenTerminalTranscriptURLOnClick()) {
180-
int[] xAndY = mActivity.getTerminalView().getTextSelectionCursorController().getXAndYFromEvent(e);
181-
String wordAtTap = term.getScreen().getWordAtLocation(xAndY[0], xAndY[1]);
180+
int[] columnAndRow = mActivity.getTerminalView().getColumnAndRow(e, true);
181+
String wordAtTap = term.getScreen().getWordAtLocation(columnAndRow[0], columnAndRow[1]);
182182
LinkedHashSet<CharSequence> urlSet = UrlUtils.extractUrls(wordAtTap);
183183

184184
if (!urlSet.isEmpty()) {

terminal-view/src/main/java/com/termux/view/TerminalView.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,31 @@ public boolean isOpaque() {
466466
return true;
467467
}
468468

469+
/**
470+
* Get the zero indexed column and row of the terminal view for the
471+
* position of the event.
472+
*
473+
* @param event The event with the position to get the column and row for.
474+
* @param relativeToScroll If true the column number will take the scroll
475+
* position into account. E.g. if scrolled 3 lines up and the event
476+
* position is in the top left, column will be -3 if relativeToScroll is
477+
* true and 0 if relativeToScroll is false.
478+
* @return Array with the column and row.
479+
*/
480+
public int[] getColumnAndRow(MotionEvent event, boolean relativeToScroll) {
481+
int column = (int) (event.getX() / mRenderer.mFontWidth);
482+
int row = (int) ((event.getY() - mRenderer.mFontLineSpacingAndAscent) / mRenderer.mFontLineSpacing);
483+
if (relativeToScroll) {
484+
row += mTopRow;
485+
}
486+
return new int[] { column, row };
487+
}
488+
469489
/** Send a single mouse event code to the terminal. */
470490
void sendMouseEventCode(MotionEvent e, int button, boolean pressed) {
471-
int x = (int) (e.getX() / mRenderer.mFontWidth) + 1;
472-
int y = (int) ((e.getY() - mRenderer.mFontLineSpacingAndAscent) / mRenderer.mFontLineSpacing) + 1;
491+
int[] columnAndRow = getColumnAndRow(e, false);
492+
int x = columnAndRow[0] + 1;
493+
int y = columnAndRow[1] + 1;
473494
if (pressed && (button == TerminalEmulator.MOUSE_WHEELDOWN_BUTTON || button == TerminalEmulator.MOUSE_WHEELUP_BUTTON)) {
474495
if (mMouseStartDownTime == e.getDownTime()) {
475496
x = mMouseScrollStartX;
@@ -1129,7 +1150,7 @@ public void run() {
11291150
/**
11301151
* Define functions required for text selection and its handles.
11311152
*/
1132-
public TextSelectionCursorController getTextSelectionCursorController() {
1153+
TextSelectionCursorController getTextSelectionCursorController() {
11331154
if (mTextSelectionCursorController == null) {
11341155
mTextSelectionCursorController = new TextSelectionCursorController(this);
11351156

terminal-view/src/main/java/com/termux/view/textselection/TextSelectionCursorController.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,10 @@ public void render() {
8888
}
8989
}
9090

91-
public int[] getXAndYFromEvent(MotionEvent event) {
92-
int cx = (int) (event.getX() / terminalView.mRenderer.getFontWidth());
93-
final boolean eventFromMouse = event.isFromSource(InputDevice.SOURCE_MOUSE);
94-
// Offset for finger:
95-
final int SELECT_TEXT_OFFSET_Y = eventFromMouse ? 0 : -40;
96-
int cy = (int) ((event.getY() + SELECT_TEXT_OFFSET_Y) / terminalView.mRenderer.getFontLineSpacing()) + terminalView.getTopRow();
97-
return new int[] { cx, cy };
98-
}
99-
10091
public void setInitialTextSelectionPosition(MotionEvent event) {
101-
int[] xAndY = getXAndYFromEvent(event);
102-
mSelX1 = mSelX2 = xAndY[0];
103-
mSelY1 = mSelY2 = xAndY[1];
92+
int[] columnAndRow = terminalView.getColumnAndRow(event, true);
93+
mSelX1 = mSelX2 = columnAndRow[0];
94+
mSelY1 = mSelY2 = columnAndRow[1];
10495

10596
TerminalBuffer screen = terminalView.mEmulator.getScreen();
10697
if (!" ".equals(screen.getSelectedText(mSelX1, mSelY1, mSelX1, mSelY1))) {

0 commit comments

Comments
 (0)