From 996f09eb9d114274dd54f3e6e01e306971813c4a Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Sat, 19 Aug 2023 13:55:02 +0300 Subject: [PATCH] Style text color and replace certain icons --- java/res/drawable/delete.xml | 27 ++++++++++ java/res/drawable/globe.xml | 27 ++++++++++ java/res/drawable/shift.xml | 12 +++++ java/res/drawable/shiftshifted.xml | 18 +++++++ .../org/futo/inputmethod/keyboard/Key.java | 2 +- .../inputmethod/keyboard/KeyboardView.java | 2 +- .../keyboard/MainKeyboardView.java | 7 ++- .../keyboard/emoji/EmojiPalettesView.java | 14 +++--- .../internal/KeyVisualAttributes.java | 37 ++++++++------ .../keyboard/internal/KeyboardBuilder.java | 18 ++++++- .../keyboard/internal/KeyboardIconsSet.java | 15 ++---- .../org/futo/inputmethod/latin/LatinIME.kt | 50 +++++++++++++++++-- 12 files changed, 186 insertions(+), 43 deletions(-) create mode 100644 java/res/drawable/delete.xml create mode 100644 java/res/drawable/globe.xml create mode 100644 java/res/drawable/shift.xml create mode 100644 java/res/drawable/shiftshifted.xml diff --git a/java/res/drawable/delete.xml b/java/res/drawable/delete.xml new file mode 100644 index 000000000..e58b167c6 --- /dev/null +++ b/java/res/drawable/delete.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/java/res/drawable/globe.xml b/java/res/drawable/globe.xml new file mode 100644 index 000000000..308109759 --- /dev/null +++ b/java/res/drawable/globe.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/java/res/drawable/shift.xml b/java/res/drawable/shift.xml new file mode 100644 index 000000000..1a630eb5a --- /dev/null +++ b/java/res/drawable/shift.xml @@ -0,0 +1,12 @@ + + + diff --git a/java/res/drawable/shiftshifted.xml b/java/res/drawable/shiftshifted.xml new file mode 100644 index 000000000..e4ce8af2e --- /dev/null +++ b/java/res/drawable/shiftshifted.xml @@ -0,0 +1,18 @@ + + + + diff --git a/java/src/org/futo/inputmethod/keyboard/Key.java b/java/src/org/futo/inputmethod/keyboard/Key.java index 1406c8904..683e60245 100644 --- a/java/src/org/futo/inputmethod/keyboard/Key.java +++ b/java/src/org/futo/inputmethod/keyboard/Key.java @@ -402,7 +402,7 @@ public class Key implements Comparable { : altCodeInAttr; mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode, disabledIconId, visualInsetsLeft, visualInsetsRight); - mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr); + mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr, null); mHashCode = computeHashCode(this); } diff --git a/java/src/org/futo/inputmethod/keyboard/KeyboardView.java b/java/src/org/futo/inputmethod/keyboard/KeyboardView.java index d9c0e2cac..2cc9f9619 100644 --- a/java/src/org/futo/inputmethod/keyboard/KeyboardView.java +++ b/java/src/org/futo/inputmethod/keyboard/KeyboardView.java @@ -175,7 +175,7 @@ public class KeyboardView extends View { final TypedArray keyAttr = context.obtainStyledAttributes(attrs, R.styleable.Keyboard_Key, defStyle, R.style.KeyboardView); mDefaultKeyLabelFlags = keyAttr.getInt(R.styleable.Keyboard_Key_keyLabelFlags, 0); - mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr); + mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr, mDrawableProvider); keyAttr.recycle(); mPaint.setAntiAlias(true); diff --git a/java/src/org/futo/inputmethod/keyboard/MainKeyboardView.java b/java/src/org/futo/inputmethod/keyboard/MainKeyboardView.java index 3b6d8dd62..a8cf07672 100644 --- a/java/src/org/futo/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/org/futo/inputmethod/keyboard/MainKeyboardView.java @@ -51,6 +51,7 @@ import org.futo.inputmethod.keyboard.internal.MoreKeySpec; import org.futo.inputmethod.keyboard.internal.NonDistinctMultitouchHelper; import org.futo.inputmethod.keyboard.internal.SlidingKeyInputDrawingPreview; import org.futo.inputmethod.keyboard.internal.TimerHandler; +import org.futo.inputmethod.latin.KeyboardDrawableProvider; import org.futo.inputmethod.latin.R; import org.futo.inputmethod.latin.RichInputMethodSubtype; import org.futo.inputmethod.latin.SuggestedWords; @@ -214,8 +215,10 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy mBackgroundDimAlphaPaint.setAlpha(backgroundDimAlpha); mLanguageOnSpacebarTextRatio = mainKeyboardViewAttr.getFraction( R.styleable.MainKeyboardView_languageOnSpacebarTextRatio, 1, 1, 1.0f); - mLanguageOnSpacebarTextColor = mainKeyboardViewAttr.getColor( - R.styleable.MainKeyboardView_languageOnSpacebarTextColor, 0); + mLanguageOnSpacebarTextColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.MainKeyboardView_languageOnSpacebarTextColor, 0, + mainKeyboardViewAttr, mDrawableProvider + ); mLanguageOnSpacebarTextShadowRadius = mainKeyboardViewAttr.getFloat( R.styleable.MainKeyboardView_languageOnSpacebarTextShadowRadius, LANGUAGE_ON_SPACEBAR_TEXT_SHADOW_RADIUS_DISABLED); diff --git a/java/src/org/futo/inputmethod/keyboard/emoji/EmojiPalettesView.java b/java/src/org/futo/inputmethod/keyboard/emoji/EmojiPalettesView.java index 24b57d498..c9368fe7d 100644 --- a/java/src/org/futo/inputmethod/keyboard/emoji/EmojiPalettesView.java +++ b/java/src/org/futo/inputmethod/keyboard/emoji/EmojiPalettesView.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.preference.PreferenceManager; import androidx.viewpager.widget.ViewPager; import android.util.AttributeSet; @@ -374,14 +375,13 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange public void startEmojiPalettes(final String switchToAlphaLabel, final KeyVisualAttributes keyVisualAttr, final KeyboardIconsSet iconSet) { - final int deleteIconResId = iconSet.getIconResourceId(KeyboardIconsSet.NAME_DELETE_KEY); - if (deleteIconResId != 0) { - mDeleteKey.setImageResource(deleteIconResId); + final Drawable deleteDrawable = iconSet.getIconDrawable(KeyboardIconsSet.getIconId(KeyboardIconsSet.NAME_DELETE_KEY)); + if (deleteDrawable != null) { + mDeleteKey.setBackground(deleteDrawable); //? } - final int spacebarResId = iconSet.getIconResourceId(KeyboardIconsSet.NAME_SPACE_KEY); - if (spacebarResId != 0) { - // TODO: Remove this workaround to place the spacebar icon. - mSpacebarIcon.setBackgroundResource(spacebarResId); + final Drawable spacebarDrawable = iconSet.getIconDrawable(KeyboardIconsSet.getIconId(KeyboardIconsSet.NAME_SPACE_KEY)); + if (spacebarDrawable != null) { + mSpacebarIcon.setBackground(spacebarDrawable); } final KeyDrawParams params = new KeyDrawParams(); params.updateParams(mEmojiLayoutParams.getActionBarHeight(), keyVisualAttr); diff --git a/java/src/org/futo/inputmethod/keyboard/internal/KeyVisualAttributes.java b/java/src/org/futo/inputmethod/keyboard/internal/KeyVisualAttributes.java index 021b38981..10e13e2c8 100644 --- a/java/src/org/futo/inputmethod/keyboard/internal/KeyVisualAttributes.java +++ b/java/src/org/futo/inputmethod/keyboard/internal/KeyVisualAttributes.java @@ -20,6 +20,7 @@ import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.SparseIntArray; +import org.futo.inputmethod.latin.KeyboardDrawableProvider; import org.futo.inputmethod.latin.R; import org.futo.inputmethod.latin.utils.ResourceUtils; @@ -86,19 +87,19 @@ public final class KeyVisualAttributes { } @Nullable - public static KeyVisualAttributes newInstance(@Nonnull final TypedArray keyAttr) { + public static KeyVisualAttributes newInstance(@Nonnull final TypedArray keyAttr, @Nullable final KeyboardDrawableProvider provider) { final int indexCount = keyAttr.getIndexCount(); for (int i = 0; i < indexCount; i++) { final int attrId = keyAttr.getIndex(i); if (sVisualAttributeIds.get(attrId, ATTR_NOT_FOUND) == ATTR_NOT_FOUND) { continue; } - return new KeyVisualAttributes(keyAttr); + return new KeyVisualAttributes(keyAttr, provider); } return null; } - private KeyVisualAttributes(@Nonnull final TypedArray keyAttr) { + private KeyVisualAttributes(@Nonnull final TypedArray keyAttr, @Nullable final KeyboardDrawableProvider provider) { if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyTypeface)) { mTypeface = Typeface.defaultFromStyle( keyAttr.getInt(R.styleable.Keyboard_Key_keyTypeface, Typeface.NORMAL)); @@ -125,18 +126,24 @@ public final class KeyVisualAttributes { mPreviewTextRatio = ResourceUtils.getFraction(keyAttr, R.styleable.Keyboard_Key_keyPreviewTextRatio); - mTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyTextColor, 0); - mTextInactivatedColor = keyAttr.getColor( - R.styleable.Keyboard_Key_keyTextInactivatedColor, 0); - mTextShadowColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyTextShadowColor, 0); - mFunctionalTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_functionalTextColor, 0); - mHintLetterColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyHintLetterColor, 0); - mHintLabelColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyHintLabelColor, 0); - mShiftedLetterHintInactivatedColor = keyAttr.getColor( - R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor, 0); - mShiftedLetterHintActivatedColor = keyAttr.getColor( - R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor, 0); - mPreviewTextColor = keyAttr.getColor(R.styleable.Keyboard_Key_keyPreviewTextColor, 0); + mTextColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyTextColor, 0, keyAttr, provider); + mTextInactivatedColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyTextInactivatedColor, 0, keyAttr, provider); + mTextShadowColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyTextShadowColor, 0, keyAttr, provider); + mFunctionalTextColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_functionalTextColor, 0, keyAttr, provider); + mHintLetterColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyHintLetterColor, 0, keyAttr, provider); + mHintLabelColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyHintLabelColor, 0, keyAttr, provider); + mShiftedLetterHintInactivatedColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor, 0, keyAttr, provider); + mShiftedLetterHintActivatedColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor, 0, keyAttr, provider); + mPreviewTextColor = KeyboardDrawableProvider.Companion.getColorOrDefault( + R.styleable.Keyboard_Key_keyPreviewTextColor, 0, keyAttr, provider); mHintLabelVerticalAdjustment = ResourceUtils.getFraction(keyAttr, R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment, 0.0f); diff --git a/java/src/org/futo/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/org/futo/inputmethod/keyboard/internal/KeyboardBuilder.java index 7aa1e9ae4..1c01309d6 100644 --- a/java/src/org/futo/inputmethod/keyboard/internal/KeyboardBuilder.java +++ b/java/src/org/futo/inputmethod/keyboard/internal/KeyboardBuilder.java @@ -26,12 +26,15 @@ import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.util.Xml; +import android.view.ContextThemeWrapper; import org.futo.inputmethod.annotations.UsedForTesting; import org.futo.inputmethod.keyboard.Key; import org.futo.inputmethod.keyboard.Keyboard; import org.futo.inputmethod.keyboard.KeyboardId; import org.futo.inputmethod.keyboard.KeyboardTheme; +import org.futo.inputmethod.latin.KeyboardDrawableProvider; +import org.futo.inputmethod.latin.KeyboardDrawableProviderOwner; import org.futo.inputmethod.latin.R; import org.futo.inputmethod.latin.common.Constants; import org.futo.inputmethod.latin.common.StringUtils; @@ -150,8 +153,19 @@ public class KeyboardBuilder { private boolean mTopEdge; private Key mRightEdgeKey = null; + private KeyboardDrawableProvider mProvider = null; + public KeyboardBuilder(final Context context, @Nonnull final KP params) { mContext = context; + if(mContext instanceof KeyboardDrawableProviderOwner) { + mProvider = ((KeyboardDrawableProviderOwner) mContext).getDrawableProvider(); + }else if(mContext instanceof ContextThemeWrapper) { + Context baseContext = ((ContextThemeWrapper) mContext).getBaseContext(); + if(baseContext instanceof KeyboardDrawableProviderOwner) { + mProvider = ((KeyboardDrawableProviderOwner) baseContext).getDrawableProvider(); + } + } + final Resources res = context.getResources(); mResources = res; @@ -272,7 +286,7 @@ public class KeyboardBuilder { params.mDefaultRowHeight = (int)ResourceUtils.getDimensionOrFraction(keyboardAttr, R.styleable.Keyboard_rowHeight, baseHeight, baseHeight / DEFAULT_KEYBOARD_ROWS); - params.mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr); + params.mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr, mProvider); params.mMoreKeysTemplate = keyboardAttr.getResourceId( R.styleable.Keyboard_moreKeysTemplate, 0); @@ -280,7 +294,7 @@ public class KeyboardBuilder { R.styleable.Keyboard_Key_maxMoreKeysColumn, 5); params.mThemeId = keyboardAttr.getInt(R.styleable.Keyboard_themeId, 0); - params.mIconsSet.loadIcons(keyboardAttr); + params.mIconsSet.loadIcons(keyboardAttr, mProvider); params.mTextsSet.setLocale(params.mId.getLocale(), mContext); final int resourceId = keyboardAttr.getResourceId( diff --git a/java/src/org/futo/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/org/futo/inputmethod/keyboard/internal/KeyboardIconsSet.java index f41b9e810..92443a579 100644 --- a/java/src/org/futo/inputmethod/keyboard/internal/KeyboardIconsSet.java +++ b/java/src/org/futo/inputmethod/keyboard/internal/KeyboardIconsSet.java @@ -22,6 +22,7 @@ import android.graphics.drawable.Drawable; import android.util.Log; import android.util.SparseIntArray; +import org.futo.inputmethod.latin.KeyboardDrawableProvider; import org.futo.inputmethod.latin.R; import java.util.HashMap; @@ -92,7 +93,6 @@ public final class KeyboardIconsSet { private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2; private static final String[] ICON_NAMES = new String[NUM_ICONS]; private final Drawable[] mIcons = new Drawable[NUM_ICONS]; - private final int[] mIconResourceIds = new int[NUM_ICONS]; static { int iconId = ICON_UNDEFINED; @@ -108,16 +108,15 @@ public final class KeyboardIconsSet { } } - public void loadIcons(final TypedArray keyboardAttrs) { + public void loadIcons(final TypedArray keyboardAttrs, @Nullable KeyboardDrawableProvider provider) { final int size = ATTR_ID_TO_ICON_ID.size(); for (int index = 0; index < size; index++) { final int attrId = ATTR_ID_TO_ICON_ID.keyAt(index); try { - final Drawable icon = keyboardAttrs.getDrawable(attrId); + final Drawable icon = KeyboardDrawableProvider.Companion.getDrawableOrDefault(attrId, keyboardAttrs, provider); setDefaultBounds(icon); final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId); mIcons[iconId] = icon; - mIconResourceIds[iconId] = keyboardAttrs.getResourceId(attrId, 0); } catch (Resources.NotFoundException e) { Log.w(TAG, "Drawable resource for icon #" + keyboardAttrs.getResources().getResourceEntryName(attrId) @@ -143,14 +142,6 @@ public final class KeyboardIconsSet { throw new RuntimeException("unknown icon name: " + name); } - public int getIconResourceId(final String name) { - final int iconId = getIconId(name); - if (isValidIconId(iconId)) { - return mIconResourceIds[iconId]; - } - throw new RuntimeException("unknown icon name: " + name); - } - @Nullable public Drawable getIconDrawable(final int iconId) { if (isValidIconId(iconId)) { diff --git a/java/src/org/futo/inputmethod/latin/LatinIME.kt b/java/src/org/futo/inputmethod/latin/LatinIME.kt index 5c729fa12..68c1fc9e1 100644 --- a/java/src/org/futo/inputmethod/latin/LatinIME.kt +++ b/java/src/org/futo/inputmethod/latin/LatinIME.kt @@ -2,6 +2,7 @@ package org.futo.inputmethod.latin import android.content.Context import android.content.res.Configuration +import android.content.res.TypedArray import android.graphics.Color import android.graphics.drawable.Drawable import android.graphics.drawable.GradientDrawable @@ -9,10 +10,9 @@ import android.graphics.drawable.InsetDrawable import android.graphics.drawable.LayerDrawable import android.graphics.drawable.ShapeDrawable import android.graphics.drawable.StateListDrawable -import android.graphics.drawable.shapes.OvalShape import android.graphics.drawable.shapes.RoundRectShape -import android.graphics.drawable.shapes.Shape import android.inputmethodservice.InputMethodService +import android.util.AttributeSet import android.util.TypedValue import android.view.KeyEvent import android.view.View @@ -21,13 +21,13 @@ import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodSubtype import androidx.annotation.ColorInt +import androidx.appcompat.content.res.AppCompatResources import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.runtime.key import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.RectangleShape import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.ComposeView @@ -65,6 +65,22 @@ interface KeyboardDrawableProvider { val moreKeysKeyboardBackground: Drawable val popupKey: Drawable + + @ColorInt + fun getColor(i: Int): Int? + + fun getDrawable(i: Int): Drawable? + + companion object { + @ColorInt + fun getColorOrDefault(i: Int, @ColorInt default: Int, keyAttr: TypedArray, provider: KeyboardDrawableProvider?): Int { + return (provider?.getColor(i)) ?: keyAttr.getColor(i, default) + } + + fun getDrawableOrDefault(i: Int, keyAttr: TypedArray, provider: KeyboardDrawableProvider?): Drawable? { + return (provider?.getDrawable(i)) ?: keyAttr.getDrawable(i) + } + } } // TODO: Expand the number of drawables this provides so it covers the full theme, and @@ -81,6 +97,17 @@ class BasicThemeProvider(val context: Context) : KeyboardDrawableProvider { override val moreKeysKeyboardBackground: Drawable override val popupKey: Drawable + private val colors: HashMap = HashMap() + override fun getColor(i: Int): Int? { + return colors[i] + } + + + private val drawables: HashMap = HashMap() + override fun getDrawable(i: Int): Drawable? { + return drawables[i] + } + private fun dp(dp: Dp): Float { return TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, @@ -130,6 +157,23 @@ class BasicThemeProvider(val context: Context) : KeyboardDrawableProvider { val transparent = Color.TRANSPARENT + colors[R.styleable.Keyboard_Key_keyTextColor] = DarkColorScheme.onBackground.toArgb() + colors[R.styleable.Keyboard_Key_keyTextInactivatedColor] = DarkColorScheme.onBackground.copy(alpha = 0.5f).toArgb() + colors[R.styleable.Keyboard_Key_keyTextShadowColor] = 0 + colors[R.styleable.Keyboard_Key_functionalTextColor] = DarkColorScheme.onBackground.toArgb() + colors[R.styleable.Keyboard_Key_keyHintLetterColor] = DarkColorScheme.onBackground.copy(alpha = 0.5f).toArgb() + colors[R.styleable.Keyboard_Key_keyHintLabelColor] = DarkColorScheme.onBackground.copy(alpha = 0.5f).toArgb() + colors[R.styleable.Keyboard_Key_keyShiftedLetterHintInactivatedColor] = DarkColorScheme.onBackground.copy(alpha = 0.5f).toArgb() + colors[R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor] = DarkColorScheme.onBackground.copy(alpha = 0.5f).toArgb() + colors[R.styleable.Keyboard_Key_keyPreviewTextColor] = DarkColorScheme.onBackground.toArgb() + colors[R.styleable.MainKeyboardView_languageOnSpacebarTextColor] = DarkColorScheme.onBackground.copy(alpha = 0.5f).toArgb() + + drawables[R.styleable.Keyboard_iconDeleteKey] = AppCompatResources.getDrawable(context, R.drawable.delete)!! + drawables[R.styleable.Keyboard_iconLanguageSwitchKey] = AppCompatResources.getDrawable(context, R.drawable.globe)!! + + drawables[R.styleable.Keyboard_iconShiftKey] = AppCompatResources.getDrawable(context, R.drawable.shift)!! + drawables[R.styleable.Keyboard_iconShiftKeyShifted] = AppCompatResources.getDrawable(context, R.drawable.shiftshifted)!! + primaryKeyboardColor = background keyboardBackground = LayerDrawable(arrayOf(