Skip to content

Commit 63504f0

Browse files
Added: Allow users to adjust terminal horizontal and vertical margin
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
1 parent 829cc39 commit 63504f0

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.widget.EditText;
3030
import android.widget.ImageButton;
3131
import android.widget.ListView;
32+
import android.widget.RelativeLayout;
3233
import android.widget.Toast;
3334

3435
import com.termux.R;
@@ -49,6 +50,7 @@
4950
import com.termux.shared.interact.TextInputDialogUtils;
5051
import com.termux.shared.logger.Logger;
5152
import com.termux.shared.termux.TermuxUtils;
53+
import com.termux.shared.view.ViewUtils;
5254
import com.termux.terminal.TerminalSession;
5355
import com.termux.terminal.TerminalSessionClient;
5456
import com.termux.app.utils.CrashUtils;
@@ -204,6 +206,8 @@ public void onCreate(Bundle savedInstanceState) {
204206
return;
205207
}
206208

209+
setMargins();
210+
207211
mTermuxActivityRootView = findViewById(R.id.activity_termux_root_view);
208212
mTermuxActivityRootView.setActivity(this);
209213
mTermuxActivityBottomSpaceView = findViewById(R.id.activity_termux_bottom_space_view);
@@ -416,6 +420,13 @@ private void setDrawerTheme() {
416420
}
417421
}
418422

423+
private void setMargins() {
424+
RelativeLayout relativeLayout = findViewById(R.id.activity_termux_root_relative_layout);
425+
int marginHorizontal = mProperties.getTerminalMarginHorizontal();
426+
int marginVertical = mProperties.getTerminalMarginVertical();
427+
ViewUtils.setLayoutMarginsInDp(relativeLayout, marginHorizontal, marginVertical, marginHorizontal, marginVertical);
428+
}
429+
419430

420431

421432
public void addTermuxActivityRootViewGlobalLayoutListener() {
@@ -873,6 +884,7 @@ private void reloadActivityStyling() {
873884
}
874885
}
875886

887+
setMargins();
876888
setTerminalToolbarHeight();
877889

878890
if (mTermuxTerminalSessionClient != null)

app/src/main/res/layout/activity_termux.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
android:fitsSystemWindows="true">
77

88
<RelativeLayout
9+
android:id="@+id/activity_termux_root_relative_layout"
910
android:layout_width="match_parent"
1011
android:layout_height="0dp"
1112
android:layout_weight="1"
13+
android:layout_marginHorizontal="3dp"
14+
android:layout_marginVertical="0dp"
1215
android:orientation="vertical">
1316

1417
<androidx.drawerlayout.widget.DrawerLayout
@@ -22,8 +25,6 @@
2225
android:id="@+id/terminal_view"
2326
android:layout_width="match_parent"
2427
android:layout_height="match_parent"
25-
android:layout_marginRight="3dp"
26-
android:layout_marginLeft="3dp"
2728
android:focusableInTouchMode="true"
2829
android:scrollbarThumbVertical="@drawable/terminal_scroll_shape"
2930
android:scrollbars="vertical"

termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.Set;
1313

1414
/*
15-
* Version: v0.12.0
15+
* Version: v0.13.0
1616
*
1717
* Changelog
1818
*
@@ -55,6 +55,9 @@
5555
*
5656
* - 0.12.0 (2021-06-10)
5757
* - Add `*KEY_TERMINAL_CURSOR_STYLE*`.
58+
*
59+
* - 0.13.0 (2021-08-25)
60+
* - Add `*KEY_TERMINAL_MARGIN_HORIZONTAL*` and `*KEY_TERMINAL_MARGIN_VERTICAL*`.
5861
*/
5962

6063
/**
@@ -173,6 +176,20 @@ public final class TermuxPropertyConstants {
173176

174177

175178

179+
/** Defines the key for the terminal margin on left and right in dp units */
180+
public static final String KEY_TERMINAL_MARGIN_HORIZONTAL = "terminal-margin-horizontal"; // Default: "terminal-margin-horizontal"
181+
public static final int IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN = 0;
182+
public static final int IVALUE_TERMINAL_MARGIN_HORIZONTAL_MAX = 100;
183+
public static final int DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN = 3;
184+
185+
/** Defines the key for the terminal margin on top and bottom in dp units */
186+
public static final String KEY_TERMINAL_MARGIN_VERTICAL = "terminal-margin-vertical"; // Default: "terminal-margin-vertical"
187+
public static final int IVALUE_TERMINAL_MARGIN_VERTICAL_MIN = 0;
188+
public static final int IVALUE_TERMINAL_MARGIN_VERTICAL_MAX = 100;
189+
public static final int DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN = 0;
190+
191+
192+
176193
/** Defines the key for the terminal transcript rows */
177194
public static final String KEY_TERMINAL_TRANSCRIPT_ROWS = "terminal-transcript-rows"; // Default: "terminal-transcript-rows"
178195
public static final int IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN = TerminalEmulator.TERMINAL_TRANSCRIPT_ROWS_MIN;
@@ -314,6 +331,8 @@ public final class TermuxPropertyConstants {
314331
KEY_BELL_BEHAVIOUR,
315332
KEY_TERMINAL_CURSOR_BLINK_RATE,
316333
KEY_TERMINAL_CURSOR_STYLE,
334+
KEY_TERMINAL_MARGIN_HORIZONTAL,
335+
KEY_TERMINAL_MARGIN_VERTICAL,
317336
KEY_TERMINAL_TRANSCRIPT_ROWS,
318337

319338
/* float */

termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java

+48
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ public static Object getInternalTermuxPropertyValueFromValue(Context context, St
219219
return (int) getTerminalCursorBlinkRateInternalPropertyValueFromValue(value);
220220
case TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE:
221221
return (int) getTerminalCursorStyleInternalPropertyValueFromValue(value);
222+
case TermuxPropertyConstants.KEY_TERMINAL_MARGIN_HORIZONTAL:
223+
return (int) getTerminalMarginHorizontalInternalPropertyValueFromValue(value);
224+
case TermuxPropertyConstants.KEY_TERMINAL_MARGIN_VERTICAL:
225+
return (int) getTerminalMarginVerticalInternalPropertyValueFromValue(value);
222226
case TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS:
223227
return (int) getTerminalTranscriptRowsInternalPropertyValueFromValue(value);
224228

@@ -318,6 +322,42 @@ public static int getTerminalCursorStyleInternalPropertyValueFromValue(String va
318322
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);
319323
}
320324

325+
/**
326+
* Returns the int for the value if its not null and is between
327+
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN} and
328+
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_HORIZONTAL_MAX},
329+
* otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN}.
330+
*
331+
* @param value The {@link String} value to convert.
332+
* @return Returns the internal value for value.
333+
*/
334+
public static int getTerminalMarginHorizontalInternalPropertyValueFromValue(String value) {
335+
return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_HORIZONTAL,
336+
DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN),
337+
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_HORIZONTAL_MARGIN,
338+
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_HORIZONTAL_MIN,
339+
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_HORIZONTAL_MAX,
340+
true, true, LOG_TAG);
341+
}
342+
343+
/**
344+
* Returns the int for the value if its not null and is between
345+
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_VERTICAL_MIN} and
346+
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_MARGIN_VERTICAL_MAX},
347+
* otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN}.
348+
*
349+
* @param value The {@link String} value to convert.
350+
* @return Returns the internal value for value.
351+
*/
352+
public static int getTerminalMarginVerticalInternalPropertyValueFromValue(String value) {
353+
return SharedProperties.getDefaultIfNotInRange(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_VERTICAL,
354+
DataUtils.getIntFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN),
355+
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_VERTICAL_MARGIN,
356+
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_VERTICAL_MIN,
357+
TermuxPropertyConstants.IVALUE_TERMINAL_MARGIN_VERTICAL_MAX,
358+
true, true, LOG_TAG);
359+
}
360+
321361
/**
322362
* Returns the int for the value if its not null and is between
323363
* {@link TermuxPropertyConstants#IVALUE_TERMINAL_TRANSCRIPT_ROWS_MIN} and
@@ -508,6 +548,14 @@ public int getTerminalCursorStyle() {
508548
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_CURSOR_STYLE, true);
509549
}
510550

551+
public int getTerminalMarginHorizontal() {
552+
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_HORIZONTAL, true);
553+
}
554+
555+
public int getTerminalMarginVertical() {
556+
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_MARGIN_VERTICAL, true);
557+
}
558+
511559
public int getTerminalTranscriptRows() {
512560
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TRANSCRIPT_ROWS, true);
513561
}

termux-shared/src/main/java/com/termux/shared/view/ViewUtils.java

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.graphics.Rect;
99
import android.util.TypedValue;
1010
import android.view.View;
11+
import android.view.ViewGroup;
1112

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

222+
223+
public static void setLayoutMarginsInDp(@NonNull View view, int left, int top, int right, int bottom) {
224+
Context context = view.getContext();
225+
setLayoutMarginsInPixels(view, dpToPx(context, left), dpToPx(context, top), dpToPx(context, right), dpToPx(context, bottom));
226+
}
227+
228+
public static void setLayoutMarginsInPixels(@NonNull View view, int left, int top, int right, int bottom) {
229+
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
230+
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
231+
params.setMargins(left, top, right, bottom);
232+
view.setLayoutParams(params);
233+
}
234+
}
235+
221236
}

0 commit comments

Comments
 (0)