From 59ce1a1cb3b9498fdaf7ce37f09de2db8edb1e61 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Thu, 31 Aug 2023 11:52:10 +0200 Subject: [PATCH] set color state lists inside Colors --- .../inputmethod/keyboard/KeyboardView.java | 23 ++++---- .../keyboard/clipboard/ClipboardAdapter.kt | 6 +-- .../clipboard/ClipboardHistoryView.kt | 4 +- .../keyboard/emoji/EmojiPalettesView.java | 10 ++-- .../inputmethod/latin/common/Colors.java | 53 ++++++++++++++++--- .../suggestions/SuggestionStripView.java | 6 +-- 6 files changed, 62 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/KeyboardView.java b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/KeyboardView.java index ad80f4eac..e0046e127 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/KeyboardView.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/KeyboardView.java @@ -32,8 +32,6 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; -import androidx.core.graphics.drawable.DrawableCompat; - import org.dslul.openboard.inputmethod.keyboard.emoji.EmojiPageKeyboardView; import org.dslul.openboard.inputmethod.keyboard.internal.KeyDrawParams; import org.dslul.openboard.inputmethod.keyboard.internal.KeyVisualAttributes; @@ -179,20 +177,17 @@ public class KeyboardView extends View { mColors = Settings.getInstance().getCurrent().mColors; if (mColors.isCustom) { - DrawableCompat.setTintMode(mKeyBackground, PorterDuff.Mode.MULTIPLY); - if (this.getClass() == MoreKeysKeyboardView.class) { - DrawableCompat.setTintList(mKeyBackground, mColors.adjustedBackgroundStateList); - } else if (this.getClass() == EmojiPageKeyboardView.class || this.getClass() == MoreSuggestionsView.class) { - DrawableCompat.setTintList(mKeyBackground, mColors.backgroundStateList); + final Class c = this.getClass(); + if (c == MoreKeysKeyboardView.class) { + mColors.setBackgroundColor(mKeyBackground, Colors.TYPE_ADJUSTED_BACKGROUND); + } else if (c == EmojiPageKeyboardView.class || c == MoreSuggestionsView.class) { + mColors.setBackgroundColor(mKeyBackground, Colors.TYPE_BACKGROUND); } else { - DrawableCompat.setTintList(mKeyBackground, mColors.keyStateList); + mColors.setBackgroundColor(mKeyBackground, Colors.TYPE_KEY); } - DrawableCompat.setTintMode(mActionKeyBackground, PorterDuff.Mode.MULTIPLY); - DrawableCompat.setTintList(mActionKeyBackground, mColors.actionKeyStateList); - DrawableCompat.setTintMode(mSpacebarBackground, PorterDuff.Mode.MULTIPLY); - DrawableCompat.setTintList(mSpacebarBackground, mColors.spaceBarStateList); - DrawableCompat.setTintMode(mFunctionalKeyBackground, PorterDuff.Mode.MULTIPLY); - DrawableCompat.setTintList(mFunctionalKeyBackground, mColors.functionalKeyStateList); + mColors.setBackgroundColor(mActionKeyBackground, Colors.TYPE_ACTION); + mColors.setBackgroundColor(mSpacebarBackground, Colors.TYPE_SPACE); + mColors.setBackgroundColor(mFunctionalKeyBackground, Colors.TYPE_FUNCTIONAL); if (this.getClass() == MoreKeysKeyboardView.class) getBackground().setColorFilter(mColors.adjustedBackgroundFilter); else diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardAdapter.kt b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardAdapter.kt index 385e6e476..52b5f69b7 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardAdapter.kt +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardAdapter.kt @@ -1,6 +1,5 @@ package org.dslul.openboard.inputmethod.keyboard.clipboard -import android.graphics.PorterDuff import android.graphics.Typeface import android.util.TypedValue import android.view.LayoutInflater @@ -9,11 +8,11 @@ import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView -import androidx.core.graphics.drawable.DrawableCompat import androidx.recyclerview.widget.RecyclerView import org.dslul.openboard.inputmethod.latin.ClipboardHistoryEntry import org.dslul.openboard.inputmethod.latin.ClipboardHistoryManager import org.dslul.openboard.inputmethod.latin.R +import org.dslul.openboard.inputmethod.latin.common.Colors import org.dslul.openboard.inputmethod.latin.settings.Settings class ClipboardAdapter( @@ -58,8 +57,7 @@ class ClipboardAdapter( setBackgroundResource(itemBackgroundId) val colors = Settings.getInstance().current.mColors if (colors.isCustom) { - DrawableCompat.setTintList(background, colors.keyStateList) - DrawableCompat.setTintMode(background, PorterDuff.Mode.MULTIPLY) + colors.setBackgroundColor(background, Colors.TYPE_KEY) } } pinnedIconView = view.findViewById(R.id.clipboard_entry_pinned_icon).apply { diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardHistoryView.kt b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardHistoryView.kt index e5c0ff7a2..1ebb47aff 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardHistoryView.kt +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/clipboard/ClipboardHistoryView.kt @@ -9,7 +9,6 @@ import android.widget.FrameLayout import android.widget.ImageButton import android.widget.LinearLayout import android.widget.TextView -import androidx.core.graphics.drawable.DrawableCompat import androidx.recyclerview.widget.StaggeredGridLayoutManager import org.dslul.openboard.inputmethod.keyboard.KeyboardActionListener import org.dslul.openboard.inputmethod.keyboard.internal.KeyDrawParams @@ -17,6 +16,7 @@ import org.dslul.openboard.inputmethod.keyboard.internal.KeyVisualAttributes import org.dslul.openboard.inputmethod.keyboard.internal.KeyboardIconsSet import org.dslul.openboard.inputmethod.latin.ClipboardHistoryManager import org.dslul.openboard.inputmethod.latin.R +import org.dslul.openboard.inputmethod.latin.common.Colors import org.dslul.openboard.inputmethod.latin.common.Constants import org.dslul.openboard.inputmethod.latin.settings.Settings import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils @@ -117,7 +117,7 @@ class ClipboardHistoryView @JvmOverloads constructor( val colors = Settings.getInstance().current.mColors if (colors.isCustom) { setTextColor(colors.keyText) - DrawableCompat.setTintList(this.background, colors.functionalKeyStateList) + colors.setBackgroundColor(this.background, Colors.TYPE_FUNCTIONAL) } else setTextColor(params.mFunctionalTextColor) setTextSize(TypedValue.COMPLEX_UNIT_PX, params.mLabelSize.toFloat()) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java index e23819c19..57d4978d4 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/keyboard/emoji/EmojiPalettesView.java @@ -19,7 +19,6 @@ package org.dslul.openboard.inputmethod.keyboard.emoji; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; -import android.graphics.PorterDuff; import android.util.AttributeSet; import android.util.TypedValue; import android.view.LayoutInflater; @@ -276,12 +275,9 @@ public final class EmojiPalettesView extends LinearLayout final Colors colors = Settings.getInstance().getCurrent().mColors; if (colors.isCustom) { - DrawableCompat.setTintList(mAlphabetKeyLeft.getBackground(), colors.functionalKeyStateList); - DrawableCompat.setTintList(mSpacebar.getBackground(), colors.spaceBarStateList); - DrawableCompat.setTintList(mDeleteKey.getBackground(), colors.functionalKeyStateList); - DrawableCompat.setTintMode(mAlphabetKeyLeft.getBackground(), PorterDuff.Mode.MULTIPLY); - DrawableCompat.setTintMode(mSpacebar.getBackground(), PorterDuff.Mode.MULTIPLY); - DrawableCompat.setTintMode(mDeleteKey.getBackground(), PorterDuff.Mode.MULTIPLY); + colors.setBackgroundColor(mAlphabetKeyLeft.getBackground(), Colors.TYPE_FUNCTIONAL); + colors.setBackgroundColor(mDeleteKey.getBackground(), Colors.TYPE_FUNCTIONAL); + colors.setBackgroundColor(mSpacebar.getBackground(), Colors.TYPE_SPACE); getBackground().setColorFilter(colors.backgroundFilter); mEmojiCategoryPageIndicatorView.setColors(colors.accent, colors.adjustedBackground); } diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java index c29504101..a956a4f21 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/common/Colors.java @@ -3,13 +3,14 @@ package org.dslul.openboard.inputmethod.latin.common; import android.content.res.ColorStateList; import android.graphics.Color; import android.graphics.ColorFilter; +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; import androidx.core.graphics.BlendModeColorFilterCompat; import androidx.core.graphics.BlendModeCompat; import androidx.annotation.ColorInt; import androidx.core.graphics.ColorUtils; - -import org.dslul.openboard.inputmethod.keyboard.KeyboardTheme; +import androidx.core.graphics.drawable.DrawableCompat; // todo: maybe kotlin? would make it much shorter and more readable public class Colors { @@ -35,12 +36,12 @@ public class Colors { public ColorFilter accentColorFilter; public ColorFilter actionKeyIconColorFilter; - public ColorStateList backgroundStateList; - public ColorStateList keyStateList; - public ColorStateList functionalKeyStateList; - public ColorStateList actionKeyStateList; - public ColorStateList spaceBarStateList; - public ColorStateList adjustedBackgroundStateList; + private ColorStateList backgroundStateList; + private ColorStateList keyStateList; + private ColorStateList functionalKeyStateList; + private ColorStateList actionKeyStateList; + private ColorStateList spaceBarStateList; + private ColorStateList adjustedBackgroundStateList; public Colors(int _accent, int _background, int _keyBackground, int _functionalKey, int _spaceBar, int _keyText, int _keyHintText) { isCustom = true; @@ -68,6 +69,42 @@ public class Colors { keyHintText = 0; } + /** set background colors including state list to the drawable */ + // todo: this can be used for setting more complicated filters + // may be necessary for reproducing holo theme (extend Colors and override this in sth like HoloColors?) + public void setBackgroundColor(final Drawable background, final int type) { + final ColorStateList list; + switch (type) { + case TYPE_KEY: + list = keyStateList; + break; + case TYPE_SPACE: + list = spaceBarStateList; + break; + case TYPE_ADJUSTED_BACKGROUND: + list = adjustedBackgroundStateList; + break; + case TYPE_ACTION: + list = actionKeyStateList; + break; + case TYPE_FUNCTIONAL: + list = functionalKeyStateList; + break; + case TYPE_BACKGROUND: + default: + list = backgroundStateList; + } + DrawableCompat.setTintMode(background, PorterDuff.Mode.MULTIPLY); + DrawableCompat.setTintList(background, list); + } + + public static final int TYPE_BACKGROUND = 0; + public static final int TYPE_KEY = 1; + public static final int TYPE_FUNCTIONAL = 2; + public static final int TYPE_ACTION = 3; + public static final int TYPE_SPACE = 4; + public static final int TYPE_ADJUSTED_BACKGROUND = 5; + public void createColorFilters(final boolean hasKeyBorders) { final int[][] states = new int[][] { // are other states used? diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java index 7a40cce47..6d382315f 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/suggestions/SuggestionStripView.java @@ -22,14 +22,12 @@ import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Color; -import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.TypedValue; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.Menu; -import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; @@ -41,7 +39,6 @@ import android.widget.ImageButton; import android.widget.PopupMenu; import android.widget.RelativeLayout; import android.widget.TextView; -import android.widget.Toast; import org.dslul.openboard.inputmethod.accessibility.AccessibilityUtils; import org.dslul.openboard.inputmethod.keyboard.Keyboard; @@ -196,8 +193,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick // it seems to work in other places, e.g. for btn_keyboard_spacebar_lxx_base... though maybe that's the weirdly nested layer list? // todo (later): when fixing this, revert changes in themes-lxx-base[-border] (in todo) // this would allow having a different background shape in pressed state - DrawableCompat.setTintList(getBackground(), colors.backgroundStateList); - DrawableCompat.setTintMode(getBackground(), PorterDuff.Mode.MULTIPLY); + colors.setBackgroundColor(getBackground(), Colors.TYPE_BACKGROUND); mClipboardKey.setColorFilter(colors.keyText); mVoiceKey.setColorFilter(colors.keyText);