react-native-unistyles 2.8.0-beta.1 → 2.8.0-beta.3
Sign up to get free protection for your applications and to get access to all the features.
- package/android/CMakeLists.txt +8 -1
- package/android/build.gradle +4 -1
- package/android/src/main/cxx/cpp-adapter.cpp +10 -100
- package/android/src/main/cxx/helpers.h +2 -0
- package/android/src/main/cxx/platform.cpp +126 -0
- package/android/src/main/cxx/platform.h +15 -0
- package/android/src/main/java/com/unistyles/Models.kt +14 -35
- package/android/src/main/java/com/unistyles/Platform.kt +91 -10
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +84 -139
- package/cxx/Macros.h +11 -0
- package/cxx/UnistylesImpl.cpp +241 -0
- package/cxx/UnistylesModel.cpp +234 -0
- package/cxx/UnistylesModel.h +112 -0
- package/cxx/UnistylesRuntime.cpp +17 -388
- package/cxx/UnistylesRuntime.h +56 -95
- package/ios/UnistylesModule.h +8 -0
- package/ios/UnistylesModule.mm +12 -89
- package/ios/platform/Platform_Shared.h +5 -0
- package/ios/platform/Platform_Shared.mm +69 -0
- package/ios/platform/Platform_iOS.h +2 -9
- package/ios/platform/Platform_iOS.mm +47 -94
- package/ios/platform/Platform_macOS.h +1 -6
- package/ios/platform/Platform_macOS.mm +29 -29
- package/ios/platform/Platform_tvOS.h +2 -9
- package/ios/platform/Platform_tvOS.mm +30 -92
- package/ios/platform/Platform_visionOS.h +2 -8
- package/ios/platform/Platform_visionOS.mm +28 -83
- package/package.json +1 -1
- package/react-native-unistyles.podspec +3 -0
- package/android/src/main/java/com/unistyles/Config.kt +0 -116
- package/android/src/main/java/com/unistyles/Insets.kt +0 -141
- package/ios/UnistylesHelpers.h +0 -3
- package/ios/UnistylesHelpers.mm +0 -5
package/android/CMakeLists.txt
CHANGED
@@ -5,8 +5,11 @@ project(unistyles)
|
|
5
5
|
add_library(unistyles
|
6
6
|
SHARED
|
7
7
|
../cxx/UnistylesRuntime.cpp
|
8
|
+
../cxx/UnistylesModel.cpp
|
9
|
+
../cxx/UnistylesImpl.cpp
|
8
10
|
./src/main/cxx/cpp-adapter.cpp
|
9
11
|
./src/main/cxx/helpers.cpp
|
12
|
+
./src/main/cxx/platform.cpp
|
10
13
|
)
|
11
14
|
|
12
15
|
include_directories(
|
@@ -14,15 +17,19 @@ include_directories(
|
|
14
17
|
)
|
15
18
|
|
16
19
|
set_target_properties(unistyles PROPERTIES
|
17
|
-
CXX_STANDARD
|
20
|
+
CXX_STANDARD 20
|
18
21
|
CXX_STANDARD_REQUIRED ON
|
19
22
|
CXX_EXTENSIONS OFF
|
20
23
|
POSITION_INDEPENDENT_CODE ON
|
21
24
|
)
|
22
25
|
|
23
26
|
find_package(ReactAndroid REQUIRED CONFIG)
|
27
|
+
find_package(fbjni REQUIRED CONFIG)
|
24
28
|
|
25
29
|
target_link_libraries(unistyles
|
26
30
|
ReactAndroid::jsi
|
31
|
+
ReactAndroid::turbomodulejsijni
|
32
|
+
ReactAndroid::react_nativemodule_core
|
27
33
|
android
|
34
|
+
fbjni::fbjni
|
28
35
|
)
|
package/android/build.gradle
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
#include <jni.h>
|
2
2
|
#include <jsi/jsi.h>
|
3
3
|
#include <map>
|
4
|
+
#include <ReactCommon/CallInvokerHolder.h>
|
4
5
|
#include "UnistylesRuntime.h"
|
5
6
|
#include "helpers.h"
|
7
|
+
#include "platform.h"
|
6
8
|
|
7
9
|
using namespace facebook;
|
8
10
|
|
@@ -11,116 +13,24 @@ std::shared_ptr<UnistylesRuntime> unistylesRuntime = nullptr;
|
|
11
13
|
|
12
14
|
extern "C"
|
13
15
|
JNIEXPORT void JNICALL
|
14
|
-
Java_com_unistyles_UnistylesModule_nativeInstall(
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
jstring colorScheme,
|
20
|
-
jstring contentSizeCategory,
|
21
|
-
jobject insets,
|
22
|
-
jobject statusBar,
|
23
|
-
jobject navigationBar
|
24
|
-
) {
|
25
|
-
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);
|
16
|
+
Java_com_unistyles_UnistylesModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi, jobject callInvokerHolder) {
|
17
|
+
auto runtime = reinterpret_cast<jsi::Runtime *>(jsi);
|
18
|
+
auto callInvoker{
|
19
|
+
jni::alias_ref<react::CallInvokerHolder::javaobject>{ reinterpret_cast<react::CallInvokerHolder::javaobject>(callInvokerHolder)} -> cthis() ->getCallInvoker()
|
20
|
+
};
|
26
21
|
|
27
22
|
if (unistylesModule == nullptr) {
|
28
23
|
unistylesModule = env->NewGlobalRef(thiz);
|
29
24
|
}
|
30
25
|
|
31
|
-
if (
|
26
|
+
if (unistylesModule == nullptr) {
|
32
27
|
return throwKotlinException(env, "Something went wrong while initializing UnistylesModule");
|
33
28
|
}
|
34
29
|
|
35
|
-
|
36
|
-
|
37
|
-
env->ReleaseStringUTFChars(colorScheme, colorSchemeChars);
|
38
|
-
|
39
|
-
const char *contentSizeCategoryChars = env->GetStringUTFChars(contentSizeCategory, nullptr);
|
40
|
-
std::string contentSizeCategoryStr(contentSizeCategoryChars);
|
41
|
-
env->ReleaseStringUTFChars(contentSizeCategory, contentSizeCategoryChars);
|
42
|
-
|
43
|
-
unistylesRuntime = std::make_shared<UnistylesRuntime>(
|
44
|
-
jobjectToDimensions(env, screen),
|
45
|
-
colorSchemeStr,
|
46
|
-
contentSizeCategoryStr,
|
47
|
-
jobjectToInsets(env, insets),
|
48
|
-
jobjectToDimensions(env, statusBar),
|
49
|
-
jobjectToDimensions(env, navigationBar)
|
50
|
-
);
|
51
|
-
|
52
|
-
unistylesRuntime->onThemeChange([=](const std::string &theme) {
|
53
|
-
jstring themeStr = env->NewStringUTF(theme.c_str());
|
54
|
-
jclass cls = env->GetObjectClass(unistylesModule);
|
55
|
-
jmethodID methodId = env->GetMethodID(cls, "onThemeChange", "(Ljava/lang/String;)V");
|
56
|
-
|
57
|
-
env->CallVoidMethod(unistylesModule, methodId, themeStr);
|
58
|
-
env->DeleteLocalRef(themeStr);
|
59
|
-
env->DeleteLocalRef(cls);
|
60
|
-
});
|
61
|
-
|
62
|
-
unistylesRuntime->onLayoutChange([=](const std::string &breakpoint, const std::string &orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar) {
|
63
|
-
jstring breakpointStr = env->NewStringUTF(breakpoint.c_str());
|
64
|
-
jstring orientationStr = env->NewStringUTF(orientation.c_str());
|
65
|
-
jclass cls = env->GetObjectClass(unistylesModule);
|
66
|
-
jclass dimensionsClass = env->FindClass("com/unistyles/Dimensions");
|
67
|
-
jmethodID dimensionsConstructor = env->GetMethodID(dimensionsClass, "<init>", "(II)V");
|
68
|
-
jobject screenObj = env->NewObject(dimensionsClass, dimensionsConstructor, screen.width, screen.height);
|
69
|
-
jobject statusBarObj = env->NewObject(dimensionsClass, dimensionsConstructor, statusBar.width, statusBar.height);
|
70
|
-
jobject navigationBarObj = env->NewObject(dimensionsClass, dimensionsConstructor, navigationBar.width, navigationBar.height);
|
71
|
-
|
72
|
-
jclass insetsClass = env->FindClass("com/unistyles/Insets");
|
73
|
-
jmethodID insetsConstructor = env->GetMethodID(insetsClass, "<init>", "(IIII)V");
|
74
|
-
jobject insetsObj = env->NewObject(insetsClass, insetsConstructor, insets.left, insets.top, insets.right, insets.bottom);
|
75
|
-
jmethodID methodId = env->GetMethodID(cls, "onLayoutChange",
|
76
|
-
"(Ljava/lang/String;Ljava/lang/String;Lcom/unistyles/Dimensions;Lcom/unistyles/Dimensions;Lcom/unistyles/Insets;Lcom/unistyles/Dimensions;)V");
|
77
|
-
|
78
|
-
env->CallVoidMethod(unistylesModule, methodId, breakpointStr, orientationStr, screenObj, statusBarObj, insetsObj, navigationBarObj);
|
79
|
-
env->DeleteLocalRef(breakpointStr);
|
80
|
-
env->DeleteLocalRef(orientationStr);
|
81
|
-
env->DeleteLocalRef(cls);
|
82
|
-
});
|
83
|
-
|
84
|
-
unistylesRuntime->onPluginChange([=]() {
|
85
|
-
jclass cls = env->GetObjectClass(unistylesModule);
|
86
|
-
jmethodID methodId = env->GetMethodID(cls, "onPluginChange", "()V");
|
87
|
-
|
88
|
-
env->CallVoidMethod(unistylesModule, methodId);
|
89
|
-
env->DeleteLocalRef(cls);
|
90
|
-
});
|
91
|
-
|
92
|
-
unistylesRuntime->onContentSizeCategoryChange([=](const std::string &contentSizeCategory) {
|
93
|
-
jstring contentSizeCategoryStr = env->NewStringUTF(contentSizeCategory.c_str());
|
94
|
-
jclass cls = env->GetObjectClass(unistylesModule);
|
95
|
-
jmethodID methodId = env->GetMethodID(cls, "onContentSizeCategoryChange", "(Ljava/lang/String;)V");
|
96
|
-
|
97
|
-
env->CallVoidMethod(unistylesModule, methodId, contentSizeCategoryStr);
|
98
|
-
env->DeleteLocalRef(contentSizeCategoryStr);
|
99
|
-
env->DeleteLocalRef(cls);
|
100
|
-
});
|
101
|
-
|
102
|
-
unistylesRuntime->onSetNavigationBarColor([=](const std::string &color) {
|
103
|
-
jstring colorStr = env->NewStringUTF(color.c_str());
|
104
|
-
jclass cls = env->GetObjectClass(unistylesModule);
|
105
|
-
jmethodID methodId = env->GetMethodID(cls, "onSetNavigationBarColor", "(Ljava/lang/String;)V");
|
106
|
-
|
107
|
-
env->CallVoidMethod(unistylesModule, methodId, colorStr);
|
108
|
-
env->DeleteLocalRef(colorStr);
|
109
|
-
env->DeleteLocalRef(cls);
|
110
|
-
});
|
111
|
-
|
112
|
-
unistylesRuntime->onSetStatusBarColor([=](const std::string &color) {
|
113
|
-
jstring colorStr = env->NewStringUTF(color.c_str());
|
114
|
-
jclass cls = env->GetObjectClass(unistylesModule);
|
115
|
-
jmethodID methodId = env->GetMethodID(cls, "onSetStatusBarColor", "(Ljava/lang/String;)V");
|
116
|
-
|
117
|
-
env->CallVoidMethod(unistylesModule, methodId, colorStr);
|
118
|
-
env->DeleteLocalRef(colorStr);
|
119
|
-
env->DeleteLocalRef(cls);
|
120
|
-
});
|
30
|
+
unistylesRuntime = std::make_shared<UnistylesRuntime>(*runtime, callInvoker);
|
31
|
+
makeShared(env, unistylesModule, unistylesRuntime);
|
121
32
|
|
122
33
|
jsi::Object hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);
|
123
|
-
|
124
34
|
runtime->global().setProperty(*runtime, "__UNISTYLES__", std::move(hostObject));
|
125
35
|
}
|
126
36
|
|
@@ -0,0 +1,126 @@
|
|
1
|
+
#include "platform.h"
|
2
|
+
|
3
|
+
void makeShared(JNIEnv *env, jobject unistylesModule, std::shared_ptr<UnistylesRuntime> unistylesRuntime) {
|
4
|
+
unistylesRuntime->setScreenDimensionsCallback([&](){
|
5
|
+
return getScreenDimensions(env, unistylesModule);
|
6
|
+
});
|
7
|
+
|
8
|
+
unistylesRuntime->setColorSchemeCallback([&](){
|
9
|
+
return getColorScheme(env, unistylesModule);
|
10
|
+
});
|
11
|
+
|
12
|
+
unistylesRuntime->setStatusBarDimensionsCallback([&](){
|
13
|
+
return getStatusBarDimensions(env, unistylesModule);
|
14
|
+
});
|
15
|
+
|
16
|
+
unistylesRuntime->setNavigationBarDimensionsCallback([&](){
|
17
|
+
return getNavigationBarDimensions(env, unistylesModule);
|
18
|
+
});
|
19
|
+
|
20
|
+
unistylesRuntime->setInsetsCallback([&](){
|
21
|
+
return getInsets(env, unistylesModule);
|
22
|
+
});
|
23
|
+
|
24
|
+
unistylesRuntime->setContentSizeCategoryCallback([&](){
|
25
|
+
return getContentSizeCategory(env, unistylesModule);
|
26
|
+
});
|
27
|
+
|
28
|
+
unistylesRuntime->setNavigationBarColorCallback([=](const std::string &color) {
|
29
|
+
setNavigationBarColor(env, unistylesModule, color);
|
30
|
+
});
|
31
|
+
|
32
|
+
unistylesRuntime->setStatusBarColorCallback([=](const std::string &color) {
|
33
|
+
setStatusBarColor(env, unistylesModule, color);
|
34
|
+
});
|
35
|
+
|
36
|
+
unistylesRuntime->screen = getScreenDimensions(env, unistylesModule);
|
37
|
+
unistylesRuntime->contentSizeCategory = getContentSizeCategory(env, unistylesModule);
|
38
|
+
unistylesRuntime->colorScheme = getColorScheme(env, unistylesModule);
|
39
|
+
unistylesRuntime->statusBar = getStatusBarDimensions(env, unistylesModule);
|
40
|
+
unistylesRuntime->insets = getInsets(env, unistylesModule);
|
41
|
+
unistylesRuntime->navigationBar = getNavigationBarDimensions(env, unistylesModule);
|
42
|
+
}
|
43
|
+
|
44
|
+
Dimensions getScreenDimensions(JNIEnv *env, jobject unistylesModule) {
|
45
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
46
|
+
jmethodID methodId = env->GetMethodID(cls, "getScreenDimensions", "()Lcom/unistyles/Dimensions;");
|
47
|
+
jobject dimensionsObj = env->CallObjectMethod(unistylesModule, methodId);
|
48
|
+
Dimensions screenDimensions = jobjectToDimensions(env, dimensionsObj);
|
49
|
+
|
50
|
+
return screenDimensions;
|
51
|
+
}
|
52
|
+
|
53
|
+
std::string getColorScheme(JNIEnv *env, jobject unistylesModule) {
|
54
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
55
|
+
jmethodID methodId = env->GetMethodID(cls, "getColorScheme", "()Ljava/lang/String;");
|
56
|
+
jstring colorScheme = (jstring) env->CallObjectMethod(unistylesModule, methodId);
|
57
|
+
const char *colorSchemeChars = env->GetStringUTFChars(colorScheme, nullptr);
|
58
|
+
std::string colorSchemeStr = std::string(colorSchemeChars);
|
59
|
+
|
60
|
+
env->ReleaseStringUTFChars(colorScheme, colorSchemeChars);
|
61
|
+
env->DeleteLocalRef(colorScheme);
|
62
|
+
env->DeleteLocalRef(cls);
|
63
|
+
|
64
|
+
return colorSchemeStr;
|
65
|
+
}
|
66
|
+
|
67
|
+
Dimensions getStatusBarDimensions(JNIEnv *env, jobject unistylesModule) {
|
68
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
69
|
+
jmethodID methodId = env->GetMethodID(cls, "getStatusBarDimensions", "()Lcom/unistyles/Dimensions;");
|
70
|
+
jobject dimensionsObj = env->CallObjectMethod(unistylesModule, methodId);
|
71
|
+
Dimensions statusBarDimensions = jobjectToDimensions(env, dimensionsObj);
|
72
|
+
|
73
|
+
return statusBarDimensions;
|
74
|
+
}
|
75
|
+
|
76
|
+
Dimensions getNavigationBarDimensions(JNIEnv *env, jobject unistylesModule) {
|
77
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
78
|
+
jmethodID methodId = env->GetMethodID(cls, "getNavigationBarDimensions", "()Lcom/unistyles/Dimensions;");
|
79
|
+
jobject dimensionsObj = env->CallObjectMethod(unistylesModule, methodId);
|
80
|
+
Dimensions navigationBarDimensions = jobjectToDimensions(env, dimensionsObj);
|
81
|
+
|
82
|
+
return navigationBarDimensions;
|
83
|
+
}
|
84
|
+
|
85
|
+
Insets getInsets(JNIEnv *env, jobject unistylesModule) {
|
86
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
87
|
+
jmethodID methodId = env->GetMethodID(cls, "getInsets", "()Lcom/unistyles/Insets;");
|
88
|
+
jobject insetsObj = env->CallObjectMethod(unistylesModule, methodId);
|
89
|
+
Insets insets = jobjectToInsets(env, insetsObj);
|
90
|
+
|
91
|
+
return insets;
|
92
|
+
}
|
93
|
+
|
94
|
+
std::string getContentSizeCategory(JNIEnv *env, jobject unistylesModule) {
|
95
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
96
|
+
jmethodID methodId = env->GetMethodID(cls, "getContentSizeCategory", "()Ljava/lang/String;");
|
97
|
+
jstring contentSizeCategory = (jstring) env->CallObjectMethod(unistylesModule, methodId);
|
98
|
+
const char *contentSizeCategoryChars = env->GetStringUTFChars(contentSizeCategory, nullptr);
|
99
|
+
std::string contentSizeCategoryStr = std::string(contentSizeCategoryChars);
|
100
|
+
|
101
|
+
env->ReleaseStringUTFChars(contentSizeCategory, contentSizeCategoryChars);
|
102
|
+
env->DeleteLocalRef(contentSizeCategory);
|
103
|
+
env->DeleteLocalRef(cls);
|
104
|
+
|
105
|
+
return contentSizeCategoryStr;
|
106
|
+
}
|
107
|
+
|
108
|
+
void setStatusBarColor(JNIEnv *env, jobject unistylesModule, std::string color) {
|
109
|
+
jstring colorStr = env->NewStringUTF(color.c_str());
|
110
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
111
|
+
jmethodID methodId = env->GetMethodID(cls, "onSetStatusBarColor", "(Ljava/lang/String;)V");
|
112
|
+
|
113
|
+
env->CallVoidMethod(unistylesModule, methodId, colorStr);
|
114
|
+
env->DeleteLocalRef(colorStr);
|
115
|
+
env->DeleteLocalRef(cls);
|
116
|
+
}
|
117
|
+
|
118
|
+
void setNavigationBarColor(JNIEnv *env, jobject unistylesModule, std::string color) {
|
119
|
+
jstring colorStr = env->NewStringUTF(color.c_str());
|
120
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
121
|
+
jmethodID methodId = env->GetMethodID(cls, "onSetNavigationBarColor", "(Ljava/lang/String;)V");
|
122
|
+
|
123
|
+
env->CallVoidMethod(unistylesModule, methodId, colorStr);
|
124
|
+
env->DeleteLocalRef(colorStr);
|
125
|
+
env->DeleteLocalRef(cls);
|
126
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include "UnistylesRuntime.h"
|
4
|
+
#include "helpers.h"
|
5
|
+
|
6
|
+
void makeShared(JNIEnv *env, jobject unistylesModule, std::shared_ptr<UnistylesRuntime> unistylesRuntime);
|
7
|
+
Dimensions getScreenDimensions(JNIEnv *env, jobject unistylesModule);
|
8
|
+
std::string getColorScheme(JNIEnv *env, jobject unistylesModule);
|
9
|
+
Dimensions getStatusBarDimensions(JNIEnv *env, jobject unistylesModule);
|
10
|
+
Dimensions getNavigationBarDimensions(JNIEnv *env, jobject unistylesModule);
|
11
|
+
Insets getInsets(JNIEnv *env, jobject unistylesModule);
|
12
|
+
std::string getContentSizeCategory(JNIEnv *env, jobject unistylesModule);
|
13
|
+
|
14
|
+
void setNavigationBarColor(JNIEnv *env, jobject unistylesModule, std::string color);
|
15
|
+
void setStatusBarColor(JNIEnv *env, jobject unistylesModule, std::string color);
|
@@ -36,50 +36,29 @@ class Insets(var top: Int, var bottom: Int, var left: Int, var right: Int) {
|
|
36
36
|
}
|
37
37
|
}
|
38
38
|
|
39
|
-
class
|
40
|
-
val
|
41
|
-
val
|
42
|
-
val
|
43
|
-
val navigationBar: Dimensions
|
39
|
+
class InsetsCompat(
|
40
|
+
val statusBar: Insets,
|
41
|
+
val navigationBar: Insets,
|
42
|
+
val cutout: Insets
|
44
43
|
) {
|
45
|
-
fun isEqual(config: LayoutConfig): Boolean {
|
46
|
-
if (!this.screen.isEqual(config.screen)) {
|
47
|
-
return false
|
48
|
-
}
|
49
|
-
|
50
|
-
if (!this.insets.isEqual(config.insets)) {
|
51
|
-
return false
|
52
|
-
}
|
53
|
-
|
54
|
-
if (!this.statusBar.isEqual(config.statusBar)) {
|
55
|
-
return false
|
56
|
-
}
|
57
|
-
|
58
|
-
return this.navigationBar.isEqual(config.navigationBar)
|
59
|
-
}
|
60
|
-
|
61
44
|
override fun toString(): String {
|
62
45
|
return buildString {
|
63
|
-
append("screen=")
|
64
|
-
append(screen)
|
65
|
-
append(" insets=")
|
66
|
-
append(insets)
|
67
46
|
append(" statusBar=")
|
68
47
|
append(statusBar)
|
69
48
|
append(" navigationBar=")
|
70
49
|
append(navigationBar)
|
50
|
+
append(" cutout=")
|
51
|
+
append(cutout)
|
71
52
|
}
|
72
53
|
}
|
73
|
-
}
|
74
54
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
)
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
return "colorScheme=${colorScheme} contentSizeCategory:${contentSizeCategory}"
|
55
|
+
companion object {
|
56
|
+
fun getDefaults(): InsetsCompat {
|
57
|
+
return InsetsCompat(
|
58
|
+
Insets(0, 0, 0, 0),
|
59
|
+
Insets(0, 0, 0, 0),
|
60
|
+
Insets(0, 0, 0, 0)
|
61
|
+
)
|
62
|
+
}
|
84
63
|
}
|
85
64
|
}
|
@@ -1,26 +1,107 @@
|
|
1
1
|
package com.unistyles
|
2
2
|
|
3
|
+
import android.content.res.Configuration
|
4
|
+
import android.graphics.Rect
|
5
|
+
import android.util.DisplayMetrics
|
6
|
+
import android.view.View
|
7
|
+
import androidx.core.view.WindowInsetsCompat
|
3
8
|
import com.facebook.react.bridge.ReactApplicationContext
|
9
|
+
import kotlin.math.max
|
4
10
|
|
5
|
-
class Platform(reactApplicationContext: ReactApplicationContext) {
|
6
|
-
private val
|
11
|
+
class Platform(private val reactApplicationContext: ReactApplicationContext) {
|
12
|
+
private val displayMetrics: DisplayMetrics = reactApplicationContext.resources.displayMetrics
|
13
|
+
private val density: Float = reactApplicationContext.resources.displayMetrics.density
|
14
|
+
private var insetsCompat: InsetsCompat = InsetsCompat.getDefaults()
|
7
15
|
|
8
16
|
var defaultNavigationBarColor: Int? = null
|
9
17
|
var defaultStatusBarColor: Int? = null
|
10
18
|
|
11
|
-
fun
|
12
|
-
|
19
|
+
fun getScreenDimensions(): Dimensions {
|
20
|
+
val screenWidth = (displayMetrics.widthPixels / density).toInt()
|
21
|
+
val screenHeight = (displayMetrics.heightPixels / density).toInt()
|
22
|
+
|
23
|
+
return Dimensions(screenWidth, screenHeight)
|
24
|
+
}
|
25
|
+
|
26
|
+
fun getColorScheme(): String {
|
27
|
+
val uiMode = this.reactApplicationContext.resources.configuration.uiMode
|
28
|
+
|
29
|
+
val colorScheme = when (uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
|
30
|
+
Configuration.UI_MODE_NIGHT_YES -> "dark"
|
31
|
+
Configuration.UI_MODE_NIGHT_NO -> "light"
|
32
|
+
else -> "unspecified"
|
33
|
+
}
|
34
|
+
|
35
|
+
return colorScheme
|
36
|
+
}
|
37
|
+
|
38
|
+
fun getStatusBarDimensions(): Dimensions {
|
39
|
+
val screenWidth = (displayMetrics.widthPixels / density).toInt()
|
40
|
+
|
41
|
+
return Dimensions(screenWidth, getStatusBarHeight())
|
42
|
+
}
|
43
|
+
|
44
|
+
fun getNavigationBarDimensions(): Dimensions {
|
45
|
+
val screenWidth = (displayMetrics.widthPixels / density).toInt()
|
46
|
+
|
47
|
+
return Dimensions(screenWidth, getNavigationBarHeight())
|
13
48
|
}
|
14
49
|
|
15
|
-
fun
|
16
|
-
|
50
|
+
fun getContentSizeCategory(): String {
|
51
|
+
val fontScale = reactApplicationContext.resources.configuration.fontScale
|
52
|
+
|
53
|
+
val contentSizeCategory = when {
|
54
|
+
fontScale <= 0.85f -> "Small"
|
55
|
+
fontScale <= 1.0f -> "Default"
|
56
|
+
fontScale <= 1.15f -> "Large"
|
57
|
+
fontScale <= 1.3f -> "ExtraLarge"
|
58
|
+
fontScale <= 1.5f -> "Huge"
|
59
|
+
fontScale <= 1.8 -> "ExtraHuge"
|
60
|
+
else -> "ExtraExtraHuge"
|
61
|
+
}
|
62
|
+
|
63
|
+
return contentSizeCategory
|
64
|
+
}
|
65
|
+
|
66
|
+
fun setInsetsCompat(insetsCompat: WindowInsetsCompat, decorView: View) {
|
67
|
+
val statusBar = insetsCompat.getInsets(WindowInsetsCompat.Type.statusBars())
|
68
|
+
val navigationBar = insetsCompat.getInsets(WindowInsetsCompat.Type.navigationBars())
|
69
|
+
val cutout = insetsCompat.getInsets(WindowInsetsCompat.Type.displayCutout())
|
70
|
+
|
71
|
+
// get the visible frame of the window to detect translucent status bar
|
72
|
+
// react native (and expo) are setting top inset to 0
|
73
|
+
// so there is no other way to detect if status bar is hidden or translucent
|
74
|
+
val visibleFrame = Rect()
|
75
|
+
decorView.getWindowVisibleDisplayFrame(visibleFrame)
|
76
|
+
|
77
|
+
val visibleTopFrame = max(visibleFrame.top, statusBar.top)
|
78
|
+
|
79
|
+
this.insetsCompat = InsetsCompat(
|
80
|
+
Insets(visibleTopFrame, statusBar.bottom, statusBar.left, statusBar.right),
|
81
|
+
Insets(navigationBar.top, navigationBar.bottom, navigationBar.left, navigationBar.right),
|
82
|
+
Insets(cutout.top, cutout.bottom, cutout.left, cutout.right)
|
83
|
+
)
|
84
|
+
}
|
85
|
+
|
86
|
+
fun getInsets(): Insets {
|
87
|
+
val top = max(this.insetsCompat.cutout.top, this.insetsCompat.statusBar.top)
|
88
|
+
val bottom = this.insetsCompat.navigationBar.bottom
|
89
|
+
val left = this.insetsCompat.statusBar.left
|
90
|
+
val right = this.insetsCompat.statusBar.right
|
91
|
+
|
92
|
+
return Insets(
|
93
|
+
(top / density).toInt(),
|
94
|
+
(bottom / density).toInt(),
|
95
|
+
(left / density).toInt(),
|
96
|
+
(right / density).toInt()
|
97
|
+
)
|
17
98
|
}
|
18
99
|
|
19
|
-
fun
|
20
|
-
return this.
|
100
|
+
private fun getStatusBarHeight(): Int {
|
101
|
+
return (this.insetsCompat.statusBar.top / density).toInt()
|
21
102
|
}
|
22
103
|
|
23
|
-
fun
|
24
|
-
return this.
|
104
|
+
private fun getNavigationBarHeight(): Int {
|
105
|
+
return (this.insetsCompat.navigationBar.bottom / density).toInt()
|
25
106
|
}
|
26
107
|
}
|