mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-07 03:05:18 +08:00
ExtraKeysView: Handle multiple and popup SpecialButtons
This commit is contained in:
committed by
Leonid Pliushch
parent
6d1b0efd3b
commit
2b6e9ade07
@@ -6,6 +6,8 @@ import android.os.Build;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
@@ -13,6 +15,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.HapticFeedbackConstants;
|
import android.view.HapticFeedbackConstants;
|
||||||
@@ -129,15 +132,30 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
|
|
||||||
private static class SpecialButtonState {
|
private static class SpecialButtonState {
|
||||||
boolean isOn = false;
|
boolean isOn = false;
|
||||||
ToggleButton button = null;
|
boolean isActive = false;
|
||||||
|
List<ToggleButton> buttons = new ArrayList<>();
|
||||||
|
|
||||||
|
void toggle(boolean value) {
|
||||||
|
isActive = value;
|
||||||
|
buttons.forEach(button -> {
|
||||||
|
button.setChecked(value);
|
||||||
|
button.setTextColor(value ? INTERESTING_COLOR : TEXT_COLOR);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<SpecialButton, SpecialButtonState> specialButtons = new HashMap<SpecialButton, SpecialButtonState>() {{
|
private final Map<SpecialButton, SpecialButtonState> specialButtons = new HashMap<SpecialButton, SpecialButtonState>() {{
|
||||||
put(SpecialButton.CTRL, new SpecialButtonState());
|
put(SpecialButton.CTRL, new SpecialButtonState());
|
||||||
put(SpecialButton.ALT, new SpecialButtonState());
|
put(SpecialButton.ALT, new SpecialButtonState());
|
||||||
put(SpecialButton.FN, new SpecialButtonState());
|
put(SpecialButton.FN, new SpecialButtonState());
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
private final List<String> specialButtonsKeys = specialButtons.keySet().stream().map(Enum::name).collect(Collectors.toList());
|
||||||
|
|
||||||
|
private boolean isSpecialButton(ExtraKeyButton button) {
|
||||||
|
return specialButtonsKeys.contains(button.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
private ScheduledExecutorService scheduledExecutor;
|
private ScheduledExecutorService scheduledExecutor;
|
||||||
private PopupWindow popupWindow;
|
private PopupWindow popupWindow;
|
||||||
private int longPressCount;
|
private int longPressCount;
|
||||||
@@ -147,30 +165,40 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
if (state == null)
|
if (state == null)
|
||||||
throw new RuntimeException("Must be a valid special button (see source)");
|
throw new RuntimeException("Must be a valid special button (see source)");
|
||||||
|
|
||||||
if (! state.isOn)
|
if (!state.isOn)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (state.button == null) {
|
if (!state.isActive) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.button.isPressed())
|
state.toggle(false);
|
||||||
return true;
|
|
||||||
|
|
||||||
if (! state.button.isChecked())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
state.button.setChecked(false);
|
|
||||||
state.button.setTextColor(TEXT_COLOR);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void popup(View view, String text) {
|
private ToggleButton createSpecialButton(String buttonKey, boolean needUpdate) {
|
||||||
|
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonKey));
|
||||||
|
state.isOn = true;
|
||||||
|
ToggleButton toggleButton = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
||||||
|
toggleButton.setTextColor(state.isActive ? INTERESTING_COLOR : TEXT_COLOR);
|
||||||
|
if (needUpdate) {
|
||||||
|
state.buttons.add(toggleButton);
|
||||||
|
}
|
||||||
|
return toggleButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
void popup(View view, ExtraKeyButton extraButton) {
|
||||||
int width = view.getMeasuredWidth();
|
int width = view.getMeasuredWidth();
|
||||||
int height = view.getMeasuredHeight();
|
int height = view.getMeasuredHeight();
|
||||||
Button button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
Button button;
|
||||||
button.setText(text);
|
if(isSpecialButton(extraButton)) {
|
||||||
button.setTextColor(TEXT_COLOR);
|
button = createSpecialButton(extraButton.getKey(), false);
|
||||||
|
} else {
|
||||||
|
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
||||||
|
button.setTextColor(TEXT_COLOR);
|
||||||
|
}
|
||||||
|
button.setText(extraButton.getDisplay());
|
||||||
button.setPadding(0, 0, 0, 0);
|
button.setPadding(0, 0, 0, 0);
|
||||||
button.setMinHeight(0);
|
button.setMinHeight(0);
|
||||||
button.setMinWidth(0);
|
button.setMinWidth(0);
|
||||||
@@ -219,7 +247,7 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
for(SpecialButtonState state : specialButtons.values())
|
for(SpecialButtonState state : specialButtons.values())
|
||||||
state.button = null;
|
state.buttons = new ArrayList<>();
|
||||||
|
|
||||||
removeAllViews();
|
removeAllViews();
|
||||||
|
|
||||||
@@ -233,10 +261,8 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
final ExtraKeyButton buttonInfo = buttons[row][col];
|
final ExtraKeyButton buttonInfo = buttons[row][col];
|
||||||
|
|
||||||
Button button;
|
Button button;
|
||||||
if(Arrays.asList("CTRL", "ALT", "FN").contains(buttonInfo.getKey())) {
|
if(isSpecialButton(buttonInfo)) {
|
||||||
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonInfo.getKey())); // for valueOf: https://stackoverflow.com/a/604426/1980630
|
button = createSpecialButton(buttonInfo.getKey(), true);
|
||||||
state.isOn = true;
|
|
||||||
button = state.button = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
|
||||||
button.setClickable(true);
|
button.setClickable(true);
|
||||||
} else {
|
} else {
|
||||||
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
||||||
@@ -262,10 +288,9 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
View root = getRootView();
|
View root = getRootView();
|
||||||
if (Arrays.asList("CTRL", "ALT", "FN").contains(buttonInfo.getKey())) {
|
if (isSpecialButton(buttonInfo)) {
|
||||||
ToggleButton self = (ToggleButton) finalButton;
|
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonInfo.getKey()));
|
||||||
self.setChecked(self.isChecked());
|
state.toggle(!state.isActive);
|
||||||
self.setTextColor(self.isChecked() ? INTERESTING_COLOR : TEXT_COLOR);
|
|
||||||
} else {
|
} else {
|
||||||
sendKey(root, buttonInfo);
|
sendKey(root, buttonInfo);
|
||||||
}
|
}
|
||||||
@@ -295,8 +320,7 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
scheduledExecutor = null;
|
scheduledExecutor = null;
|
||||||
}
|
}
|
||||||
v.setBackgroundColor(BUTTON_COLOR);
|
v.setBackgroundColor(BUTTON_COLOR);
|
||||||
String extraButtonDisplayedText = buttonInfo.getPopup().getDisplay();
|
popup(v, buttonInfo.getPopup());
|
||||||
popup(v, extraButtonDisplayedText);
|
|
||||||
}
|
}
|
||||||
if (popupWindow != null && event.getY() > 0) {
|
if (popupWindow != null && event.getY() > 0) {
|
||||||
v.setBackgroundColor(BUTTON_PRESSED_COLOR);
|
v.setBackgroundColor(BUTTON_PRESSED_COLOR);
|
||||||
@@ -325,7 +349,12 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
popupWindow.dismiss();
|
popupWindow.dismiss();
|
||||||
popupWindow = null;
|
popupWindow = null;
|
||||||
if (buttonInfo.getPopup() != null) {
|
if (buttonInfo.getPopup() != null) {
|
||||||
sendKey(root, buttonInfo.getPopup());
|
if (isSpecialButton(buttonInfo.getPopup())) {
|
||||||
|
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonInfo.getPopup().getKey()));
|
||||||
|
state.toggle(!state.isActive);
|
||||||
|
} else {
|
||||||
|
sendKey(root, buttonInfo.getPopup());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
v.performClick();
|
v.performClick();
|
||||||
|
Reference in New Issue
Block a user