Skip to content

Commit 4e3c4e0

Browse files
committed
feat(view): Enable mouse cursor movement in shell
1 parent 7bceab8 commit 4e3c4e0

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,12 @@ public boolean onTouchEvent(MotionEvent event) {
611611
updateFloatingToolbarVisibility(event);
612612
mGestureRecognizer.onTouchEvent(event);
613613
return true;
614-
} else if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
615-
if (event.isButtonPressed(MotionEvent.BUTTON_SECONDARY)) {
614+
// vvv THIS IS THE CORRECTED LINE vvv
615+
} else if (event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) {
616+
if (event.isFromSource(InputDevice.SOURCE_MOUSE) && event.isButtonPressed(MotionEvent.BUTTON_SECONDARY)) {
616617
if (action == MotionEvent.ACTION_DOWN) showContextMenu();
617618
return true;
618-
} else if (event.isButtonPressed(MotionEvent.BUTTON_TERTIARY)) {
619+
} else if (event.isFromSource(InputDevice.SOURCE_MOUSE) && event.isButtonPressed(MotionEvent.BUTTON_TERTIARY)) {
619620
ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
620621
ClipData clipData = clipboardManager.getPrimaryClip();
621622
if (clipData != null) {
@@ -636,6 +637,55 @@ public boolean onTouchEvent(MotionEvent event) {
636637
break;
637638
}
638639
}
640+
else { // Mouse tracking is OFF, handle for the shell
641+
if (event.getAction() == MotionEvent.ACTION_DOWN) {
642+
// --- FINAL 1D LOGIC ---
643+
// Get target row and column from the click
644+
int[] targetCoords = getColumnAndRow(event, false);
645+
int targetColumn = targetCoords[0];
646+
int targetRow = targetCoords[1];
647+
648+
// vvv NEW, MORE ROBUST BOUNDARY CHECK vvv
649+
try {
650+
// Use the getSelectedText method to check the character at the clicked position.
651+
String clickedChar = mEmulator.getScreen().getSelectedText(targetColumn, targetRow, targetColumn + 1, targetRow).toString();
652+
653+
// If the character at the clicked spot is empty or just a space, do nothing.
654+
if (clickedChar == null || clickedChar.trim().isEmpty()) {
655+
return true;
656+
}
657+
} catch (Exception e) {
658+
// If there's any error getting the text, it's safer to do nothing.
659+
return true;
660+
}
661+
// ^^^ END OF NEW BOUNDARY CHECK ^^^
662+
663+
// Get cursor's current visual position
664+
int currentRow = mEmulator.getCursorRow();
665+
int currentColumn = mEmulator.getCursorCol();
666+
667+
// Get the width of the terminal in characters
668+
int terminalWidth = mEmulator.mColumns;
669+
670+
// Calculate the 1D index for both the click position and the cursor position.
671+
// This treats the multi-line input as a single long line of text.
672+
int targetIndex = (targetRow * terminalWidth) + targetColumn;
673+
int currentIndex = (currentRow * terminalWidth) + currentColumn;
674+
675+
int diff = targetIndex - currentIndex;
676+
677+
// --- Now, only send Left or Right commands ---
678+
if (diff > 0) { // Need to move right
679+
for (int i = 0; i < diff; i++) {
680+
handleKeyCode(android.view.KeyEvent.KEYCODE_DPAD_RIGHT, 0);
681+
}
682+
} else if (diff < 0) { // Need to move left
683+
for (int i = 0; i < -diff; i++) {
684+
handleKeyCode(android.view.KeyEvent.KEYCODE_DPAD_LEFT, 0);
685+
}
686+
}
687+
}
688+
}
639689
}
640690

641691
mGestureRecognizer.onTouchEvent(event);

0 commit comments

Comments
 (0)