fix some issues with key texts and icons

fixes #750
This commit is contained in:
Helium314 2024-05-12 14:09:43 +02:00
parent b3aac1a05a
commit 7f1161218e
3 changed files with 22 additions and 8 deletions

View file

@ -430,7 +430,13 @@ public class KeyboardView extends View {
paint.setTextAlign(Align.CENTER);
}
if (key.needsAutoXScale()) {
final float ratio = Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) / TypefaceUtils.getStringWidth(label, paint));
final int width;
if (key.needsToKeepBackgroundAspectRatio(mDefaultKeyLabelFlags)) {
// make sure the text stays inside bounds of background drawable
Drawable bg = key.selectBackgroundDrawable(mKeyBackground, mFunctionalKeyBackground, mSpacebarBackground, mActionKeyBackground);
width = Math.min(bg.getBounds().bottom, bg.getBounds().right);
} else width = keyWidth;
final float ratio = Math.min(1.0f, (width * MAX_LABEL_RATIO) / TypefaceUtils.getStringWidth(label, paint));
if (key.needsAutoScale()) {
final float autoSize = paint.getTextSize() * ratio;
paint.setTextSize(autoSize);
@ -442,6 +448,8 @@ public class KeyboardView extends View {
if (key.isEnabled()) {
if (StringUtils.mightBeEmoji(label))
paint.setColor(key.selectTextColor(params) | 0xFF000000); // ignore alpha for emojis (though actually color isn't applied anyway and we could just set white)
else if (key.hasActionKeyBackground())
paint.setColor(mColors.get(ColorType.ACTION_KEY_ICON));
else
paint.setColor(key.selectTextColor(params));
// Set a drop shadow for the text if the shadow radius is positive value.
@ -644,6 +652,8 @@ public class KeyboardView extends View {
} else if (key.getCode() == Constants.CODE_SPACE || key.getCode() == KeyCode.ZWNJ) {
// set color of default number pad space bar icon for Holo style, or for zero-width non-joiner (zwnj) on some layouts like nepal
mColors.setColor(icon, ColorType.KEY_ICON);
} else {
mColors.setColor(icon, ColorType.KEY_TEXT);
}
}

View file

@ -9,6 +9,7 @@ import androidx.annotation.StringRes
import helium314.keyboard.keyboard.Key
import helium314.keyboard.keyboard.Key.KeyParams
import helium314.keyboard.keyboard.KeyboardId
import helium314.keyboard.keyboard.KeyboardTheme
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.keyboard.internal.KeyboardParams
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyData
@ -315,7 +316,10 @@ abstract class KeyboardParser(private val params: KeyboardParams, private val co
newLabel = "${getActionKeyLabel()}|${getActionKeyCode()}",
newPopup = popup.merge(getActionKeyPopupKeys()?.let { SimplePopups(it) }),
// the label change is messing with toKeyParams, so we need to supply the appropriate BG type here
newType = type ?: KeyType.ENTER_EDITING
newType = type ?: KeyType.ENTER_EDITING,
newLabelFlags = Key.LABEL_FLAGS_PRESERVE_CASE or Key.LABEL_FLAGS_AUTO_X_SCALE or
Key.LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO or Key.LABEL_FLAGS_FOLLOW_FUNCTIONAL_TEXT_COLOR or
Key.LABEL_FLAGS_HAS_POPUP_HINT or KeyboardTheme.getThemeActionAndEmojiKeyLabelFlags(params.mThemeId)
)
else -> {
// this is ugly...

View file

@ -251,7 +251,7 @@ sealed interface KeyData : AbstractKeyData {
KeyLabel.SYMBOL_ALPHA, KeyLabel.SYMBOL, KeyLabel.ALPHA, KeyLabel.COMMA, KeyLabel.PERIOD, KeyLabel.DELETE,
KeyLabel.EMOJI, KeyLabel.COM, KeyLabel.LANGUAGE_SWITCH, KeyLabel.NUMPAD -> return Key.BACKGROUND_TYPE_FUNCTIONAL
KeyLabel.SPACE, KeyLabel.ZWNJ -> return Key.BACKGROUND_TYPE_SPACEBAR
KeyLabel.ACTION -> return Key.BACKGROUND_TYPE_ACTION
// KeyLabel.ACTION -> return Key.BACKGROUND_TYPE_ACTION
KeyLabel.SHIFT -> return getShiftBackground(params)
}
if (type == KeyType.PLACEHOLDER) return Key.BACKGROUND_TYPE_EMPTY
@ -313,11 +313,11 @@ sealed interface KeyData : AbstractKeyData {
return when (label) {
KeyLabel.ALPHA, KeyLabel.SYMBOL_ALPHA, KeyLabel.SYMBOL -> Key.LABEL_FLAGS_PRESERVE_CASE or Key.LABEL_FLAGS_FOLLOW_FUNCTIONAL_TEXT_COLOR
KeyLabel.PERIOD, KeyLabel.COMMA -> Key.LABEL_FLAGS_HAS_POPUP_HINT // todo: period also has defaultLabelFlags -> when is this relevant?
KeyLabel.ACTION -> {
Key.LABEL_FLAGS_PRESERVE_CASE or Key.LABEL_FLAGS_AUTO_X_SCALE or
Key.LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO or Key.LABEL_FLAGS_FOLLOW_FUNCTIONAL_TEXT_COLOR or
Key.LABEL_FLAGS_HAS_POPUP_HINT or KeyboardTheme.getThemeActionAndEmojiKeyLabelFlags(params.mThemeId)
}
// KeyLabel.ACTION -> {
// Key.LABEL_FLAGS_PRESERVE_CASE or Key.LABEL_FLAGS_AUTO_X_SCALE or
// Key.LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO or Key.LABEL_FLAGS_FOLLOW_FUNCTIONAL_TEXT_COLOR or
// Key.LABEL_FLAGS_HAS_POPUP_HINT or KeyboardTheme.getThemeActionAndEmojiKeyLabelFlags(params.mThemeId)
// }
KeyLabel.SPACE -> if (params.mId.isNumberLayout) Key.LABEL_FLAGS_ALIGN_ICON_TO_BOTTOM else 0
KeyLabel.SHIFT -> Key.LABEL_FLAGS_PRESERVE_CASE or if (!params.mId.isAlphabetKeyboard) Key.LABEL_FLAGS_FOLLOW_FUNCTIONAL_TEXT_COLOR else 0
KeyLabel.EMOJI -> KeyboardTheme.getThemeActionAndEmojiKeyLabelFlags(params.mThemeId)