Skip to content

Commit

Permalink
Allow users to adjust termux toolbar height with termux.properties
Browse files Browse the repository at this point in the history
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 termux#1857
  • Loading branch information
agnostic-apollo committed Mar 16, 2021
1 parent 3ab5904 commit b1db7e1
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 14 deletions.
25 changes: 21 additions & 4 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ public final class TermuxActivity extends Activity implements ServiceConnection
* If between onResume() and onStop(). Note that only one session is in the foreground of the terminal view at the
* time, so if the session causing a change is not in the foreground it should probably be treated as background.
*/
boolean mIsVisible;
private boolean mIsVisible;

int mNavBarHeight;
private int mNavBarHeight;

private int mTerminalToolbarDefaultHeight;

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

ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams();
layoutParams.height = layoutParams.height * (mProperties.getExtraKeysInfo() == null ? 0 : mProperties.getExtraKeysInfo().getMatrix().length);
terminalToolbarViewPager.setLayoutParams(layoutParams);
mTerminalToolbarDefaultHeight = layoutParams.height;

setTerminalToolbarHeight();

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

private void setTerminalToolbarHeight() {
final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager);
if(terminalToolbarViewPager == null) return;
ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams();
layoutParams.height = (int) Math.round(mTerminalToolbarDefaultHeight *
(mProperties.getExtraKeysInfo() == null ? 0 : mProperties.getExtraKeysInfo().getMatrix().length) *
mProperties.getTerminalToolbarHeightScaleFactor());
terminalToolbarViewPager.setLayoutParams(layoutParams);
}

public void toggleTerminalToolbar() {
final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager);
if(terminalToolbarViewPager == null) return;

final boolean showNow = mPreferences.toogleShowTerminalToolbar();
terminalToolbarViewPager.setVisibility(showNow ? View.VISIBLE : View.GONE);
if (showNow && terminalToolbarViewPager.getCurrentItem() == 1) {
Expand Down Expand Up @@ -690,6 +705,8 @@ public void onReceive(Context context, Intent intent) {
}
}

setTerminalToolbarHeight();

// To change the activity and drawer theme, activity needs to be recreated.
// But this will destroy the activity, and will call the onCreate() again.
// We need to investigate if enabling this is wise, since all stored variables and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.termux.app.TermuxConstants;
import com.termux.app.utils.Logger;
import com.termux.app.utils.TermuxUtils;
import com.termux.app.utils.TextDataUtils;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -89,7 +90,7 @@ public int getFontSize() {
} catch (NumberFormatException | ClassCastException e) {
fontSize = DEFAULT_FONTSIZE;
}
fontSize = clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);
fontSize = TextDataUtils.clamp(fontSize, MIN_FONTSIZE, MAX_FONTSIZE);

return fontSize;
}
Expand Down Expand Up @@ -147,11 +148,6 @@ public void setTerminalViewKeyLoggingEnabled(boolean value) {



/**
* If value is not in the range [min, max], set it to either min or max.
*/
static int clamp(int value, int min, int max) {
return Math.min(Math.max(value, min), max);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Set;

/*
* Version: v0.2.0
* Version: v0.3.0
*
* Changelog
*
Expand All @@ -20,6 +20,8 @@
* - Renamed `HOME_PATH` to `TERMUX_HOME_DIR_PATH`
* - Renamed `TERMUX_PROPERTIES_PRIMARY_PATH` to `TERMUX_PROPERTIES_PRIMARY_FILE_PATH`
* - Renamed `TERMUX_PROPERTIES_SECONDARY_FILE_PATH` to `TERMUX_PROPERTIES_SECONDARY_FILE_PATH`
* - 0.3.0 (2021-03-16)
* - Add `*TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR*`
*
*/

Expand Down Expand Up @@ -116,6 +118,14 @@ public final class TermuxPropertyConstants {



/** Defines the key for the bell behaviour */
public static final String KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR = "terminal-toolbar-height"; // Default: "terminal-toolbar-height"
public static final float IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN = 0.4f;
public static final float IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX = 3;
public static final float DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR = 1;



/** Defines the key for create session shortcut */
public static final String KEY_SHORTCUT_CREATE_SESSION = "shortcut.create-session"; // Default: "shortcut.create-session"
/** Defines the key for next session shortcut */
Expand Down Expand Up @@ -176,6 +186,9 @@ public final class TermuxPropertyConstants {
// int
KEY_BELL_BEHAVIOUR,

// float
KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR,

// Integer
KEY_SHORTCUT_CREATE_SESSION,
KEY_SHORTCUT_NEXT_SESSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.termux.app.terminal.io.extrakeys.ExtraKeysInfo;
import com.termux.app.terminal.io.KeyboardShortcut;
import com.termux.app.utils.Logger;
import com.termux.app.utils.TextDataUtils;

import org.json.JSONException;

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

// float
case TermuxPropertyConstants.KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR:
return (float) getTerminalToolbarHeightScaleFactorInternalPropertyValueFromValue(value);

// Integer (may be null)
case TermuxPropertyConstants.KEY_SHORTCUT_CREATE_SESSION:
case TermuxPropertyConstants.KEY_SHORTCUT_NEXT_SESSION:
Expand Down Expand Up @@ -391,7 +396,8 @@ public static boolean getUseBlackUIInternalPropertyValueFromValue(Context contex
}

/**
* Returns {@code true} if value is not {@code null} and equals {@link TermuxPropertyConstants#VALUE_VOLUME_KEY_BEHAVIOUR_VOLUME}, otherwise {@code false}.
* Returns {@code true} if value is not {@code null} and equals
* {@link TermuxPropertyConstants#VALUE_VOLUME_KEY_BEHAVIOUR_VOLUME}, otherwise {@code false}.
*
* @param value The {@link String} value to convert.
* @return Returns the internal value for value.
Expand All @@ -401,7 +407,9 @@ public static boolean getVolumeKeysDisabledInternalPropertyValueFromValue(String
}

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

/**
* Returns the int for the value if its not null and is between
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX},
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}.
*
* @param value The {@link String} value to convert.
* @return Returns the internal value for value.
*/
public static float getTerminalToolbarHeightScaleFactorInternalPropertyValueFromValue(String value) {
return rangeTerminalToolbarHeightScaleFactorValue(TextDataUtils.getFloatFromString(value, TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR));
}

/**
* Returns the value itself if it is between
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN} and
* {@code TermuxPropertyConstants#IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX},
* otherwise returns {@code TermuxPropertyConstants#DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR}.
*
* @param value The value to clamp.
* @return Returns the clamped value.
*/
public static float rangeTerminalToolbarHeightScaleFactorValue(float value) {
return TextDataUtils.rangedOrDefault(value,
TermuxPropertyConstants.DEFAULT_IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR,
TermuxPropertyConstants.IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MIN,
TermuxPropertyConstants.IVALUE_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR_MAX);
}

/**
* Returns the code point for the value if key is not {@code null} and value is not {@code null} and is valid,
* otherwise returns {@code null}.
Expand Down Expand Up @@ -517,6 +554,10 @@ public int getBellBehaviour() {
return (int) getInternalPropertyValue(TermuxPropertyConstants.KEY_BELL_BEHAVIOUR, true);
}

public float getTerminalToolbarHeightScaleFactor() {
return rangeTerminalToolbarHeightScaleFactorValue((float) getInternalPropertyValue(TermuxPropertyConstants.KEY_TERMINAL_TOOLBAR_HEIGHT_SCALE_FACTOR, true));
}

public List<KeyboardShortcut> getSessionShortcuts() {
return mSessionShortcuts;
}
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/com/termux/app/utils/TextDataUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,47 @@ public static String getTruncatedCommandOutput(String text, int maxLength) {
return text;
}

public static float getFloatFromString(String value, float def) {
if(value == null) return def;

try {
return Float.parseFloat(value);
}
catch (Exception e) {
return def;
}
}

public static int getIntFromString(String value, int def) {
if(value == null) return def;

try {
return Integer.parseInt(value);
}
catch (Exception e) {
return def;
}
}

/**
* If value is not in the range [min, max], set it to either min or max.
*/
public static int clamp(int value, int min, int max) {
return Math.min(Math.max(value, min), max);
}

/**
* If value is not in the range [min, max], set it to default.
*/
public static float rangedOrDefault(float value, float def, float min, float max) {
if (value < min || value > max)
return def;
else
return value;
}



public static LinkedHashSet<CharSequence> extractUrls(String text) {

StringBuilder regex_sb = new StringBuilder();
Expand Down

0 comments on commit b1db7e1

Please sign in to comment.