use visual insets instead of spacers for edge keys (fixes #308, #294)

This commit is contained in:
Helium314 2023-12-04 12:30:06 +01:00
parent 2232bc3848
commit d438438aca
3 changed files with 37 additions and 5 deletions

View file

@ -947,6 +947,8 @@ public class Key implements Comparable<Key> {
private final KeyboardParams mKeyboardParams; // for reading gaps and keyboard width / height
public float mRelativeWidth;
public float mRelativeHeight; // also should allow negative values, indicating absolute height is defined
public float mRelativeVisualInsetLeft;
public float mRelativeVisualInsetRight;
// params that may change
public float mFullWidth;
@ -965,7 +967,7 @@ public class Key implements Comparable<Key> {
public final int mBackgroundType;
public final int mActionFlags;
@Nullable public final KeyVisualAttributes mKeyVisualAttributes;
@Nullable public final OptionalAttributes mOptionalAttributes;
@Nullable public OptionalAttributes mOptionalAttributes;
public final boolean mEnabled;
public static KeyParams newSpacer(final TypedArray keyAttr, final KeyStyle keyStyle,
@ -999,6 +1001,18 @@ public class Key implements Comparable<Key> {
yPos = newY;
mFullWidth = mRelativeWidth * mKeyboardParams.mBaseWidth;
mFullHeight = mRelativeHeight * mKeyboardParams.mBaseHeight;
// set visual insets if any
if (mRelativeVisualInsetRight != 0f || mRelativeVisualInsetLeft != 0f) {
final int insetLeft = (int) (mRelativeVisualInsetLeft * mKeyboardParams.mBaseWidth);
final int insetRight = (int) (mRelativeVisualInsetRight * mKeyboardParams.mBaseWidth);
if (mOptionalAttributes == null) {
mOptionalAttributes = OptionalAttributes.newInstance(null, CODE_UNSPECIFIED, ICON_UNDEFINED, insetLeft, insetRight);
} else {
mOptionalAttributes = OptionalAttributes
.newInstance(mOptionalAttributes.mOutputText, mOptionalAttributes.mAltCode, mOptionalAttributes.mDisabledIconId, insetLeft, insetRight);
}
}
}
private static int getMoreKeysColumnAndFlagsAndSetNullInArray(final KeyboardParams params, final String[] moreKeys) {
@ -1298,7 +1312,6 @@ public class Key implements Comparable<Key> {
: altCodeInAttr;
mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode,
// disabled icon only ever for old version of shortcut key, visual insets can be replaced with spacer
// todo (much later): can the 3 below be removed completely?
KeyboardIconsSet.ICON_UNDEFINED, 0, 0);
// KeyVisualAttributes for a key essentially are what the theme has, but on a per-key base
// could be used e.g. for having a color gradient on key color

View file

@ -164,7 +164,7 @@ open class KeyboardBuilder<KP : KeyboardParams>(protected val mContext: Context,
if (loadFromAssets(id) != null) {
if (!DebugFlags.DEBUG_ENABLED)
return this
// comparison of old and new parser below, remove once testing is complete
// comparison of old and new parser below, todo: remove once testing is complete
val keysInRowsFromXml = XmlKeyboardParser(xmlId, mParams, mContext).use { keyboardParser ->
keyboardParser.parseKeyboard()
}

View file

@ -115,7 +115,8 @@ abstract class KeyboardParser(private val params: KeyboardParams, private val co
spacerWidth = 0f
keyWidth = availableWidth / row.size
}
if (spacerWidth != 0f) {
if (spacerWidth != 0f && functionalKeysLeft.isNotEmpty()) {
// add a spacer between left functional key and keyboard key
paramsRow.add(KeyParams.newSpacer(params, spacerWidth))
}
if (keyWidth < params.mDefaultRelativeKeyWidth * 0.82 && spacerWidth == 0f) {
@ -137,7 +138,25 @@ abstract class KeyboardParser(private val params: KeyboardParams, private val co
Log.d(TAG, "adding key ${keyParams.mLabel}, ${keyParams.mCode}")
}
if (spacerWidth != 0f) {
paramsRow.add(KeyParams.newSpacer(params, spacerWidth))
// todo: the spacer-or-key-extension logic should go into a separate function
if (functionalKeysLeft.isEmpty()) {
// we did not add a spacer above, but extend the key to the edge and set visual insets
paramsRow.first().mRelativeWidth += spacerWidth
paramsRow.first().mRelativeVisualInsetLeft = spacerWidth
}
if (functionalKeysRight.isEmpty()) {
// extend right key to the edge and set visual insets
paramsRow.last().mRelativeWidth += spacerWidth
paramsRow.last().mRelativeVisualInsetRight = spacerWidth
} else {
// add a spacer between keyboard key and right functional key (like for the left side)
// unless it's the enter key, in that case increase the key's width (to match original layout in tablet mode)
if (functionalKeysRight.singleOrNull()?.mBackgroundType == Key.BACKGROUND_TYPE_ACTION) {
functionalKeysRight.single().mRelativeWidth += spacerWidth
} else {
paramsRow.add(KeyParams.newSpacer(params, spacerWidth))
}
}
}
functionalKeysRight.forEach { paramsRow.add(it) }
keysInRows.add(0, paramsRow) // we're doing it backwards, so add on top