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

new pinned toolbar & history view keys long press functions #691

Merged
merged 10 commits into from
May 1, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public interface KeyboardActionListener {
// TODO: change this to send an Event object instead
void onCodeInput(int primaryCode, int x, int y, boolean isKeyRepeat);

/**
* Maps a given key code point to its corresponding long-click toolbar key code.
*
* @param primaryCode The key code point to be mapped.
* @return The corresponding long-click toolbar key code.
*/
int getLongClickToolbarKeyCode(int primaryCode);

/**
* Sends a string of characters to the listener.
*
Expand Down Expand Up @@ -115,6 +123,8 @@ public void onReleaseKey(int primaryCode, boolean withSliding) {}
@Override
public void onCodeInput(int primaryCode, int x, int y, boolean isKeyRepeat) {}
@Override
public int getLongClickToolbarKeyCode(int primaryCode) {return Constants.NOT_A_CODE;}
@Override
public void onTextInput(String text) {}
@Override
public void onStartBatchInput() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
override fun onCodeInput(primaryCode: Int, x: Int, y: Int, isKeyRepeat: Boolean) =
latinIME.onCodeInput(primaryCode, x, y, isKeyRepeat)

override fun getLongClickToolbarKeyCode(primaryCode: Int): Int {
return latinIME.getLongClickToolbarKeyCode(primaryCode)
}

override fun onTextInput(text: String?) = latinIME.onTextInput(text)

override fun onStartBatchInput() = latinIME.onStartBatchInput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -49,6 +50,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
private EmojiPalettesView mEmojiPalettesView;
private View mEmojiTabStripView;
private LinearLayout mClipboardStripView;
private HorizontalScrollView mClipboardStripScrollView;
private View mSuggestionStripView;
private ClipboardHistoryView mClipboardHistoryView;
private LatinIME mLatinIME;
Expand Down Expand Up @@ -280,7 +282,7 @@ private void setMainKeyboardFrame(
mEmojiPalettesView.setVisibility(View.GONE);
mEmojiPalettesView.stopEmojiPalettes();
mEmojiTabStripView.setVisibility(View.GONE);
mClipboardStripView.setVisibility(View.GONE);
mClipboardStripScrollView.setVisibility(View.GONE);
mSuggestionStripView.setVisibility(View.VISIBLE);
mClipboardHistoryView.setVisibility(View.GONE);
mClipboardHistoryView.stopClipboardHistory();
Expand All @@ -299,7 +301,7 @@ public void setEmojiKeyboard() {
// @see LatinIME#onComputeInset(android.inputmethodservice.InputMethodService.Insets)
mKeyboardView.setVisibility(View.GONE);
mSuggestionStripView.setVisibility(View.GONE);
mClipboardStripView.setVisibility(View.GONE);
mClipboardStripScrollView.setVisibility(View.GONE);
mEmojiTabStripView.setVisibility(View.VISIBLE);
mClipboardHistoryView.setVisibility(View.GONE);
mEmojiPalettesView.startEmojiPalettes(
Expand All @@ -322,7 +324,8 @@ public void setClipboardKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mEmojiTabStripView.setVisibility(View.GONE);
mSuggestionStripView.setVisibility(View.GONE);
mClipboardStripView.setVisibility(View.VISIBLE);
mClipboardStripScrollView.post(() -> mClipboardStripScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT));
mClipboardStripScrollView.setVisibility(View.VISIBLE);
mEmojiPalettesView.setVisibility(View.GONE);
mClipboardHistoryView.startClipboardHistory(
mLatinIME.getClipboardHistoryManager(),
Expand Down Expand Up @@ -571,6 +574,7 @@ public View onCreateInputView(@NonNull Context displayContext, final boolean isH
mClipboardHistoryView.setKeyboardActionListener(mLatinIME.mKeyboardActionListener);
mEmojiTabStripView = mCurrentInputView.findViewById(R.id.emoji_tab_strip);
mClipboardStripView = mCurrentInputView.findViewById(R.id.clipboard_strip);
mClipboardStripScrollView = mCurrentInputView.findViewById(R.id.clipboard_strip_scroll_view);
mSuggestionStripView = mCurrentInputView.findViewById(R.id.suggestion_strip_view);

return mCurrentInputView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ClipboardHistoryView @JvmOverloads constructor(
attrs: AttributeSet?,
defStyle: Int = R.attr.clipboardHistoryViewStyle
) : LinearLayout(context, attrs, defStyle), View.OnTouchListener, View.OnClickListener,
ClipboardHistoryManager.OnHistoryChangeListener, OnKeyEventListener {
ClipboardHistoryManager.OnHistoryChangeListener, OnKeyEventListener, View.OnLongClickListener {

private val clipboardLayoutParams = ClipboardLayoutParams(context.resources)
private val pinIconId: Int
Expand Down Expand Up @@ -69,7 +69,7 @@ class ClipboardHistoryView @JvmOverloads constructor(
// even when state is activated, the not activated color is set
// in suggestionStripView the same thing works correctly, wtf?
// need to properly fix it (and maybe undo the inverted isActivated) when adding a toggle key
listOf(ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.COPY, ToolbarKey.CUT, ToolbarKey.CLEAR_CLIPBOARD, ToolbarKey.SELECT_WORD, ToolbarKey.SELECT_ALL, ToolbarKey.CLOSE_HISTORY)
listOf(ToolbarKey.ONE_HANDED, ToolbarKey.UNDO, ToolbarKey.UP, ToolbarKey.DOWN, ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.CLEAR_CLIPBOARD, ToolbarKey.COPY, ToolbarKey.CUT, ToolbarKey.SELECT_WORD, ToolbarKey.CLOSE_HISTORY)
.forEach { toolbarKeys.add(createToolbarKey(context, keyboardAttr, it)) }
keyboardAttr.recycle()
}
Expand Down Expand Up @@ -121,6 +121,7 @@ class ClipboardHistoryView @JvmOverloads constructor(
clipboardStrip.addView(it)
it.setOnTouchListener(this@ClipboardHistoryView)
it.setOnClickListener(this@ClipboardHistoryView)
it.setOnLongClickListener(this@ClipboardHistoryView)
colors.setColor(it, ColorType.TOOL_BAR_KEY)
colors.setBackground(it, ColorType.STRIP_BACKGROUND)
}
Expand Down Expand Up @@ -240,6 +241,26 @@ class ClipboardHistoryView @JvmOverloads constructor(
}
}

override fun onLongClick(view: View): Boolean {
val tag = view.tag
if (tag is ToolbarKey) {
val code = getCodeForToolbarKey(tag)
if (code != null) {
val longClickCode =
keyboardActionListener?.getLongClickToolbarKeyCode(code) ?: Constants.NOT_A_CODE
if (longClickCode != Constants.NOT_A_CODE) {
keyboardActionListener?.onCodeInput(
longClickCode,
Constants.SUGGESTION_STRIP_COORDINATE,
Constants.SUGGESTION_STRIP_COORDINATE,
false
)
}
}
}
return true
codokie marked this conversation as resolved.
Show resolved Hide resolved
}

override fun onKeyDown(clipId: Long) {
keyboardActionListener?.onPressKey(KeyCode.NOT_SPECIFIED, 0, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ object KeyCode {
const val CLIPBOARD_CLEAR_HISTORY = -36
const val CLIPBOARD_CLEAR_FULL_HISTORY = -37
const val CLIPBOARD_CLEAR_PRIMARY_CLIP = -38
const val CLIPBOARD_COPY_ALL = -39

const val COMPACT_LAYOUT_TO_LEFT = -111
const val COMPACT_LAYOUT_TO_RIGHT = -112
Expand Down Expand Up @@ -129,8 +130,10 @@ object KeyCode {
const val SHIFT_ENTER = -10005
const val ACTION_NEXT = -10006
const val ACTION_PREVIOUS = -10007
const val MOVE_PAGE_UP = -10008
const val MOVE_PAGE_DOWN = -10009
// Code value representing the code is not specified.
const val NOT_SPECIFIED = -10008 // todo: not sure if there is need to have the "old" unspecified keyCode different, just test it and maybe merge
const val NOT_SPECIFIED = -10010 // todo: not sure if there is need to have the "old" unspecified keyCode different, just test it and maybe merge
Copy link
Owner

Choose a reason for hiding this comment

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

Actually there is no need to have the NOT_SPECIFIED code last, you can also just leave it at -10008.
Though it would only be relevant if someone would use this code in a layout, which is quite unlikely.


/** to make sure a FlorisBoard code works when reading a JSON layout */
fun Int.checkAndConvertCode(): Int = if (this > 0) this else when (this) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/LatinIME.java
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,12 @@ public void onCodeInput(final int codePoint, final int x, final int y, final boo
onEvent(event);
}

// Maps a key code point to its corresponding long-click toolbar key code
@Override
public int getLongClickToolbarKeyCode(final int codePoint) {
return mInputLogic.getLongClickToolbarKeyCode(codePoint);
}

// This method is public for testability of LatinIME, but also in the future it should
// completely replace #onCodeInput.
public void onEvent(@NonNull final Event event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,15 @@ public void copyText() {
cm.setPrimaryClip(ClipData.newPlainText("copied text", text));
}

public void copyAllText() {
final CharSequence beforeText = getTextBeforeCursor(Integer.MAX_VALUE, InputConnection.GET_TEXT_WITH_STYLES);
final CharSequence afterText = getTextAfterCursor(Integer.MAX_VALUE, InputConnection.GET_TEXT_WITH_STYLES);
final String allText = beforeText.toString() + afterText.toString();
if (allText.isEmpty()) return;
final ClipboardManager cm = (ClipboardManager) mParent.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setPrimaryClip(ClipData.newPlainText("copied text", allText));
}

public void commitCorrection(final CorrectionInfo correctionInfo) {
if (DEBUG_BATCH_NESTING) checkBatchEdit();
if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,9 @@ private void handleFunctionalEvent(final Event event, final InputTransaction inp
case KeyCode.CLIPBOARD_COPY:
mConnection.copyText();
break;
case KeyCode.CLIPBOARD_COPY_ALL:
mConnection.copyAllText();
break;
case KeyCode.CLIPBOARD_CUT:
if (mConnection.hasSelection()) {
mConnection.copyText();
Expand Down Expand Up @@ -750,6 +753,12 @@ private void handleFunctionalEvent(final Event event, final InputTransaction inp
case KeyCode.MOVE_END_OF_LINE:
sendDownUpKeyEvent(KeyEvent.KEYCODE_MOVE_END);
break;
case KeyCode.MOVE_PAGE_UP:
sendDownUpKeyEvent(KeyEvent.KEYCODE_PAGE_UP);
break;
case KeyCode.MOVE_PAGE_DOWN:
sendDownUpKeyEvent(KeyEvent.KEYCODE_PAGE_DOWN);
break;
case KeyCode.VOICE_INPUT:
// switching to shortcut IME, shift state, keyboard,... is handled by LatinIME,
// {@link KeyboardSwitcher#onEvent(Event)}, or {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}.
Expand All @@ -769,6 +778,25 @@ private void handleFunctionalEvent(final Event event, final InputTransaction inp
throw new RuntimeException("Unknown key code : " + event.getMKeyCode());
}
}
/**
* Maps a given key code point to its corresponding long-click toolbar key code.
*
* @param codePoint The key code point to be mapped.
* @return The corresponding long-click toolbar key code.
*/
public int getLongClickToolbarKeyCode(final int codePoint) {
return switch (codePoint) {
case KeyCode.ARROW_RIGHT -> KeyCode.MOVE_END_OF_LINE;
case KeyCode.ARROW_LEFT -> KeyCode.MOVE_START_OF_LINE;
case KeyCode.ARROW_UP -> KeyCode.MOVE_PAGE_UP;
case KeyCode.ARROW_DOWN -> KeyCode.MOVE_PAGE_DOWN;
case KeyCode.UNDO -> KeyCode.REDO;
case KeyCode.REDO -> KeyCode.UNDO;
case KeyCode.CLIPBOARD_COPY -> KeyCode.CLIPBOARD_COPY_ALL;
case KeyCode.CLIPBOARD_SELECT_WORD -> KeyCode.CLIPBOARD_SELECT_ALL;
default -> Constants.NOT_A_CODE;
};
}

/**
* Handle an event that is not a functional event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
public interface Listener {
void pickSuggestionManually(SuggestedWordInfo word);
void onCodeInput(int primaryCode, int x, int y, boolean isKeyRepeat);
int getLongClickToolbarKeyCode(int codePoint);
void onTextInput(final String rawText);
void removeSuggestion(final String word);
CharSequence getSelection();
Expand Down Expand Up @@ -375,8 +376,16 @@ public boolean onLongClick(final View view) {
}

private void onLongClickToolKey(final View view) {
if (ToolbarKey.CLIPBOARD == view.getTag() && view.getParent() == mPinnedKeys) {
onLongClickClipboardKey(); // long click pinned clipboard key
if (view.getParent() == mPinnedKeys) {
final ToolbarKey tag = (ToolbarKey) view.getTag();
if (tag.equals(ToolbarKey.CLIPBOARD)) {
onLongClickClipboardKey(); // long click pinned clipboard key
} else {
final int longClickCode = mListener.getLongClickToolbarKeyCode(getCodeForToolbarKey(tag));
if (longClickCode != Constants.NOT_A_CODE) {
mListener.onCodeInput(longClickCode, Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, false);
}
}
} else if (view.getParent() == mToolbar) {
final ToolbarKey tag = (ToolbarKey) view.getTag();
final View pinnedKeyView = mPinnedKeys.findViewWithTag(tag);
Expand Down
27 changes: 17 additions & 10 deletions app/src/main/res/layout/strip_container.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
style="?attr/suggestionStripViewStyle" />
<LinearLayout
android:id="@+id/clipboard_strip"
android:orientation="horizontal"
<HorizontalScrollView
android:id="@+id/clipboard_strip_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="?attr/suggestionStripViewStyle" >
<!-- empty view to move buttons to the right -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
android:visibility="gone"
Copy link
Owner

Choose a reason for hiding this comment

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

Why did you add android:visibility="gone"? Is it necessary now for some reason?

android:scrollbarThumbHorizontal="@color/toolbar_scrollbar" >
<LinearLayout
android:id="@+id/clipboard_strip"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="?attr/suggestionStripViewStyle" >
<!-- empty view to move buttons to the right -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>