add symbol popup

This commit is contained in:
david
2018-03-05 16:35:26 +08:00
committed by Fredrik Fornwall
parent 594a5dfe25
commit 3a16f461e7

View File

@@ -1,6 +1,7 @@
package com.termux.app; package com.termux.app;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@@ -14,6 +15,7 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.GridLayout; import android.widget.GridLayout;
import android.widget.PopupWindow;
import android.widget.ToggleButton; import android.widget.ToggleButton;
import com.termux.R; import com.termux.R;
@@ -88,6 +90,7 @@ public final class ExtraKeysView extends GridLayout {
private ToggleButton altButton; private ToggleButton altButton;
private ToggleButton fnButton; private ToggleButton fnButton;
private ScheduledExecutorService scheduledExecutor; private ScheduledExecutorService scheduledExecutor;
private PopupWindow popupWindow;
private int longPressCount; private int longPressCount;
public boolean readControlButton() { public boolean readControlButton() {
@@ -120,6 +123,30 @@ public final class ExtraKeysView extends GridLayout {
return result; return result;
} }
void popup(View view, String text) {
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
Button button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
button.setText(text);
button.setMinHeight(0);
button.setMinWidth(0);
button.setMinimumWidth(0);
button.setMinimumHeight(0);
button.setPadding(0, 0, 0, 0);
button.setTextColor(TEXT_COLOR);
button.setWidth(width);
button.setHeight(height);
button.setBackgroundColor(BUTTON_PRESSED_COLOR);
popupWindow = new PopupWindow(this);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(button);
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(false);
popupWindow.showAsDropDown(view, 0, -2 * height);
}
void reload() { void reload() {
altButton = controlButton = null; altButton = controlButton = null;
removeAllViews(); removeAllViews();
@@ -190,7 +217,7 @@ public final class ExtraKeysView extends GridLayout {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
longPressCount = 0; longPressCount = 0;
v.setBackgroundColor(BUTTON_PRESSED_COLOR); v.setBackgroundColor(BUTTON_PRESSED_COLOR);
if (!"CTRLALT".contains(buttonText)) { if ("↑↓←→".contains(buttonText)) {
scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
scheduledExecutor.scheduleWithFixedDelay(new Runnable() { scheduledExecutor.scheduleWithFixedDelay(new Runnable() {
@Override @Override
@@ -201,16 +228,38 @@ public final class ExtraKeysView extends GridLayout {
}, 400, 100, TimeUnit.MILLISECONDS); }, 400, 100, TimeUnit.MILLISECONDS);
} }
return true; return true;
case MotionEvent.ACTION_MOVE:
if ("-/".contains(buttonText)) {
if (popupWindow == null && event.getY() < 0) {
v.setBackgroundColor(BUTTON_COLOR);
String text = "-".equals(buttonText) ? "|" : "\\";
popup(v, text);
}
if (popupWindow != null && event.getY() > 0) {
v.setBackgroundColor(BUTTON_PRESSED_COLOR);
popupWindow.dismiss();
popupWindow = null;
}
}
return true;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
performClick();
v.setBackgroundColor(BUTTON_COLOR); v.setBackgroundColor(BUTTON_COLOR);
if (scheduledExecutor != null) { if (scheduledExecutor != null) {
scheduledExecutor.shutdownNow(); scheduledExecutor.shutdownNow();
scheduledExecutor = null; scheduledExecutor = null;
} }
if (longPressCount == 0) { if (longPressCount == 0) {
if (popupWindow != null) {
popupWindow.setContentView(null);
popupWindow.dismiss();
popupWindow = null;
sendKey(root, "-".equals(buttonText) ? "|" : "\\");
} else {
v.performClick(); v.performClick();
} }
}
return true; return true;
default: default:
return true; return true;