add support of configurable extra keys

This commit is contained in:
neverwin
2018-04-06 18:11:57 +08:00
committed by Fredrik Fornwall
parent 57a3a9b111
commit f0eeb4781b
3 changed files with 28 additions and 11 deletions

View File

@@ -33,8 +33,6 @@ public final class ExtraKeysView extends GridLayout {
public ExtraKeysView(Context context, AttributeSet attrs) { public ExtraKeysView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
reload();
} }
static void sendKey(View view, String keyName) { static void sendKey(View view, String keyName) {
@@ -148,24 +146,24 @@ public final class ExtraKeysView extends GridLayout {
popupWindow.showAsDropDown(view, 0, -2 * height); popupWindow.showAsDropDown(view, 0, -2 * height);
} }
void reload() { void reload(final String[][] buttons) {
altButton = controlButton = null; altButton = controlButton = null;
removeAllViews(); removeAllViews();
String[][] buttons = { int mx = 0;
{"ESC", "/", "", "HOME", "", "END", "PGUP"}, for (int row = 0; row < rows; row++) {
{"TAB", "CTRL", "ALT", "", "", "", "PGDN"} if(buttons[row].length > mx) mx = buttons[row].length;
}; }
final int rows = buttons.length; final int rows = buttons.length;
final int[] cols = {buttons[0].length, buttons[1].length}; final int cols = mx;
setRowCount(rows); setRowCount(rows);
setColumnCount(cols[0]); setColumnCount(cols[0]);
for (int row = 0; row < rows; row++) { for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols[row]; col++) { for (int col = 0; col < cols; col++) {
final String buttonText = buttons[row][col]; final String buttonText = (buttons[row][col] == null ? " " : buttons[row][col]);
Button button; Button button;
switch (buttonText) { switch (buttonText) {
case "CTRL": case "CTRL":

View File

@@ -218,6 +218,11 @@ public final class TermuxActivity extends Activity implements ServiceConnection
final ViewPager viewPager = findViewById(R.id.viewpager); final ViewPager viewPager = findViewById(R.id.viewpager);
if (mSettings.isShowExtraKeys()) viewPager.setVisibility(View.VISIBLE); if (mSettings.isShowExtraKeys()) viewPager.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams layoutParams = viewPager.getLayoutParams();
layoutParams.height = layoutParams.height * mSettings.mExtraKeys.length;
viewPager.setLayoutParams(layoutParams);
viewPager.setAdapter(new PagerAdapter() { viewPager.setAdapter(new PagerAdapter() {
@Override @Override
public int getCount() { public int getCount() {
@@ -236,6 +241,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
View layout; View layout;
if (position == 0) { if (position == 0) {
layout = mExtraKeysView = (ExtraKeysView) inflater.inflate(R.layout.extra_keys_main, collection, false); layout = mExtraKeysView = (ExtraKeysView) inflater.inflate(R.layout.extra_keys_main, collection, false);
mExtraKeysView.reload(mSettings.mExtraKeys);
} else { } else {
layout = inflater.inflate(R.layout.extra_keys_right, collection, false); layout = inflater.inflate(R.layout.extra_keys_right, collection, false);
final EditText editText = layout.findViewById(R.id.text_input); final EditText editText = layout.findViewById(R.id.text_input);

View File

@@ -17,6 +17,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.json.JSONArray;
final class TermuxPreferences { final class TermuxPreferences {
@@ -104,6 +105,8 @@ final class TermuxPreferences {
return null; return null;
} }
public String[][] mExtraKeys;
public void reloadFromProperties(Context context) { public void reloadFromProperties(Context context) {
try { try {
File propsFile = new File(TermuxService.HOME_PATH + "/.termux/termux.properties"); File propsFile = new File(TermuxService.HOME_PATH + "/.termux/termux.properties");
@@ -129,6 +132,16 @@ final class TermuxPreferences {
break; break;
} }
JSONArray arr = new JSONArray(props.getProperty("extrakeys", "[[\"ESC\",\"CTRL\",\"ALT\",\"TAB\",\"\",\"/\",\"|\"]]"));
mExtraKeys = new String[arr.length()][];
for(int i = 0; i < arr.length(); i++) {
JSONArray line = arr.getJSONArray(i);
mExtraKeys[i] = new String[line.length()];
for(int j = 0; j < line.length(); j++) {
mExtraKeys[i][j] = line.getString(j);
}
}
mBackIsEscape = "escape".equals(props.getProperty("back-key", "back")); mBackIsEscape = "escape".equals(props.getProperty("back-key", "back"));
shortcuts.clear(); shortcuts.clear();