show toolbar icons in toolbar key dialog

This commit is contained in:
Helium314 2024-04-06 12:22:14 +02:00
parent 52f567ff9e
commit 4ee01527a2
4 changed files with 35 additions and 5 deletions

View file

@ -60,15 +60,18 @@ public final class PreferencesSettingsFragment extends SubScreenFragment {
setLocalizedNumberRowVisibility(); setLocalizedNumberRowVisibility();
findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false)); findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setVisible(getSharedPreferences().getBoolean(Settings.PREF_SHOW_HINTS, false));
findPreference(Settings.PREF_POPUP_KEYS_ORDER).setOnPreferenceClickListener((pref) -> { 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; return true;
}); });
findPreference(Settings.PREF_POPUP_KEYS_LABELS_ORDER).setOnPreferenceClickListener((pref) -> { 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; return true;
}); });
findPreference(Settings.PREF_TOOLBAR_KEYS).setOnPreferenceClickListener((pref) -> { 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; return true;
}); });
} }

View file

@ -1,8 +1,11 @@
package helium314.keyboard.latin.utils package helium314.keyboard.latin.utils
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Switch import android.widget.Switch
import android.widget.TextView import android.widget.TextView
import androidx.annotation.StringRes 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. * Items are semicolon-separated, see e.g. [POPUP_KEYS_LABEL_DEFAULT] for an example.
*/ */
// this should probably be a class // 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 prefs = DeviceProtectedUtils.getSharedPreferences(context)
val orderedItems = prefs.getString(key, defaultSetting)!!.split(";").mapTo(ArrayList()) { val orderedItems = prefs.getString(key, defaultSetting)!!.split(";").mapTo(ArrayList()) {
val both = it.split(",") val both = it.split(",")
@ -85,6 +94,11 @@ fun reorderDialog(context: Context, key: String, defaultSetting: String, @String
val pos = orderedItems.indexOfFirst { it.first == text } val pos = orderedItems.indexOfFirst { it.first == text }
orderedItems[pos] = text to isChecked orderedItems[pos] = text to isChecked
} }
val icon = getIcon(text)
viewHolder.itemView.findViewById<ImageView>(R.id.reorder_item_icon)?.let {
it.visibility = if (icon == null) View.GONE else View.VISIBLE
it.setImageDrawable(icon)
}
} }
} }
rv.adapter = adapter rv.adapter = adapter

View file

@ -4,9 +4,12 @@ package helium314.keyboard.latin.utils
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.TypedArray import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.edit import androidx.core.content.edit
import helium314.keyboard.keyboard.KeyboardTheme
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
import helium314.keyboard.latin.R import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.Settings
@ -76,6 +79,15 @@ private fun getStyleableIconId(key: ToolbarKey) = when (key) {
SELECT_WORD -> R.styleable.Keyboard_iconSelectWord 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) // 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, SELECT_WORD, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN, VOICE, CLIPBOARD, UNDO, REDO, SETTINGS, SELECT_ALL, SELECT_WORD, COPY, ONE_HANDED, LEFT, RIGHT, UP, DOWN,

View file

@ -14,7 +14,8 @@
<ImageView <ImageView
android:id="@+id/reorder_item_icon" android:id="@+id/reorder_item_icon"
android:visibility="gone" android:visibility="gone"
android:layout_width="wrap_content" android:paddingEnd="4dp"
android:layout_width="30dp"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<TextView <TextView
android:id="@+id/reorder_item_name" android:id="@+id/reorder_item_name"