craft-native 0.0.199 → 0.0.201

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.
@@ -50,6 +50,14 @@ export interface CraftAndroidConfig {
50
50
  /** Reach the person's own devices on the local network over plain HTTP (a Mac companion). */
51
51
  enableLocalNetwork?: boolean;
52
52
  urlSchemes?: string[];
53
+ /** HTTPS hosts owned by the app; path rules are ordered, with `*` matching any path text. */
54
+ appLinks?: Array<{
55
+ host: string;
56
+ paths?: Array<{
57
+ pattern: string;
58
+ exclude?: boolean;
59
+ }>;
60
+ }>;
53
61
  trustedOrigins?: string[];
54
62
  appIconPath?: string;
55
63
  googleServicesFile?: string;
@@ -188,6 +188,7 @@ var DEFAULT_CONFIG = {
188
188
  enableKeepAwake: false,
189
189
  enableDeepLinks: false,
190
190
  urlSchemes: [],
191
+ appLinks: [],
191
192
  trustedOrigins: [],
192
193
  minSdk: ANDROID_MIN_SDK,
193
194
  compileSdk: 36,
@@ -255,8 +256,21 @@ function normalizeAndroidNetworkConfig(config) {
255
256
  throw new Error("Android deep-link schemes must be valid URI schemes");
256
257
  }
257
258
  config.urlSchemes = [...new Set(schemes)];
258
- if (config.enableDeepLinks && config.urlSchemes.length === 0) {
259
- throw new Error("Android deep links require at least one URL scheme");
259
+ config.appLinks = (config.appLinks ?? []).map((link) => {
260
+ const host = link.host.trim().toLowerCase();
261
+ const parsed = new URL(`https://${host}`);
262
+ if (!host || parsed.hostname !== host || parsed.port || parsed.username || parsed.password || parsed.pathname !== "/" || parsed.search || parsed.hash)
263
+ throw new Error("Android App Links need a hostname without a scheme, port or path");
264
+ for (const rule of link.paths ?? []) {
265
+ if (!rule.pattern.startsWith("/") || /[\x00-\x1f]/.test(rule.pattern) || rule.exclude !== undefined && typeof rule.exclude !== "boolean")
266
+ throw new Error("Android App Link path rules need an absolute path pattern and a boolean exclude");
267
+ }
268
+ return { ...link, host };
269
+ });
270
+ if (new Set(config.appLinks.map((link) => link.host)).size !== config.appLinks.length)
271
+ throw new Error("Android App Link hosts must be unique");
272
+ if (config.enableDeepLinks && config.urlSchemes.length === 0 && config.appLinks.length === 0) {
273
+ throw new Error("Android deep links require at least one URL scheme or an App Link host");
260
274
  }
261
275
  const trustedOrigins = (config.trustedOrigins ?? []).map((value) => {
262
276
  return androidWebUrl(value, "Android trusted origin").origin;
@@ -454,12 +468,25 @@ function renderAndroidDeepLinks(config) {
454
468
  if (!config.enableDeepLinks)
455
469
  return "";
456
470
  const schemes = [...new Set(config.urlSchemes?.map((value) => value.trim()).filter((value) => /^[a-z][a-z0-9+.-]*$/i.test(value)) ?? [])];
457
- return schemes.map((scheme) => ` <intent-filter>
471
+ const custom = schemes.map((scheme) => ` <intent-filter>
458
472
  <action android:name="android.intent.action.VIEW" />
459
473
  <category android:name="android.intent.category.DEFAULT" />
460
474
  <category android:name="android.intent.category.BROWSABLE" />
461
475
  <data android:scheme="${scheme}" />
462
476
  </intent-filter>`).join(`
477
+ `);
478
+ const verified = (config.appLinks ?? []).map((link) => {
479
+ if (!/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/i.test(link.host))
480
+ throw new Error("Invalid Android App Link host");
481
+ return ` <intent-filter android:autoVerify="true">
482
+ <action android:name="android.intent.action.VIEW" />
483
+ <category android:name="android.intent.category.DEFAULT" />
484
+ <category android:name="android.intent.category.BROWSABLE" />
485
+ <data android:scheme="https" android:host="${link.host}" />
486
+ </intent-filter>`;
487
+ }).join(`
488
+ `);
489
+ return [custom, verified].filter(Boolean).join(`
463
490
  `);
464
491
  }
465
492
  var RUNTIME_ABIS = ["arm64-v8a", "x86_64"];
@@ -555,6 +582,7 @@ async function init(options) {
555
582
  const mainActivityTemplate = readFileSync(join(TEMPLATES_DIR, config.renderer === "native" ? "MainActivityNative.kt.template" : "MainActivity.kt.template"), "utf-8");
556
583
  const mainActivity = mainActivityTemplate.replace(/\{\{PACKAGE_NAME\}\}/g, finalPackageName).replace(/\{\{APP_NAME\}\}/g, name);
557
584
  writeFileSync(join(output, "app/src/main/java", packagePath, "MainActivity.kt"), mainActivity);
585
+ writeFileSync(join(output, "app/src/main/java", packagePath, "CraftAppLinks.kt"), readFileSync(join(TEMPLATES_DIR, "CraftAppLinks.kt.template"), "utf8").replace(/\{\{PACKAGE_NAME\}\}/g, finalPackageName));
558
586
  writeFileSync(join(output, "app/src/main/java", packagePath, "CraftCrashReporter.kt"), readFileSync(join(TEMPLATES_DIR, "CraftCrashReporter.kt.template"), "utf-8").replace(/\{\{PACKAGE_NAME\}\}/g, finalPackageName).replace(/\{\{CRAFT_VERSION\}\}/g, CRAFT_ANDROID_VERSION));
559
587
  if (config.renderer === "native") {
560
588
  const mutationTemplate = readFileSync(join(TEMPLATES_DIR, "CraftNativeMutation.kt.template"), "utf-8");
@@ -8,6 +8,12 @@
8
8
  <!-- craft.speech: from Android 11 an app cannot see, and so cannot bind,
9
9
  a text-to-speech engine it has not declared it will look for. -->
10
10
  <queries>
11
+ <intent><action android:name="android.support.customtabs.action.CustomTabsService" /></intent>
12
+ <intent>
13
+ <action android:name="android.intent.action.VIEW" />
14
+ <category android:name="android.intent.category.BROWSABLE" />
15
+ <data android:scheme="https" />
16
+ </intent>
11
17
  <intent>
12
18
  <action android:name="android.intent.action.TTS_SERVICE" />
13
19
  </intent>
@@ -0,0 +1,47 @@
1
+ package {{PACKAGE_NAME}}
2
+
3
+ import android.app.Activity
4
+ import android.content.Intent
5
+ import android.net.Uri
6
+ import androidx.browser.customtabs.CustomTabsClient
7
+ import androidx.browser.customtabs.CustomTabsIntent
8
+ import org.json.JSONObject
9
+
10
+ /** Ordered website path rules, also publishable as Android 15 dynamic App Links. */
11
+ internal object CraftAppLinks {
12
+ fun opensInBrowser(config: JSONObject?, uri: Uri): Boolean {
13
+ if (uri.scheme != "https") return false
14
+ val hosts = config?.optJSONArray("appLinks") ?: return false
15
+ for (index in 0 until hosts.length()) {
16
+ val host = hosts.optJSONObject(index) ?: continue
17
+ if (!host.optString("host").equals(uri.host, ignoreCase = true)) continue
18
+ val rules = host.optJSONArray("paths") ?: return false
19
+ val path = uri.path.orEmpty().ifEmpty { "/" }
20
+ for (ruleIndex in 0 until rules.length()) {
21
+ val rule = rules.optJSONObject(ruleIndex) ?: continue
22
+ val pattern = rule.optString("pattern")
23
+ val regex = pattern.split('*').joinToString(".*") { Regex.escape(it) }
24
+ if (Regex("^$regex$").matches(path)) return rule.optBoolean("exclude", false)
25
+ }
26
+ return true
27
+ }
28
+ return false
29
+ }
30
+
31
+ /** An explicit browser package avoids handing a verified site URL back to ourselves. */
32
+ fun openBrowser(activity: Activity, uri: Uri) {
33
+ val customTabsPackage = CustomTabsClient.getPackageName(activity, null)
34
+ if (customTabsPackage != null && customTabsPackage != activity.packageName) {
35
+ val tab = CustomTabsIntent.Builder().setShowTitle(true).build()
36
+ tab.intent.setPackage(customTabsPackage)
37
+ tab.launchUrl(activity, uri)
38
+ return
39
+ }
40
+ val probe = Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com")).addCategory(Intent.CATEGORY_BROWSABLE)
41
+ val browser = activity.packageManager.queryIntentActivities(probe, 0)
42
+ .firstOrNull { it.activityInfo.packageName != activity.packageName }
43
+ ?.activityInfo?.packageName
44
+ ?: throw IllegalStateException("No browser is installed")
45
+ activity.startActivity(Intent(Intent.ACTION_VIEW, uri).addCategory(Intent.CATEGORY_BROWSABLE).setPackage(browser))
46
+ }
47
+ }
@@ -17,7 +17,6 @@ import android.speech.tts.UtteranceProgressListener
17
17
  import android.util.Log
18
18
  import android.view.WindowManager
19
19
  import androidx.appcompat.app.AlertDialog
20
- import androidx.browser.customtabs.CustomTabsIntent
21
20
  import androidx.core.app.ActivityCompat
22
21
  import androidx.core.app.NotificationManagerCompat
23
22
  import androidx.core.content.ContextCompat
@@ -204,7 +203,7 @@ internal class CraftNativeServices(
204
203
  pendingBrowser = callback to done
205
204
  browserOpened = true
206
205
  try {
207
- CustomTabsIntent.Builder().setShowTitle(true).build().launchUrl(host, uri)
206
+ CraftAppLinks.openBrowser(host, uri)
208
207
  } catch (_: ActivityNotFoundException) {
209
208
  host.startActivity(Intent(Intent.ACTION_VIEW, uri))
210
209
  }
@@ -99,6 +99,11 @@ class MainActivity : AppCompatActivity() {
99
99
  // reading its intent, which onNewIntent may have replaced with a
100
100
  // later warm link.
101
101
  val launchedFromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
102
+ if (savedInstanceState == null && !launchedFromHistory && intent.data?.let { CraftAppLinks.opensInBrowser(configJson(), it) } == true) {
103
+ CraftAppLinks.openBrowser(this, intent.data!!)
104
+ finish()
105
+ return
106
+ }
102
107
  if (savedInstanceState == null) {
103
108
  if (!launchedFromHistory) {
104
109
  intent.data?.toString()?.let(craftBridge::receiveDeepLink)
@@ -300,6 +305,12 @@ class MainActivity : AppCompatActivity() {
300
305
  override fun onNewIntent(intent: Intent) {
301
306
  super.onNewIntent(intent)
302
307
  setIntent(intent)
308
+ intent.data?.let { link ->
309
+ if (CraftAppLinks.opensInBrowser(configJson(), link)) {
310
+ CraftAppLinks.openBrowser(this, link)
311
+ return
312
+ }
313
+ }
303
314
  intent.data?.toString()?.let(craftBridge::receiveDeepLink)
304
315
  handleIncomingShortcut(intent)
305
316
  }
@@ -3458,6 +3458,11 @@ class MainActivity : AppCompatActivity() {
3458
3458
  routes = CraftNativeRoutes(config)
3459
3459
  stores = CraftNativeStores.get(this, config)
3460
3460
  services = CraftNativeServices(this, config) { this }
3461
+ if (savedInstanceState == null && intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY == 0 && intent.data?.let { CraftAppLinks.opensInBrowser(config, it) } == true) {
3462
+ CraftAppLinks.openBrowser(this, intent.data!!)
3463
+ finish()
3464
+ return
3465
+ }
3461
3466
  services.onBackgroundComplete = { ok, id ->
3462
3467
  if (id == null) {
3463
3468
  val waiting = backgroundWaiters.values.toList()
@@ -6182,6 +6187,10 @@ class MainActivity : AppCompatActivity() {
6182
6187
  }
6183
6188
  intent.data?.let { link ->
6184
6189
  if (::services.isInitialized && services.deepLink(link)) return
6190
+ if (CraftAppLinks.opensInBrowser(config, link)) {
6191
+ CraftAppLinks.openBrowser(this, link)
6192
+ return
6193
+ }
6185
6194
  publish("DEEP_LINK", deepLinkData(link).put("initial", false))
6186
6195
  if (::routes.isInitialized && !routes.isEmpty) routes.appPath(link.toString(), urlSchemes)?.let { navigateOpen(it, fromLink = true) }
6187
6196
  }
package/dist/cli.js CHANGED
@@ -3424,7 +3424,7 @@ function pinCraftNativeDependency(packagePath, craftVersion) {
3424
3424
  `);
3425
3425
  }
3426
3426
  // package.json
3427
- var version = "0.0.199";
3427
+ var version = "0.0.201";
3428
3428
 
3429
3429
  // bin/cli.ts
3430
3430
  var spawnedFrom = process6.env[CRAFT_CLI_SPAWN_MARKER];
package/dist/index.cjs CHANGED
@@ -309,7 +309,7 @@ function craftBinaryNotFoundMessage(triedPath) {
309
309
  `);
310
310
  }
311
311
  // package.json
312
- var version = "0.0.199";
312
+ var version = "0.0.201";
313
313
 
314
314
  // src/sdk-version.ts
315
315
  var SDK_VERSION = version;
@@ -10403,11 +10403,17 @@ ${e.stdout ?? ""}`;
10403
10403
  };
10404
10404
  }
10405
10405
  async function verifyBundleTrust(appPath, policy = {}) {
10406
- const identity = await readBundleIdentity(appPath);
10407
- const requireNotarized = policy.requireNotarized ?? true;
10408
10406
  if (!import_fs3.existsSync(appPath)) {
10409
- return { ok: false, identity, gatekeeperSource: null, reason: "unreadable", detail: `No such bundle: ${appPath}` };
10407
+ return {
10408
+ ok: false,
10409
+ identity: { identifier: null, teamId: null, authority: null },
10410
+ gatekeeperSource: null,
10411
+ reason: "unreadable",
10412
+ detail: `No such bundle: ${appPath}`
10413
+ };
10410
10414
  }
10415
+ const identity = await readBundleIdentity(appPath);
10416
+ const requireNotarized = policy.requireNotarized ?? true;
10411
10417
  try {
10412
10418
  await execFileAsync("codesign", ["--verify", "--deep", "--strict", "--verbose=2", appPath]);
10413
10419
  } catch (error) {
package/dist/index.js CHANGED
@@ -57,7 +57,7 @@ function craftBinaryNotFoundMessage(triedPath) {
57
57
  `);
58
58
  }
59
59
  // package.json
60
- var version = "0.0.199";
60
+ var version = "0.0.201";
61
61
 
62
62
  // src/sdk-version.ts
63
63
  var SDK_VERSION = version;
@@ -10162,11 +10162,17 @@ ${e.stdout ?? ""}`;
10162
10162
  };
10163
10163
  }
10164
10164
  async function verifyBundleTrust(appPath, policy = {}) {
10165
- const identity = await readBundleIdentity(appPath);
10166
- const requireNotarized = policy.requireNotarized ?? true;
10167
10165
  if (!existsSync4(appPath)) {
10168
- return { ok: false, identity, gatekeeperSource: null, reason: "unreadable", detail: `No such bundle: ${appPath}` };
10166
+ return {
10167
+ ok: false,
10168
+ identity: { identifier: null, teamId: null, authority: null },
10169
+ gatekeeperSource: null,
10170
+ reason: "unreadable",
10171
+ detail: `No such bundle: ${appPath}`
10172
+ };
10169
10173
  }
10174
+ const identity = await readBundleIdentity(appPath);
10175
+ const requireNotarized = policy.requireNotarized ?? true;
10170
10176
  try {
10171
10177
  await execFileAsync("codesign", ["--verify", "--deep", "--strict", "--verbose=2", appPath]);
10172
10178
  } catch (error) {
@@ -1022,6 +1022,7 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1022
1022
  private var tabBarHeight: CGFloat = 0
1023
1023
  private var tabSwipeRecognizers: [UIGestureRecognizer] = []
1024
1024
  private var activeTabSwipeTarget: String?
1025
+ private var tabSwipeSettling = false
1025
1026
  /// The edge the incoming tab started from for the current gesture. Keep
1026
1027
  /// this separate from the live frame: deriving it after the drag begins
1027
1028
  /// makes the target jump when the translation crosses the midpoint.
@@ -1077,6 +1078,7 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1077
1078
  }
1078
1079
 
1079
1080
  @objc private func tabSwipeRecognized(_ recognizer: UIPanGestureRecognizer) {
1081
+ guard !tabSwipeSettling else { return }
1080
1082
  let tabs = hybrid.tabIds
1081
1083
  guard let index = tabs.firstIndex(of: currentTab), tabs.count > 1 else { return }
1082
1084
  let translation = recognizer.translation(in: view).x
@@ -1101,9 +1103,19 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1101
1103
  CraftChrome.shared.swipe(to: tabs[next])
1102
1104
  return
1103
1105
  }
1104
- activeTabSwipeTarget = nil
1105
- let complete = recognizer.state == .ended && (abs(translation) >= max(32, view.bounds.width * 0.28) || abs(velocity) > 700)
1106
+ // Keep the target until the settling animation has read it and
1107
+ // finished. Clearing it here made finishInteractiveTabSwipe return
1108
+ // early, leaving cancelled screens offset and completed swipes
1109
+ // jumping straight to their destination.
1110
+ tabSwipeSettling = true
1111
+ let direction: CGFloat = tabSwipeFrom > 0 ? -1 : 1
1112
+ let progress = translation * direction
1113
+ let speed = velocity * direction
1114
+ let complete = recognizer.state == .ended && speed > -700
1115
+ && (progress >= max(32, view.bounds.width * 0.28) || speed > 700)
1106
1116
  finishInteractiveTabSwipe(completed: complete) { [weak self] in
1117
+ self?.activeTabSwipeTarget = nil
1118
+ self?.tabSwipeSettling = false
1107
1119
  guard complete else {
1108
1120
  CraftChrome.shared.cancelTabSwipe()
1109
1121
  return
@@ -1121,8 +1133,19 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1121
1133
  /// transparent until its page route catches up. Two web roots still use
1122
1134
  /// the route-aware fallback because one WebView cannot render both pages.
1123
1135
  private func beginInteractiveTabSwipe(to id: String, direction: CGFloat) -> Bool {
1124
- guard let current = visibleStack, let target = stacks[id],
1125
- current.view !== target.view,
1136
+ guard let current = visibleStack, view.bounds.width > 0 else { return false }
1137
+ let target: UINavigationController
1138
+ if let mounted = stacks[id] {
1139
+ target = mounted
1140
+ } else {
1141
+ // A native tab must also follow the first swipe to it. Do not
1142
+ // create a second web root here: the shared WebView cannot draw
1143
+ // both pages during the same gesture.
1144
+ guard hybrid.routes.match(id) != nil else { return false }
1145
+ target = makeStack(tab: id, rootPath: id)
1146
+ (target.topViewController as? CraftNativeScreenController)?.screenVisibilityChanged(false)
1147
+ }
1148
+ guard current.view !== target.view,
1126
1149
  !(current.topViewController is CraftHybridWebSlot && target.topViewController is CraftHybridWebSlot),
1127
1150
  view.bounds.width > 0 else { return false }
1128
1151
  if let slot = target.topViewController as? CraftHybridWebSlot {
@@ -1155,7 +1178,11 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1155
1178
  guard let targetId = activeTabSwipeTarget, let current = visibleStack,
1156
1179
  let target = stacks[targetId] else { return }
1157
1180
  let width = max(1, view.bounds.width)
1158
- let offset = min(width, max(-width, translation))
1181
+ // Reversing past the start stays at zero progress, rather than
1182
+ // pulling both screens away and exposing the background.
1183
+ let offset = tabSwipeFrom > 0
1184
+ ? min(0, max(-width, translation))
1185
+ : max(0, min(width, translation))
1159
1186
  target.view.frame.origin.x = tabSwipeFrom + offset
1160
1187
  current.view.frame.origin.x = offset
1161
1188
  }
@@ -1164,14 +1191,19 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1164
1191
  guard let targetId = activeTabSwipeTarget, let current = visibleStack,
1165
1192
  let target = stacks[targetId] else { completion(); return }
1166
1193
  let width = view.bounds.width
1167
- let direction: CGFloat = target.view.frame.minX >= 0 ? 1 : -1
1194
+ let direction: CGFloat = tabSwipeFrom > 0 ? 1 : -1
1168
1195
  let end = completed ? 0 : direction * width
1169
- let duration = CraftChrome.motion(.easeOut(duration: completed ? 0.22 : 0.18)) == nil ? 0 : 0.22
1170
- UIView.animate(withDuration: duration, animations: {
1196
+ let settlingDuration = completed ? 0.22 : 0.18
1197
+ let duration = CraftChrome.motion(.easeOut(duration: settlingDuration)) == nil ? 0 : settlingDuration
1198
+ UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseOut, .beginFromCurrentState], animations: {
1171
1199
  target.view.frame.origin.x = end
1172
1200
  current.view.frame.origin.x = completed ? -direction * width : 0
1173
1201
  }, completion: { _ in
1174
- if !completed {
1202
+ if completed {
1203
+ current.view.isHidden = true
1204
+ current.view.frame = self.view.bounds
1205
+ target.view.frame = self.view.bounds
1206
+ } else {
1175
1207
  target.view.isHidden = true
1176
1208
  target.view.frame = self.view.bounds
1177
1209
  current.view.frame = self.view.bounds
@@ -1690,7 +1722,8 @@ final class CraftHybridController: UIViewController, UINavigationControllerDeleg
1690
1722
 
1691
1723
  func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
1692
1724
  if tabSwipeRecognizers.contains(where: { $0 === gestureRecognizer }) {
1693
- guard hybrid.tabIds.count > 1, let nav = visibleStack, let top = nav.topViewController else { return false }
1725
+ guard activeTabSwipeTarget == nil, !tabSwipeSettling,
1726
+ hybrid.tabIds.count > 1, let nav = visibleStack, let top = nav.topViewController else { return false }
1694
1727
  guard CraftChrome.shared.tabSwipeEnabled else { return false }
1695
1728
  // Only between the tabs' own first screens, as the bar shows them:
1696
1729
  // deeper in a tab a swipe to the right is Back, and a screen that
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "craft-native",
3
- "version": "0.0.199",
3
+ "version": "0.0.201",
4
4
  "type": "module",
5
5
  "description": "Build desktop apps with web languages - TypeScript SDK for Craft",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",