From 4ee01527a2ccd14167d0f576c4047f201efbd17d Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sat, 6 Apr 2024 12:22:14 +0200 Subject: [PATCH] show toolbar icons in toolbar key dialog --- .../settings/PreferencesSettingsFragment.java | 9 ++++++--- .../keyboard/latin/utils/DialogUtils.kt | 16 +++++++++++++++- .../keyboard/latin/utils/ToolbarUtils.kt | 12 ++++++++++++ app/src/main/res/layout/reorder_dialog_item.xml | 3 ++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java b/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java index 8fdc97b4c..528e06f18 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/PreferencesSettingsFragment.java @@ -60,15 +60,18 @@ public final class PreferencesSettingsFragment extends SubScreenFragment { setLocalizedNumberRowVisibility(); findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false)); findPreference(Settings.PREF_POPUP_KEYS_ORDER).setOnPreferenceClickListener((pref) -> { - DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_ORDER, PopupKeysUtilsKt.POPUP_KEYS_ORDER_DEFAULT, R.string.popup_order); + DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_ORDER, + PopupKeysUtilsKt.POPUP_KEYS_ORDER_DEFAULT, R.string.popup_order, (x) -> null); return true; }); findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setOnPreferenceClickListener((pref) -> { - DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_LABELS_ORDER, PopupKeysUtilsKt.POPUP_KEYS_LABEL_DEFAULT, R.string.hint_source); + DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_POPUP_KEYS_LABELS_ORDER, + PopupKeysUtilsKt.POPUP_KEYS_LABEL_DEFAULT, R.string.hint_source, (x) -> null); return true; }); findPreference(Settings.PREF_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> { - DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultToolbarPref(), R.string.toolbar_keys); + DialogUtilsKt.reorderDialog(requireContext(), Settings.PREF_TOOLBAR_KEYS, ToolbarUtilsKt.getDefaultToolbarPref(), + R.string.toolbar_keys, (name) -> ToolbarUtilsKt.getToolbarIconByName(name, requireContext())); return true; }); } diff --git a/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt index 1f356bd9c..4f58d176e 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/DialogUtils.kt @@ -1,8 +1,11 @@ package helium314.keyboard.latin.utils import android.content.Context +import android.graphics.drawable.Drawable import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup +import android.widget.ImageView import android.widget.Switch import android.widget.TextView import androidx.annotation.StringRes @@ -51,7 +54,13 @@ fun infoDialog(context: Context, message: String) { * Items are semicolon-separated, see e.g. [POPUP_KEYS_LABEL_DEFAULT] for an example. */ // this should probably be a class -fun reorderDialog(context: Context, key: String, defaultSetting: String, @StringRes dialogTitleId: Int) { +fun reorderDialog( + context: Context, + key: String, + defaultSetting: String, + @StringRes dialogTitleId: Int, + getIcon: (String) -> Drawable? = { null } +) { val prefs = DeviceProtectedUtils.getSharedPreferences(context) val orderedItems = prefs.getString(key, defaultSetting)!!.split(";").mapTo(ArrayList()) { val both = it.split(",") @@ -85,6 +94,11 @@ fun reorderDialog(context: Context, key: String, defaultSetting: String, @String val pos = orderedItems.indexOfFirst { it.first == text } orderedItems[pos] = text to isChecked } + val icon = getIcon(text) + viewHolder.itemView.findViewById(R.id.reorder_item_icon)?.let { + it.visibility = if (icon == null) View.GONE else View.VISIBLE + it.setImageDrawable(icon) + } } } rv.adapter = adapter diff --git a/app/src/main/java/helium314/keyboard/latin/utils/ToolbarUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/ToolbarUtils.kt index b5b578f6a..ce13e2a96 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/ToolbarUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/ToolbarUtils.kt @@ -4,9 +4,12 @@ package helium314.keyboard.latin.utils import android.content.Context import android.content.SharedPreferences import android.content.res.TypedArray +import android.graphics.drawable.Drawable import android.widget.ImageButton import android.widget.ImageView +import androidx.appcompat.view.ContextThemeWrapper import androidx.core.content.edit +import helium314.keyboard.keyboard.KeyboardTheme import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode import helium314.keyboard.latin.R import helium314.keyboard.latin.settings.Settings @@ -76,6 +79,15 @@ private fun getStyleableIconId(key: ToolbarKey) = when (key) { SELECT_WORD -> R.styleable.Keyboard_iconSelectWord } +fun getToolbarIconByName(name: String, context: Context): Drawable? { + val key = entries.firstOrNull { it.name == name } ?: return null + val themeContext = ContextThemeWrapper(context, KeyboardTheme.getKeyboardTheme(context).mStyleId) + val attrs = themeContext.obtainStyledAttributes(null, R.styleable.Keyboard) + val icon = attrs.getDrawable(getStyleableIconId(key))?.mutate() + attrs.recycle() + return icon +} + // names need to be aligned with resources strings (using lowercase of key.name) enum class ToolbarKey { VOICE, CLIPBOARD, UNDO, REDO, SETTINGS, SELECT_ALL, SELECT_WORD, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, diff --git a/app/src/main/res/layout/reorder_dialog_item.xml b/app/src/main/res/layout/reorder_dialog_item.xml index 7ba6d7f9e..bd6ecc78b 100644 --- a/app/src/main/res/layout/reorder_dialog_item.xml +++ b/app/src/main/res/layout/reorder_dialog_item.xml @@ -14,7 +14,8 @@