mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Refactor Ctrl, Alt, Fn code
This commit is contained in:
committed by
Fredrik Fornwall
parent
d03e420e75
commit
55efdb2f56
@@ -87,56 +87,58 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
session.write(KeyName);
|
session.write(KeyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ToggleButton controlButton;
|
|
||||||
private ToggleButton altButton;
|
|
||||||
private ToggleButton fnButton;
|
|
||||||
|
|
||||||
private boolean hasControlButton = false;
|
public enum SpecialButton {
|
||||||
private boolean hasAltButton = false;
|
CTRL, ALT, FN
|
||||||
private boolean hasFnButton = false;
|
}
|
||||||
|
|
||||||
|
private static class SpecialButtonState {
|
||||||
|
boolean isOn = false;
|
||||||
|
ToggleButton button = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Boolean> specialButtons = new HashMap<String, Boolean>() {{
|
||||||
|
put(SpecialButton.CTRL, new SpecialButtonState());
|
||||||
|
put(SpecialButton.ALT, new SpecialButtonState());
|
||||||
|
put(SpecialButton.FN, new SpecialButtonState());
|
||||||
|
}};
|
||||||
|
|
||||||
private ScheduledExecutorService scheduledExecutor;
|
private ScheduledExecutorService scheduledExecutor;
|
||||||
private PopupWindow popupWindow;
|
private PopupWindow popupWindow;
|
||||||
private int longPressCount;
|
private int longPressCount;
|
||||||
|
|
||||||
|
/** @deprecated, call readSpecialButton(SpecialButton.CTRL); */
|
||||||
public boolean readControlButton() {
|
public boolean readControlButton() {
|
||||||
boolean result = false;
|
return readSpecialButton(SpecialButton.FN);
|
||||||
if (hasControlButton) {
|
|
||||||
if (controlButton.isPressed()) return true;
|
|
||||||
result = controlButton.isChecked();
|
|
||||||
if (result) {
|
|
||||||
controlButton.setChecked(false);
|
|
||||||
controlButton.setTextColor(TEXT_COLOR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated, call readSpecialButton(SpecialButton.ALT); */
|
||||||
public boolean readAltButton() {
|
public boolean readAltButton() {
|
||||||
boolean result = false;
|
return readSpecialButton(SpecialButton.FN);
|
||||||
if (hasAltButton) {
|
|
||||||
if (altButton.isPressed()) return true;
|
|
||||||
result = altButton.isChecked();
|
|
||||||
if (result) {
|
|
||||||
altButton.setChecked(false);
|
|
||||||
altButton.setTextColor(TEXT_COLOR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated, call readSpecialButton(SpecialButton.FN); */
|
||||||
public boolean readFnButton() {
|
public boolean readFnButton() {
|
||||||
boolean result = false;
|
return readSpecialButton(SpecialButton.FN);
|
||||||
if (hasFnButton) {
|
}
|
||||||
if (fnButton.isPressed()) return true;
|
|
||||||
result = fnButton.isChecked();
|
public boolean readSpecialButton(SpecialButton name) {
|
||||||
if (result) {
|
SpecialButtonState state = specialButtons.get(name);
|
||||||
fnButton.setChecked(false);
|
if(state == null)
|
||||||
fnButton.setTextColor(TEXT_COLOR);
|
throws Exception("Must be a valid special button (see source)");
|
||||||
}
|
|
||||||
|
if (! state.isOn)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (state.button.isPressed())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (state.button.isChecked()) {
|
||||||
|
state.button.setChecked(false);
|
||||||
|
state.button.setTextColor(TEXT_COLOR);
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
|
return state.button.isChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void popup(View view, String text) {
|
void popup(View view, String text) {
|
||||||
@@ -307,7 +309,9 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
* "-_-" will input the string "-_-"
|
* "-_-" will input the string "-_-"
|
||||||
*/
|
*/
|
||||||
void reload(String[][] buttons, CharDisplayMap charDisplayMap) {
|
void reload(String[][] buttons, CharDisplayMap charDisplayMap) {
|
||||||
altButton = controlButton = fnButton = null;
|
for(SpecialButtonState state : specialButtons.values())
|
||||||
|
state.button = null;
|
||||||
|
|
||||||
removeAllViews();
|
removeAllViews();
|
||||||
|
|
||||||
replaceAliases(buttons); // modifies the array
|
replaceAliases(buttons); // modifies the array
|
||||||
@@ -320,33 +324,24 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
|
|
||||||
for (int row = 0; row < rows; row++) {
|
for (int row = 0; row < rows; row++) {
|
||||||
for (int col = 0; col < cols; col++) {
|
for (int col = 0; col < cols; col++) {
|
||||||
final String buttonText = (buttons[row][col] == null ? " " : buttons[row][col]);
|
String buttonText = buttons[row][col];
|
||||||
// if (buttons[row][col] == null) then the button will be an empty button outputting a space character
|
|
||||||
|
if(buttonText == null) {
|
||||||
|
// The button will be an empty button outputting a space character, like s space bar
|
||||||
|
buttonText = " ";
|
||||||
|
}
|
||||||
|
|
||||||
Button button;
|
Button button;
|
||||||
switch (buttonText) {
|
if(Arrays.asList("CTRL", "ALT", "FN").contains(buttonText)) {
|
||||||
case "CTRL":
|
state = specialButtons.get(SpecialButton.valueOf(buttonText)); // for valueOf: https://stackoverflow.com/a/604426/1980630
|
||||||
hasControlButton = true;
|
state.isOn = true;
|
||||||
button = controlButton = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
button = state.button = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
||||||
button.setClickable(true);
|
button.setClickable(true);
|
||||||
break;
|
} else {
|
||||||
case "ALT":
|
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
||||||
hasAltButton = true;
|
|
||||||
button = altButton = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
|
||||||
button.setClickable(true);
|
|
||||||
break;
|
|
||||||
case "FN":
|
|
||||||
hasFnButton = true;
|
|
||||||
button = fnButton = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
|
||||||
button.setClickable(true);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final String displayedText = charDisplayMap.get(buttonText, buttonText);
|
final String displayedText = charDisplayMap.get(buttonText, buttonText);
|
||||||
|
|
||||||
button.setText(displayedText);
|
button.setText(displayedText);
|
||||||
button.setTextColor(TEXT_COLOR);
|
button.setTextColor(TEXT_COLOR);
|
||||||
button.setPadding(0, 0, 0, 0);
|
button.setPadding(0, 0, 0, 0);
|
||||||
@@ -357,17 +352,12 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
finalButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
|
finalButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
|
||||||
View root = getRootView();
|
View root = getRootView();
|
||||||
switch (buttonText) {
|
if(Arrays.asList("CTRL", "ALT", "FN").contains(buttonText)) {
|
||||||
case "CTRL":
|
ToggleButton self = (ToggleButton) finalButton;
|
||||||
case "ALT":
|
self.setChecked(self.isChecked());
|
||||||
case "FN":
|
self.setTextColor(self.isChecked() ? INTERESTING_COLOR : TEXT_COLOR);
|
||||||
ToggleButton self = (ToggleButton) finalButton;
|
} else {
|
||||||
self.setChecked(self.isChecked());
|
sendKey(root, buttonText);
|
||||||
self.setTextColor(self.isChecked() ? INTERESTING_COLOR : TEXT_COLOR);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
sendKey(root, buttonText);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -427,15 +417,14 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
default:
|
default:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
LayoutParams param = new GridLayout.LayoutParams();
|
LayoutParams param = new GridLayout.LayoutParams();
|
||||||
param.width = 0;
|
param.width = 0;
|
||||||
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP){ //special handle api 21
|
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) { //special handle api 21
|
||||||
param.height = (int)(37.5 * getResources().getDisplayMetrics().density + 0.5); // 37.5 equal to R.id.viewpager layout_height / rows in DP
|
param.height = (int)(37.5 * getResources().getDisplayMetrics().density + 0.5); // 37.5 equal to R.id.viewpager layout_height / rows in DP
|
||||||
}else{
|
} else {
|
||||||
param.height = 0;
|
param.height = 0;
|
||||||
}
|
}
|
||||||
param.setMargins(0, 0, 0, 0);
|
param.setMargins(0, 0, 0, 0);
|
||||||
|
Reference in New Issue
Block a user