allow customizing toolbar keys

This commit is contained in:
Helium314 2023-12-28 22:47:31 +01:00
parent 26168b0e09
commit b5d68986be
7 changed files with 87 additions and 15 deletions

View file

@ -25,6 +25,7 @@ import org.dslul.openboard.inputmethod.latin.AudioAndHapticFeedbackManager;
import org.dslul.openboard.inputmethod.latin.R;
import org.dslul.openboard.inputmethod.latin.RichInputMethodManager;
import org.dslul.openboard.inputmethod.latin.utils.MoreKeysUtilsKt;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarUtilsKt;
import kotlin.collections.ArraysKt;
@ -77,6 +78,10 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
MoreKeysUtilsKt.reorderMoreKeysDialog(requireContext(), Settings.PREF_MORE_KEYS_LABELS_ORDER, MoreKeysUtilsKt.MORE_KEYS_LABEL_DEFAULT, R.string.hint_source);
return true;
});
findPreference(Settings.PREF_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
MoreKeysUtilsKt.reorderMoreKeysDialog(requireContext(), Settings.PREF_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultToolbarPref(), R.string.toolbar_keys);
return true;
});
}
@Override
@ -89,7 +94,7 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
refreshEnablingsOfKeypressSoundAndVibrationAndHistRetentionSettings();
if (key == null) return;
switch (key) {
case Settings.PREF_MORE_KEYS_ORDER, Settings.PREF_SHOW_POPUP_HINTS, Settings.PREF_SHOW_NUMBER_ROW, Settings.PREF_MORE_KEYS_LABELS_ORDER
case Settings.PREF_MORE_KEYS_ORDER, Settings.PREF_SHOW_POPUP_HINTS, Settings.PREF_SHOW_NUMBER_ROW, Settings.PREF_MORE_KEYS_LABELS_ORDER, "toolbar"
-> mReloadKeyboard = true;
case Settings.PREF_LOCALIZED_NUMBER_ROW -> KeyboardLayoutSet.onSystemLocaleChanged();
case Settings.PREF_SHOW_HINTS

View file

@ -134,6 +134,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG = "pref_dont_show_missing_dict_dialog";
public static final String PREF_PINNED_KEYS = "pref_pinned_keys";
public static final String PREF_TOOLBAR_KEYS = "pref_toolbar_keys";
// Emoji
public static final String PREF_EMOJI_RECENT_KEYS = "emoji_recent_keys";
@ -184,6 +185,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
mRes = context.getResources();
mPrefs = DeviceProtectedUtils.getSharedPreferences(context);
mPrefs.registerOnSharedPreferenceChangeListener(this);
ToolbarUtilsKt.upgradeToolbarPref(mPrefs);
}
public void onDestroy() {

View file

@ -59,6 +59,7 @@ import org.dslul.openboard.inputmethod.latin.suggestions.MoreSuggestionsView.Mor
import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils;
import org.dslul.openboard.inputmethod.latin.utils.DialogUtils;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarKey;
import org.dslul.openboard.inputmethod.latin.utils.ToolbarUtilsKt;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
@ -78,10 +79,6 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
public static boolean DEBUG_SUGGESTIONS;
private static final float DEBUG_INFO_TEXT_SIZE_IN_DIP = 6.5f;
// tags of keys to be added to toolbar, in order (all tags must be considered in getStyleableIconId)
private static final ToolbarKey[] toolbarKeys = new ToolbarKey[] {ToolbarKey.VOICE, ToolbarKey.CLIPBOARD,
ToolbarKey.SELECT_ALL, ToolbarKey.COPY, ToolbarKey.UNDO, ToolbarKey.REDO, ToolbarKey.ONE_HANDED,
ToolbarKey.SETTINGS, ToolbarKey.LEFT, ToolbarKey.RIGHT, ToolbarKey.UP, ToolbarKey.DOWN};
private final ViewGroup mSuggestionsStrip;
private final ImageButton mToolbarExpandKey;
@ -191,7 +188,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
getResources().getDimensionPixelSize(R.dimen.config_suggestions_strip_edge_key_width),
LinearLayout.LayoutParams.MATCH_PARENT
);
for (final ToolbarKey key : toolbarKeys) {
for (final ToolbarKey key : ToolbarUtilsKt.getEnabledToolbarKeys(DeviceProtectedUtils.getSharedPreferences(context))) {
final ImageButton button = createToolbarKey(context, keyboardAttr, key);
button.setLayoutParams(toolbarKeyLayoutParams);
setupKey(button, colors);
@ -217,7 +214,9 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mToolbarExpandKey.getLayoutParams().width *= 0.82;
for (final ToolbarKey pinnedKey : Settings.getInstance().getCurrent().mPinnedKeys) {
mToolbar.findViewWithTag(pinnedKey).setBackground(mEnabledToolKeyBackground);
final View pinnedKeyInToolbar = mToolbar.findViewWithTag(pinnedKey);
if (pinnedKeyInToolbar == null) continue;
pinnedKeyInToolbar.setBackground(mEnabledToolKeyBackground);
addKeyToPinnedKeys(pinnedKey);
}
@ -236,7 +235,9 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
final int visibility = shouldBeVisible ? VISIBLE : (isFullscreenMode ? GONE : INVISIBLE);
setVisibility(visibility);
final SettingsValues currentSettingsValues = Settings.getInstance().getCurrent();
mToolbar.findViewWithTag(ToolbarKey.VOICE).setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE);
final View toolbarVoiceKey = mToolbar.findViewWithTag(ToolbarKey.VOICE);
if (toolbarVoiceKey != null)
toolbarVoiceKey.setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE);
final View pinnedVoiceKey = mPinnedKeys.findViewWithTag(ToolbarKey.VOICE);
if (pinnedVoiceKey != null)
pinnedVoiceKey.setVisibility(currentSettingsValues.mShowsVoiceInputKey ? VISIBLE : GONE);
@ -662,7 +663,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
copy.setScaleType(original.getScaleType());
copy.setScaleX(original.getScaleX());
copy.setScaleY(original.getScaleY());
copy.setContentDescription(original.getContentDescription()); // todo (later): add some content description
copy.setContentDescription(original.getContentDescription());
copy.setImageDrawable(original.getDrawable());
copy.setLayoutParams(original.getLayoutParams());
setupKey(copy, Settings.getInstance().getCurrent().mColors);

View file

@ -117,7 +117,7 @@ fun reorderMoreKeysDialog(context: Context, key: String, defaultSetting: String,
}
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
val (text, wasChecked) = orderedItems[p1]
val displayTextId = context.resources.getIdentifier(text, "string", context.packageName)
val displayTextId = context.resources.getIdentifier(text.lowercase(), "string", context.packageName)
val displayText = if (displayTextId == 0) text else context.getString(displayTextId)
p0.itemView.findViewById<TextView>(R.id.morekeys_type)?.text = displayText
val switch = p0.itemView.findViewById<SwitchCompat>(R.id.morekeys_switch)

View file

@ -1,9 +1,12 @@
// SPDX-License-Identifier: GPL-3.0-only
package org.dslul.openboard.inputmethod.latin.utils
import android.content.Context
import android.content.SharedPreferences
import android.content.res.TypedArray
import android.widget.ImageButton
import android.widget.ImageView
import androidx.core.content.edit
import org.dslul.openboard.inputmethod.latin.R
import org.dslul.openboard.inputmethod.latin.common.Constants.*
import org.dslul.openboard.inputmethod.latin.settings.Settings
@ -14,6 +17,9 @@ fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey
val button = ImageButton(context, null, R.attr.suggestionWordStyle)
button.scaleType = ImageView.ScaleType.CENTER
button.tag = key
val contentDescriptionId = context.resources.getIdentifier(key.name.lowercase(), "string", context.packageName)
if (contentDescriptionId != 0)
button.contentDescription = context.getString(contentDescriptionId)
val icon = keyboardAttr.getDrawable(getStyleableIconId(key))
if (key == LEFT || key == RIGHT || key == UP || key == DOWN) {
// arrows look a little awkward when not scaled
@ -56,8 +62,45 @@ private fun getStyleableIconId(key: ToolbarKey) = when (key) {
CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey
}
// names need to be aligned with resources strings (using lowercase of key.name)
enum class ToolbarKey {
VOICE, CLIPBOARD, CLEAR_CLIPBOARD, SETTINGS, SELECT_ALL, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, UNDO, REDO
}
fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name }
val defaultToolbarPref = entries.joinToString(";") { if (it != CLEAR_CLIPBOARD) "${it.name},true" else "${it.name},false" }
/** add missing keys, typically because a new key has been added */
fun upgradeToolbarPref(prefs: SharedPreferences) {
val list = prefs.getString(Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref)!!.split(";").toMutableList()
if (list.size == ToolbarKey.entries.size) return
ToolbarKey.entries.forEach { key ->
if (list.none { it.startsWith("${key.name},") })
list.add("${key.name},true")
}
// likely not needed, but better prepare for possibility of key removal
list.removeAll {
try {
ToolbarKey.valueOf(it.substringBefore(","))
false
} catch (_: IllegalArgumentException) {
true
}
}
prefs.edit { putString(Settings.PREF_TOOLBAR_KEYS, list.joinToString(";")) }
}
fun getEnabledToolbarKeys(prefs: SharedPreferences): List<ToolbarKey> {
val string = prefs.getString("toolbar", defaultToolbarPref)!!
return string.split(";").mapNotNull {
val split = it.split(",")
if (split.last() == "true") {
try {
ToolbarKey.valueOf(split.first())
} catch (_: IllegalArgumentException) {
null
}
} else null
}
}

View file

@ -238,11 +238,28 @@
<string name="hint_source">Select hint source</string>
<!-- Title of the settings to set popup key order -->
<string name="popup_order">Select popup key order</string>
<string name="more_keys_number">Number row</string>
<string name="more_keys_language">Language</string>
<string name="more_keys_language_priority">Language (priority)</string>
<string name="more_keys_layout">Layout</string>
<string name="more_keys_symbols">Symbols</string>
<!-- Names of the more key classes -->
<string name="more_keys_number" tools:keep="@string/more_keys_number">Number row</string>
<string name="more_keys_language" tools:keep="@string/more_keys_language">Language</string>
<string name="more_keys_language_priority" tools:keep="@string/more_keys_language_priority">Language (priority)</string>
<string name="more_keys_layout" tools:keep="@string/more_keys_layout">Layout</string>
<string name="more_keys_symbols" tools:keep="@string/more_keys_symbols">Symbols</string>
<!-- Title of the settings to set toolbar keys -->
<string name="toolbar_keys">Select toolbar keys</string>
<!-- Names of the toolbar keys-->
<string name="copy" tools:keep="@string/copy">Copy</string>
<string name="clipboard" tools:keep="@string/clipboard">Clipboard</string>
<string name="clear_clipboard" tools:keep="@string/clear_clipboard">Clear clipboard</string>
<string name="voice" tools:keep="@string/voice">Voice input</string>
<string name="settings" tools:keep="@string/settings">Settings</string>
<string name="select_all" tools:keep="@string/select_all">Select all</string>
<string name="one_handed" tools:keep="@string/one_handed">One-handed mode</string>
<string name="left" tools:keep="@string/left">Left</string>
<string name="right" tools:keep="@string/right">Right</string>
<string name="up" tools:keep="@string/up">Up</string>
<string name="down" tools:keep="@string/down">Down</string>
<string name="undo" tools:keep="@string/undo">Undo</string>
<string name="redo" tools:keep="@string/redo">Redo</string>
<!-- Title of the settings to show "..." as hints for more functionality on long-press -->
<string name="show_popup_hints">Show functional hints</string>
<!-- Description of the show_popup_hints setting -->

View file

@ -94,6 +94,10 @@
android:defaultValue="false"
android:persistent="true" />
<Preference
android:key="pref_toolbar_keys"
android:title="@string/toolbar_keys" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_category_clipboard_history">