Skip to content

Commit

Permalink
Added: Advanced Mouse Mode setting
Browse files Browse the repository at this point in the history
Adds a setting so users can choose whether dragging the touch screen sends mouse move events, or scroll wheel up/down.
Defaults to scroll wheel (the old behavior).

Closes termux#1384
  • Loading branch information
rigrig committed Aug 7, 2024
1 parent 57e4ef4 commit 5a8b8d7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public void putBoolean(String key, boolean value) {
case "soft_keyboard_enabled_only_if_no_hardware":
mPreferences.setSoftKeyboardEnabledOnlyIfNoHardware(value);
break;
case "advanced_mouse":
mPreferences.setAdvancedMouse(value);
break;
default:
break;
}
Expand All @@ -74,6 +77,8 @@ public boolean getBoolean(String key, boolean defValue) {
return mPreferences.isSoftKeyboardEnabled();
case "soft_keyboard_enabled_only_if_no_hardware":
return mPreferences.isSoftKeyboardEnabledOnlyIfNoHardware();
case "advanced_mouse":
return mPreferences.isAdvancedMouse();
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.media.AudioManager;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.termux.R;
import com.termux.app.TermuxActivity;
Expand Down Expand Up @@ -101,6 +97,11 @@ public void onStart() {
boolean isTerminalViewKeyLoggingEnabled = mActivity.getPreferences().isTerminalViewKeyLoggingEnabled();
mActivity.getTerminalView().setIsTerminalViewKeyLoggingEnabled(isTerminalViewKeyLoggingEnabled);

// Set {@link TerminalView#ADVANCED_MOUSE} value
// Also required if user changed the preference from {@link TermuxSettings} activity and returns
boolean isAdvancedMouse = mActivity.getPreferences().isAdvancedMouse();
mActivity.getTerminalView().setAdvancedMouse(isAdvancedMouse);

// Piggyback on the terminal view key logging toggle for now, should add a separate toggle in future
mActivity.getTermuxActivityRootView().setIsRootViewLoggingEnabled(isTerminalViewKeyLoggingEnabled);
ViewUtils.setIsViewUtilsLoggingEnabled(isTerminalViewKeyLoggingEnabled);
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@
no hardware keyboard is connected.</string>


<!-- Mouse Category -->
<string name="termux_mouse_header">Mouse</string>

<!-- Touch Dragging -->
<string name="termux_advanced_mouse_title">Advanced Mouse Mode</string>
<string name="termux_advanced_mouse_off">Dragging the touch screen sends scroll wheel up/down events. (Default)</string>
<string name="termux_advanced_mouse_on">Dragging the touch screen sends mouse move events.</string>


<!-- Terminal View Preferences -->
<string name="termux_terminal_view_preferences_title">Terminal View</string>
<string name="termux_terminal_view_preferences_summary">Preferences for terminal view</string>
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/xml/termux_terminal_io_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@

</PreferenceCategory>

<PreferenceCategory
app:key="mouse"
app:title="@string/termux_mouse_header">

<SwitchPreferenceCompat
app:key="advanced_mouse"
app:summaryOff="@string/termux_advanced_mouse_off"
app:summaryOn="@string/termux_advanced_mouse_on"
app:title="@string/termux_advanced_mouse_title" />

</PreferenceCategory>
</PreferenceScreen>
17 changes: 15 additions & 2 deletions terminal-view/src/main/java/com/termux/view/TerminalView.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public final class TerminalView extends View {
/** Log terminal view key and IME events. */
private static boolean TERMINAL_VIEW_KEY_LOGGING_ENABLED = false;

/** Send full mouse events for dragging the touch screen, rather than scroll-wheep up/down */
private static boolean ADVANCED_MOUSE = false;

/** The currently displayed terminal session, whose emulator is {@link #mEmulator}. */
public TerminalSession mTermSession;
/** Our terminal emulator whose session is {@link #mTermSession}. */
Expand Down Expand Up @@ -85,7 +88,7 @@ public final class TerminalView extends View {
/** If non-zero, this is the last unicode code point received if that was a combining character. */
int mCombiningAccent;

private final boolean mAccessibilityEnabled;
private final boolean mAccessibilityEnabled;;

/** The {@link KeyEvent} is generated from a virtual keyboard, like manually with the {@link KeyEvent#KeyEvent(int, int)} constructor. */
public final static int KEY_EVENT_SOURCE_VIRTUAL_KEYBOARD = KeyCharacterMap.VIRTUAL_KEYBOARD; // -1
Expand Down Expand Up @@ -131,7 +134,8 @@ public boolean onSingleTapUp(MotionEvent event) {
@Override
public boolean onScroll(MotionEvent e, float distanceX, float distanceY) {
if (mEmulator == null) return true;
if (mEmulator.isMouseTrackingActive() && e.isFromSource(InputDevice.SOURCE_MOUSE)) {
if (ADVANCED_MOUSE || (mEmulator.isMouseTrackingActive() && e.isFromSource(InputDevice.SOURCE_MOUSE))) {
// Unless using advanced mouse mode:
// If moving with mouse pointer while pressing button, report that instead of scroll.
// This means that we never report moving with button press-events for touch input,
// since we cannot just start sending these events without a starting press event,
Expand Down Expand Up @@ -242,6 +246,15 @@ public void setIsTerminalViewKeyLoggingEnabled(boolean value) {
TERMINAL_VIEW_KEY_LOGGING_ENABLED = value;
}

/**
* Sets whether dragging the touch screen sends mouse move events, or up/down scroll wheel
*
* @param value True to send mouse swipe events, false for scroll wheel
*/
public void setAdvancedMouse(boolean value) {
ADVANCED_MOUSE = value;
}



/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ public void setSoftKeyboardEnabledOnlyIfNoHardware(boolean value) {
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_SOFT_KEYBOARD_ENABLED_ONLY_IF_NO_HARDWARE, value, false);
}

public boolean isAdvancedMouse() {
return SharedPreferenceUtils.getBoolean(mSharedPreferences, TERMUX_APP.KEY_ADVANCED_MOUSE, TERMUX_APP.DEFAULT_VALUE_KEY_ADVANCED_MOUSE);
}

public void setAdvancedMouse(boolean value) {
SharedPreferenceUtils.setBoolean(mSharedPreferences, TERMUX_APP.KEY_ADVANCED_MOUSE, value, false);
}



public boolean shouldKeepScreenOn() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
* - 0.16.0 (2022-06-11)
* - Added following to `TERMUX_APP`:
* `KEY_APP_SHELL_NUMBER_SINCE_BOOT` and `KEY_TERMINAL_SESSION_NUMBER_SINCE_BOOT`.
*
* - 0.??.? (2024-08-07
* - Added following to `TERMUX_APP`:
* `KEY_ADVANCED_MOUSE` and `DEFAULT_VALUE_KEY_ADVANCED_MOUSE`
*/

import com.termux.shared.shell.command.ExecutionCommand;
Expand Down Expand Up @@ -118,6 +122,12 @@ public static final class TERMUX_APP {
public static final boolean DEFAULT_VALUE_KEY_SOFT_KEYBOARD_ENABLED_ONLY_IF_NO_HARDWARE = false;


/**
* Defines the key for whether dragging the touch screen sends full mouse events instead of scroll wheel up/down
*/
public static final String KEY_ADVANCED_MOUSE = "advanced_mouse";
public static final boolean DEFAULT_VALUE_KEY_ADVANCED_MOUSE = false;

/**
* Defines the key for whether to always keep screen on.
*/
Expand Down

0 comments on commit 5a8b8d7

Please sign in to comment.