add home, end and select word toolbar keys, fixes #409

This commit is contained in:
Helium314 2024-01-18 10:18:22 +01:00
parent e8e4354600
commit c9d52e8090
13 changed files with 74 additions and 6 deletions

View file

@ -68,7 +68,7 @@ class ClipboardHistoryView @JvmOverloads constructor(
// even when state is activated, the not activated color is set // even when state is activated, the not activated color is set
// in suggestionStripView the same thing works correctly, wtf? // in suggestionStripView the same thing works correctly, wtf?
// need to properly fix it (and maybe undo the inverted isActivated) when adding a toggle key // need to properly fix it (and maybe undo the inverted isActivated) when adding a toggle key
listOf(ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.COPY, ToolbarKey.SELECT_ALL, ToolbarKey.CLEAR_CLIPBOARD, ToolbarKey.ONE_HANDED) listOf(ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.COPY, ToolbarKey.SELECT_WORD, ToolbarKey.SELECT_ALL, ToolbarKey.CLEAR_CLIPBOARD, ToolbarKey.ONE_HANDED)
.forEach { toolbarKeys.add(createToolbarKey(context, keyboardAttr, it)) } .forEach { toolbarKeys.add(createToolbarKey(context, keyboardAttr, it)) }
keyboardAttr.recycle() keyboardAttr.recycle()
} }

View file

@ -641,8 +641,16 @@ public final class RichInputConnection implements PrivateCommandPerformer {
} }
public void selectAll() { public void selectAll() {
if (!isConnected()) return;
mIC.performContextMenuAction(android.R.id.selectAll); mIC.performContextMenuAction(android.R.id.selectAll);
// the rest is done via LatinIME.onUpdateSelection }
public void selectWord(final SpacingAndPunctuations spacingAndPunctuations, final int scriptId) {
if (!isConnected()) return;
if (mExpectedSelStart != mExpectedSelEnd) return; // already something selected
final TextRange range = getWordRangeAtCursor(spacingAndPunctuations, scriptId, false);
if (range == null) return;
mIC.setSelection(mExpectedSelStart - range.getNumberOfCharsInWordBeforeCursor(), mExpectedSelStart + range.getNumberOfCharsInWordAfterCursor());
} }
public void copyText() { public void copyText() {

View file

@ -8,7 +8,6 @@ package org.dslul.openboard.inputmethod.latin.common;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.dslul.openboard.inputmethod.annotations.UsedForTesting;
import org.dslul.openboard.inputmethod.latin.BuildConfig; import org.dslul.openboard.inputmethod.latin.BuildConfig;
public final class Constants { public final class Constants {
@ -229,6 +228,9 @@ public final class Constants {
public static final int CODE_REDO = -30; public static final int CODE_REDO = -30;
public static final int CODE_TOGGLE_AUTOCORRECT = -31; public static final int CODE_TOGGLE_AUTOCORRECT = -31;
public static final int CODE_TOGGLE_INCOGNITO = -32; public static final int CODE_TOGGLE_INCOGNITO = -32;
public static final int CODE_HOME = -33;
public static final int CODE_END = -34;
public static final int CODE_SELECT_WORD = -35;
// Code value representing the code is not specified. // Code value representing the code is not specified.
public static final int CODE_UNSPECIFIED = -200; public static final int CODE_UNSPECIFIED = -200;

View file

@ -709,6 +709,9 @@ public final class InputLogic {
case Constants.CODE_SELECT_ALL: case Constants.CODE_SELECT_ALL:
mConnection.selectAll(); mConnection.selectAll();
break; break;
case Constants.CODE_SELECT_WORD:
mConnection.selectWord(inputTransaction.getMSettingsValues().mSpacingAndPunctuations, currentKeyboardScriptId);
break;
case Constants.CODE_COPY: case Constants.CODE_COPY:
mConnection.copyText(); mConnection.copyText();
break; break;
@ -730,6 +733,12 @@ public final class InputLogic {
case Constants.CODE_REDO: case Constants.CODE_REDO:
sendDownUpKeyEventWithMetaState(KeyEvent.KEYCODE_Z, KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON); sendDownUpKeyEventWithMetaState(KeyEvent.KEYCODE_Z, KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON);
break; break;
case Constants.CODE_HOME:
sendDownUpKeyEvent(KeyEvent.KEYCODE_MOVE_HOME);
break;
case Constants.CODE_END:
sendDownUpKeyEvent(KeyEvent.KEYCODE_MOVE_END);
break;
case Constants.CODE_SHORTCUT: case Constants.CODE_SHORTCUT:
// switching to shortcut IME, shift state, keyboard,... is handled by LatinIME, // 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)}. // {@link KeyboardSwitcher#onEvent(Event)}, or {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}.

View file

@ -11,7 +11,6 @@ import org.dslul.openboard.inputmethod.latin.R
import org.dslul.openboard.inputmethod.latin.common.Constants.* import org.dslul.openboard.inputmethod.latin.common.Constants.*
import org.dslul.openboard.inputmethod.latin.settings.Settings import org.dslul.openboard.inputmethod.latin.settings.Settings
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey.* import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey.*
import java.util.EnumSet
fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey): ImageButton { fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey): ImageButton {
val button = ImageButton(context, null, R.attr.suggestionWordStyle) val button = ImageButton(context, null, R.attr.suggestionWordStyle)
@ -50,6 +49,9 @@ fun getCodeForToolbarKey(key: ToolbarKey) = when (key) {
REDO -> CODE_REDO REDO -> CODE_REDO
INCOGNITO -> CODE_TOGGLE_INCOGNITO INCOGNITO -> CODE_TOGGLE_INCOGNITO
AUTOCORRECT -> CODE_TOGGLE_AUTOCORRECT AUTOCORRECT -> CODE_TOGGLE_AUTOCORRECT
FULL_LEFT -> CODE_HOME
FULL_RIGHT -> CODE_END
SELECT_WORD -> CODE_SELECT_WORD
CLEAR_CLIPBOARD -> null // not managed via code input CLEAR_CLIPBOARD -> null // not managed via code input
} }
@ -69,18 +71,22 @@ private fun getStyleableIconId(key: ToolbarKey) = when (key) {
INCOGNITO -> R.styleable.Keyboard_iconIncognitoKey INCOGNITO -> R.styleable.Keyboard_iconIncognitoKey
AUTOCORRECT -> R.styleable.Keyboard_iconLanguageSwitchKey AUTOCORRECT -> R.styleable.Keyboard_iconLanguageSwitchKey
CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey
FULL_LEFT -> R.styleable.Keyboard_iconFullLeft
FULL_RIGHT -> R.styleable.Keyboard_iconFullRight
SELECT_WORD -> R.styleable.Keyboard_iconSelectWord
} }
// names need to be aligned with resources strings (using lowercase of key.name) // names need to be aligned with resources strings (using lowercase of key.name)
enum class ToolbarKey { enum class ToolbarKey {
VOICE, CLIPBOARD, UNDO, REDO, SETTINGS, SELECT_ALL, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, INCOGNITO, AUTOCORRECT, CLEAR_CLIPBOARD VOICE, CLIPBOARD, UNDO, REDO, SETTINGS, SELECT_ALL, SELECT_WORD, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN,
FULL_LEFT, FULL_RIGHT, INCOGNITO, AUTOCORRECT, CLEAR_CLIPBOARD
} }
fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name } fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name }
val defaultToolbarPref = entries.filterNot { it == CLEAR_CLIPBOARD }.joinToString(";") { val defaultToolbarPref = entries.filterNot { it == CLEAR_CLIPBOARD }.joinToString(";") {
when (it) { when (it) {
INCOGNITO, AUTOCORRECT, UP, DOWN, ONE_HANDED -> "${it.name},false" INCOGNITO, AUTOCORRECT, UP, DOWN, ONE_HANDED, FULL_LEFT, FULL_RIGHT -> "${it.name},false"
else -> "${it.name},true" else -> "${it.name},true"
} }
} }

View file

@ -0,0 +1,12 @@
<!--
icon from pictogrammers.com
SPDX-License-Identifier: Apache-2.0
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportHeight="24"
android:viewportWidth="24" >
<path android:fillColor="#FFF"
android:pathData="M4,3H5V5H3V4A1,1 0,0 1,4 3M20,3A1,1 0,0 1,21 4V5H19V3H20M15,5V3H17V5H15M11,5V3H13V5H11M7,5V3H9V5H7M21,20A1,1 0,0 1,20 21H19V19H21V20M15,21V19H17V21H15M11,21V19H13V21H11M7,21V19H9V21H7M4,21A1,1 0,0 1,3 20V19H5V21H4M3,15H5V17H3V15M21,15V17H19V15H21M3,11H5V13H3V11M21,11V13H19V11H21M3,7H5V9H3V7M21,7V9H19V7H21Z"/>
</vector>

View file

@ -0,0 +1,12 @@
<!--
icon available in Android Studio
SPDX-License-Identifier: Apache-2.0
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportHeight="24"
android:viewportWidth="24" >
<path android:fillColor="#FFF"
android:pathData="M5.59,7.41L10.18,12l-4.59,4.59L7,18l6,-6 -6,-6zM16,6h2v12h-2z"/>
</vector>

View file

@ -0,0 +1,4 @@
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="180"
android:drawable="@drawable/ic_to_end">
</rotate>

View file

@ -271,6 +271,9 @@
<attr name="iconArrowRight" format="reference" /> <attr name="iconArrowRight" format="reference" />
<attr name="iconArrowUp" format="reference" /> <attr name="iconArrowUp" format="reference" />
<attr name="iconArrowDown" format="reference" /> <attr name="iconArrowDown" format="reference" />
<attr name="iconFullLeft" format="reference" />
<attr name="iconFullRight" format="reference" />
<attr name="iconSelectWord" format="reference" />
<attr name="iconBin" format="reference" /> <attr name="iconBin" format="reference" />
<attr name="iconUndo" format="reference" /> <attr name="iconUndo" format="reference" />
<attr name="iconRedo" format="reference" /> <attr name="iconRedo" format="reference" />

View file

@ -43,5 +43,8 @@
<item name="iconBin">@drawable/ic_delete</item> <item name="iconBin">@drawable/ic_delete</item>
<item name="iconUndo">@drawable/ic_undo</item> <item name="iconUndo">@drawable/ic_undo</item>
<item name="iconRedo">@drawable/ic_redo</item> <item name="iconRedo">@drawable/ic_redo</item>
<item name="iconFullLeft">@drawable/ic_to_start</item>
<item name="iconFullRight">@drawable/ic_to_end</item>
<item name="iconSelectWord">@drawable/ic_select</item>
</style> </style>
</resources> </resources>

View file

@ -48,5 +48,8 @@
<item name="iconBin">@drawable/ic_delete</item> <item name="iconBin">@drawable/ic_delete</item>
<item name="iconUndo">@drawable/ic_undo</item> <item name="iconUndo">@drawable/ic_undo</item>
<item name="iconRedo">@drawable/ic_redo</item> <item name="iconRedo">@drawable/ic_redo</item>
<item name="iconFullLeft">@drawable/ic_to_start</item>
<item name="iconFullRight">@drawable/ic_to_end</item>
<item name="iconSelectWord">@drawable/ic_select</item>
</style> </style>
</resources> </resources>

View file

@ -47,5 +47,8 @@
<item name="iconBin">@drawable/ic_delete_rounded</item> <item name="iconBin">@drawable/ic_delete_rounded</item>
<item name="iconUndo">@drawable/ic_undo_rounded</item> <item name="iconUndo">@drawable/ic_undo_rounded</item>
<item name="iconRedo">@drawable/ic_redo_rounded</item> <item name="iconRedo">@drawable/ic_redo_rounded</item>
<item name="iconFullLeft">@drawable/ic_to_start</item>
<item name="iconFullRight">@drawable/ic_to_end</item>
<item name="iconSelectWord">@drawable/ic_select</item>
</style> </style>
</resources> </resources>

View file

@ -242,7 +242,10 @@
<string name="voice" tools:keep="@string/voice">Voice input</string> <string name="voice" tools:keep="@string/voice">Voice input</string>
<string name="settings" tools:keep="@string/settings">Settings</string> <string name="settings" tools:keep="@string/settings">Settings</string>
<string name="select_all" tools:keep="@string/select_all" translatable="false">@android:string/selectAll</string> <string name="select_all" tools:keep="@string/select_all" translatable="false">@android:string/selectAll</string>
<string name="select_word" tools:keep="@string/select_word">Select word</string>
<string name="one_handed" tools:keep="@string/one_handed">One-handed mode</string> <string name="one_handed" tools:keep="@string/one_handed">One-handed mode</string>
<string name="full_left" tools:keep="@string/foll_left">Full left</string>
<string name="full_right" tools:keep="@string/full_right">Full right</string>
<string name="left" tools:keep="@string/left">Left</string> <string name="left" tools:keep="@string/left">Left</string>
<string name="right" tools:keep="@string/right">Right</string> <string name="right" tools:keep="@string/right">Right</string>
<string name="up" tools:keep="@string/up">Up</string> <string name="up" tools:keep="@string/up">Up</string>