Skip to content

Commit

Permalink
Added: Allow users to adjust terminal horizontal and vertical margin
Browse files Browse the repository at this point in the history
The `terminal-margin-horizontal` key can be used to adjust the terminal left/right margin and the `terminal-margin-vertical` can be used to adjust the terminal top/bottom margin. This will also affect drawer. The user can set an integer value between `0` and `100` as `dp` units. The default value is still `3` for horizontal and `0` for vertical margin. So adding an entry like `terminal-margin-horizontal=10` to `termux.properties` file will allow users to set a horizontal margin of `10dp`. After updating the value, either restart termux or run `termux-reload-settings` for changes to take effect.

This was added since for some users text on edges would not be shown on the screen or they had screen protectors/cases that covered screen edges (Of course, that would require fixing every single app and android system UI itself, so kinda stupid to use). Moreover, horizontal margin of like `10dp` may be helpful with peek-and-slide for people having gesture navigation enabled on android `10+` since they won't be to touch at exactly the edge of the screen to trigger peek (#1325).

Closes #2210
  • Loading branch information
agnostic-apollo committed Aug 25, 2021
1 parent 829cc39 commit 63504f0
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.termux.R;
Expand All @@ -49,6 +50,7 @@
import com.termux.shared.interact.TextInputDialogUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxUtils;
import com.termux.shared.view.ViewUtils;
import com.termux.terminal.TerminalSession;
import com.termux.terminal.TerminalSessionClient;
import com.termux.app.utils.CrashUtils;
Expand Down Expand Up @@ -204,6 +206,8 @@ public void onCreate(Bundle savedInstanceState) {
return;
}

setMargins();

mTermuxActivityRootView = findViewById(R.id.activity_termux_root_view);
mTermuxActivityRootView.setActivity(this);
mTermuxActivityBottomSpaceView = findViewById(R.id.activity_termux_bottom_space_view);
Expand Down Expand Up @@ -416,6 +420,13 @@ private void setDrawerTheme() {
}
}

private void setMargins() {
RelativeLayout relativeLayout = findViewById(R.id.activity_termux_root_relative_layout);
int marginHorizontal = mProperties.getTerminalMarginHorizontal();
int marginVertical = mProperties.getTerminalMarginVertical();
ViewUtils.setLayoutMarginsInDp(relativeLayout, marginHorizontal, marginVertical, marginHorizontal, marginVertical);
}



public void addTermuxActivityRootViewGlobalLayoutListener() {
Expand Down Expand Up @@ -873,6 +884,7 @@ private void reloadActivityStyling() {
}
}

setMargins();
setTerminalToolbarHeight();

if (mTermuxTerminalSessionClient != null)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_termux.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
android:fitsSystemWindows="true">

<RelativeLayout
android:id="@+id/activity_termux_root_relative_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginHorizontal="3dp"
android:layout_marginVertical="0dp"
android:orientation="vertical">

<androidx.drawerlayout.widget.DrawerLayout
Expand All @@ -22,8 +25,6 @@
android:id="@+id/terminal_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="3dp"
android:layout_marginLeft="3dp"
android:focusableInTouchMode="true"
android:scrollbarThumbVertical="@drawable/terminal_scroll_shape"
android:scrollbars="vertical"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Set;

/*
* Version: v0.12.0
* Version: v0.13.0
*
* Changelog
*
Expand Down Expand Up @@ -55,6 +55,9 @@
*
* - 0.12.0 (2021-06-10)
* - Add `*KEY_TERMINAL_CURSOR_STYLE*`.
*
* - 0.13.0 (2021-08-25)
* - Add `*KEY_TERMINAL_MARGIN_HORIZONTAL*` and `*KEY_TERMINAL_MARGIN_VERTICAL*`.
*/

/**
Expand Down Expand Up @@ -173,6 +176,20 @@ public final class TermuxPropertyConstants {



/** Defines the key for the terminal margin on left and right in dp units */
public static final String KEY_TERMINAL_MARGIN_HORIZONTAL = "terminal-margin-horizontal"; // Default: "terminal-margin-horizontal"
public static final int IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN = 0;
public static final int IVALUE_TERMINAL_MARGIN_HORIZONTAL_MAX = 100;
public static final int DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN = 3;

/** Defines the key for the terminal margin on top and bottom in dp units */
public static final String KEY_TERMINAL_MARGIN_VERTICAL = "terminal-margin-vertical"; // Default: "terminal-margin-vertical"
public static final int IVALUE_TERMINAL_MARGIN_VERTICAL_MIN = 0;
public static final int IVALUE_TERMINAL_MARGIN_VERTICAL_MAX = 100;
public static final int DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN = 0;



/** Defines the key for the terminal transcript rows */
public static final String KEY_TERMINAL_TRANSCRIPT_ROWS = "terminal-transcript-rows"; // Default: "terminal-transcript-rows"
public static final int IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN = TerminalEmulator.TERMINAL_TRANSCRIPT_ROWS_MIN;
Expand Down Expand Up @@ -314,6 +331,8 @@ public final class TermuxPropertyConstants {
KEY_BELL_BEHAVIOUR,
KEY_TERMINAL_CURSOR_BLINK_RATE,
KEY_TERMINAL_CURSOR_STYLE,
KEY_TERMINAL_MARGIN_HORIZONTAL,
KEY_TERMINAL_MARGIN_VERTICAL,
KEY_TERMINAL_TRANSCRIPT_ROWS,

/* float */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public static Object getInternalTermuxPropertyValueFromValue(Context context, St
return (int) getTerminalCursorBlinkRateInternalPropertyValueFromValue(value);
case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE:
return (int) getTerminalCursorStyleInternalPropertyValueFromValue(value);
case TermuxPropertyConstants.KEY_TERMINAL_MARGIN_HORIZONTAL:
return (int) getTerminalMarginHorizontalInternalPropertyValueFromValue(value);
case TermuxPropertyConstants.KEY_TERMINAL_MARGIN_VERTICAL:
return (int) getTerminalMarginVerticalInternalPropertyValueFromValue(value);
case TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS:
return (int) getTerminalTranscriptRowsInternalPropertyValueFromValue(value);

Expand Down Expand Up @@ -318,6 +322,42 @@ public static int getTerminalCursorStyleInternalPropertyValueFromValue(String va
return (int) SharedProperties.getDefaultIfNotInMap(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE, TermuxPropertyConstants.MAP_TERMINAL_CURSOR_STYLE, SharedProperties.toLowerCase(value), TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_CURSOR_STYLE, true, LOG_TAG);
}

/**
* Returns the int for the value if its not null and is between
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN} and
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_HORIZONTAL_MAX},
* otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN}.
*
* @param value The {@link String} value to convert.
* @return Returns the internal value for value.
*/
public static int getTerminalMarginHorizontalInternalPropertyValueFromValue(String value) {
return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_HORIZONTAL,
DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN),
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN,
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN,
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_HORIZONTAL_MAX,
true, true, LOG_TAG);
}

/**
* Returns the int for the value if its not null and is between
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_VERTICAL_MIN} and
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_VERTICAL_MAX},
* otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN}.
*
* @param value The {@link String} value to convert.
* @return Returns the internal value for value.
*/
public static int getTerminalMarginVerticalInternalPropertyValueFromValue(String value) {
return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_VERTICAL,
DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN),
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN,
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_VERTICAL_MIN,
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_VERTICAL_MAX,
true, true, LOG_TAG);
}

/**
* Returns the int for the value if its not null and is between
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN} and
Expand Down Expand Up @@ -508,6 +548,14 @@ public int getTerminalCursorStyle() {
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE, true);
}

public int getTerminalMarginHorizontal() {
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_HORIZONTAL, true);
}

public int getTerminalMarginVertical() {
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_VERTICAL, true);
}

public int getTerminalTranscriptRows() {
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS, true);
}
Expand Down
15 changes: 15 additions & 0 deletions termux-shared/src/main/java/com/termux/shared/view/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -218,4 +219,18 @@ public static int dpToPx(Context context, int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}


public static void setLayoutMarginsInDp(@NonNull View view, int left, int top, int right, int bottom) {
Context context = view.getContext();
setLayoutMarginsInPixels(view, dpToPx(context, left), dpToPx(context, top), dpToPx(context, right), dpToPx(context, bottom));
}

public static void setLayoutMarginsInPixels(@NonNull View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
params.setMargins(left, top, right, bottom);
view.setLayoutParams(params);
}
}

}

2 comments on commit 63504f0

@ChiefMikeK
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm expecting some future [Feature request] asking for left-right/top-bottom soon ಠ_ಠ

:neckbeard:

@agnostic-apollo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.