Skip to content

Commit ff0440d

Browse files
Allow users to adjust termux toolbar height with termux.properties
This `terminal-toolbar-height` key can be used to adjust the toolbar height. The user can set a float value between `0.4` and `3.0` which will be used as the scaling factor for the default height. The default scaling factor is `1`. So adding an entry like `terminal-toolbar-height=2.0` to `termux.properties` file will make the toolbar height twice its original height. Running `termux-reload-settings` command will also update the height instantaneously if changed. Fixes #1857
1 parent c9e18e5 commit ff0440d

File tree

5 files changed

+122
-14
lines changed

5 files changed

+122
-14
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ public final class TermuxActivity extends Activity implements ServiceConnection
124124
* If between onResume() and onStop(). Note that only one session is in the foreground of the terminal view at the
125125
* time, so if the session causing a change is not in the foreground it should probably be treated as background.
126126
*/
127-
boolean mIsVisible;
127+
private boolean mIsVisible;
128128

129-
int mNavBarHeight;
129+
private int mNavBarHeight;
130+
131+
private int mTerminalToolbarDefaultHeight;
130132

131133
private static final int CONTEXT_MENU_SELECT_URL_ID = 0;
132134
private static final int CONTEXT_MENU_SHARE_TRANSCRIPT_ID = 1;
@@ -341,8 +343,9 @@ private void setTerminalToolbarView(Bundle savedInstanceState) {
341343
if (mPreferences.getShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE);
342344

343345
ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams();
344-
layoutParams.height = layoutParams.height * (mProperties.getExtraKeysInfo() == null ? 0 : mProperties.getExtraKeysInfo().getMatrix().length);
345-
terminalToolbarViewPager.setLayoutParams(layoutParams);
346+
mTerminalToolbarDefaultHeight = layoutParams.height;
347+
348+
setTerminalToolbarHeight();
346349

347350
String savedTextInput = null;
348351
if(savedInstanceState != null)
@@ -352,8 +355,20 @@ private void setTerminalToolbarView(Bundle savedInstanceState) {
352355
terminalToolbarViewPager.addOnPageChangeListener(new TerminalToolbarViewPager.OnPageChangeListener(this, terminalToolbarViewPager));
353356
}
354357

358+
private void setTerminalToolbarHeight() {
359+
final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager);
360+
if(terminalToolbarViewPager == null) return;
361+
ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams();
362+
layoutParams.height = (int) Math.round(mTerminalToolbarDefaultHeight *
363+
(mProperties.getExtraKeysInfo() == null ? 0 : mProperties.getExtraKeysInfo().getMatrix().length) *
364+
mProperties.getTerminalToolbarHeightScaleFactor());
365+
terminalToolbarViewPager.setLayoutParams(layoutParams);
366+
}
367+
355368
public void toggleTerminalToolbar() {
356369
final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager);
370+
if(terminalToolbarViewPager == null) return;
371+
357372
final boolean showNow = mPreferences.toogleShowTerminalToolbar();
358373
terminalToolbarViewPager.setVisibility(showNow ? View.VISIBLE : View.GONE);
359374
if (showNow && terminalToolbarViewPager.getCurrentItem() == 1) {
@@ -690,6 +705,8 @@ public void onReceive(Context context, Intent intent) {
690705
}
691706
}
692707

708+
setTerminalToolbarHeight();
709+
693710
// To change the activity and drawer theme, activity needs to be recreated.
694711
// But this will destroy the activity, and will call the onCreate() again.
695712
// We need to investigate if enabling this is wise, since all stored variables and

app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.termux.app.TermuxConstants;
88
import com.termux.app.utils.Logger;
99
import com.termux.app.utils.TermuxUtils;
10+
import com.termux.app.utils.TextDataUtils;
1011

1112
import javax.annotation.Nonnull;
1213

@@ -89,7 +90,7 @@ public int getFontSize() {
8990
} catch (NumberFormatException | ClassCastException e) {
9091
fontSize = DEFAULT_FONTSIZE;
9192
}
92-
fontSize = clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
93+
fontSize = TextDataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
9394

9495
return fontSize;
9596
}
@@ -147,11 +148,6 @@ public void setTerminalViewKeyLoggingEnabled(boolean value) {
147148

148149

149150

150-
/**
151-
* If value is not in the range [min, max], set it to either min or max.
152-
*/
153-
static int clamp(int value, int min, int max) {
154-
return Math.min(Math.max(value, min), max);
155-
}
151+
156152

157153
}

app/src/main/java/com/termux/app/settings/properties/TermuxPropertyConstants.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Set;
1111

1212
/*
13-
* Version: v0.2.0
13+
* Version: v0.3.0
1414
*
1515
* Changelog
1616
*
@@ -20,6 +20,8 @@
2020
* - Renamed `HOME_PATH` to `TERMUX_HOME_DIR_PATH`
2121
* - Renamed `TERMUX_PROPERTIES_PRIMARY_PATH` to `TERMUX_PROPERTIES_PRIMARY_FILE_PATH`
2222
* - Renamed `TERMUX_PROPERTIES_SECONDARY_FILE_PATH` to `TERMUX_PROPERTIES_SECONDARY_FILE_PATH`
23+
* - 0.3.0 (2021-03-16)
24+
* - Add `*TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR*`
2325
*
2426
*/
2527

@@ -116,6 +118,14 @@ public final class TermuxPropertyConstants {
116118

117119

118120

121+
/** Defines the key for the bell behaviour */
122+
public static final String KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR = "terminal-toolbar-height"; // Default: "terminal-toolbar-height"
123+
public static final float IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN = 0.4f;
124+
public static final float IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX = 3;
125+
public static final float DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR = 1;
126+
127+
128+
119129
/** Defines the key for create session shortcut */
120130
public static final String KEY_SHORTCUT_CREATE_SESSION = "shortcut.create-session"; // Default: "shortcut.create-session"
121131
/** Defines the key for next session shortcut */
@@ -176,6 +186,9 @@ public final class TermuxPropertyConstants {
176186
// int
177187
KEY_BELL_BEHAVIOUR,
178188

189+
// float
190+
KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR,
191+
179192
// Integer
180193
KEY_SHORTCUT_CREATE_SESSION,
181194
KEY_SHORTCUT_NEXT_SESSION,

app/src/main/java/com/termux/app/settings/properties/TermuxSharedProperties.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.termux.app.terminal.io.extrakeys.ExtraKeysInfo;
99
import com.termux.app.terminal.io.KeyboardShortcut;
1010
import com.termux.app.utils.Logger;
11+
import com.termux.app.utils.TextDataUtils;
1112

1213
import org.json.JSONException;
1314

@@ -313,6 +314,10 @@ public static Object getInternalTermuxPropertyValueFromValue(Context context, St
313314
case TermuxPropertyConstants.KEY_BELL_BEHAVIOUR:
314315
return (int) getBellBehaviourInternalPropertyValueFromValue(value);
315316

317+
// float
318+
case TermuxPropertyConstants.KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR:
319+
return (float) getTerminalToolbarHeightScaleFactorInternalPropertyValueFromValue(value);
320+
316321
// Integer (may be null)
317322
case TermuxPropertyConstants.KEY_SHORTCUT_CREATE_SESSION:
318323
case TermuxPropertyConstants.KEY_SHORTCUT_NEXT_SESSION:
@@ -391,7 +396,8 @@ public static boolean getUseBlackUIInternalPropertyValueFromValue(Context contex
391396
}
392397

393398
/**
394-
* Returns {@code true} if value is not {@code null} and equals {@link TermuxPropertyConstants#VALUE_VOLUME_KEY_BEHAVIOUR_VOLUME}, otherwise {@code false}.
399+
* Returns {@code true} if value is not {@code null} and equals
400+
* {@link TermuxPropertyConstants#VALUE_VOLUME_KEY_BEHAVIOUR_VOLUME}, otherwise {@code false}.
395401
*
396402
* @param value The {@link String} value to convert.
397403
* @return Returns the internal value for value.
@@ -401,7 +407,9 @@ public static boolean getVolumeKeysDisabledInternalPropertyValueFromValue(String
401407
}
402408

403409
/**
404-
* Returns {@code true} if value is not {@code null} and equals {@link TermuxPropertyConstants#VALUE_BACK_KEY_BEHAVIOUR_ESCAPE}, otherwise {@code false}.
410+
* Returns the internal value after mapping it based on
411+
* {@code TermuxPropertyConstants#MAP_BELL_BEHAVIOUR} if the value is not {@code null}
412+
* and is valid, otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_BELL_BEHAVIOUR}.
405413
*
406414
* @param value The {@link String} value to convert.
407415
* @return Returns the internal value for value.
@@ -410,6 +418,35 @@ public static int getBellBehaviourInternalPropertyValueFromValue(String value) {
410418
return getDefaultIfNull(TermuxPropertyConstants.MAP_BELL_BEHAVIOUR.get(toLowerCase(value)), TermuxPropertyConstants.DEFAULT_IVALUE_BELL_BEHAVIOUR);
411419
}
412420

421+
/**
422+
* Returns the int for the value if its not null and is between
423+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and
424+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX},
425+
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}.
426+
*
427+
* @param value The {@link String} value to convert.
428+
* @return Returns the internal value for value.
429+
*/
430+
public static float getTerminalToolbarHeightScaleFactorInternalPropertyValueFromValue(String value) {
431+
return rangeTerminalToolbarHeightScaleFactorValue(TextDataUtils.getFloatFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR));
432+
}
433+
434+
/**
435+
* Returns the value itself if it is between
436+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and
437+
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX},
438+
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}.
439+
*
440+
* @param value The value to clamp.
441+
* @return Returns the clamped value.
442+
*/
443+
public static float rangeTerminalToolbarHeightScaleFactorValue(float value) {
444+
return TextDataUtils.rangedOrDefault(value,
445+
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR,
446+
TermuxPropertyConstants.IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN,
447+
TermuxPropertyConstants.IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX);
448+
}
449+
413450
/**
414451
* Returns the code point for the value if key is not {@code null} and value is not {@code null} and is valid,
415452
* otherwise returns {@code null}.
@@ -517,6 +554,10 @@ public int getBellBehaviour() {
517554
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_BELL_BEHAVIOUR, true);
518555
}
519556

557+
public float getTerminalToolbarHeightScaleFactor() {
558+
return rangeTerminalToolbarHeightScaleFactorValue((float) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR, true));
559+
}
560+
520561
public List<KeyboardShortcut> getSessionShortcuts() {
521562
return mSessionShortcuts;
522563
}

app/src/main/java/com/termux/app/utils/TextDataUtils.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@ public static String getTruncatedCommandOutput(String text, int maxLength) {
2121
return text;
2222
}
2323

24+
public static float getFloatFromString(String value, float def) {
25+
if(value == null) return def;
26+
27+
try {
28+
return Float.parseFloat(value);
29+
}
30+
catch (Exception e) {
31+
return def;
32+
}
33+
}
34+
35+
public static int getIntFromString(String value, int def) {
36+
if(value == null) return def;
37+
38+
try {
39+
return Integer.parseInt(value);
40+
}
41+
catch (Exception e) {
42+
return def;
43+
}
44+
}
45+
46+
/**
47+
* If value is not in the range [min, max], set it to either min or max.
48+
*/
49+
public static int clamp(int value, int min, int max) {
50+
return Math.min(Math.max(value, min), max);
51+
}
52+
53+
/**
54+
* If value is not in the range [min, max], set it to default.
55+
*/
56+
public static float rangedOrDefault(float value, float def, float min, float max) {
57+
if (value < min || value > max)
58+
return def;
59+
else
60+
return value;
61+
}
62+
63+
64+
2465
public static LinkedHashSet<CharSequence> extractUrls(String text) {
2566

2667
StringBuilder regex_sb = new StringBuilder();

0 commit comments

Comments
 (0)