From ff0440d7d239a306d0f3f4a7383795d8b6228d1f Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Tue, 16 Mar 2021 07:04:55 +0500 Subject: [PATCH] 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 --- .../java/com/termux/app/TermuxActivity.java | 25 +++++++++-- .../preferences/TermuxSharedPreferences.java | 10 ++--- .../properties/TermuxPropertyConstants.java | 15 ++++++- .../properties/TermuxSharedProperties.java | 45 ++++++++++++++++++- .../com/termux/app/utils/TextDataUtils.java | 41 +++++++++++++++++ 5 files changed, 122 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index f2fc16882d..fe2b2bc667 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -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; @@ -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) @@ -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) { @@ -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 diff --git a/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java b/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java index dc54d69671..c0e9ab0a51 100644 --- a/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java +++ b/app/src/main/java/com/termux/app/settings/preferences/TermuxSharedPreferences.java @@ -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; @@ -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; } @@ -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); - } + } diff --git a/app/src/main/java/com/termux/app/settings/properties/TermuxPropertyConstants.java b/app/src/main/java/com/termux/app/settings/properties/TermuxPropertyConstants.java index 6e3dd673d2..50ae0982cd 100644 --- a/app/src/main/java/com/termux/app/settings/properties/TermuxPropertyConstants.java +++ b/app/src/main/java/com/termux/app/settings/properties/TermuxPropertyConstants.java @@ -10,7 +10,7 @@ import java.util.Set; /* - * Version: v0.2.0 + * Version: v0.3.0 * * Changelog * @@ -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*` * */ @@ -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 */ @@ -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, diff --git a/app/src/main/java/com/termux/app/settings/properties/TermuxSharedProperties.java b/app/src/main/java/com/termux/app/settings/properties/TermuxSharedProperties.java index 623ab13dd2..e8acf5fd31 100644 --- a/app/src/main/java/com/termux/app/settings/properties/TermuxSharedProperties.java +++ b/app/src/main/java/com/termux/app/settings/properties/TermuxSharedProperties.java @@ -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; @@ -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: @@ -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. @@ -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. @@ -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}. @@ -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 getSessionShortcuts() { return mSessionShortcuts; } diff --git a/app/src/main/java/com/termux/app/utils/TextDataUtils.java b/app/src/main/java/com/termux/app/utils/TextDataUtils.java index e61e373cdf..cbf8c3fb0b 100644 --- a/app/src/main/java/com/termux/app/utils/TextDataUtils.java +++ b/app/src/main/java/com/termux/app/utils/TextDataUtils.java @@ -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 extractUrls(String text) { StringBuilder regex_sb = new StringBuilder();