set keyboard icon ids per style in keyboardIconsSet instead of using styleable

increases flexibility
preparation for planned changes
This commit is contained in:
Helium314 2024-08-24 23:46:13 +02:00
parent 8a457c1821
commit aed5704582
15 changed files with 274 additions and 340 deletions

View file

@ -13,6 +13,7 @@ import android.widget.FrameLayout
import android.widget.ImageButton
import helium314.keyboard.keyboard.KeyboardActionListener
import helium314.keyboard.keyboard.KeyboardSwitcher
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
import helium314.keyboard.latin.common.ColorType
import helium314.keyboard.latin.common.Constants
@ -31,9 +32,6 @@ class KeyboardWrapperView @JvmOverloads constructor(
private lateinit var stopOneHandedModeBtn: ImageButton
private lateinit var switchOneHandedModeBtn: ImageButton
private lateinit var resizeOneHandedModeBtn: ImageButton
private val iconStopOneHandedModeId: Int
private val iconSwitchOneHandedModeId: Int
private val iconResizeOneHandedModeId: Int
var oneHandedModeEnabled = false
set(enabled) {
@ -53,14 +51,16 @@ class KeyboardWrapperView @JvmOverloads constructor(
@SuppressLint("ClickableViewAccessibility")
override fun onFinishInflate() {
super.onFinishInflate()
val keyboardIconsSet = KeyboardIconsSet.instance
keyboardIconsSet.loadIcons(context)
stopOneHandedModeBtn = findViewById(R.id.btn_stop_one_handed_mode)
stopOneHandedModeBtn.setImageResource(iconStopOneHandedModeId)
stopOneHandedModeBtn.setImageDrawable(keyboardIconsSet.getNewDrawable(KeyboardIconsSet.NAME_STOP_ONEHANDED_KEY, context))
stopOneHandedModeBtn.visibility = GONE
switchOneHandedModeBtn = findViewById(R.id.btn_switch_one_handed_mode)
switchOneHandedModeBtn.setImageResource(iconSwitchOneHandedModeId)
switchOneHandedModeBtn.setImageDrawable(keyboardIconsSet.getNewDrawable(KeyboardIconsSet.NAME_SWITCH_ONEHANDED_KEY, context))
switchOneHandedModeBtn.visibility = GONE
resizeOneHandedModeBtn = findViewById(R.id.btn_resize_one_handed_mode)
resizeOneHandedModeBtn.setImageResource(iconResizeOneHandedModeId)
resizeOneHandedModeBtn.setImageDrawable(keyboardIconsSet.getNewDrawable(KeyboardIconsSet.NAME_RESIZE_ONEHANDED_KEY, context))
resizeOneHandedModeBtn.visibility = GONE
stopOneHandedModeBtn.setOnClickListener(this)
@ -163,13 +163,4 @@ class KeyboardWrapperView @JvmOverloads constructor(
switchOneHandedModeBtn.setLayout((keyboardView.measuredHeight * 0.5f).toInt())
resizeOneHandedModeBtn.setLayout((keyboardView.measuredHeight * 0.8f).toInt())
}
init {
@SuppressLint("CustomViewStyleable")
val keyboardAttr = context.obtainStyledAttributes(attrs, R.styleable.Keyboard, defStyle, R.style.Keyboard)
iconStopOneHandedModeId = keyboardAttr.getResourceId(R.styleable.Keyboard_iconStopOneHandedMode, 0)
iconSwitchOneHandedModeId = keyboardAttr.getResourceId(R.styleable.Keyboard_iconSwitchOneHandedMode, 0)
iconResizeOneHandedModeId = keyboardAttr.getResourceId(R.styleable.Keyboard_iconResizeOneHandedMode, 0)
keyboardAttr.recycle()
}
}

View file

@ -45,6 +45,7 @@ import helium314.keyboard.compat.ConfigurationCompatKt;
import helium314.keyboard.compat.EditorInfoCompatUtils;
import helium314.keyboard.keyboard.KeyboardActionListener;
import helium314.keyboard.keyboard.KeyboardActionListenerImpl;
import helium314.keyboard.keyboard.internal.KeyboardIconsSet;
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode;
import helium314.keyboard.latin.common.InsetsOutlineProvider;
import helium314.keyboard.dictionarypack.DictionaryPackConstants;
@ -570,6 +571,7 @@ public class LatinIME extends InputMethodService implements
Settings.init(this);
DebugFlags.init(this);
SubtypeSettingsKt.init(this);
KeyboardIconsSet.Companion.getInstance().loadIcons(this);
RichInputMethodManager.init(this);
mRichImm = RichInputMethodManager.getInstance();
AudioAndHapticFeedbackManager.init(this);

View file

@ -4,11 +4,11 @@ import android.content.SharedPreferences
import android.os.Bundle
import androidx.preference.Preference
import helium314.keyboard.keyboard.KeyboardSwitcher
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R
import helium314.keyboard.latin.utils.defaultClipboardToolbarPref
import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
import helium314.keyboard.latin.utils.defaultToolbarPref
import helium314.keyboard.latin.utils.getToolbarIconByName
import helium314.keyboard.latin.utils.reorderDialog
class ToolbarSettingsFragment : SubScreenFragment() {
@ -16,6 +16,8 @@ class ToolbarSettingsFragment : SubScreenFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val iconsSet = KeyboardIconsSet.instance
iconsSet.loadIcons(requireContext())
addPreferencesFromResource(R.xml.prefs_screen_toolbar)
findPreference<Preference>(Settings.PREF_TOOLBAR_KEYS)?.onPreferenceClickListener =
@ -23,7 +25,7 @@ class ToolbarSettingsFragment : SubScreenFragment() {
reorderDialog(
requireContext(), Settings.PREF_TOOLBAR_KEYS, defaultToolbarPref,
R.string.toolbar_keys
) { getToolbarIconByName(it, requireContext()) }
) { iconsSet.getNewDrawable(it, requireContext()) }
true
}
findPreference<Preference>(Settings.PREF_PINNED_TOOLBAR_KEYS)?.onPreferenceClickListener =
@ -31,7 +33,7 @@ class ToolbarSettingsFragment : SubScreenFragment() {
reorderDialog(
requireContext(), Settings.PREF_PINNED_TOOLBAR_KEYS, defaultPinnedToolbarPref,
R.string.pinned_toolbar_keys
) { getToolbarIconByName(it, requireContext()) }
) { iconsSet.getNewDrawable(it, requireContext()) }
true
}
findPreference<Preference>(Settings.PREF_CLIPBOARD_TOOLBAR_KEYS)?.onPreferenceClickListener =
@ -39,7 +41,7 @@ class ToolbarSettingsFragment : SubScreenFragment() {
reorderDialog(
requireContext(), Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, defaultClipboardToolbarPref,
R.string.clipboard_toolbar_keys
) { getToolbarIconByName(it, requireContext()) }
) { iconsSet.getNewDrawable(it, requireContext()) }
true
}
}

View file

@ -13,7 +13,6 @@ import android.app.KeyguardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
@ -43,6 +42,7 @@ import helium314.keyboard.keyboard.Keyboard;
import helium314.keyboard.keyboard.KeyboardSwitcher;
import helium314.keyboard.keyboard.MainKeyboardView;
import helium314.keyboard.keyboard.PopupKeysPanel;
import helium314.keyboard.keyboard.internal.KeyboardIconsSet;
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode;
import helium314.keyboard.latin.AudioAndHapticFeedbackManager;
import helium314.keyboard.latin.Dictionary;
@ -181,18 +181,17 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
R.dimen.config_more_suggestions_modal_tolerance);
mMoreSuggestionsSlidingDetector = new GestureDetector(context, mMoreSuggestionsSlidingListener);
@SuppressLint("CustomViewStyleable")
final TypedArray keyboardAttr = context.obtainStyledAttributes(attrs, R.styleable.Keyboard, defStyle, R.style.SuggestionStripView);
mIncognitoIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconIncognitoKey);
mToolbarArrowIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconToolbarKey);
mBinIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconBin);
final KeyboardIconsSet iconsSet = KeyboardIconsSet.Companion.getInstance();
mIncognitoIcon = iconsSet.getNewDrawable(KeyboardIconsSet.NAME_INCOGNITO_KEY, context);
mToolbarArrowIcon = iconsSet.getNewDrawable(KeyboardIconsSet.NAME_TOOLBAR_KEY, context);
mBinIcon = iconsSet.getNewDrawable(KeyboardIconsSet.NAME_BIN, context);
final LinearLayout.LayoutParams toolbarKeyLayoutParams = new LinearLayout.LayoutParams(
getResources().getDimensionPixelSize(R.dimen.config_suggestions_strip_edge_key_width),
LinearLayout.LayoutParams.MATCH_PARENT
);
for (final ToolbarKey key : ToolbarUtilsKt.getEnabledToolbarKeys(prefs)) {
final ImageButton button = createToolbarKey(context, keyboardAttr, key);
final ImageButton button = createToolbarKey(context, iconsSet, key);
button.setLayoutParams(toolbarKeyLayoutParams);
setupKey(button, colors);
mToolbar.addView(button);
@ -216,7 +215,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mToolbarExpandKey.getLayoutParams().width *= 0.82;
for (final ToolbarKey pinnedKey : ToolbarUtilsKt.getPinnedToolbarKeys(prefs)) {
final ImageButton button = createToolbarKey(context, keyboardAttr, pinnedKey);
final ImageButton button = createToolbarKey(context, iconsSet, pinnedKey);
button.setLayoutParams(toolbarKeyLayoutParams);
setupKey(button, colors);
mPinnedKeys.addView(button);
@ -226,7 +225,6 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
}
colors.setBackground(this, ColorType.STRIP_BACKGROUND);
keyboardAttr.recycle();
}
/**

View file

@ -3,13 +3,10 @@ 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.KeyboardIconsSet
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode
import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings
@ -17,7 +14,7 @@ import helium314.keyboard.latin.utils.ToolbarKey.*
import java.util.EnumMap
import java.util.Locale
fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey): ImageButton {
fun createToolbarKey(context: Context, iconsSet: KeyboardIconsSet, key: ToolbarKey): ImageButton {
val button = ImageButton(context, null, R.attr.suggestionWordStyle)
button.scaleType = ImageView.ScaleType.CENTER
button.tag = key
@ -30,7 +27,7 @@ fun createToolbarKey(context: Context, keyboardAttr: TypedArray, key: ToolbarKey
AUTOCORRECT -> Settings.getInstance().current.mAutoCorrectionEnabledPerUserSettings
else -> true
}
button.setImageDrawable(keyboardAttr.getDrawable(getStyleableIconId(key))?.mutate())
button.setImageDrawable(iconsSet.getNewDrawable(key.name, context))
return button
}
@ -85,47 +82,6 @@ fun getCodeForToolbarKeyLongClick(key: ToolbarKey) = when (key) {
else -> KeyCode.UNSPECIFIED
}
fun getStyleableIconId(key: ToolbarKey) = when (key) {
VOICE -> R.styleable.Keyboard_iconShortcutKey
CLIPBOARD -> R.styleable.Keyboard_iconClipboardNormalKey
NUMPAD -> R.styleable.Keyboard_iconNumpadKey
UNDO -> R.styleable.Keyboard_iconUndo
REDO -> R.styleable.Keyboard_iconRedo
SETTINGS -> R.styleable.Keyboard_iconSettingsKey
SELECT_ALL -> R.styleable.Keyboard_iconSelectAll
SELECT_WORD -> R.styleable.Keyboard_iconSelectWord
COPY -> R.styleable.Keyboard_iconCopyKey
CUT -> R.styleable.Keyboard_iconCutKey
PASTE -> R.styleable.Keyboard_iconPasteKey
ONE_HANDED -> R.styleable.Keyboard_iconStartOneHandedMode
INCOGNITO -> R.styleable.Keyboard_iconIncognitoKey
AUTOCORRECT -> R.styleable.Keyboard_iconAutoCorrect
CLEAR_CLIPBOARD -> R.styleable.Keyboard_iconClearClipboardKey
CLOSE_HISTORY -> R.styleable.Keyboard_iconClose
EMOJI -> R.styleable.Keyboard_iconEmojiNormalKey
LEFT -> R.styleable.Keyboard_iconArrowLeft
RIGHT -> R.styleable.Keyboard_iconArrowRight
UP -> R.styleable.Keyboard_iconArrowUp
DOWN -> R.styleable.Keyboard_iconArrowDown
WORD_LEFT -> R.styleable.Keyboard_iconWordLeft
WORD_RIGHT -> R.styleable.Keyboard_iconWordRight
PAGE_UP -> R.styleable.Keyboard_iconPageUp
PAGE_DOWN -> R.styleable.Keyboard_iconPageDown
FULL_LEFT -> R.styleable.Keyboard_iconFullLeft
FULL_RIGHT -> R.styleable.Keyboard_iconFullRight
PAGE_START -> R.styleable.Keyboard_iconPageStart
PAGE_END -> R.styleable.Keyboard_iconPageEnd
}
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, NUMPAD, UNDO, REDO, SETTINGS, SELECT_ALL, SELECT_WORD, COPY, CUT, PASTE, ONE_HANDED,