Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[useAnimatedKeyboard][Android] Fix state after animation dismiss #5741

Merged
merged 10 commits into from
Mar 1, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class Keyboard {
private KeyboardState mState;
private int mHeight = 0;
private int mActiveTransitionCounter = 0;
private static final int CONTENT_TYPE_MASK = WindowInsetsCompat.Type.ime();
private static final int SYSTEM_BAR_TYPE_MASK = WindowInsetsCompat.Type.systemBars();

Expand All @@ -20,15 +21,31 @@ public int getHeight() {
public void updateHeight(WindowInsetsCompat insets) {
int contentBottomInset = insets.getInsets(CONTENT_TYPE_MASK).bottom;
int systemBarBottomInset = insets.getInsets(SYSTEM_BAR_TYPE_MASK).bottom;
int keyboardHeightDip = contentBottomInset - systemBarBottomInset;
mHeight = (int) PixelUtil.toDIPFromPixel(Math.max(0, keyboardHeightDip));
int keyboardHeightDip = Math.max(0, contentBottomInset - systemBarBottomInset);
if (keyboardHeightDip == 0 && mState == KeyboardState.OPEN) {
/*
When the keyboard is being canceling, for one frame the insets show a keyboard height of 0,
causing a jump of the keyboard. We can avoid it by ignoring that frame and calling
the listeners on the following frame.
*/
return;
}
mHeight = (int) PixelUtil.toDIPFromPixel(keyboardHeightDip);
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
}

public void onAnimationStart() {
mState = mHeight == 0 ? KeyboardState.OPENING : KeyboardState.CLOSING;
if (mActiveTransitionCounter > 0) {
mState = mState == KeyboardState.OPENING ? KeyboardState.CLOSING : KeyboardState.OPENING;
} else {
mState = mHeight == 0 ? KeyboardState.OPENING : KeyboardState.CLOSING;
}
mActiveTransitionCounter++;
}

public void onAnimationEnd() {
mState = mHeight == 0 ? KeyboardState.CLOSED : KeyboardState.OPEN;
mActiveTransitionCounter--;
if (mActiveTransitionCounter == 0) {
mState = mHeight == 0 ? KeyboardState.CLOSED : KeyboardState.OPEN;
}
}
}
Loading