@momo-kits/native-kits 0.162.17-debug → 0.162.18-debug
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +1 -1
- package/compose/build.gradle.kts +1 -1
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +5 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/NavigationContainer.kt +3 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +4 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/KitFontScale.kt +22 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +8 -2
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +93 -75
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +9 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +66 -0
- package/gradle.properties +1 -1
- package/ios/Typography/FontScaleStore.swift +19 -0
- package/ios/Typography/Text.swift +3 -0
- package/ios/Typography/Typography.swift +3 -0
- package/package.json +1 -1
package/CLAUDE.md
CHANGED
|
@@ -40,7 +40,7 @@ Publishing is automated (`publish.sh` + GitLab CI on `[ci build]` / `main`). Do
|
|
|
40
40
|
## Architecture essentials
|
|
41
41
|
|
|
42
42
|
- **Entry point:** wrap content in `NavigationContainer(...)` from `navigation/`. It builds the `Navigator`, takes a host-supplied `Localize` (falls back to an empty one), and provides the CompositionLocals below. **Do not use** `ApplicationContainer` (`application/NavigationContainer.kt`) — deprecated and does **not** wire `LocalLocalize` / `LocalNavigator` / `LocalMaxApi`.
|
|
43
|
-
- **Configuration flows via CompositionLocals**, not params. Public ones: `AppTheme`, `AppStatusBar`, `AppNavigationBar` (`const/Theme.kt`); `ApplicationContext`, `AppConfig`, `AppLanguage`, `LocalLocalize
|
|
43
|
+
- **Configuration flows via CompositionLocals**, not params. Public ones: `AppTheme`, `AppStatusBar`, `AppNavigationBar` (`const/Theme.kt`); `ApplicationContext`, `AppConfig`, `AppLanguage`, `LocalLocalize` (`application/`); `LocalNavigator`, `LocalMaxApi` (`navigation/`). Read with `X.current`.
|
|
44
44
|
- **Platform code** goes through `expect`/`actual` in `platform/` — never reference Android/iOS APIs from `commonMain` directly.
|
|
45
45
|
- **Native bridge:** `IMaxApi` (`vn.momo.maxapi`, exposed as `LocalMaxApi`) is the host-app bridge (dismiss, tracking, device info, language).
|
|
46
46
|
- Navigation uses a runtime `DynamicScreenRegistry`; overlays (modal/bottom-sheet/snackbar) go through `Navigator` + `OverplayComponentRegistry`.
|
package/compose/build.gradle.kts
CHANGED
|
@@ -29,6 +29,8 @@ data class MiniAppContext(
|
|
|
29
29
|
val permissions: List<Map<String, Any>>? = emptyList(),
|
|
30
30
|
val features: FeatureFlags? = null,
|
|
31
31
|
val scaleSizeMaxRate: Float? = null,
|
|
32
|
+
val userScaleRate: Float? = null,
|
|
33
|
+
val useOSScaleRate: Boolean? = null,
|
|
32
34
|
) {
|
|
33
35
|
companion object {
|
|
34
36
|
|
|
@@ -61,6 +63,8 @@ data class MiniAppContext(
|
|
|
61
63
|
permissions = if (!parent.permissions.isNullOrEmpty()) parent.permissions else child.permissions,
|
|
62
64
|
features = mergeFeatureFlags(parent.features, child.features),
|
|
63
65
|
scaleSizeMaxRate = parent.scaleSizeMaxRate ?: child.scaleSizeMaxRate,
|
|
66
|
+
userScaleRate = parent.userScaleRate ?: child.userScaleRate,
|
|
67
|
+
useOSScaleRate = parent.useOSScaleRate ?: child.useOSScaleRate,
|
|
64
68
|
)
|
|
65
69
|
}
|
|
66
70
|
|
|
@@ -98,4 +102,5 @@ val LocalComponentInformation = staticCompositionLocalOf<ComponentInformation?>
|
|
|
98
102
|
null
|
|
99
103
|
}
|
|
100
104
|
|
|
105
|
+
@Deprecated("Max font scale is fixed at MAX_FONT_SCALE (1.5); this CompositionLocal is no longer read.")
|
|
101
106
|
val ScaleSizeMaxRate = staticCompositionLocalOf<Float?> { null }
|
|
@@ -7,6 +7,7 @@ import androidx.compose.ui.Modifier
|
|
|
7
7
|
import androidx.compose.ui.unit.Dp
|
|
8
8
|
import vn.momo.kits.const.*
|
|
9
9
|
import vn.momo.kits.modifier.DeprecatedModifier
|
|
10
|
+
import vn.momo.kits.platform.ProvideOsFontScale
|
|
10
11
|
import vn.momo.kits.platform.getStatusBarHeight
|
|
11
12
|
|
|
12
13
|
@Deprecated("Use IMaxApi instead", ReplaceWith("IMaxApi"))
|
|
@@ -93,6 +94,7 @@ fun ApplicationContainer(
|
|
|
93
94
|
|
|
94
95
|
val appStatusBarHeight = statusBarHeight ?: getStatusBarHeight()
|
|
95
96
|
|
|
97
|
+
ProvideOsFontScale {
|
|
96
98
|
CompositionLocalProvider(
|
|
97
99
|
AppTheme provides appTheme,
|
|
98
100
|
PlatformApi provides composeApi,
|
|
@@ -103,5 +105,6 @@ fun ApplicationContainer(
|
|
|
103
105
|
content()
|
|
104
106
|
}
|
|
105
107
|
}
|
|
108
|
+
}
|
|
106
109
|
|
|
107
110
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
package vn.momo.kits.application
|
|
2
|
+
|
|
3
|
+
import androidx.compose.runtime.Composable
|
|
4
|
+
import androidx.compose.runtime.CompositionLocalProvider
|
|
5
|
+
import androidx.compose.runtime.LaunchedEffect
|
|
6
|
+
import androidx.compose.runtime.remember
|
|
7
|
+
import vn.momo.kits.const.AppTheme
|
|
8
|
+
import vn.momo.kits.const.Theme
|
|
9
|
+
import vn.momo.kits.navigation.LocalMaxApi
|
|
10
|
+
import vn.momo.kits.platform.ProvideOsFontScale
|
|
11
|
+
import vn.momo.maxapi.IMaxApi
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@Composable
|
|
15
|
+
fun WidgetContainer(
|
|
16
|
+
theme: Theme? = null,
|
|
17
|
+
miniAppContext: MiniAppContext? = null,
|
|
18
|
+
maxApi: IMaxApi? = null,
|
|
19
|
+
localize: Localize? = null,
|
|
20
|
+
content: @Composable () -> Unit,
|
|
21
|
+
) {
|
|
22
|
+
val parentContext = LocalContext.current
|
|
23
|
+
val mergedContext = remember(parentContext, miniAppContext) {
|
|
24
|
+
MiniAppContext.merge(parentContext, miniAppContext)
|
|
25
|
+
}
|
|
26
|
+
val resolvedTheme = theme ?: AppTheme.current
|
|
27
|
+
val resolvedLocalize = localize ?: LocalLocalize.current
|
|
28
|
+
val resolvedMaxApi = maxApi ?: LocalMaxApi.current
|
|
29
|
+
|
|
30
|
+
LaunchedEffect(resolvedMaxApi, localize) {
|
|
31
|
+
val api = resolvedMaxApi ?: return@LaunchedEffect
|
|
32
|
+
val target = localize ?: return@LaunchedEffect
|
|
33
|
+
runCatching {
|
|
34
|
+
api.getLanguage(callback = { data ->
|
|
35
|
+
if (parseLanguage(data) == Localize.EN) target.changeLanguage(Localize.EN)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
ProvideOsFontScale {
|
|
41
|
+
CompositionLocalProvider(
|
|
42
|
+
AppTheme provides resolvedTheme,
|
|
43
|
+
LocalContext provides mergedContext,
|
|
44
|
+
LocalLocalize provides resolvedLocalize,
|
|
45
|
+
LocalMaxApi provides resolvedMaxApi,
|
|
46
|
+
) {
|
|
47
|
+
content()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
private fun parseLanguage(raw: Map<String, Any?>?): String? {
|
|
53
|
+
val code = (raw?.get("response") as? String)?.trim()?.lowercase()
|
|
54
|
+
if (code.isNullOrEmpty()) return null
|
|
55
|
+
return if (code.startsWith(Localize.EN)) Localize.EN else Localize.VI
|
|
56
|
+
}
|
|
@@ -4,6 +4,10 @@ import androidx.compose.runtime.Composable
|
|
|
4
4
|
import androidx.compose.runtime.CompositionLocalProvider
|
|
5
5
|
import vn.momo.kits.application.ScaleSizeMaxRate
|
|
6
6
|
|
|
7
|
+
@Deprecated(
|
|
8
|
+
"Max font scale is fixed at MAX_FONT_SCALE (1.5); this override is no longer applied. Remove the wrapper.",
|
|
9
|
+
ReplaceWith("content()"),
|
|
10
|
+
)
|
|
7
11
|
@Composable
|
|
8
12
|
fun ScaleSizeScope(
|
|
9
13
|
scaleSizeMaxRate: Float? = null,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
package vn.momo.kits.const
|
|
2
|
+
|
|
3
|
+
import androidx.compose.runtime.getValue
|
|
4
|
+
import androidx.compose.runtime.mutableStateOf
|
|
5
|
+
import androidx.compose.runtime.setValue
|
|
6
|
+
|
|
7
|
+
// App-wide font-scale holder. The kit is sandboxed and can't read the host cache directly, so the
|
|
8
|
+
// host (which sees both core and this kit) seeds this once at startup and pushes runtime updates into
|
|
9
|
+
// it. scaleSize() reads it as the fallback, so every screen — including direct NavigationContainer
|
|
10
|
+
// screens with no host-pushed MiniAppContext — scales with no per-render prop and no async flicker.
|
|
11
|
+
// Backed by snapshot state: writes recompose text automatically.
|
|
12
|
+
object KitFontScale {
|
|
13
|
+
var userScaleRate by mutableStateOf(1f)
|
|
14
|
+
private set
|
|
15
|
+
var useOSScaleRate by mutableStateOf(true)
|
|
16
|
+
private set
|
|
17
|
+
|
|
18
|
+
fun update(userScaleRate: Float, useOSScaleRate: Boolean) {
|
|
19
|
+
this.userScaleRate = userScaleRate
|
|
20
|
+
this.useOSScaleRate = useOSScaleRate
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -11,7 +11,7 @@ import androidx.compose.ui.unit.*
|
|
|
11
11
|
import org.jetbrains.compose.resources.Font
|
|
12
12
|
import org.jetbrains.compose.resources.FontResource
|
|
13
13
|
import org.jetbrains.compose.resources.InternalResourceApi
|
|
14
|
-
import vn.momo.kits.application.
|
|
14
|
+
import vn.momo.kits.application.LocalContext
|
|
15
15
|
import vn.momo.kits.platform.getScreenDimensions
|
|
16
16
|
import vn.momo.uikits.resources.*
|
|
17
17
|
import kotlin.math.max
|
|
@@ -23,7 +23,13 @@ const val MAX_DEVICE_SCALE = 5
|
|
|
23
23
|
|
|
24
24
|
@Composable
|
|
25
25
|
fun scaleSize(size: Float): Float {
|
|
26
|
-
val
|
|
26
|
+
val context = LocalContext.current
|
|
27
|
+
// Host-pushed context wins; otherwise fall back to the app-wide KitFontScale holder.
|
|
28
|
+
val useOsScale = context?.useOSScaleRate ?: KitFontScale.useOSScaleRate
|
|
29
|
+
if (!useOsScale) {
|
|
30
|
+
return size * (context?.userScaleRate ?: KitFontScale.userScaleRate)
|
|
31
|
+
}
|
|
32
|
+
val scaleSizeMaxRate: Float = MAX_FONT_SCALE
|
|
27
33
|
val deviceWidth = getScreenDimensions().width
|
|
28
34
|
val deviceScale = deviceWidth / DEFAULT_SCREEN_SIZE
|
|
29
35
|
|
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
package vn.momo.kits.navigation
|
|
2
2
|
|
|
3
|
-
import androidx.compose.animation.*
|
|
4
3
|
import androidx.compose.animation.core.tween
|
|
5
|
-
import androidx.compose.
|
|
4
|
+
import androidx.compose.animation.fadeIn
|
|
5
|
+
import androidx.compose.animation.slideInHorizontally
|
|
6
|
+
import androidx.compose.animation.slideInVertically
|
|
7
|
+
import androidx.compose.animation.slideOutHorizontally
|
|
8
|
+
import androidx.compose.animation.slideOutVertically
|
|
9
|
+
import androidx.compose.runtime.Composable
|
|
10
|
+
import androidx.compose.runtime.CompositionLocalProvider
|
|
11
|
+
import androidx.compose.runtime.DisposableEffect
|
|
12
|
+
import androidx.compose.runtime.LaunchedEffect
|
|
13
|
+
import androidx.compose.runtime.mutableStateOf
|
|
14
|
+
import androidx.compose.runtime.remember
|
|
6
15
|
import androidx.compose.runtime.saveable.rememberSaveable
|
|
16
|
+
import androidx.compose.runtime.staticCompositionLocalOf
|
|
7
17
|
import androidx.compose.ui.unit.Dp
|
|
8
18
|
import androidx.navigation.compose.NavHost
|
|
9
19
|
import androidx.navigation.compose.composable
|
|
10
20
|
import androidx.navigation.compose.rememberNavController
|
|
11
21
|
import androidx.navigation.toRoute
|
|
12
|
-
import vn.momo.kits.application
|
|
13
|
-
import vn.momo.kits.
|
|
22
|
+
import vn.momo.kits.application.LocalContext
|
|
23
|
+
import vn.momo.kits.application.LocalLocalize
|
|
24
|
+
import vn.momo.kits.application.Localize
|
|
25
|
+
import vn.momo.kits.application.MiniAppContext
|
|
26
|
+
import vn.momo.kits.const.AppNavigationBar
|
|
27
|
+
import vn.momo.kits.const.AppStatusBar
|
|
28
|
+
import vn.momo.kits.const.AppTheme
|
|
29
|
+
import vn.momo.kits.const.Theme
|
|
30
|
+
import vn.momo.kits.const.defaultTheme
|
|
14
31
|
import vn.momo.kits.platform.ProvideNavigationEventDispatcherOwner
|
|
32
|
+
import vn.momo.kits.platform.ProvideOsFontScale
|
|
15
33
|
import vn.momo.kits.utils.getAppStatusBarHeight
|
|
16
34
|
import vn.momo.kits.utils.getNavigationBarHeight
|
|
17
35
|
import vn.momo.maxapi.IMaxApi
|
|
@@ -27,7 +45,7 @@ fun NavigationContainer(
|
|
|
27
45
|
setNavigator: ((Navigator) -> Unit)? = null,
|
|
28
46
|
statusBarHeight: Dp? = null,
|
|
29
47
|
localize: Localize? = null,
|
|
30
|
-
){
|
|
48
|
+
) {
|
|
31
49
|
val navController = rememberNavController()
|
|
32
50
|
val registry = remember { DynamicScreenRegistry() }
|
|
33
51
|
val navigator = remember { Navigator(navController = navController, maxApi = maxApi, registry = registry) }
|
|
@@ -57,77 +75,79 @@ fun NavigationContainer(
|
|
|
57
75
|
registry.bind(screenId, initialScreenName, initialScreen, options)
|
|
58
76
|
val startDestination = remember(screenId) { DynamicScreenRoute(screenId) }
|
|
59
77
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
78
|
+
ProvideOsFontScale {
|
|
79
|
+
ProvideNavigationEventDispatcherOwner {
|
|
80
|
+
CompositionLocalProvider(
|
|
81
|
+
LocalNavigator provides navigator,
|
|
82
|
+
LocalDynamicScreenRegistry provides registry,
|
|
83
|
+
LocalMaxApi provides maxApi,
|
|
84
|
+
AppTheme provides theme.value,
|
|
85
|
+
AppStatusBar provides statusBarHeight,
|
|
86
|
+
AppNavigationBar provides navigationBarHeight,
|
|
87
|
+
LocalContext provides mergedContext,
|
|
88
|
+
LocalLocalize provides resolvedLocalize,
|
|
70
89
|
) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
NavHost(navController, startDestination = startDestination) {
|
|
76
|
-
composable<DynamicScreenRoute>(
|
|
77
|
-
enterTransition = {
|
|
78
|
-
slideInHorizontally(
|
|
79
|
-
animationSpec = tween(300),
|
|
80
|
-
initialOffsetX = { it }
|
|
81
|
-
)
|
|
82
|
-
},
|
|
83
|
-
exitTransition = null,
|
|
84
|
-
popEnterTransition = { fadeIn(animationSpec = tween(0)) },
|
|
85
|
-
popExitTransition = {
|
|
86
|
-
slideOutHorizontally(
|
|
87
|
-
animationSpec = tween(300),
|
|
88
|
-
targetOffsetX = { it }
|
|
89
|
-
)
|
|
90
|
-
}
|
|
91
|
-
) { backStackEntry ->
|
|
92
|
-
val route = backStackEntry.toRoute<DynamicScreenRoute>()
|
|
93
|
-
val screen = registry.getScreen(route.id)
|
|
94
|
-
|
|
95
|
-
if (screen != null) {
|
|
96
|
-
StackScreen(
|
|
97
|
-
id = route.id,
|
|
98
|
-
name = screen.name,
|
|
99
|
-
content = screen.content,
|
|
100
|
-
navigationOptions = screen.options
|
|
101
|
-
)
|
|
102
|
-
}
|
|
90
|
+
LaunchedEffect(Unit) {
|
|
91
|
+
setNavigator?.invoke(navigator)
|
|
103
92
|
}
|
|
104
93
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
94
|
+
NavHost(navController, startDestination = startDestination) {
|
|
95
|
+
composable<DynamicScreenRoute>(
|
|
96
|
+
enterTransition = {
|
|
97
|
+
slideInHorizontally(
|
|
98
|
+
animationSpec = tween(300),
|
|
99
|
+
initialOffsetX = { it }
|
|
100
|
+
)
|
|
101
|
+
},
|
|
102
|
+
exitTransition = null,
|
|
103
|
+
popEnterTransition = { fadeIn(animationSpec = tween(0)) },
|
|
104
|
+
popExitTransition = {
|
|
105
|
+
slideOutHorizontally(
|
|
106
|
+
animationSpec = tween(300),
|
|
107
|
+
targetOffsetX = { it }
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
) { backStackEntry ->
|
|
111
|
+
val route = backStackEntry.toRoute<DynamicScreenRoute>()
|
|
112
|
+
val screen = registry.getScreen(route.id)
|
|
113
|
+
|
|
114
|
+
if (screen != null) {
|
|
115
|
+
StackScreen(
|
|
116
|
+
id = route.id,
|
|
117
|
+
name = screen.name,
|
|
118
|
+
content = screen.content,
|
|
119
|
+
navigationOptions = screen.options
|
|
120
|
+
)
|
|
121
|
+
}
|
|
119
122
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
123
|
+
|
|
124
|
+
composable<DynamicDialogRoute>(
|
|
125
|
+
enterTransition = {
|
|
126
|
+
slideInVertically(
|
|
127
|
+
animationSpec = tween(300),
|
|
128
|
+
initialOffsetY = { it }
|
|
129
|
+
)
|
|
130
|
+
},
|
|
131
|
+
exitTransition = null,
|
|
132
|
+
popEnterTransition = { fadeIn(animationSpec = tween(0)) },
|
|
133
|
+
popExitTransition = {
|
|
134
|
+
slideOutVertically(
|
|
135
|
+
animationSpec = tween(300),
|
|
136
|
+
targetOffsetY = { it }
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
) { backStackEntry ->
|
|
140
|
+
val route = backStackEntry.toRoute<DynamicDialogRoute>()
|
|
141
|
+
val screen = registry.getScreen(route.id)
|
|
142
|
+
|
|
143
|
+
if (screen != null) {
|
|
144
|
+
StackScreen(
|
|
145
|
+
id = route.id,
|
|
146
|
+
name = screen.name,
|
|
147
|
+
content = screen.content,
|
|
148
|
+
navigationOptions = screen.options
|
|
149
|
+
)
|
|
150
|
+
}
|
|
131
151
|
}
|
|
132
152
|
}
|
|
133
153
|
}
|
|
@@ -142,9 +162,7 @@ fun NavigationContainer(
|
|
|
142
162
|
}
|
|
143
163
|
}
|
|
144
164
|
|
|
145
|
-
val LocalMaxApi = staticCompositionLocalOf<IMaxApi?> {
|
|
146
|
-
error("No MaxApi provided")
|
|
147
|
-
}
|
|
165
|
+
val LocalMaxApi = staticCompositionLocalOf<IMaxApi?> { null }
|
|
148
166
|
|
|
149
167
|
private fun parseLanguage(raw: Map<String, Any?>?): String? {
|
|
150
168
|
val code = (raw?.get("response") as? String)?.trim()?.lowercase()
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
package vn.momo.kits.platform
|
|
2
|
+
|
|
3
|
+
import androidx.compose.runtime.Composable
|
|
4
|
+
|
|
5
|
+
// Overrides LocalDensity.fontScale with the live OS Dynamic Type scale so text re-scales while a
|
|
6
|
+
// screen is visible. Wrapped inside the containers so every screen is covered, not just callers of
|
|
7
|
+
// the app-level wrappers.
|
|
8
|
+
@Composable
|
|
9
|
+
expect fun ProvideOsFontScale(content: @Composable () -> Unit)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
package vn.momo.kits.platform
|
|
2
|
+
|
|
3
|
+
import androidx.compose.runtime.Composable
|
|
4
|
+
import androidx.compose.runtime.CompositionLocalProvider
|
|
5
|
+
import androidx.compose.runtime.DisposableEffect
|
|
6
|
+
import androidx.compose.runtime.mutableStateOf
|
|
7
|
+
import androidx.compose.runtime.remember
|
|
8
|
+
import androidx.compose.ui.platform.LocalDensity
|
|
9
|
+
import androidx.compose.ui.unit.Density
|
|
10
|
+
import kotlinx.cinterop.ExperimentalForeignApi
|
|
11
|
+
import kotlinx.cinterop.ObjCAction
|
|
12
|
+
import platform.Foundation.NSNotification
|
|
13
|
+
import platform.Foundation.NSNotificationCenter
|
|
14
|
+
import platform.Foundation.NSSelectorFromString
|
|
15
|
+
import platform.UIKit.UIContentSizeCategoryDidChangeNotification
|
|
16
|
+
import platform.UIKit.UIFont
|
|
17
|
+
import platform.UIKit.UIFontTextStyleBody
|
|
18
|
+
import platform.darwin.NSObject
|
|
19
|
+
|
|
20
|
+
// Compose MP iOS reads LocalDensity.fontScale once at ComposeUIViewController creation and does not
|
|
21
|
+
// recompose it when UIContentSizeCategory changes (Android tracks Configuration.fontScale live). We
|
|
22
|
+
// observe the notification and override LocalDensity.fontScale so scaleSize() recomposes live. The
|
|
23
|
+
// formula matches the SwiftUI kit (UIFont body point size / 17) so both iOS surfaces scale alike.
|
|
24
|
+
private const val DEFAULT_BODY_POINT_SIZE = 17.0
|
|
25
|
+
|
|
26
|
+
private fun currentOsFontScale(): Float =
|
|
27
|
+
(UIFont.preferredFontForTextStyle(UIFontTextStyleBody).pointSize / DEFAULT_BODY_POINT_SIZE).toFloat()
|
|
28
|
+
|
|
29
|
+
@OptIn(ExperimentalForeignApi::class)
|
|
30
|
+
@Composable
|
|
31
|
+
actual fun ProvideOsFontScale(content: @Composable () -> Unit) {
|
|
32
|
+
val osFontScale = remember { mutableStateOf(currentOsFontScale()) }
|
|
33
|
+
|
|
34
|
+
val listener = remember {
|
|
35
|
+
object : NSObject() {
|
|
36
|
+
@Suppress("unused")
|
|
37
|
+
@ObjCAction
|
|
38
|
+
fun contentSizeChanged(arg: NSNotification) {
|
|
39
|
+
osFontScale.value = currentOsFontScale()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
DisposableEffect(Unit) {
|
|
45
|
+
NSNotificationCenter.defaultCenter.addObserver(
|
|
46
|
+
observer = listener,
|
|
47
|
+
selector = NSSelectorFromString(listener::contentSizeChanged.name + ":"),
|
|
48
|
+
name = UIContentSizeCategoryDidChangeNotification,
|
|
49
|
+
`object` = null,
|
|
50
|
+
)
|
|
51
|
+
onDispose {
|
|
52
|
+
NSNotificationCenter.defaultCenter.removeObserver(
|
|
53
|
+
observer = listener,
|
|
54
|
+
name = UIContentSizeCategoryDidChangeNotification,
|
|
55
|
+
`object` = null,
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
val density = LocalDensity.current
|
|
61
|
+
CompositionLocalProvider(
|
|
62
|
+
LocalDensity provides Density(density.density, osFontScale.value),
|
|
63
|
+
) {
|
|
64
|
+
content()
|
|
65
|
+
}
|
|
66
|
+
}
|
package/gradle.properties
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import CoreGraphics
|
|
2
|
+
|
|
3
|
+
/// App-wide font-scale preference for the SwiftUI kit. An exported holder the host writes once at
|
|
4
|
+
/// app startup (from the cache-backed `FontScaleProvider`); `scaleSize`/`appFont` read the snapshot.
|
|
5
|
+
/// No realtime: the SwiftUI kit has no navigation container to scope reactive updates, so a startup
|
|
6
|
+
/// snapshot is enough. When `useOSScaleRate` is false the kit applies `userScaleRate` instead of the
|
|
7
|
+
/// OS Dynamic Type factor.
|
|
8
|
+
public final class FontScaleStore {
|
|
9
|
+
public static let shared = FontScaleStore()
|
|
10
|
+
private init() {}
|
|
11
|
+
|
|
12
|
+
public private(set) var userScaleRate: CGFloat = 1
|
|
13
|
+
public private(set) var useOSScaleRate: Bool = true
|
|
14
|
+
|
|
15
|
+
public func update(userScaleRate: CGFloat, useOSScaleRate: Bool) {
|
|
16
|
+
self.userScaleRate = userScaleRate
|
|
17
|
+
self.useOSScaleRate = useOSScaleRate
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import SwiftUI
|
|
2
2
|
|
|
3
3
|
public func scaleSize(_ size: CGFloat, _ scaleRate: CGFloat? = nil) -> CGFloat {
|
|
4
|
+
if !FontScaleStore.shared.useOSScaleRate {
|
|
5
|
+
return size * FontScaleStore.shared.userScaleRate
|
|
6
|
+
}
|
|
4
7
|
let defaultScreenSize: CGFloat = 375
|
|
5
8
|
let maxFontScale: CGFloat = scaleRate ?? 1.5
|
|
6
9
|
let maxDeviceScale: CGFloat = 5
|
|
@@ -11,6 +11,9 @@ public extension View {
|
|
|
11
11
|
|
|
12
12
|
public extension Font {
|
|
13
13
|
static func appFont(size: CGFloat) -> Font {
|
|
14
|
+
if !FontScaleStore.shared.useOSScaleRate {
|
|
15
|
+
return Font.system(size: size * FontScaleStore.shared.userScaleRate)
|
|
16
|
+
}
|
|
14
17
|
let defaultScreenSize: CGFloat = 375
|
|
15
18
|
let maxFontScale: CGFloat = 1.5
|
|
16
19
|
let maxDeviceScale: CGFloat = 5
|