mirror of https://github.com/termux/termux-app
Partial refactor of the mess that is TerminalView
- Decouple the `CursorController`, `TextSelectionCursorController`(previously `SelectionModifierCursorController`) and `TextSelectionHandleView` (previously `HandleView`) from `TerminalView` by moving them to their own class files. - Fixes #1501 which caused the `java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.` exception to be thrown when long pressing the down key while simultaneously long pressing the terminal view for text selection.
This commit is contained in:
parent
93a5bf8d29
commit
ada5087f67
|
@ -230,4 +230,12 @@ public final class TerminalRenderer {
|
|||
|
||||
if (savedMatrix) canvas.restore();
|
||||
}
|
||||
|
||||
public float getFontWidth() {
|
||||
return mFontWidth;
|
||||
}
|
||||
|
||||
public int getFontLineSpacing() {
|
||||
return mFontLineSpacing;
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,55 @@
|
|||
package com.termux.view.textselection;
|
||||
|
||||
import android.view.MotionEvent;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
import com.termux.view.TerminalView;
|
||||
|
||||
/**
|
||||
* A CursorController instance can be used to control cursors in the text.
|
||||
* It is not used outside of {@link TerminalView}.
|
||||
*/
|
||||
public interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener {
|
||||
/**
|
||||
* Show the cursors on screen. Will be drawn by {@link #render()} by a call during onDraw.
|
||||
* See also {@link #hide()}.
|
||||
*/
|
||||
void show(MotionEvent event);
|
||||
|
||||
/**
|
||||
* Hide the cursors from screen.
|
||||
* See also {@link #show(MotionEvent event)}.
|
||||
*/
|
||||
boolean hide();
|
||||
|
||||
/**
|
||||
* Render the cursors.
|
||||
*/
|
||||
void render();
|
||||
|
||||
/**
|
||||
* Update the cursor positions.
|
||||
*/
|
||||
void updatePosition(TextSelectionHandleView handle, int x, int y);
|
||||
|
||||
/**
|
||||
* This method is called by {@link #onTouchEvent(MotionEvent)} and gives the cursors
|
||||
* a chance to become active and/or visible.
|
||||
*
|
||||
* @param event The touch event
|
||||
*/
|
||||
boolean onTouchEvent(MotionEvent event);
|
||||
|
||||
/**
|
||||
* Called when the view is detached from window. Perform house keeping task, such as
|
||||
* stopping Runnable thread that would otherwise keep a reference on the context, thus
|
||||
* preventing the activity to be recycled.
|
||||
*/
|
||||
void onDetached();
|
||||
|
||||
/**
|
||||
* @return true if the cursors are currently active.
|
||||
*/
|
||||
boolean isActive();
|
||||
|
||||
}
|
|
@ -0,0 +1,382 @@
|
|||
package com.termux.view.textselection;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Rect;
|
||||
import android.text.TextUtils;
|
||||
import android.view.ActionMode;
|
||||
import android.view.InputDevice;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import com.termux.terminal.TerminalBuffer;
|
||||
import com.termux.terminal.WcWidth;
|
||||
import com.termux.view.R;
|
||||
import com.termux.view.TerminalView;
|
||||
|
||||
public class TextSelectionCursorController implements CursorController {
|
||||
|
||||
private final TerminalView terminalView;
|
||||
private final TextSelectionHandleView mStartHandle, mEndHandle;
|
||||
private boolean mIsSelectingText = false;
|
||||
private long mShowStartTime = System.currentTimeMillis();
|
||||
|
||||
private final int mHandleHeight;
|
||||
private int mSelX1 = -1, mSelX2 = -1, mSelY1 = -1, mSelY2 = -1;
|
||||
|
||||
private ActionMode mActionMode;
|
||||
private final int ACTION_COPY = 1;
|
||||
private final int ACTION_PASTE = 2;
|
||||
private final int ACTION_MORE = 3;
|
||||
|
||||
public TextSelectionCursorController(TerminalView terminalView) {
|
||||
this.terminalView = terminalView;
|
||||
mStartHandle = new TextSelectionHandleView(terminalView, this, TextSelectionHandleView.LEFT);
|
||||
mEndHandle = new TextSelectionHandleView(terminalView, this, TextSelectionHandleView.RIGHT);
|
||||
|
||||
mHandleHeight = Math.max(mStartHandle.getHandleHeight(), mEndHandle.getHandleHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(MotionEvent event) {
|
||||
setInitialTextSelectionPosition(event);
|
||||
mStartHandle.positionAtCursor(mSelX1, mSelY1, true);
|
||||
mEndHandle.positionAtCursor(mSelX2 + 1, mSelY2, true);
|
||||
|
||||
setActionModeCallBacks();
|
||||
mShowStartTime = System.currentTimeMillis();
|
||||
mIsSelectingText = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hide() {
|
||||
if (!isActive()) return false;
|
||||
|
||||
// prevent hide calls right after a show call, like long pressing the down key
|
||||
// 300ms seems long enough that it wouldn't cause hide problems if action button
|
||||
// is quickly clicked after the show, otherwise decrease it
|
||||
if (System.currentTimeMillis() - mShowStartTime < 300) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mStartHandle.hide();
|
||||
mEndHandle.hide();
|
||||
|
||||
if (mActionMode != null) {
|
||||
// This will hide the TextSelectionCursorController
|
||||
mActionMode.finish();
|
||||
}
|
||||
|
||||
mSelX1 = mSelY1 = mSelX2 = mSelY2 = -1;
|
||||
mIsSelectingText = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
if (!isActive()) return;
|
||||
|
||||
mStartHandle.positionAtCursor(mSelX1, mSelY1, false);
|
||||
mEndHandle.positionAtCursor(mSelX2 + 1, mSelY2, false);
|
||||
|
||||
if (mActionMode != null) {
|
||||
mActionMode.invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
public void setInitialTextSelectionPosition(MotionEvent event) {
|
||||
int cx = (int) (event.getX() / terminalView.mRenderer.getFontWidth());
|
||||
final boolean eventFromMouse = event.isFromSource(InputDevice.SOURCE_MOUSE);
|
||||
// Offset for finger:
|
||||
final int SELECT_TEXT_OFFSET_Y = eventFromMouse ? 0 : -40;
|
||||
int cy = (int) ((event.getY() + SELECT_TEXT_OFFSET_Y) / terminalView.mRenderer.getFontLineSpacing()) + terminalView.getTopRow();
|
||||
|
||||
mSelX1 = mSelX2 = cx;
|
||||
mSelY1 = mSelY2 = cy;
|
||||
|
||||
TerminalBuffer screen = terminalView.mEmulator.getScreen();
|
||||
if (!" ".equals(screen.getSelectedText(mSelX1, mSelY1, mSelX1, mSelY1))) {
|
||||
// Selecting something other than whitespace. Expand to word.
|
||||
while (mSelX1 > 0 && !"".equals(screen.getSelectedText(mSelX1 - 1, mSelY1, mSelX1 - 1, mSelY1))) {
|
||||
mSelX1--;
|
||||
}
|
||||
while (mSelX2 < terminalView.mEmulator.mColumns - 1 && !"".equals(screen.getSelectedText(mSelX2 + 1, mSelY1, mSelX2 + 1, mSelY1))) {
|
||||
mSelX2++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setActionModeCallBacks() {
|
||||
final ActionMode.Callback callback = new ActionMode.Callback() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
int show = MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT;
|
||||
|
||||
ClipboardManager clipboard = (ClipboardManager) terminalView.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
menu.add(Menu.NONE, ACTION_COPY, Menu.NONE, R.string.copy_text).setShowAsAction(show);
|
||||
menu.add(Menu.NONE, ACTION_PASTE, Menu.NONE, R.string.paste_text).setEnabled(clipboard.hasPrimaryClip()).setShowAsAction(show);
|
||||
menu.add(Menu.NONE, ACTION_MORE, Menu.NONE, R.string.text_selection_more);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||
if (!isActive()) {
|
||||
// Fix issue where the dialog is pressed while being dismissed.
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case ACTION_COPY:
|
||||
String selectedText = terminalView.mEmulator.getSelectedText(mSelX1, mSelY1, mSelX2, mSelY2).trim();
|
||||
terminalView.mTermSession.clipboardText(selectedText);
|
||||
terminalView.stopTextSelectionMode();
|
||||
break;
|
||||
case ACTION_PASTE:
|
||||
terminalView.stopTextSelectionMode();
|
||||
ClipboardManager clipboard = (ClipboardManager) terminalView.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
ClipData clipData = clipboard.getPrimaryClip();
|
||||
if (clipData != null) {
|
||||
CharSequence paste = clipData.getItemAt(0).coerceToText(terminalView.getContext());
|
||||
if (!TextUtils.isEmpty(paste)) terminalView.mEmulator.paste(paste.toString());
|
||||
}
|
||||
break;
|
||||
case ACTION_MORE:
|
||||
terminalView.stopTextSelectionMode(); //we stop text selection first, otherwise handles will show above popup
|
||||
terminalView.showContextMenu();
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
mActionMode = terminalView.startActionMode(new ActionMode.Callback2() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
return callback.onCreateActionMode(mode, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||
return callback.onActionItemClicked(mode, item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
// Ignore.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
|
||||
int x1 = Math.round(mSelX1 * terminalView.mRenderer.getFontWidth());
|
||||
int x2 = Math.round(mSelX2 * terminalView.mRenderer.getFontWidth());
|
||||
int y1 = Math.round((mSelY1 - 1 - terminalView.getTopRow()) * terminalView.mRenderer.getFontLineSpacing());
|
||||
int y2 = Math.round((mSelY2 + 1 - terminalView.getTopRow()) * terminalView.mRenderer.getFontLineSpacing());
|
||||
|
||||
|
||||
if (x1 > x2) {
|
||||
int tmp = x1;
|
||||
x1 = x2;
|
||||
x2 = tmp;
|
||||
}
|
||||
|
||||
outRect.set(x1, y1 + mHandleHeight, x2, y2 + mHandleHeight);
|
||||
}
|
||||
}, ActionMode.TYPE_FLOATING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePosition(TextSelectionHandleView handle, int x, int y) {
|
||||
TerminalBuffer screen = terminalView.mEmulator.getScreen();
|
||||
final int scrollRows = screen.getActiveRows() - terminalView.mEmulator.mRows;
|
||||
if (handle == mStartHandle) {
|
||||
mSelX1 = terminalView.getCursorX(x);
|
||||
mSelY1 = terminalView.getCursorY(y);
|
||||
if (mSelX1 < 0) {
|
||||
mSelX1 = 0;
|
||||
}
|
||||
|
||||
if (mSelY1 < -scrollRows) {
|
||||
mSelY1 = -scrollRows;
|
||||
|
||||
} else if (mSelY1 > terminalView.mEmulator.mRows - 1) {
|
||||
mSelY1 = terminalView.mEmulator.mRows - 1;
|
||||
|
||||
}
|
||||
|
||||
if (mSelY1 > mSelY2) {
|
||||
mSelY1 = mSelY2;
|
||||
}
|
||||
if (mSelY1 == mSelY2 && mSelX1 > mSelX2) {
|
||||
mSelX1 = mSelX2;
|
||||
}
|
||||
|
||||
if (!terminalView.mEmulator.isAlternateBufferActive()) {
|
||||
int topRow = terminalView.getTopRow();
|
||||
|
||||
if (mSelY1 <= topRow) {
|
||||
topRow--;
|
||||
if (topRow < -scrollRows) {
|
||||
topRow = -scrollRows;
|
||||
}
|
||||
} else if (mSelY1 >= topRow + terminalView.mEmulator.mRows) {
|
||||
topRow++;
|
||||
if (topRow > 0) {
|
||||
topRow = 0;
|
||||
}
|
||||
}
|
||||
|
||||
terminalView.setTopRow(topRow);
|
||||
}
|
||||
|
||||
mSelX1 = getValidCurX(screen, mSelY1, mSelX1);
|
||||
|
||||
} else {
|
||||
mSelX2 = terminalView.getCursorX(x);
|
||||
mSelY2 = terminalView.getCursorY(y);
|
||||
if (mSelX2 < 0) {
|
||||
mSelX2 = 0;
|
||||
}
|
||||
|
||||
if (mSelY2 < -scrollRows) {
|
||||
mSelY2 = -scrollRows;
|
||||
} else if (mSelY2 > terminalView.mEmulator.mRows - 1) {
|
||||
mSelY2 = terminalView.mEmulator.mRows - 1;
|
||||
}
|
||||
|
||||
if (mSelY1 > mSelY2) {
|
||||
mSelY2 = mSelY1;
|
||||
}
|
||||
if (mSelY1 == mSelY2 && mSelX1 > mSelX2) {
|
||||
mSelX2 = mSelX1;
|
||||
}
|
||||
|
||||
if (!terminalView.mEmulator.isAlternateBufferActive()) {
|
||||
int topRow = terminalView.getTopRow();
|
||||
|
||||
if (mSelY2 <= topRow) {
|
||||
topRow--;
|
||||
if (topRow < -scrollRows) {
|
||||
topRow = -scrollRows;
|
||||
}
|
||||
} else if (mSelY2 >= topRow + terminalView.mEmulator.mRows) {
|
||||
topRow++;
|
||||
if (topRow > 0) {
|
||||
topRow = 0;
|
||||
}
|
||||
}
|
||||
|
||||
terminalView.setTopRow(topRow);
|
||||
}
|
||||
|
||||
mSelX2 = getValidCurX(screen, mSelY2, mSelX2);
|
||||
}
|
||||
|
||||
terminalView.invalidate();
|
||||
}
|
||||
|
||||
private int getValidCurX(TerminalBuffer screen, int cy, int cx) {
|
||||
String line = screen.getSelectedText(0, cy, cx, cy);
|
||||
if (!TextUtils.isEmpty(line)) {
|
||||
int col = 0;
|
||||
for (int i = 0, len = line.length(); i < len; i++) {
|
||||
char ch1 = line.charAt(i);
|
||||
if (ch1 == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
int wc;
|
||||
if (Character.isHighSurrogate(ch1) && i + 1 < len) {
|
||||
char ch2 = line.charAt(++i);
|
||||
wc = WcWidth.width(Character.toCodePoint(ch1, ch2));
|
||||
} else {
|
||||
wc = WcWidth.width(ch1);
|
||||
}
|
||||
|
||||
final int cend = col + wc;
|
||||
if (cx > col && cx < cend) {
|
||||
return cend;
|
||||
}
|
||||
if (cend == col) {
|
||||
return col;
|
||||
}
|
||||
col = cend;
|
||||
}
|
||||
}
|
||||
return cx;
|
||||
}
|
||||
|
||||
public void decrementYTextSelectionCursors(int decrement) {
|
||||
mSelY1 -= decrement;
|
||||
mSelY2 -= decrement;
|
||||
}
|
||||
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void onTouchModeChanged(boolean isInTouchMode) {
|
||||
if (!isInTouchMode) {
|
||||
terminalView.stopTextSelectionMode();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetached() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return mIsSelectingText;
|
||||
}
|
||||
|
||||
public void getSelectors(int[] sel) {
|
||||
if (sel == null || sel.length != 4) {
|
||||
return;
|
||||
}
|
||||
|
||||
sel[0] = mSelY1;
|
||||
sel[1] = mSelY2;
|
||||
sel[2] = mSelX1;
|
||||
sel[3] = mSelX2;
|
||||
}
|
||||
|
||||
public ActionMode getActionMode() {
|
||||
return mActionMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this controller is currently used to move the start selection.
|
||||
*/
|
||||
public boolean isSelectionStartDragged() {
|
||||
return mStartHandle.isDragging();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this controller is currently used to move the end selection.
|
||||
*/
|
||||
public boolean isSelectionEndDragged() {
|
||||
return mEndHandle.isDragging();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,345 @@
|
|||
package com.termux.view.textselection;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.SystemClock;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.PopupWindow;
|
||||
|
||||
import com.termux.view.R;
|
||||
import com.termux.view.TerminalView;
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
public class TextSelectionHandleView extends View {
|
||||
private final TerminalView terminalView;
|
||||
private PopupWindow mHandle;
|
||||
private final CursorController mCursorController;
|
||||
|
||||
private final Drawable mHandleLeftDrawable;
|
||||
private final Drawable mHandleRightDrawable;
|
||||
private Drawable mHandleDrawable;
|
||||
|
||||
private boolean mIsDragging;
|
||||
|
||||
final int[] mTempCoords = new int[2];
|
||||
Rect mTempRect;
|
||||
|
||||
private int mPointX;
|
||||
private int mPointY;
|
||||
private float mTouchToWindowOffsetX;
|
||||
private float mTouchToWindowOffsetY;
|
||||
private float mHotspotX;
|
||||
private float mHotspotY;
|
||||
private float mTouchOffsetY;
|
||||
private int mLastParentX;
|
||||
private int mLastParentY;
|
||||
|
||||
private int mHandleHeight;
|
||||
private int mHandleWidth;
|
||||
|
||||
private final int mInitialOrientation;
|
||||
private int mOrientation;
|
||||
|
||||
public static final int LEFT = 0;
|
||||
public static final int RIGHT = 2;
|
||||
|
||||
private long mLastTime;
|
||||
|
||||
public TextSelectionHandleView(TerminalView terminalView, CursorController cursorController, int initialOrientation) {
|
||||
super(terminalView.getContext());
|
||||
this.terminalView = terminalView;
|
||||
mCursorController = cursorController;
|
||||
mInitialOrientation = initialOrientation;
|
||||
|
||||
mHandleLeftDrawable = getContext().getDrawable(R.drawable.text_select_handle_left_material);
|
||||
mHandleRightDrawable = getContext().getDrawable(R.drawable.text_select_handle_right_material);
|
||||
|
||||
setOrientation(mInitialOrientation);
|
||||
}
|
||||
|
||||
private void initHandle() {
|
||||
mHandle = new PopupWindow(terminalView.getContext(), null,
|
||||
android.R.attr.textSelectHandleWindowStyle);
|
||||
mHandle.setSplitTouchEnabled(true);
|
||||
mHandle.setClippingEnabled(false);
|
||||
mHandle.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
|
||||
mHandle.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
mHandle.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
mHandle.setBackgroundDrawable(null);
|
||||
mHandle.setAnimationStyle(0);
|
||||
mHandle.setEnterTransition(null);
|
||||
mHandle.setExitTransition(null);
|
||||
mHandle.setContentView(this);
|
||||
}
|
||||
|
||||
public void setOrientation(int orientation) {
|
||||
mOrientation = orientation;
|
||||
int handleWidth = 0;
|
||||
switch (orientation) {
|
||||
case LEFT: {
|
||||
mHandleDrawable = mHandleLeftDrawable;
|
||||
handleWidth = mHandleDrawable.getIntrinsicWidth();
|
||||
mHotspotX = (handleWidth * 3) / (float) 4;
|
||||
break;
|
||||
}
|
||||
|
||||
case RIGHT: {
|
||||
mHandleDrawable = mHandleRightDrawable;
|
||||
handleWidth = mHandleDrawable.getIntrinsicWidth();
|
||||
mHotspotX = handleWidth / (float) 4;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mHandleHeight = mHandleDrawable.getIntrinsicHeight();
|
||||
|
||||
mHandleWidth = handleWidth;
|
||||
mTouchOffsetY = -mHandleHeight * 0.3f;
|
||||
mHotspotY = 0;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void show() {
|
||||
if (!isPositionVisible()) {
|
||||
hide();
|
||||
return;
|
||||
}
|
||||
|
||||
// We remove handle from its parent first otherwise the following exception may be thrown
|
||||
// java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
|
||||
removeFromParent();
|
||||
|
||||
initHandle(); // init the handle
|
||||
invalidate(); // invalidate to make sure onDraw is called
|
||||
|
||||
final int[] coords = mTempCoords;
|
||||
terminalView.getLocationInWindow(coords);
|
||||
coords[0] += mPointX;
|
||||
coords[1] += mPointY;
|
||||
|
||||
if(mHandle != null)
|
||||
mHandle.showAtLocation(terminalView, 0, coords[0], coords[1]);
|
||||
}
|
||||
|
||||
public void hide() {
|
||||
mIsDragging = false;
|
||||
|
||||
if(mHandle != null) {
|
||||
mHandle.dismiss();
|
||||
|
||||
// We remove handle from its parent, otherwise it may still be shown in some cases even after the dismiss call
|
||||
removeFromParent();
|
||||
mHandle = null; // garbage collect the handle
|
||||
}
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void removeFromParent() {
|
||||
if(!isParentNull()) {
|
||||
((ViewGroup)this.getParent()).removeView(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void positionAtCursor(final int cx, final int cy, boolean forceOrientationCheck) {
|
||||
int x = terminalView.getPointX(cx);
|
||||
int y = terminalView.getPointY(cy + 1);
|
||||
moveTo(x, y, forceOrientationCheck);
|
||||
}
|
||||
|
||||
private void moveTo(int x, int y, boolean forceOrientationCheck) {
|
||||
float oldHotspotX = mHotspotX;
|
||||
checkChangedOrientation(x, forceOrientationCheck);
|
||||
mPointX = (int) (x - (isShowing() ? oldHotspotX : mHotspotX));
|
||||
mPointY = y;
|
||||
|
||||
if (isPositionVisible()) {
|
||||
int[] coords = null;
|
||||
|
||||
if (isShowing()) {
|
||||
coords = mTempCoords;
|
||||
terminalView.getLocationInWindow(coords);
|
||||
int x1 = coords[0] + mPointX;
|
||||
int y1 = coords[1] + mPointY;
|
||||
if (mHandle != null)
|
||||
mHandle.update(x1, y1, getWidth(), getHeight());
|
||||
} else {
|
||||
show();
|
||||
}
|
||||
|
||||
if (mIsDragging) {
|
||||
if (coords == null) {
|
||||
coords = mTempCoords;
|
||||
terminalView.getLocationInWindow(coords);
|
||||
}
|
||||
if (coords[0] != mLastParentX || coords[1] != mLastParentY) {
|
||||
mTouchToWindowOffsetX += coords[0] - mLastParentX;
|
||||
mTouchToWindowOffsetY += coords[1] - mLastParentY;
|
||||
mLastParentX = coords[0];
|
||||
mLastParentY = coords[1];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
hide();
|
||||
}
|
||||
}
|
||||
|
||||
public void changeOrientation(int orientation) {
|
||||
if (mOrientation != orientation) {
|
||||
setOrientation(orientation);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkChangedOrientation(int posX, boolean force) {
|
||||
if (!mIsDragging && !force) {
|
||||
return;
|
||||
}
|
||||
long millis = SystemClock.currentThreadTimeMillis();
|
||||
if (millis - mLastTime < 50 && !force) {
|
||||
return;
|
||||
}
|
||||
mLastTime = millis;
|
||||
|
||||
final TerminalView hostView = terminalView;
|
||||
final int left = hostView.getLeft();
|
||||
final int right = hostView.getWidth();
|
||||
final int top = hostView.getTop();
|
||||
final int bottom = hostView.getHeight();
|
||||
|
||||
if (mTempRect == null) {
|
||||
mTempRect = new Rect();
|
||||
}
|
||||
final Rect clip = mTempRect;
|
||||
clip.left = left + terminalView.getPaddingLeft();
|
||||
clip.top = top + terminalView.getPaddingTop();
|
||||
clip.right = right - terminalView.getPaddingRight();
|
||||
clip.bottom = bottom - terminalView.getPaddingBottom();
|
||||
|
||||
final ViewParent parent = hostView.getParent();
|
||||
if (parent == null || !parent.getChildVisibleRect(hostView, clip, null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (posX - mHandleWidth < clip.left) {
|
||||
changeOrientation(RIGHT);
|
||||
} else if (posX + mHandleWidth > clip.right) {
|
||||
changeOrientation(LEFT);
|
||||
} else {
|
||||
changeOrientation(mInitialOrientation);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPositionVisible() {
|
||||
// Always show a dragging handle.
|
||||
if (mIsDragging) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final TerminalView hostView = terminalView;
|
||||
final int left = 0;
|
||||
final int right = hostView.getWidth();
|
||||
final int top = 0;
|
||||
final int bottom = hostView.getHeight();
|
||||
|
||||
if (mTempRect == null) {
|
||||
mTempRect = new Rect();
|
||||
}
|
||||
final Rect clip = mTempRect;
|
||||
clip.left = left + terminalView.getPaddingLeft();
|
||||
clip.top = top + terminalView.getPaddingTop();
|
||||
clip.right = right - terminalView.getPaddingRight();
|
||||
clip.bottom = bottom - terminalView.getPaddingBottom();
|
||||
|
||||
final ViewParent parent = hostView.getParent();
|
||||
if (parent == null || !parent.getChildVisibleRect(hostView, clip, null)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final int[] coords = mTempCoords;
|
||||
hostView.getLocationInWindow(coords);
|
||||
final int posX = coords[0] + mPointX + (int) mHotspotX;
|
||||
final int posY = coords[1] + mPointY + (int) mHotspotY;
|
||||
|
||||
return posX >= clip.left && posX <= clip.right &&
|
||||
posY >= clip.top && posY <= clip.bottom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas c) {
|
||||
final int width = mHandleDrawable.getIntrinsicWidth();
|
||||
int height = mHandleDrawable.getIntrinsicHeight();
|
||||
mHandleDrawable.setBounds(0, 0, width, height);
|
||||
mHandleDrawable.draw(c);
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
terminalView.updateFloatingToolbarVisibility(event);
|
||||
switch (event.getActionMasked()) {
|
||||
case MotionEvent.ACTION_DOWN: {
|
||||
final float rawX = event.getRawX();
|
||||
final float rawY = event.getRawY();
|
||||
mTouchToWindowOffsetX = rawX - mPointX;
|
||||
mTouchToWindowOffsetY = rawY - mPointY;
|
||||
final int[] coords = mTempCoords;
|
||||
terminalView.getLocationInWindow(coords);
|
||||
mLastParentX = coords[0];
|
||||
mLastParentY = coords[1];
|
||||
mIsDragging = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case MotionEvent.ACTION_MOVE: {
|
||||
final float rawX = event.getRawX();
|
||||
final float rawY = event.getRawY();
|
||||
|
||||
final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX;
|
||||
final float newPosY = rawY - mTouchToWindowOffsetY + mHotspotY + mTouchOffsetY;
|
||||
|
||||
mCursorController.updatePosition(this, Math.round(newPosX), Math.round(newPosY));
|
||||
break;
|
||||
}
|
||||
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
mIsDragging = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
setMeasuredDimension(mHandleDrawable.getIntrinsicWidth(),
|
||||
mHandleDrawable.getIntrinsicHeight());
|
||||
}
|
||||
|
||||
public int getHandleHeight() {
|
||||
return mHandleHeight;
|
||||
}
|
||||
|
||||
public int getHandleWidth() {
|
||||
return mHandleWidth;
|
||||
}
|
||||
|
||||
public boolean isShowing() {
|
||||
if (mHandle != null)
|
||||
return mHandle.isShowing();
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isParentNull() {
|
||||
return this.getParent() == null;
|
||||
}
|
||||
|
||||
public boolean isDragging() {
|
||||
return mIsDragging;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue