Skip to content

Commit

Permalink
fix: make sure overlays are always in LorieView bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Jan 21, 2025
1 parent 4510545 commit 1d7cbd8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 17 additions & 16 deletions app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 @@ -256,6 +257,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();
boolean stylusMenuEnabled = prefs.showStylusClickOverride.get() && LorieView.connected();
final float menuUnselectedTrasparency = 0.66f;
final float menuSelectedTrasparency = 1.0f;
Expand Down Expand Up @@ -295,7 +297,7 @@ private void initStylusAuxButtons() {

//Calculate screen border making sure btn is fully inside the view
float maxX = frm.getWidth() - 4 * left.getWidth();
float maxY = frm.getHeight() - 4 * left.getHeight();
float maxY = lorieView.getHeight() - 4 * left.getHeight();

//Make sure the Stylus menu is fully inside the screen
overlay.setX(MathUtils.clamp(overlay.getX(), 0, maxX));
Expand All @@ -317,7 +319,7 @@ 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 = frm.getHeight() - visibility.getHeight();
float maxY = lorieView.getHeight() - visibility.getHeight();

switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
Expand Down Expand Up @@ -362,22 +364,17 @@ private void showStylusAuxButtons(boolean show) {

private void makeSureHelpersAreVisibleAndInScreenBounds() {
int[] offset = new int[2];
int[] offset2 = new int[2];
View mouseAuxButtons = findViewById(R.id.mouse_buttons);
View stylusAuxButtons = findViewById(R.id.mouse_helper_visibility);
frm.getLocationInWindow(offset2);
View lorieView = getLorieView();

if (mouseAuxButtons.getVisibility() == View.VISIBLE) {
mouseAuxButtons.getLocationInWindow(offset);
mouseAuxButtons.setX(MathUtils.clamp(offset[0], offset2[0], offset2[0] + frm.getWidth() - mouseAuxButtons.getWidth()));
mouseAuxButtons.setY(MathUtils.clamp(offset[1], offset2[1], offset2[1] + frm.getHeight() - mouseAuxButtons.getHeight()));
}
frm.getLocationInWindow(offset);

if (stylusAuxButtons.getVisibility() == View.VISIBLE) {
stylusAuxButtons.getLocationInWindow(offset);
stylusAuxButtons.setX(MathUtils.clamp(offset[0], offset2[0], offset2[0] + frm.getWidth() - stylusAuxButtons.getWidth()));
stylusAuxButtons.setY(MathUtils.clamp(offset[1], offset2[1], offset2[1] + frm.getHeight() - stylusAuxButtons.getHeight()));
}
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()));

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()));
}

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

@SuppressLint("ClickableViewAccessibility")
void initMouseAuxButtons() {
View lorieView = getLorieView();
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 @@ -431,7 +429,7 @@ void initMouseAuxButtons() {
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] + frm.getHeight() - primaryLayer.getHeight()));
primaryLayer.setY(MathUtils.clamp(primaryLayer.getY(), offset[1], offset[1] + lorieView.getHeight() - primaryLayer.getHeight()));
}, 10);
});

Expand All @@ -458,6 +456,7 @@ 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 @@ -474,7 +473,7 @@ public boolean onTouch(View v, MotionEvent e) {
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] + frm.getHeight() - primaryLayer.getHeight()));
primaryLayer.setY(MathUtils.clamp(offset[1] - startOffset[1] + e.getY(), offset2[1], offset2[1] + lorieView.getHeight() - primaryLayer.getHeight()));
break;
}
case MotionEvent.ACTION_UP: {
Expand Down Expand Up @@ -869,6 +868,8 @@ void clientConnectedStateChanged() {
tryConnect();
else
getLorieView().setPointerIcon(PointerIcon.getSystemIcon(this, PointerIcon.TYPE_NULL));

onWindowFocusChanged(hasWindowFocus());
});
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="80dp"
android:layout_marginBottom="120dp"
android:orientation="vertical"
android:alpha="0.7"
android:translationX="0px"
Expand Down

0 comments on commit 1d7cbd8

Please sign in to comment.