Skip to content

Commit

Permalink
fix(MainActivity.java): fix mouse and stylus helper coordinate bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Jan 22, 2025
1 parent db74d87 commit 5641465
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
Expand Down Expand Up @@ -257,7 +256,7 @@ protected void onDestroy() {
//Register the needed events to handle stylus as left, middle and right click
@SuppressLint("ClickableViewAccessibility")
private void initStylusAuxButtons() {
View lorieView = getLorieView();
final ViewPager pager = getTerminalToolbarViewPager();
boolean stylusMenuEnabled = prefs.showStylusClickOverride.get() && LorieView.connected();
final float menuUnselectedTrasparency = 0.66f;
final float menuSelectedTrasparency = 1.0f;
Expand Down Expand Up @@ -297,7 +296,9 @@ private void initStylusAuxButtons() {

//Calculate screen border making sure btn is fully inside the view
float maxX = frm.getWidth() - 4 * left.getWidth();
float maxY = lorieView.getHeight() - 4 * left.getHeight();
float maxY = frm.getHeight() - 4 * left.getHeight();
if (prefs.adjustHeightForEK.get() && pager.getVisibility() == View.VISIBLE)
maxY -= pager.getHeight();

//Make sure the Stylus menu is fully inside the screen
overlay.setX(MathUtils.clamp(overlay.getX(), 0, maxX));
Expand All @@ -319,7 +320,9 @@ public void onDrawShadow(@NonNull Canvas canvas) {}
frm.setOnDragListener((v2, event) -> {
//Calculate screen border making sure btn is fully inside the view
float maxX = frm.getWidth() - visibility.getWidth();
float maxY = lorieView.getHeight() - visibility.getHeight();
float maxY = frm.getHeight() - visibility.getHeight();
if (prefs.adjustHeightForEK.get() && pager.getVisibility() == View.VISIBLE)
maxY -= pager.getHeight();

switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
Expand Down Expand Up @@ -363,18 +366,16 @@ private void showStylusAuxButtons(boolean show) {
}

private void makeSureHelpersAreVisibleAndInScreenBounds() {
int[] offset = new int[2];
final ViewPager pager = getTerminalToolbarViewPager();
View mouseAuxButtons = findViewById(R.id.mouse_buttons);
View stylusAuxButtons = findViewById(R.id.mouse_helper_visibility);
View lorieView = getLorieView();

frm.getLocationInWindow(offset);
int maxYDecrement = (prefs.adjustHeightForEK.get() && pager.getVisibility() == View.VISIBLE) ? pager.getHeight() : 0;

mouseAuxButtons.setX(MathUtils.clamp(mouseAuxButtons.getX(), offset[0], offset[0] + frm.getWidth() - mouseAuxButtons.getWidth()));
mouseAuxButtons.setY(MathUtils.clamp(mouseAuxButtons.getY(), offset[1], offset[1] + lorieView.getHeight() - mouseAuxButtons.getHeight()));
mouseAuxButtons.setX(MathUtils.clamp(mouseAuxButtons.getX(), frm.getX(), frm.getX() + frm.getWidth() - mouseAuxButtons.getWidth()));
mouseAuxButtons.setY(MathUtils.clamp(mouseAuxButtons.getY(), frm.getY(), frm.getY() + frm.getHeight() - mouseAuxButtons.getHeight() - maxYDecrement));

stylusAuxButtons.setX(MathUtils.clamp(stylusAuxButtons.getX(), offset[0], offset[0] + frm.getWidth() - stylusAuxButtons.getWidth()));
stylusAuxButtons.setY(MathUtils.clamp(stylusAuxButtons.getY(), offset[1], offset[1] + lorieView.getHeight() - stylusAuxButtons.getHeight()));
stylusAuxButtons.setX(MathUtils.clamp(stylusAuxButtons.getX(), frm.getX(), frm.getX() + frm.getWidth() - stylusAuxButtons.getWidth()));
stylusAuxButtons.setY(MathUtils.clamp(stylusAuxButtons.getY(), frm.getY(), frm.getY() + frm.getHeight() - stylusAuxButtons.getHeight() - maxYDecrement));
}

public void toggleStylusAuxButtons() {
Expand Down Expand Up @@ -404,7 +405,7 @@ void setSize(View v, int width, int height) {

@SuppressLint("ClickableViewAccessibility")
void initMouseAuxButtons() {
View lorieView = getLorieView();
final ViewPager pager = getTerminalToolbarViewPager();
Button left = findViewById(R.id.mouse_button_left_click);
Button right = findViewById(R.id.mouse_button_right_click);
Button middle = findViewById(R.id.mouse_button_middle_click);
Expand All @@ -426,10 +427,12 @@ void initMouseAuxButtons() {
secondaryLayer.setOrientation(LinearLayout.HORIZONTAL);
}
handler.postDelayed(() -> {
int[] offset = new int[2];
frm.getLocationInWindow(offset);
primaryLayer.setX(MathUtils.clamp(primaryLayer.getX(), offset[0], offset[0] + frm.getWidth() - primaryLayer.getWidth()));
primaryLayer.setY(MathUtils.clamp(primaryLayer.getY(), offset[1], offset[1] + lorieView.getHeight() - primaryLayer.getHeight()));
float maxX = frm.getX() + frm.getWidth() - primaryLayer.getWidth();
float maxY = frm.getY() + frm.getHeight() - primaryLayer.getHeight();
if (prefs.adjustHeightForEK.get() && pager.getVisibility() == View.VISIBLE)
maxY -= pager.getHeight();
primaryLayer.setX(MathUtils.clamp(primaryLayer.getX(), frm.getX(), maxX));
primaryLayer.setY(MathUtils.clamp(primaryLayer.getY(), frm.getY(), maxY));
}, 10);
});

Expand All @@ -456,7 +459,6 @@ void initMouseAuxButtons() {
final float[] startOffset = new float[2];
final int[] startPosition = new int[2];
long startTime;
View lorieView = getLorieView();
@Override
public boolean onTouch(View v, MotionEvent e) {
switch(e.getAction()) {
Expand All @@ -468,12 +470,16 @@ public boolean onTouch(View v, MotionEvent e) {
pos.setPressed(true);
break;
case MotionEvent.ACTION_MOVE: {
final ViewPager pager = getTerminalToolbarViewPager();
int[] offset = new int[2];
int[] offset2 = new int[2];
primaryLayer.getLocationInWindow(offset);
frm.getLocationInWindow(offset2);
primaryLayer.setX(MathUtils.clamp(offset[0] - startOffset[0] + e.getX(), offset2[0], offset2[0] + frm.getWidth() - primaryLayer.getWidth()));
primaryLayer.setY(MathUtils.clamp(offset[1] - startOffset[1] + e.getY(), offset2[1], offset2[1] + lorieView.getHeight() - primaryLayer.getHeight()));
float maxX = frm.getX() + frm.getWidth() - primaryLayer.getWidth();
float maxY = frm.getY() + frm.getHeight() - primaryLayer.getHeight();
if (prefs.adjustHeightForEK.get() && pager.getVisibility() == View.VISIBLE)
maxY -= pager.getHeight();

primaryLayer.setX(MathUtils.clamp(offset[0] - startOffset[0] + e.getX(), frm.getX(), maxX));
primaryLayer.setY(MathUtils.clamp(offset[1] - startOffset[1] + e.getY(), frm.getY(), maxY));
break;
}
case MotionEvent.ACTION_UP: {
Expand Down

0 comments on commit 5641465

Please sign in to comment.