Skip to content

Commit

Permalink
Add PASTE extra key for pasting text from clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed Jul 19, 2021
1 parent 00d80b9 commit 7d76e8b
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,25 @@ public void onSessionFinished(final TerminalSession finishedSession) {
}

@Override
public void onClipboardText(TerminalSession session, String text) {
public void onCopyTextToClipboard(TerminalSession session, String text) {
if (!mActivity.isVisible()) return;

ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(new ClipData(null, new String[]{"text/plain"}, new ClipData.Item(text)));
}

@Override
public void onPasteTextFromClipboard(TerminalSession session) {
if (!mActivity.isVisible()) return;

ClipboardManager clipboard = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = clipboard.getPrimaryClip();
if (clipData != null) {
CharSequence paste = clipData.getItemAt(0).coerceToText(mActivity);
if (!TextUtils.isEmpty(paste)) mActivity.getTerminalView().mEmulator.paste(paste.toString());
}
}

@Override
public void onBell(TerminalSession session) {
if (!mActivity.isVisible()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public Object instantiateItem(@NonNull ViewGroup collection, int position) {
layout = inflater.inflate(R.layout.view_terminal_toolbar_extra_keys, collection, false);
ExtraKeysView extraKeysView = (ExtraKeysView) layout;
extraKeysView.setTermuxTerminalViewClient(mActivity.getTermuxTerminalViewClient());
extraKeysView.setTermuxTerminalSessionClient(mActivity.getTermuxTerminalSessionClient());
mActivity.setExtraKeysView(extraKeysView);
extraKeysView.reload(mActivity.getProperties().getExtraKeysInfo());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ static class CharDisplayMap extends CleverMap<String, String> {}
put("DEL", "⌦"); // U+2326 ⌦ ERASE TO THE RIGHT not well known but easy to understand
put("DRAWER", "☰"); // U+2630 ☰ TRIGRAM FOR HEAVEN not well known but easy to understand
put("KEYBOARD", "⌨"); // U+2328 ⌨ KEYBOARD not well known but easy to understand
//put("PASTE", "📋"); // U+2328 ⌨ KEYBOARD not well known but easy to understand
put("PASTE", "⎘"); // U+2328 ⌨ KEYBOARD not well known but easy to understand
}};

static final CharDisplayMap lessKnownCharactersDisplay = new CharDisplayMap() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.widget.PopupWindow;

import com.termux.R;
import com.termux.app.terminal.TermuxTerminalSessionClient;
import com.termux.app.terminal.TermuxTerminalViewClient;
import com.termux.view.TerminalView;

Expand All @@ -45,6 +46,7 @@ public final class ExtraKeysView extends GridLayout {
private static final int BUTTON_PRESSED_COLOR = 0xFF7F7F7F;

TermuxTerminalViewClient mTermuxTerminalViewClient;
TermuxTerminalSessionClient mTermuxTerminalSessionClient;

public ExtraKeysView(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down Expand Up @@ -89,6 +91,9 @@ private void sendKey(View view, String keyName, boolean forceCtrlDown, boolean f
} else if ("DRAWER".equals(keyName)) {
DrawerLayout drawer = view.findViewById(R.id.drawer_layout);
drawer.openDrawer(Gravity.LEFT);
} else if ("PASTE".equals(keyName)) {
if(mTermuxTerminalSessionClient != null)
mTermuxTerminalSessionClient.onPasteTextFromClipboard(null);
} else if (keyCodesForString.containsKey(keyName)) {
Integer keyCode = keyCodesForString.get(keyName);
if (keyCode == null) return;
Expand Down Expand Up @@ -389,4 +394,8 @@ public void setTermuxTerminalViewClient(TermuxTerminalViewClient termuxTerminalV
this.mTermuxTerminalViewClient = termuxTerminalViewClient;
}

public void setTermuxTerminalSessionClient(TermuxTerminalSessionClient termuxTerminalSessionClient) {
this.mTermuxTerminalSessionClient = termuxTerminalSessionClient;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ private void doOscSetTextParameters(String bellOrStringTerminator) {
int startIndex = textParameter.indexOf(";") + 1;
try {
String clipboardText = new String(Base64.decode(textParameter.substring(startIndex), 0), StandardCharsets.UTF_8);
mSession.clipboardText(clipboardText);
mSession.onCopyTextToClipboard(clipboardText);
} catch (Exception e) {
mClient.logError(LOG_TAG, "OSC Manipulate selection, invalid string '" + textParameter + "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public final void write(String data) {
/** Notify the terminal client that the terminal title has changed. */
public abstract void titleChanged(String oldTitle, String newTitle);

/** Notify the terminal client that the terminal title has changed. */
public abstract void clipboardText(String text);
/** Notify the terminal client that text should be copied to clipboard. */
public abstract void onCopyTextToClipboard(String text);

/** Notify the terminal client that text should be pasted from clipboard. */
public abstract void onPasteTextFromClipboard();

/** Notify the terminal client that a bell character (ASCII 7, bell, BEL, \a, ^G)) has been received. */
public abstract void onBell();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,13 @@ public synchronized int getExitStatus() {
}

@Override
public void clipboardText(String text) {
mClient.onClipboardText(this, text);
public void onCopyTextToClipboard(String text) {
mClient.onCopyTextToClipboard(this, text);
}

@Override
public void onPasteTextFromClipboard() {
mClient.onPasteTextFromClipboard(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public interface TerminalSessionClient {

void onSessionFinished(TerminalSession finishedSession);

void onClipboardText(TerminalSession session, String text);
void onCopyTextToClipboard(TerminalSession session, String text);

void onPasteTextFromClipboard(TerminalSession session);

void onBell(TerminalSession session);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ public void titleChanged(String oldTitle, String newTitle) {
}

@Override
public void clipboardText(String text) {
public void onCopyTextToClipboard(String text) {
clipboardPuts.add(text);
}

@Override
public void onPasteTextFromClipboard() {
}

@Override
public void onBell() {
bellsRung++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,12 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case ACTION_COPY:
String selectedText = terminalView.mEmulator.getSelectedText(mSelX1, mSelY1, mSelX2, mSelY2).trim();
terminalView.mTermSession.clipboardText(selectedText);
terminalView.mTermSession.onCopyTextToClipboard(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());
}
terminalView.mTermSession.onPasteTextFromClipboard();
break;
case ACTION_MORE:
terminalView.stopTextSelectionMode(); //we stop text selection first, otherwise handles will show above popup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public void onSessionFinished(final TerminalSession finishedSession) {
}

@Override
public void onClipboardText(TerminalSession session, String text) {
public void onCopyTextToClipboard(TerminalSession session, String text) {
}

@Override
public void onPasteTextFromClipboard(TerminalSession session) {
}

@Override
Expand Down

2 comments on commit 7d76e8b

@ChiefMikeK
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe fix the Unicode comments in ExtraKeysInfo.java?
119 //put("PASTE", "📋"); // U+1F4CB CLIPBOARD
120 put("PASTE", "⎘"); // U+2398 NEXT PAGE not well known but easy to understand

@agnostic-apollo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was already fixed in 9febca9

Please sign in to comment.