这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 53 additions & 32 deletions app/src/main/java/com/termux/app/ExtraKeysView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import android.provider.Settings;
import android.util.AttributeSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledExecutorService;

import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
import java.util.stream.Collectors;

import android.view.Gravity;
import android.view.HapticFeedbackConstants;
Expand All @@ -23,7 +27,6 @@
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.PopupWindow;
import android.widget.ToggleButton;

import com.termux.R;
import com.termux.view.TerminalView;
Expand Down Expand Up @@ -129,15 +132,27 @@ public enum SpecialButton {

private static class SpecialButtonState {
boolean isOn = false;
ToggleButton button = null;
boolean isActive = false;
List<Button> buttons = new ArrayList<>();

void setIsActive(boolean value) {
isActive = value;
buttons.forEach(button -> 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.ALT, new SpecialButtonState());
put(SpecialButton.FN, new SpecialButtonState());
}};

private final Set<String> specialButtonsKeys = specialButtons.keySet().stream().map(Enum::name).collect(Collectors.toSet());

private boolean isSpecialButton(ExtraKeyButton button) {
return specialButtonsKeys.contains(button.getKey());
}

private ScheduledExecutorService scheduledExecutor;
private PopupWindow popupWindow;
private int longPressCount;
Expand All @@ -147,30 +162,36 @@ public boolean readSpecialButton(SpecialButton name) {
if (state == null)
throw new RuntimeException("Must be a valid special button (see source)");

if (! state.isOn)
if (!state.isOn || !state.isActive)
return false;

if (state.button == null) {
return false;
}
state.setIsActive(false);

if (state.button.isPressed())
return true;

if (! state.button.isChecked())
return false;

state.button.setChecked(false);
state.button.setTextColor(TEXT_COLOR);
return true;
}

void popup(View view, String text) {
private Button createSpecialButton(String buttonKey, boolean needUpdate) {
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonKey));
state.isOn = true;
Button button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
button.setTextColor(state.isActive ? INTERESTING_COLOR : TEXT_COLOR);
if (needUpdate) {
state.buttons.add(button);
}
return button;
}

void popup(View view, ExtraKeyButton extraButton) {
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
Button button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
button.setText(text);
button.setTextColor(TEXT_COLOR);
Button button;
if(isSpecialButton(extraButton)) {
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.setMinHeight(0);
button.setMinWidth(0);
Expand Down Expand Up @@ -219,7 +240,7 @@ void reload(ExtraKeysInfos infos) {
return;

for(SpecialButtonState state : specialButtons.values())
state.button = null;
state.buttons = new ArrayList<>();

removeAllViews();

Expand All @@ -233,11 +254,8 @@ void reload(ExtraKeysInfos infos) {
final ExtraKeyButton buttonInfo = buttons[row][col];

Button button;
if(Arrays.asList("CTRL", "ALT", "FN").contains(buttonInfo.getKey())) {
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonInfo.getKey())); // for valueOf: https://stackoverflow.com/a/604426/1980630
state.isOn = true;
button = state.button = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
button.setClickable(true);
if(isSpecialButton(buttonInfo)) {
button = createSpecialButton(buttonInfo.getKey(), true);
} else {
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
}
Expand All @@ -262,10 +280,9 @@ void reload(ExtraKeysInfos infos) {
}

View root = getRootView();
if (Arrays.asList("CTRL", "ALT", "FN").contains(buttonInfo.getKey())) {
ToggleButton self = (ToggleButton) finalButton;
self.setChecked(self.isChecked());
self.setTextColor(self.isChecked() ? INTERESTING_COLOR : TEXT_COLOR);
if (isSpecialButton(buttonInfo)) {
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonInfo.getKey()));
state.setIsActive(!state.isActive);
} else {
sendKey(root, buttonInfo);
}
Expand Down Expand Up @@ -295,8 +312,7 @@ void reload(ExtraKeysInfos infos) {
scheduledExecutor = null;
}
v.setBackgroundColor(BUTTON_COLOR);
String extraButtonDisplayedText = buttonInfo.getPopup().getDisplay();
popup(v, extraButtonDisplayedText);
popup(v, buttonInfo.getPopup());
}
if (popupWindow != null && event.getY() > 0) {
v.setBackgroundColor(BUTTON_PRESSED_COLOR);
Expand Down Expand Up @@ -325,7 +341,12 @@ void reload(ExtraKeysInfos infos) {
popupWindow.dismiss();
popupWindow = null;
if (buttonInfo.getPopup() != null) {
sendKey(root, buttonInfo.getPopup());
if (isSpecialButton(buttonInfo.getPopup())) {
SpecialButtonState state = specialButtons.get(SpecialButton.valueOf(buttonInfo.getPopup().getKey()));
state.setIsActive(!state.isActive);
} else {
sendKey(root, buttonInfo.getPopup());
}
}
} else {
v.performClick();
Expand Down