@nativue/core 0.10.0 → 0.11.1

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/AGENTS.md CHANGED
@@ -89,6 +89,10 @@ Import the runtime from `vue`: `ref`, `computed`, `StyleSheet`, `Animated`, `Lay
89
89
  - Commands: `npm run ios` / `npm run android` build and open the app, then reload on save;
90
90
  `npm run typecheck` checks scripts and templates; `npm run build` makes the release bundle;
91
91
  `npm run format` formats with Prettier.
92
+ - Native code the app needs (a platform API no package covers) goes in `native/<name>/ios` and
93
+ `native/<name>/android`, as `<Name>Plugin` classes registering functions JavaScript calls with
94
+ `requireNativeFunction`; libraries it uses (Swift packages, Maven) go in `native/<name>/nativue.json`.
95
+ See the docs' "Native code in your app". Rebuild after changing it.
92
96
  - Check changes with `npm run typecheck`. `ios/` and `android/` are the native host apps: change them
93
97
  only for native settings (permissions in `Info.plist` or `AndroidManifest.xml`, the app's name and
94
98
  icon).
@@ -2,7 +2,10 @@
2
2
  // package.json has
3
3
  // "nativue": { "android": { "sources": "android", "plugin": "dev.nativue.haptics.HapticsPlugin" } }
4
4
  // becomes modules the app depends on (sources: the folder with build.gradle.kts, which depends on
5
- // project(":nativue")). The plugin classes reach the app as the string resource nativue_plugins.
5
+ // project(":nativue")). The app's own native code, native/<name>/android, is compiled into the app;
6
+ // its plugin is <Name>Plugin, with no package, and native/<name>/nativue.json can list the Maven
7
+ // libraries it uses: { "android": { "dependencies": ["com.squareup.okhttp3:okhttp:4.12.0"] } }.
8
+ // The plugin classes reach the app as the string resource nativue_plugins.
6
9
  import groovy.json.JsonSlurper
7
10
  import java.io.File
8
11
 
@@ -36,8 +39,24 @@ fun androidPlugin(name: String, appDirectory: File): AndroidPlugin? {
36
39
  return AndroidPlugin(projectPath, android["plugin"] as String)
37
40
  }
38
41
 
42
+ /** 'face-id' -> 'FaceId', as the CLI names the iOS side. */
43
+ fun pascalCase(name: String): String =
44
+ name.split(Regex("[^a-zA-Z0-9]+")).filter { it.isNotEmpty() }.joinToString("") { it.replaceFirstChar(Char::uppercaseChar) }
45
+
39
46
  val appDirectory: File = settingsDir.parentFile
40
47
  val androidPlugins = dependencyNames(appDirectory).mapNotNull { name -> androidPlugin(name, appDirectory) }
48
+ val appSourceDirectories = File(appDirectory, "native").listFiles().orEmpty()
49
+ .sortedBy { it.name }
50
+ .map { File(it, "android") }
51
+ .filter { it.isDirectory }
52
+ val appPluginClasses = appSourceDirectories.map { pascalCase(it.parentFile.name) + "Plugin" }
53
+ val appLibraries = appSourceDirectories
54
+ .map { File(it.parentFile, "nativue.json") }
55
+ .filter { it.isFile }
56
+ .flatMap { ((readJson(it)["android"] as? Map<*, *>)?.get("dependencies") as? List<*>).orEmpty() }
57
+ .map { it.toString() }
58
+ .distinct()
59
+ val pluginClasses = androidPlugins.map { it.pluginClass } + appPluginClasses
41
60
 
42
61
  // Through Groovy's dynamic calls: the Android Gradle plugin's types aren't on a settings script's classpath.
43
62
  gradle.beforeProject {
@@ -45,11 +64,20 @@ gradle.beforeProject {
45
64
 
46
65
  pluginManager.withPlugin("com.android.application") {
47
66
  androidPlugins.forEach { plugin -> dependencies.add("implementation", project(plugin.projectPath)) }
67
+ appLibraries.forEach { library -> dependencies.add("implementation", library) }
48
68
 
49
69
  extensions.getByName("android").withGroovyBuilder {
50
70
  "getBuildFeatures"()!!.withGroovyBuilder { "setResValues"(true) }
51
71
  "getDefaultConfig"()!!.withGroovyBuilder {
52
- "resValue"("string", "nativue_plugins", androidPlugins.joinToString(",") { it.pluginClass })
72
+ "resValue"("string", "nativue_plugins", pluginClasses.joinToString(","))
73
+ }
74
+ "getSourceSets"()!!.withGroovyBuilder {
75
+ "getByName"("main")!!.withGroovyBuilder {
76
+ // Kotlin and Java: each source set compiles only its own language.
77
+ listOf("getKotlin", "getJava").forEach { sourceSet ->
78
+ sourceSet()!!.withGroovyBuilder { appSourceDirectories.forEach { "srcDir"(it) } }
79
+ }
80
+ }
53
81
  }
54
82
  }
55
83
  }
@@ -19,7 +19,6 @@ import android.view.View
19
19
  import android.view.ViewGroup
20
20
  import android.view.WindowInsets
21
21
  import android.view.WindowInsetsAnimation
22
- import android.view.WindowInsetsController
23
22
  import android.graphics.Insets
24
23
  import android.view.inputmethod.EditorInfo
25
24
  import android.view.inputmethod.InputMethodManager
@@ -184,9 +183,7 @@ class Bridge(private val activity: Activity, private val rootView: RootView) {
184
183
  // Content not in the window now, like a hidden <Modal>'s, is repainted too.
185
184
  nodesById.values.forEach { applyThemeColors(it.view) }
186
185
  // The theme set them when the window was created; it isn't recreated (configChanges has uiMode).
187
- val lightBars = WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or
188
- WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
189
- activity.window.insetsController?.setSystemBarsAppearance(if (ThemeColors.isDark) 0 else lightBars, lightBars)
186
+ SystemBars.setLight(activity.window, !ThemeColors.isDark)
190
187
  evaluate { runtime?.call("__colorSchemeChanged", colorScheme()) }
191
188
  }
192
189
 
@@ -18,6 +18,7 @@ open class NativueActivity : Activity() {
18
18
  override fun onCreate(savedInstanceState: Bundle?) {
19
19
  super.onCreate(savedInstanceState)
20
20
  SoLoader.init(this, false) // Yoga loads its native library through it
21
+ SystemBars.drawBehind(window)
21
22
  ThemeColors.update(this)
22
23
 
23
24
  val rootView = RootView(this)
@@ -0,0 +1,40 @@
1
+ package dev.nativue
2
+
3
+ import android.graphics.Color
4
+ import android.os.Build
5
+ import android.view.View
6
+ import android.view.Window
7
+ import android.view.WindowInsetsController
8
+ import android.view.WindowManager
9
+
10
+ /** The status and navigation bars: transparent, over the screens, with icons that contrast with them. */
11
+ @Suppress("DEPRECATION") // the APIs older Android versions need
12
+ object SystemBars {
13
+ /**
14
+ * Screens draw under the bars, which SafeAreaView and headers pad for: the default from Android 15,
15
+ * and the same on older versions instead of a gray status bar.
16
+ */
17
+ fun drawBehind(window: Window) {
18
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) return
19
+
20
+ window.setDecorFitsSystemWindows(false)
21
+ window.statusBarColor = Color.TRANSPARENT
22
+ window.navigationBarColor = Color.TRANSPARENT
23
+ window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS
24
+ }
25
+
26
+ /** Dark icons on light screens, light icons on dark ones. */
27
+ fun setLight(window: Window, isLight: Boolean) {
28
+ val lightBars = WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or
29
+ WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
30
+ window.insetsController?.setSystemBarsAppearance(if (isLight) lightBars else 0, lightBars)
31
+
32
+ // Android 11 keeps the theme's light-bar flags over setSystemBarsAppearance: they're set too.
33
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) return
34
+
35
+ val decorView = window.decorView
36
+ val flags = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
37
+ decorView.systemUiVisibility =
38
+ if (isLight) decorView.systemUiVisibility or flags else decorView.systemUiVisibility and flags.inv()
39
+ }
40
+ }
package/cli/link.mjs CHANGED
@@ -3,6 +3,10 @@
3
3
  // ios/NativuePlugins becomes a Swift package with a target per package (a link to its sources) and
4
4
  // `nativuePlugins`, the list App.swift passes to NativueView. The app's Xcode project depends on it
5
5
  // alone; it re-exports Nativue. (Android finds them itself: android/nativue.settings.gradle.kts.)
6
+ // The app's own native code links the same way: native/<name>/ios, whose plugin is <Name>Plugin.
7
+ // Swift packages the native code uses are listed in "ios": { "dependencies": [...] }, in the
8
+ // package.json's "nativue" field or native/<name>/nativue.json:
9
+ // { "url": "https://github.com/stripe/stripe-ios-spm", "from": "24.0.0", "products": ["StripePaymentSheet"] }
6
10
  import {
7
11
  existsSync,
8
12
  mkdirSync,
@@ -18,6 +22,7 @@ import { dirname, join, relative, resolve } from 'node:path'
18
22
  const PLUGINS_DIRECTORY = 'ios/NativuePlugins'
19
23
  const SOURCES_DIRECTORY = `${PLUGINS_DIRECTORY}/Sources`
20
24
  const LIST_TARGET = 'NativuePlugins'
25
+ const APP_NATIVE_DIRECTORY = 'native'
21
26
 
22
27
  function readJson(path) {
23
28
  return JSON.parse(readFileSync(path, 'utf8'))
@@ -50,7 +55,36 @@ function iosPlugins() {
50
55
  const ios = directory && readJson(join(directory, 'package.json')).nativue?.ios
51
56
  if (!ios) return []
52
57
 
53
- return [{ module: moduleName(name), sources: join(directory, ios.sources), plugin: ios.plugin }]
58
+ return [
59
+ {
60
+ module: moduleName(name),
61
+ sources: join(directory, ios.sources),
62
+ plugin: ios.plugin,
63
+ dependencies: ios.dependencies ?? [],
64
+ },
65
+ ]
66
+ })
67
+ }
68
+
69
+ /** native/<name>/ios: module App<Name> (not <Name>, which could hide a system module like Photos). */
70
+ function appPlugins() {
71
+ if (!existsSync(APP_NATIVE_DIRECTORY)) return []
72
+
73
+ return readdirSync(APP_NATIVE_DIRECTORY).flatMap((name) => {
74
+ const sources = resolve(APP_NATIVE_DIRECTORY, name, 'ios')
75
+ if (!existsSync(sources)) return []
76
+
77
+ const settingsPath = join(APP_NATIVE_DIRECTORY, name, 'nativue.json')
78
+ const settings = existsSync(settingsPath) ? readJson(settingsPath) : {}
79
+
80
+ return [
81
+ {
82
+ module: `App${moduleName(name)}`,
83
+ sources,
84
+ plugin: `${moduleName(name)}Plugin`,
85
+ dependencies: settings.ios?.dependencies ?? [],
86
+ },
87
+ ]
54
88
  })
55
89
  }
56
90
 
@@ -81,12 +115,37 @@ function linkSources(plugins) {
81
115
  }
82
116
  }
83
117
 
118
+ /** SwiftPM's identity of a package: the last part of its URL, without .git, lowercased. */
119
+ function packageIdentity(url) {
120
+ return url
121
+ .replace(/\.git$/, '')
122
+ .split('/')
123
+ .pop()
124
+ .toLowerCase()
125
+ }
126
+
127
+ /** One `.package` per URL: the first plugin to list a package sets its version. */
128
+ function swiftPackages(plugins) {
129
+ const versionsByUrl = new Map()
130
+ plugins.forEach(({ dependencies }) =>
131
+ dependencies.forEach(({ url, from }) => versionsByUrl.has(url) || versionsByUrl.set(url, from)),
132
+ )
133
+
134
+ return [...versionsByUrl].map(
135
+ ([url, from]) => ` .package(url: "${url}", from: "${from}"),\n`,
136
+ )
137
+ }
138
+
84
139
  function packageManifest(plugins, corePath) {
85
140
  const nativue = '.product(name: "Nativue", package: "core")'
86
- const pluginTargets = plugins.map(
87
- ({ module }) =>
88
- ` .target(name: "${module}", dependencies: [${nativue}], path: "Sources/${module}"),\n`,
89
- )
141
+ const pluginTargets = plugins.map(({ module, dependencies }) => {
142
+ const products = dependencies.flatMap(({ url, products }) =>
143
+ products.map((product) => `.product(name: "${product}", package: "${packageIdentity(url)}")`),
144
+ )
145
+ const targetDependencies = [nativue, ...products].join(', ')
146
+
147
+ return ` .target(name: "${module}", dependencies: [${targetDependencies}], path: "Sources/${module}"),\n`
148
+ })
90
149
  const listDependencies = [nativue, ...plugins.map(({ module }) => `"${module}"`)].join(', ')
91
150
 
92
151
  return `// swift-tools-version: 5.9
@@ -97,7 +156,9 @@ let package = Package(
97
156
  name: "NativuePlugins",
98
157
  platforms: [.iOS(.v17)],
99
158
  products: [.library(name: "NativuePlugins", targets: ["${LIST_TARGET}"])],
100
- dependencies: [.package(path: "${corePath}")],
159
+ dependencies: [
160
+ .package(path: "${corePath}"),
161
+ ${swiftPackages(plugins).join('')} ],
101
162
  targets: [
102
163
  ${pluginTargets.join('')} .target(name: "${LIST_TARGET}", dependencies: [${listDependencies}], path: "Sources/${LIST_TARGET}"),
103
164
  ]
@@ -120,7 +181,7 @@ public let nativuePlugins: [NativuePlugin] = [${instances}]
120
181
  export function linkIosPlugins(coreDirectory) {
121
182
  if (!existsSync('ios') || !existsSync('package.json')) return
122
183
 
123
- const plugins = iosPlugins()
184
+ const plugins = [...iosPlugins(), ...appPlugins()]
124
185
  const corePath = relative(resolve(PLUGINS_DIRECTORY), coreDirectory)
125
186
 
126
187
  linkSources(plugins)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativue/core",
3
- "version": "0.10.0",
3
+ "version": "0.11.1",
4
4
  "description": "Vue rendering real native views on iOS and Android",
5
5
  "keywords": [
6
6
  "vue",