Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve input method compatibility and add enable/disable input composing property. #2933

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.media.AudioManager;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.termux.R;
import com.termux.app.TermuxActivity;
Expand Down Expand Up @@ -216,6 +212,11 @@ public boolean shouldEnforceCharBasedInput() {
return mActivity.getProperties().isEnforcingCharBasedInput();
}

@Override
public boolean isInputComposingDisabled() {
return mActivity.getProperties().isInputComposingDisabled();
}

@Override
public boolean shouldUseCtrlSpaceWorkaround() {
return mActivity.getProperties().isUsingCtrlSpaceWorkaround();
Expand Down
71 changes: 71 additions & 0 deletions terminal-view/src/main/java/com/termux/view/ChangeWatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.termux.view;

import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.SpanWatcher;
import android.text.Spannable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;

import com.termux.view.inputmethod.TerminalInputConnection;

class ChangeWatcher implements TextWatcher, SpanWatcher {
final TerminalView mTerminalView;

ChangeWatcher(TerminalView terminalView) {
this.mTerminalView = terminalView;
}

private void sendUpdateSelection() {
final InputMethodManager imm = (InputMethodManager) mTerminalView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
final Editable text = mTerminalView.getImeBuffer();
if (imm != null && text != null) {
final int selectionStart = Selection.getSelectionStart(text);
final int selectionEnd = Selection.getSelectionEnd(text);

final int candStart = TerminalInputConnection.getComposingSpanStart(text);
final int candEnd = TerminalInputConnection.getComposingSpanEnd(text);
imm.updateSelection(mTerminalView, selectionStart, selectionEnd, candStart, candEnd);
}
}


private void spanChange(Object what) {
if (what == Selection.SELECTION_START || what == Selection.SELECTION_END) {
sendUpdateSelection();
}
}

@Override
public void onSpanAdded(Spannable text, Object what, int start, int end) {

spanChange(what);
}


@Override
public void onSpanRemoved(Spannable text, Object what, int start, int end) {
spanChange(what);

}

@Override
public void onSpanChanged(Spannable text, Object what, int ostart, int oend, int nstart, int nend) {
spanChange(what);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
mTerminalView.invalidate();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.termux.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
Expand Down Expand Up @@ -55,7 +56,8 @@ public TerminalRenderer(int textSize, Typeface typeface) {

/** Render the terminal to a canvas with at a specified row scroll, and an optional rectangular selection. */
public final void render(TerminalEmulator mEmulator, Canvas canvas, int topRow,
int selectionY1, int selectionY2, int selectionX1, int selectionX2) {
int selectionY1, int selectionY2, int selectionX1, int selectionX2,
CharSequence imeBuffer) {
final boolean reverseVideo = mEmulator.isReverseVideo();
final int endRow = topRow + mEmulator.mRows;
final int columns = mEmulator.mColumns;
Expand Down Expand Up @@ -153,6 +155,12 @@ public final void render(TerminalEmulator mEmulator, Canvas canvas, int topRow,
}
drawTextRun(canvas, line, palette, heightOffset, lastRunStartColumn, columnWidthSinceLastRun, lastRunStartIndex, charsSinceLastRun,
measuredWidthForRun, cursorColor, cursorShape, lastRunStyle, reverseVideo || invertCursorTextColor || lastRunInsideSelection);

if (row == cursorRow) {
//ime fore color
int imeFore = Color.DKGRAY;
drawImeBuffer(canvas, heightOffset, cursorCol, mEmulator.mColors.mCurrentColors[TextStyle.COLOR_INDEX_CURSOR], imeFore, imeBuffer, columns);
}
}
}

Expand Down Expand Up @@ -239,6 +247,35 @@ private void drawTextRun(Canvas canvas, char[] text, int[] palette, float y, int
if (savedMatrix) canvas.restore();
}

private void drawImeBuffer(Canvas canvas, float y, int cursorX, int cursorColor, int imeFore, CharSequence imeBuffer, int columns) {
// draw ime buffer
final int bufLen = imeBuffer.length();
if (bufLen > 0) {
float cursorHeight = mFontLineSpacingAndAscent - mFontAscent;

final float left = cursorX * mFontWidth;
final int maxWidth = (int) (columns * mFontWidth - left);
if (maxWidth > 0) {
final float[] widths = bufLen > asciiMeasures.length ? new float[bufLen] : asciiMeasures;

final int count = mTextPaint.getTextWidths(imeBuffer, 0, bufLen, widths);
int offset = count - 1;
int width = 0;
for (; offset >= 0 && width < maxWidth; offset--) {
width += widths[offset];
}
offset++;

mTextPaint.setColor(cursorColor);
canvas.drawRect(left, y - cursorHeight, left + width,
y, mTextPaint);

mTextPaint.setColor(imeFore);
canvas.drawText(imeBuffer, offset, bufLen, left, y - mFontLineSpacingAndAscent, mTextPaint);
}
}
}

public float getFontWidth() {
return mFontWidth;
}
Expand Down
28 changes: 27 additions & 1 deletion terminal-view/src/main/java/com/termux/view/TerminalView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import android.os.SystemClock;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ActionMode;
Expand All @@ -39,6 +41,7 @@
import com.termux.terminal.KeyHandler;
import com.termux.terminal.TerminalEmulator;
import com.termux.terminal.TerminalSession;
import com.termux.view.inputmethod.TerminalInputConnection;
import com.termux.view.textselection.TextSelectionCursorController;

/** View displaying and interacting with a {@link TerminalSession}. */
Expand Down Expand Up @@ -95,6 +98,7 @@ public final class TerminalView extends View {

private static final String LOG_TAG = "TerminalView";

private final SpannableStringBuilder imeBuffer = new SpannableStringBuilder("");
public TerminalView(Context context, AttributeSet attributes) { // NO_UCD (unused code)
super(context, attributes);
mGestureRecognizer = new GestureAndScaleRecognizer(context, new GestureAndScaleRecognizer.Listener() {
Expand Down Expand Up @@ -221,6 +225,11 @@ public void onLongPress(MotionEvent event) {
mScroller = new Scroller(context);
AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
mAccessibilityEnabled = am.isEnabled();


imeBuffer.setSpan(new ChangeWatcher(this),
0, imeBuffer.length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE | (100 << Spanned.SPAN_PRIORITY_SHIFT));
}


Expand Down Expand Up @@ -265,8 +274,25 @@ public boolean attachSession(TerminalSession session) {
return true;
}


public Editable getImeBuffer() {
return imeBuffer;
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
if(!mClient.isInputComposingDisabled()){
outAttrs.imeOptions =
EditorInfo.IME_FLAG_NO_EXTRACT_UI |
EditorInfo.IME_FLAG_NO_FULLSCREEN |
EditorInfo.IME_FLAG_NO_ENTER_ACTION;
outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
outAttrs.hintText = "";
outAttrs.initialSelStart = -1;
outAttrs.initialSelEnd = -1;
outAttrs.initialCapsMode = 0;
return new TerminalInputConnection(this);
}
// Ensure that inputType is only set if TerminalView is selected view with the keyboard and
// an alternate view is not selected, like an EditText. This is necessary if an activity is
// initially started with the alternate view or if activity is returned to from another app
Expand Down Expand Up @@ -975,7 +1001,7 @@ protected void onDraw(Canvas canvas) {
mTextSelectionCursorController.getSelectors(sel);
}

mRenderer.render(mEmulator, canvas, mTopRow, sel[0], sel[1], sel[2], sel[3]);
mRenderer.render(mEmulator, canvas, mTopRow, sel[0], sel[1], sel[2], sel[3], imeBuffer);

// render the text selection handles
renderTextSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface TerminalViewClient {

boolean isTerminalViewSelected();


boolean isInputComposingDisabled();

void copyModeChanged(boolean copyMode);

Expand Down
Loading