mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-06-11 00:49:33 +00:00
make long-press toolbar pinning optional
This commit is contained in:
parent
0f503389b3
commit
388366c242
10 changed files with 98 additions and 58 deletions
|
@ -11,6 +11,8 @@ import helium314.keyboard.latin.settings.USER_DICTIONARY_SUFFIX
|
|||
import helium314.keyboard.latin.utils.CUSTOM_LAYOUT_PREFIX
|
||||
import helium314.keyboard.latin.utils.DeviceProtectedUtils
|
||||
import helium314.keyboard.latin.utils.DictionaryInfoUtils
|
||||
import helium314.keyboard.latin.utils.ToolbarKey
|
||||
import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
|
||||
import helium314.keyboard.latin.utils.getCustomLayoutFile
|
||||
import helium314.keyboard.latin.utils.onCustomLayoutFileListChanged
|
||||
import helium314.keyboard.latin.utils.upgradeToolbarPrefs
|
||||
|
@ -39,7 +41,6 @@ fun checkVersionUpgrade(context: Context) {
|
|||
val oldVersion = prefs.getInt(Settings.PREF_VERSION_CODE, 0)
|
||||
if (oldVersion == BuildConfig.VERSION_CODE)
|
||||
return
|
||||
upgradeToolbarPrefs(prefs)
|
||||
// clear extracted dictionaries, in case updated version contains newer ones
|
||||
DictionaryInfoUtils.getCachedDirectoryList(context)?.forEach {
|
||||
if (!it.isDirectory) return@forEach
|
||||
|
@ -73,6 +74,22 @@ fun checkVersionUpgrade(context: Context) {
|
|||
putString(Settings.PREF_SELECTED_SUBTYPE, selectedSubtype)
|
||||
}
|
||||
}
|
||||
if (oldVersion <= 2000) {
|
||||
// upgrade pinned toolbar keys pref
|
||||
val oldPinnedKeysPref = prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, "")!!
|
||||
val pinnedKeys = oldPinnedKeysPref.split(";").mapNotNull {
|
||||
try {
|
||||
ToolbarKey.valueOf(it)
|
||||
} catch (_: IllegalArgumentException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
val newPinnedKeysPref = (pinnedKeys.map { "${it.name},true" } + defaultPinnedToolbarPref.split(";"))
|
||||
.distinctBy { it.split(",").first() }
|
||||
.joinToString(";")
|
||||
prefs.edit { putString(Settings.PREF_PINNED_TOOLBAR_KEYS, newPinnedKeysPref) }
|
||||
}
|
||||
upgradeToolbarPrefs(prefs)
|
||||
onCustomLayoutFileListChanged() // just to be sure
|
||||
prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) }
|
||||
}
|
||||
|
|
|
@ -71,9 +71,14 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
|
|||
R.string.toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
||||
return true;
|
||||
});
|
||||
findPreference(Settings.PREF_PINNED_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
|
||||
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultPinnedToolbarPref(),
|
||||
R.string.pinned_toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
||||
return true;
|
||||
});
|
||||
findPreference(Settings.PREF_CLIPBOARD_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> {
|
||||
DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultClipboardToolbarPref(),
|
||||
R.string.toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
||||
R.string.clipboard_toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext()));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
@ -89,7 +94,8 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
|
|||
if (key == null) return;
|
||||
switch (key) {
|
||||
case Settings.PREF_POPUP_KEYS_ORDER, Settings.PREF_SHOW_POPUP_HINTS, Settings.PREF_SHOW_NUMBER_ROW,
|
||||
Settings.PREF_POPUP_KEYS_LABELS_ORDER, Settings.PREF_TOOLBAR_KEYS, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS
|
||||
Settings.PREF_POPUP_KEYS_LABELS_ORDER, Settings.PREF_TOOLBAR_KEYS, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS,
|
||||
Settings.PREF_PINNED_TOOLBAR_KEYS, Settings.PREF_QUICK_PIN_TOOLBAR_KEYS
|
||||
-> mReloadKeyboard = true;
|
||||
case Settings.PREF_LOCALIZED_NUMBER_ROW -> KeyboardLayoutSet.onSystemLocaleChanged();
|
||||
case Settings.PREF_SHOW_HINTS
|
||||
|
|
|
@ -44,8 +44,6 @@ import helium314.keyboard.latin.utils.ResourceUtils;
|
|||
import helium314.keyboard.latin.utils.RunInLocaleKt;
|
||||
import helium314.keyboard.latin.utils.StatsUtils;
|
||||
import helium314.keyboard.latin.utils.SubtypeSettingsKt;
|
||||
import helium314.keyboard.latin.utils.ToolbarKey;
|
||||
import helium314.keyboard.latin.utils.ToolbarUtilsKt;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
|
@ -148,6 +146,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
public static final String PREF_USE_SYSTEM_LOCALES = "use_system_locales";
|
||||
public static final String PREF_URL_DETECTION = "url_detection";
|
||||
public static final String PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG = "dont_show_missing_dict_dialog";
|
||||
public static final String PREF_QUICK_PIN_TOOLBAR_KEYS = "quick_pin_toolbar_keys";
|
||||
public static final String PREF_PINNED_TOOLBAR_KEYS = "pinned_toolbar_keys";
|
||||
public static final String PREF_TOOLBAR_KEYS = "toolbar_keys";
|
||||
public static final String PREF_CLIPBOARD_TOOLBAR_KEYS = "clipboard_toolbar_keys";
|
||||
|
@ -486,29 +485,6 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
}
|
||||
}
|
||||
|
||||
public static ArrayList<ToolbarKey> readPinnedKeys(final SharedPreferences prefs) {
|
||||
final ArrayList<ToolbarKey> list = new ArrayList<>();
|
||||
for (final String key : prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, "").split(";")) {
|
||||
try {
|
||||
list.add(ToolbarKey.valueOf(key));
|
||||
} catch (IllegalArgumentException ignored) { } // may happen if toolbar key is removed from app
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void addPinnedKey(final SharedPreferences prefs, final ToolbarKey key) {
|
||||
final ArrayList<ToolbarKey> keys = readPinnedKeys(prefs);
|
||||
if (keys.contains(key)) return;
|
||||
keys.add(key);
|
||||
prefs.edit().putString(Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.toToolbarKeyString(keys)).apply();
|
||||
}
|
||||
|
||||
public static void removePinnedKey(final SharedPreferences prefs, final ToolbarKey key) {
|
||||
final ArrayList<ToolbarKey> keys = readPinnedKeys(prefs);
|
||||
keys.remove(key);
|
||||
prefs.edit().putString(Settings.PREF_PINNED_TOOLBAR_KEYS, ToolbarUtilsKt.toToolbarKeyString(keys)).apply();
|
||||
}
|
||||
|
||||
public static int readMorePopupKeysPref(final SharedPreferences prefs) {
|
||||
return switch (prefs.getString(Settings.PREF_MORE_POPUP_KEYS, "normal")) {
|
||||
case "all" -> LocaleKeyboardInfosKt.POPUP_KEYS_ALL;
|
||||
|
|
|
@ -99,6 +99,7 @@ public class SettingsValues {
|
|||
public final boolean mEnableEmojiAltPhysicalKey;
|
||||
public final boolean mIsSplitKeyboardEnabled;
|
||||
public final float mSplitKeyboardSpacerRelativeWidth;
|
||||
public final boolean mQuickPinToolbarKeys;
|
||||
public final int mScreenMetrics;
|
||||
public final boolean mAddToPersonalDictionary;
|
||||
public final boolean mUseContactsDictionary;
|
||||
|
@ -181,6 +182,7 @@ public class SettingsValues {
|
|||
mSplitKeyboardSpacerRelativeWidth = mIsSplitKeyboardEnabled
|
||||
? Math.min(Math.max((displayWidthDp - 600) / 6000f + 0.15f, 0.15f), 0.25f) * prefs.getFloat(Settings.PREF_SPLIT_SPACER_SCALE, DEFAULT_SIZE_SCALE)
|
||||
: 0f;
|
||||
mQuickPinToolbarKeys = prefs.getBoolean(Settings.PREF_QUICK_PIN_TOOLBAR_KEYS, false);
|
||||
mScreenMetrics = Settings.readScreenMetrics(res);
|
||||
|
||||
// Compute other readable settings
|
||||
|
|
|
@ -217,13 +217,13 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
|
|||
mToolbarExpandKey.getLayoutParams().height *= 0.82; // shrink the whole key a little (drawable not affected)
|
||||
mToolbarExpandKey.getLayoutParams().width *= 0.82;
|
||||
|
||||
for (final ToolbarKey pinnedKey : Settings.readPinnedKeys(prefs)) {
|
||||
for (final ToolbarKey pinnedKey : ToolbarUtilsKt.getPinnedToolbarKeys(prefs)) {
|
||||
final ImageButton button = createToolbarKey(context, keyboardAttr, pinnedKey);
|
||||
button.setLayoutParams(toolbarKeyLayoutParams);
|
||||
setupKey(button, colors);
|
||||
mPinnedKeys.addView(button);
|
||||
final View pinnedKeyInToolbar = mToolbar.findViewWithTag(pinnedKey);
|
||||
if (pinnedKeyInToolbar != null)
|
||||
if (pinnedKeyInToolbar != null && Settings.getInstance().getCurrent().mQuickPinToolbarKeys)
|
||||
pinnedKeyInToolbar.setBackground(mEnabledToolKeyBackground);
|
||||
}
|
||||
|
||||
|
@ -374,7 +374,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
|
|||
|
||||
private void onLongClickToolKey(final View view) {
|
||||
if (!(view.getTag() instanceof ToolbarKey tag)) return;
|
||||
if (view.getParent() == mPinnedKeys) {
|
||||
if (view.getParent() == mPinnedKeys || !Settings.getInstance().getCurrent().mQuickPinToolbarKeys) {
|
||||
final int longClickCode = getCodeForToolbarKeyLongClick(tag);
|
||||
if (longClickCode != KeyCode.UNSPECIFIED) {
|
||||
mListener.onCodeInput(longClickCode, Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, false);
|
||||
|
@ -384,9 +384,9 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
|
|||
if (pinnedKeyView == null) {
|
||||
addKeyToPinnedKeys(tag);
|
||||
mToolbar.findViewWithTag(tag).setBackground(mEnabledToolKeyBackground);
|
||||
Settings.addPinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
|
||||
ToolbarUtilsKt.addPinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
|
||||
} else {
|
||||
Settings.removePinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
|
||||
ToolbarUtilsKt.removePinnedKey(DeviceProtectedUtils.getSharedPreferences(getContext()), tag);
|
||||
mToolbar.findViewWithTag(tag).setBackground(mDefaultBackground.getConstantState().newDrawable(getResources()));
|
||||
mPinnedKeys.removeView(pinnedKeyView);
|
||||
}
|
||||
|
|
|
@ -114,8 +114,6 @@ enum class ToolbarKey {
|
|||
|
||||
val toolbarKeyStrings: Set<String> = entries.mapTo(HashSet()) { it.toString().lowercase(Locale.US) }
|
||||
|
||||
fun toToolbarKeyString(keys: Collection<ToolbarKey>) = keys.joinToString(";") { it.name }
|
||||
|
||||
val defaultToolbarPref = entries.filterNot { it == CLOSE_HISTORY }.joinToString(";") {
|
||||
when (it) {
|
||||
INCOGNITO, AUTOCORRECT, UP, DOWN, ONE_HANDED, FULL_LEFT, FULL_RIGHT, CUT, CLEAR_CLIPBOARD -> "${it.name},false"
|
||||
|
@ -123,6 +121,10 @@ val defaultToolbarPref = entries.filterNot { it == CLOSE_HISTORY }.joinToString(
|
|||
}
|
||||
}
|
||||
|
||||
val defaultPinnedToolbarPref = entries.filterNot { it == CLOSE_HISTORY }.joinToString(";") {
|
||||
"${it.name},false"
|
||||
}
|
||||
|
||||
val defaultClipboardToolbarPref by lazy {
|
||||
val default = listOf(ONE_HANDED, UNDO, UP, DOWN, LEFT, RIGHT, CLEAR_CLIPBOARD, COPY, CUT, SELECT_WORD, CLOSE_HISTORY)
|
||||
val others = entries.filterNot { it in default }
|
||||
|
@ -132,6 +134,7 @@ val defaultClipboardToolbarPref by lazy {
|
|||
/** add missing keys, typically because a new key has been added */
|
||||
fun upgradeToolbarPrefs(prefs: SharedPreferences) {
|
||||
upgradeToolbarPref(prefs, Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref)
|
||||
upgradeToolbarPref(prefs, Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)
|
||||
upgradeToolbarPref(prefs, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, defaultClipboardToolbarPref)
|
||||
}
|
||||
|
||||
|
@ -159,8 +162,31 @@ private fun upgradeToolbarPref(prefs: SharedPreferences, pref: String, default:
|
|||
|
||||
fun getEnabledToolbarKeys(prefs: SharedPreferences) = getEnabledToolbarKeys(prefs, Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref)
|
||||
|
||||
fun getPinnedToolbarKeys(prefs: SharedPreferences) = getEnabledToolbarKeys(prefs, Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)
|
||||
|
||||
fun getEnabledClipboardToolbarKeys(prefs: SharedPreferences) = getEnabledToolbarKeys(prefs, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, defaultClipboardToolbarPref)
|
||||
|
||||
fun addPinnedKey(prefs: SharedPreferences, key: ToolbarKey) {
|
||||
// remove the existing version of this key and add the enabled one after the last currently enabled key
|
||||
val string = prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)!!
|
||||
val keys = string.split(";").toMutableList()
|
||||
keys.removeAll { it.startsWith(key.name + ",") }
|
||||
val lastEnabledIndex = keys.indexOfLast { it.endsWith("true") }
|
||||
keys.add(lastEnabledIndex + 1, key.name + ",true")
|
||||
prefs.edit { putString(Settings.PREF_PINNED_TOOLBAR_KEYS, keys.joinToString(";")) }
|
||||
}
|
||||
|
||||
fun removePinnedKey(prefs: SharedPreferences, key: ToolbarKey) {
|
||||
// just set it to disabled
|
||||
val string = prefs.getString(Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref)!!
|
||||
val result = string.split(";").joinToString(";") {
|
||||
if (it.startsWith(key.name + ","))
|
||||
key.name + ",false"
|
||||
else it
|
||||
}
|
||||
prefs.edit { putString(Settings.PREF_PINNED_TOOLBAR_KEYS, result) }
|
||||
}
|
||||
|
||||
private fun getEnabledToolbarKeys(prefs: SharedPreferences, pref: String, default: String): List<ToolbarKey> {
|
||||
val string = prefs.getString(pref, default)!!
|
||||
return string.split(";").mapNotNull {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue