Skip to content

Commit

Permalink
feat: add option to disable animated insets
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Aug 30, 2024
1 parent 7580965 commit 908fdd2
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 7 deletions.
18 changes: 18 additions & 0 deletions android/src/main/cxx/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void makeShared(JNIEnv *env, jobject unistylesModule, std::shared_ptr<UnistylesR
setRootViewBackgroundColor(env, unistylesModule, color, alpha);
});

unistylesRuntime->setDisableAnimatedInsetsAndroidCallback([=]() {
disableAnimatedInsets(env, unistylesModule);
});

Screen screen = getScreenDimensions(env, unistylesModule);

unistylesRuntime->screen = Dimensions{screen.width, screen.height};
Expand All @@ -62,6 +66,20 @@ void makeShared(JNIEnv *env, jobject unistylesModule, std::shared_ptr<UnistylesR
unistylesRuntime->rtl = getIsRTL(env, unistylesModule);
}

void disableAnimatedInsets(JNIEnv *env, jobject unistylesModule) {
jclass cls = env->GetObjectClass(unistylesModule);
jfieldID platformFieldId = env->GetFieldID(cls, "platform", "Lcom/unistyles/Platform;");
jobject platformInstance = env->GetObjectField(unistylesModule, platformFieldId);
jclass platformClass = env->GetObjectClass(platformInstance);
jmethodID methodId = env->GetMethodID(platformClass, "disableAnimatedInsets", "()V");

env->CallVoidMethod(platformInstance, methodId);

env->DeleteLocalRef(cls);
env->DeleteLocalRef(platformInstance);
env->DeleteLocalRef(platformClass);
}

Screen getScreenDimensions(JNIEnv *env, jobject unistylesModule) {
jobject result = JNI_callPlatform(env, unistylesModule, "getScreenDimensions", "()Lcom/unistyles/Screen;");
Screen screenDimensions = jobjectToScreen(env, result);
Expand Down
1 change: 1 addition & 0 deletions android/src/main/cxx/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ bool getIsRTL(JNIEnv *env, jobject unistylesModule);
std::string getContentSizeCategory(JNIEnv *env, jobject unistylesModule);
void setNavigationBarColor(JNIEnv *env, jobject unistylesModule, std::string color, float alpha);
void setStatusBarColor(JNIEnv *env, jobject unistylesModule, std::string color, float alpha);
void disableAnimatedInsets(JNIEnv *env, jobject unistylesModule);
13 changes: 12 additions & 1 deletion android/src/main/java/com/unistyles/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ import java.util.Locale
import kotlin.math.roundToInt

class Platform(private val reactApplicationContext: ReactApplicationContext) {
var hasAnimatedInsets = true
private var insets: Insets = Insets(0, 0, 0, 0)
private var defaultNavigationBarColor: Int = -1
private var defaultStatusBarColor: Int = -1

var orientation: Int = reactApplicationContext.resources.configuration.orientation

fun disableAnimatedInsets() {
this.hasAnimatedInsets = false
}

@Suppress("DEPRECATION")
fun getScreenDimensions(): Screen {
// function takes in count edge-to-edge layout
Expand Down Expand Up @@ -140,10 +145,16 @@ class Platform(private val reactApplicationContext: ReactApplicationContext) {
}

val insets = insetsCompat.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
val imeInsets = insetsCompat.getInsets(WindowInsetsCompat.Type.ime())

if (!hasAnimatedInsets) {
this.insets = Insets(statusBarTopInset, insets.bottom, insets.left, insets.right)

return
}

// Android 10 and below - set bottom insets to 0 while keyboard is visible and use default bottom insets otherwise
// Android 11 and above - animate bottom insets while keyboard is appearing and disappearing
val imeInsets = insetsCompat.getInsets(WindowInsetsCompat.Type.ime())
val insetBottom = when(imeInsets.bottom > 0) {
true -> {
if (Build.VERSION.SDK_INT >= 30 && animatedBottomInsets != null) {
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/unistyles/UnistylesModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
insets
}

if (Build.VERSION.SDK_INT >= 30) {
if (platform.hasAnimatedInsets && Build.VERSION.SDK_INT >= 30) {
ViewCompat.setWindowInsetsAnimationCallback(
mainView,
object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_STOP) {
Expand Down
10 changes: 10 additions & 0 deletions cxx/UnistylesImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,13 @@ jsi::Value UnistylesRuntime::setRootBackgroundColor(jsi::Runtime& rt, std::strin
jsi::Value UnistylesRuntime::getIsRtl(jsi::Runtime& rt, std::string fnName) {
return jsi::Value(this->rtl);
}

jsi::Value UnistylesRuntime::disableAnimatedInsets(jsi::Runtime& rt, std::string fnName) {
return HOST_FN(fnName, 1, {
if (this->disableAnimatedInsetsAndroid.has_value()) {
this->disableAnimatedInsetsAndroid.value()();
}

return jsi::Value::undefined();
});
}
4 changes: 4 additions & 0 deletions cxx/UnistylesModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct UnistylesModel {
std::optional<std::function<void(bool)>> setStatusBarHidden;
std::optional<std::function<void(bool)>> setImmersiveMode;
std::optional<std::function<void(std::string, float alpha)>> setRootViewBackgroundColor;
std::optional<std::function<void()>> disableAnimatedInsetsAndroid;

void setScreenDimensionsCallback(std::function<Screen()> callback) {
this->getScreenDimensions = callback;
Expand Down Expand Up @@ -103,6 +104,9 @@ struct UnistylesModel {
void setRootViewBackgroundColorCallback(std::function<void(std::string color, float alpha)> callback) {
this->setRootViewBackgroundColor = callback;
}
void setDisableAnimatedInsetsAndroidCallback(std::function<void()> callback) {
this->disableAnimatedInsetsAndroid = callback;
}

Dimensions screen = {0, 0};
Dimensions statusBar = {0, 0};
Expand Down
4 changes: 3 additions & 1 deletion cxx/UnistylesRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct JSI_EXPORT UnistylesRuntime : public jsi::HostObject, UnistylesModel {
{"fontScale", BIND_FN(getFontScale)},
{"setRootViewBackgroundColor", BIND_FN(setRootBackgroundColor)},
{"setImmersiveMode", BIND_FN(setImmersiveModeEnabled)},
{"rtl", BIND_FN(getIsRtl)}
{"rtl", BIND_FN(getIsRtl)},
{"disableAnimatedInsets", BIND_FN(disableAnimatedInsets)}
};

this->setters = {
Expand Down Expand Up @@ -66,6 +67,7 @@ struct JSI_EXPORT UnistylesRuntime : public jsi::HostObject, UnistylesModel {
jsi::Value setRootBackgroundColor(jsi::Runtime&, std::string);
jsi::Value setImmersiveModeEnabled(jsi::Runtime&, std::string);
jsi::Value getIsRtl(jsi::Runtime&, std::string);
jsi::Value disableAnimatedInsets(jsi::Runtime&, std::string);

std::optional<jsi::Value> setThemes(jsi::Runtime&, const jsi::Value&);

Expand Down
2 changes: 1 addition & 1 deletion docs/.astro/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_variables": {
"lastUpdateCheck": 1720447718845
"lastUpdateCheck": 1725018017706
}
}
3 changes: 2 additions & 1 deletion examples/expo/src/examples/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ export const HomeScreen = () => {
})
.addBreakpoints(breakpoints)
.addConfig({
initialTheme: 'light'
initialTheme: 'light',
disableAnimatedInsets: true
})

navigation.navigate(DemoNames.Keyboard)
Expand Down
6 changes: 5 additions & 1 deletion src/core/UnistyleRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UnistylesBridge, UnistylesConfig, UnistylesPlugin } from '../types'
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
import { isDev, isWeb, UnistylesError } from '../common'
import { isAndroid, isDev, isWeb, UnistylesError } from '../common'
import { cssMediaQueriesPlugin, normalizeWebStylesPlugin } from '../plugins'

export class UnistyleRegistry {
Expand Down Expand Up @@ -64,6 +64,10 @@ export class UnistyleRegistry {
this.unistylesBridge.setWindowResizeDebounceTimeMs(config.windowResizeDebounceTimeMs)
}

if (isAndroid && config.disableAnimatedInsets) {
this.unistylesBridge.disableAnimatedInsets()
}

return {
addBreakpoints: this.addBreakpoints,
addThemes: this.addThemes
Expand Down
6 changes: 5 additions & 1 deletion src/types/unistyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export type UnistylesConfig = {
initialTheme?: keyof UnistylesThemes,
plugins?: Array<UnistylesPlugin>,
experimentalCSSMediaQueries?: boolean,
windowResizeDebounceTimeMs?: number
windowResizeDebounceTimeMs?: number,
disableAnimatedInsets?: boolean
}

export type UnistylesBridge = {
Expand Down Expand Up @@ -66,6 +67,9 @@ export type UnistylesBridge = {

// web only
setWindowResizeDebounceTimeMs(timeMs: number): void

// android only
disableAnimatedInsets(): void
}

export type UnistylesThemeEvent = {
Expand Down

0 comments on commit 908fdd2

Please sign in to comment.