@native-sdk/cli 0.8.4 → 0.9.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.
Files changed (161) hide show
  1. package/build/app.zig +1178 -74
  2. package/build.zig +715 -17
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +10 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +435 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/merge_static_archives.mjs +78 -0
  12. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  13. package/packages/core/scripts/run_external_core_compiler.mjs +122 -6
  14. package/packages/core/scripts/run_external_service_compiler.mjs +280 -0
  15. package/packages/core/scripts/stage_external_core.mjs +9 -1
  16. package/packages/core/scripts/stage_external_services.mjs +172 -0
  17. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  18. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  19. package/packages/core/sdk/core.d.ts +224 -0
  20. package/packages/core/sdk/core.ts +694 -22
  21. package/packages/core/sdk/events.d.ts +89 -0
  22. package/packages/core/sdk/events.ts +128 -0
  23. package/packages/core/src/checker.ts +936 -15
  24. package/packages/core/src/cli.ts +156 -3
  25. package/packages/core/src/contract.ts +28 -5
  26. package/packages/core/src/devhost.ts +1876 -16
  27. package/packages/core/src/devhost_journal.mjs +183 -0
  28. package/packages/core/src/devhost_resolver.mjs +17 -1
  29. package/packages/core/src/devhost_service_worker.mjs +90 -0
  30. package/packages/core/src/diagnostics.ts +179 -22
  31. package/packages/core/src/frontend.ts +210 -34
  32. package/packages/core/src/infer.ts +24 -0
  33. package/packages/core/src/modules.ts +129 -80
  34. package/packages/core/src/service_contract.ts +597 -0
  35. package/packages/core/src/sqlite_cli.ts +93 -0
  36. package/packages/core/src/sqlite_codegen.ts +806 -0
  37. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  38. package/packages/core/src/typed_ast.ts +36 -8
  39. package/skill-data/automation/SKILL.md +1 -1
  40. package/skill-data/core/references/project-anatomy.md +1 -1
  41. package/skill-data/native-ui/SKILL.md +58 -5
  42. package/skill-data/ts-core/SKILL.md +123 -41
  43. package/skill-data/ts-services/SKILL.md +126 -0
  44. package/skill-data/ts-services/references/service-surface.md +604 -0
  45. package/skill-data/zig/SKILL.md +1 -1
  46. package/skills/native-sdk/SKILL.md +1 -1
  47. package/src/app_runner/no_migrations.zig +3 -0
  48. package/src/app_runner/no_services.zig +20 -0
  49. package/src/app_runner/root.zig +133 -4
  50. package/src/app_runner/ts_core_main.zig +254 -8
  51. package/src/app_runner/ts_core_mobile.zig +241 -0
  52. package/src/automation/protocol.zig +5 -1
  53. package/src/automation/snapshot.zig +31 -11
  54. package/src/embed/app_exports.zig +12 -1
  55. package/src/embed/c_api.zig +48 -2
  56. package/src/embed/conversions.zig +4 -5
  57. package/src/embed/host.zig +109 -1
  58. package/src/embed/root.zig +8 -0
  59. package/src/embed/tests.zig +241 -0
  60. package/src/embed/types.zig +51 -0
  61. package/src/embed/ui_host.zig +185 -5
  62. package/src/platform/android/NativeSdkActivity.java +131 -0
  63. package/src/platform/android/android_host.c +182 -1
  64. package/src/platform/android/native_sdk_app.h +17 -0
  65. package/src/platform/ios/native_sdk_app.h +17 -0
  66. package/src/platform/ios/uikit_host.m +115 -0
  67. package/src/platform/linux/cef_host.cpp +8 -2
  68. package/src/platform/linux/gtk_host.c +289 -22
  69. package/src/platform/linux/gtk_host.h +5 -1
  70. package/src/platform/linux/root.zig +109 -22
  71. package/src/platform/macos/appkit_host.h +24 -7
  72. package/src/platform/macos/appkit_host.m +686 -64
  73. package/src/platform/macos/cef_host.mm +400 -66
  74. package/src/platform/macos/root.zig +304 -47
  75. package/src/platform/null_platform.zig +435 -48
  76. package/src/platform/null_platform_tests.zig +113 -0
  77. package/src/platform/root.zig +19 -0
  78. package/src/platform/types.zig +281 -28
  79. package/src/platform/windows/root.zig +207 -27
  80. package/src/platform/windows/webview2_host.cpp +334 -51
  81. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  82. package/src/primitives/app_manifest/root.zig +4 -0
  83. package/src/primitives/app_manifest/tests.zig +3 -0
  84. package/src/primitives/app_manifest/types.zig +32 -0
  85. package/src/primitives/app_manifest/validation.zig +19 -1
  86. package/src/root.zig +53 -0
  87. package/src/runtime/api.zig +19 -0
  88. package/src/runtime/automation_commands.zig +35 -2
  89. package/src/runtime/automation_snapshot.zig +32 -20
  90. package/src/runtime/bridge_permission_tests.zig +58 -3
  91. package/src/runtime/bridge_responses.zig +4 -2
  92. package/src/runtime/builtin_bridge.zig +11 -1
  93. package/src/runtime/canvas_frame.zig +18 -11
  94. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  95. package/src/runtime/clock.zig +13 -1
  96. package/src/runtime/core.zig +145 -7
  97. package/src/runtime/credentials_store.zig +115 -0
  98. package/src/runtime/effects.zig +3701 -231
  99. package/src/runtime/effects_credentials_tests.zig +332 -0
  100. package/src/runtime/effects_file_tests.zig +587 -0
  101. package/src/runtime/effects_host_tests.zig +208 -1
  102. package/src/runtime/effects_notification_tests.zig +11 -0
  103. package/src/runtime/effects_relational_tests.zig +314 -0
  104. package/src/runtime/effects_store_tests.zig +211 -0
  105. package/src/runtime/file_access.zig +400 -0
  106. package/src/runtime/file_effect_crash_helper.zig +28 -0
  107. package/src/runtime/file_effect_crash_tests.zig +37 -0
  108. package/src/runtime/flow.zig +35 -10
  109. package/src/runtime/gpu_surface_events.zig +6 -1
  110. package/src/runtime/native_command_event_tests.zig +86 -2
  111. package/src/runtime/persist_store.zig +666 -0
  112. package/src/runtime/pty.zig +5 -2
  113. package/src/runtime/record_store.zig +788 -0
  114. package/src/runtime/relational_store.zig +854 -0
  115. package/src/runtime/root.zig +54 -0
  116. package/src/runtime/runtime_event_tests.zig +70 -0
  117. package/src/runtime/service_host.zig +711 -0
  118. package/src/runtime/service_pool.zig +1314 -0
  119. package/src/runtime/session_blobs.zig +4 -1
  120. package/src/runtime/session_journal.zig +158 -5
  121. package/src/runtime/session_record.zig +215 -0
  122. package/src/runtime/session_replay.zig +206 -11
  123. package/src/runtime/session_tests.zig +81 -0
  124. package/src/runtime/shell_layout.zig +1 -0
  125. package/src/runtime/shell_layout_tests.zig +120 -0
  126. package/src/runtime/sqlite_engine.zig +474 -0
  127. package/src/runtime/state.zig +16 -0
  128. package/src/runtime/system_services.zig +170 -38
  129. package/src/runtime/tests.zig +4 -0
  130. package/src/runtime/ts_core_host.zig +1067 -95
  131. package/src/runtime/ts_core_host_tests.zig +325 -11
  132. package/src/runtime/ts_ui_app.zig +953 -2
  133. package/src/runtime/ui_app.zig +434 -96
  134. package/src/runtime/ui_app_tests.zig +287 -11
  135. package/src/runtime/ui_app_window_tests.zig +61 -7
  136. package/src/runtime/validation.zig +42 -0
  137. package/src/runtime/view.zig +9 -0
  138. package/src/runtime/window_storage.zig +1 -0
  139. package/src/runtime/window_view_runtime.zig +2 -3
  140. package/src/runtime/window_views.zig +19 -0
  141. package/src/tooling/android.zig +21 -16
  142. package/src/tooling/db.zig +160 -0
  143. package/src/tooling/ios.zig +7 -1
  144. package/src/tooling/manifest.zig +214 -2
  145. package/src/tooling/package.zig +116 -0
  146. package/src/tooling/raw_manifest.zig +30 -0
  147. package/src/tooling/root.zig +1 -0
  148. package/src/tooling/templates.zig +189 -4
  149. package/src/tooling/ts_core.zig +366 -17
  150. package/src/tooling/verbs.zig +7 -0
  151. package/third_party/sqlite/README.md +13 -0
  152. package/third_party/sqlite/sqlite3.c +269649 -0
  153. package/third_party/sqlite/sqlite3.h +14349 -0
  154. package/tools/corewire/core_abi.zig +14 -2
  155. package/tools/corewire/emit.zig +98 -29
  156. package/tools/corewire/emit_facade.zig +442 -27
  157. package/tools/corewire/emit_profile.zig +6 -2
  158. package/tools/corewire/emit_service.zig +924 -0
  159. package/tools/corewire/main.zig +122 -0
  160. package/tools/corewire/service_contract.zig +250 -0
  161. package/tools/corewire/sidecar.zig +26 -15
@@ -3,12 +3,15 @@
3
3
  #include <gtk/gtk.h>
4
4
  #include <glib/gstdio.h>
5
5
  #include <dlfcn.h>
6
+ #include <errno.h>
7
+ #include <fcntl.h>
6
8
  #include <limits.h>
7
9
  #include <math.h>
8
10
  #include <stdint.h>
9
11
  #include <string.h>
10
12
  #include <stdlib.h>
11
13
  #include <stdio.h>
14
+ #include <unistd.h>
12
15
 
13
16
  /* NATIVE_SDK_ALLOW_WEBKITGTK_STUB is the build graph's declaration that
14
17
  * this app uses no web layer, and it wins over header visibility: on a
@@ -358,6 +361,7 @@ typedef struct native_sdk_gtk_window {
358
361
  int click_through;
359
362
  int activate_on_show;
360
363
  int show_on_first_present;
364
+ int policy_hidden;
361
365
  int shown;
362
366
  /* One-second safety reveal for a canvas window whose first present
363
367
  * never arrives. Zero once cancelled/fired. */
@@ -382,6 +386,13 @@ typedef struct native_sdk_gtk_window {
382
386
  int native_view_count;
383
387
  } native_sdk_gtk_window_t;
384
388
 
389
+ typedef struct native_sdk_gtk_notification_action {
390
+ char *command;
391
+ char *notification_id;
392
+ } native_sdk_gtk_notification_action_t;
393
+
394
+ static const char native_sdk_notification_action_name[] = "native-sdk-notification-action";
395
+
385
396
  struct native_sdk_gtk_host {
386
397
  GtkApplication *app;
387
398
  /* Display providers are global and the display retains them until
@@ -451,6 +462,14 @@ struct native_sdk_gtk_host {
451
462
  native_sdk_gtk_context_menu_t *context_menu_teardowns;
452
463
  /* Feeds each context menu's per-invocation action-group name. */
453
464
  uint64_t context_menu_serial;
465
+ /* Notification buttons carry only an unguessable, process-scoped
466
+ * token across the exported GApplication action. The command stays
467
+ * private to this host, so another session-bus client cannot supply
468
+ * an arbitrary command name and notifications from an earlier app
469
+ * execution cannot dispatch into this one. The id map invalidates a
470
+ * superseded same-id notification's token before replacement. */
471
+ GHashTable *notification_actions;
472
+ GHashTable *notification_action_tokens_by_id;
454
473
  native_sdk_gtk_audio_t audio;
455
474
  };
456
475
 
@@ -470,6 +489,46 @@ static char *native_sdk_strndup(const char *s, size_t len) {
470
489
  return out;
471
490
  }
472
491
 
492
+ static void native_sdk_notification_action_free(gpointer data) {
493
+ native_sdk_gtk_notification_action_t *notification_action = data;
494
+ if (!notification_action) return;
495
+ free(notification_action->command);
496
+ free(notification_action->notification_id);
497
+ free(notification_action);
498
+ }
499
+
500
+ static char *native_sdk_random_notification_action_token(void) {
501
+ uint8_t random_bytes[16];
502
+ int random_fd;
503
+ do {
504
+ random_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
505
+ } while (random_fd < 0 && errno == EINTR);
506
+ if (random_fd < 0) return NULL;
507
+
508
+ size_t offset = 0;
509
+ while (offset < sizeof(random_bytes)) {
510
+ const ssize_t count = read(
511
+ random_fd, random_bytes + offset, sizeof(random_bytes) - offset);
512
+ if (count > 0) {
513
+ offset += (size_t)count;
514
+ continue;
515
+ }
516
+ if (count < 0 && errno == EINTR) continue;
517
+ close(random_fd);
518
+ return NULL;
519
+ }
520
+ close(random_fd);
521
+
522
+ static const char hex[] = "0123456789abcdef";
523
+ char *token = g_malloc(sizeof(random_bytes) * 2 + 1);
524
+ for (size_t i = 0; i < sizeof(random_bytes); i++) {
525
+ token[i * 2] = hex[random_bytes[i] >> 4];
526
+ token[i * 2 + 1] = hex[random_bytes[i] & 0x0f];
527
+ }
528
+ token[sizeof(random_bytes) * 2] = '\0';
529
+ return token;
530
+ }
531
+
473
532
  static void native_sdk_free_string_list(char **list, int count) {
474
533
  if (!list) return;
475
534
  for (int i = 0; i < count; i++) free(list[i]);
@@ -2025,7 +2084,7 @@ static const char *native_sdk_bridge_script(void) {
2025
2084
  "});"
2026
2085
  "var os=Object.freeze({"
2027
2086
  "openUrl:function(value){var options=typeof value==='string'?{url:value}:(value||{});return invoke('native-sdk.os.openUrl',{url:ensureString(options.url,'url')});},"
2028
- "showNotification:function(value){var options=typeof value==='string'?{title:value}:(value||{});var payload={title:ensureString(options.title,'title')};if(options.subtitle!=null){payload.subtitle=ensureString(options.subtitle,'subtitle');}if(options.body!=null){payload.body=ensureString(options.body,'body');}return invoke('native-sdk.os.showNotification',payload);},"
2087
+ "showNotification:function(value){var options=typeof value==='string'?{title:value}:(value||{});var payload={title:ensureString(options.title,'title')};if(options.id!=null){payload.id=ensureString(options.id,'id');}if(options.subtitle!=null){payload.subtitle=ensureString(options.subtitle,'subtitle');}if(options.body!=null){payload.body=ensureString(options.body,'body');}if(options.actionLabel!=null){payload.actionLabel=ensureString(options.actionLabel,'actionLabel');}if(options.actionCommand!=null){payload.actionCommand=ensureString(options.actionCommand,'actionCommand');}return invoke('native-sdk.os.showNotification',payload);},"
2029
2088
  "revealPath:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.revealPath',{path:ensureString(options.path,'path')});},"
2030
2089
  "addRecentDocument:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.addRecentDocument',{path:ensureString(options.path,'path')});},"
2031
2090
  "clearRecentDocuments:function(){return invoke('native-sdk.os.clearRecentDocuments',{});}"
@@ -2518,6 +2577,7 @@ static void native_sdk_emit_window_frame(native_sdk_gtk_host_t *host, native_sdk
2518
2577
  .scale = scale,
2519
2578
  .open = open,
2520
2579
  .focused = focused,
2580
+ .hidden = open ? win->policy_hidden : 0,
2521
2581
  .label = win->label ? win->label : "",
2522
2582
  .label_len = win->label ? strlen(win->label) : 0,
2523
2583
  .title = win->title ? win->title : "",
@@ -3076,6 +3136,7 @@ static native_sdk_gtk_window_t *native_sdk_create_window_internal(native_sdk_gtk
3076
3136
  win->click_through = (window_flags & (1u << 2)) != 0;
3077
3137
  win->activate_on_show = (window_flags & (1u << 3)) == 0;
3078
3138
  win->show_on_first_present = show_policy == 1;
3139
+ win->policy_hidden = show_policy == 2;
3079
3140
  if (!win->label || !win->title) {
3080
3141
  free(win->label);
3081
3142
  free(win->title);
@@ -3201,7 +3262,7 @@ static void on_activate(GtkApplication *app, gpointer data) {
3201
3262
  host->init_show_policy, host->init_window_flags);
3202
3263
  if (!win) return;
3203
3264
 
3204
- if (!win->show_on_first_present) native_sdk_show_window_implicit(win);
3265
+ if (!win->show_on_first_present && !win->policy_hidden) native_sdk_show_window_implicit(win);
3205
3266
  native_sdk_emit_app_active_if_changed(host);
3206
3267
 
3207
3268
  native_sdk_emit(host, (native_sdk_gtk_event_t){ .kind = NATIVE_SDK_GTK_EVENT_START });
@@ -3250,6 +3311,10 @@ native_sdk_gtk_host_t *native_sdk_gtk_create(
3250
3311
  host->allowed_external_urls_count = 0;
3251
3312
 
3252
3313
  host->app = gtk_application_new(host->bundle_id, G_APPLICATION_DEFAULT_FLAGS);
3314
+ host->notification_actions = g_hash_table_new_full(
3315
+ g_str_hash, g_str_equal, g_free, native_sdk_notification_action_free);
3316
+ host->notification_action_tokens_by_id = g_hash_table_new_full(
3317
+ g_str_hash, g_str_equal, g_free, g_free);
3253
3318
 
3254
3319
  return host;
3255
3320
  }
@@ -3292,6 +3357,15 @@ void native_sdk_gtk_destroy(native_sdk_gtk_host_t *host) {
3292
3357
  g_object_unref(host->transparent_css_provider);
3293
3358
  g_object_unref(host->transparent_css_display);
3294
3359
  }
3360
+ if (host->app && g_action_map_lookup_action(
3361
+ G_ACTION_MAP(host->app), native_sdk_notification_action_name)) {
3362
+ g_action_map_remove_action(
3363
+ G_ACTION_MAP(host->app), native_sdk_notification_action_name);
3364
+ }
3365
+ if (host->notification_action_tokens_by_id) {
3366
+ g_hash_table_destroy(host->notification_action_tokens_by_id);
3367
+ }
3368
+ if (host->notification_actions) g_hash_table_destroy(host->notification_actions);
3295
3369
  g_object_unref(host->app);
3296
3370
  free(host->app_name);
3297
3371
  free(host->window_title);
@@ -3772,7 +3846,7 @@ int native_sdk_gtk_create_window(native_sdk_gtk_host_t *host, uint64_t window_id
3772
3846
  free(label);
3773
3847
  if (!win) return 0;
3774
3848
 
3775
- if (!win->show_on_first_present) native_sdk_show_window_implicit(win);
3849
+ if (!win->show_on_first_present && !win->policy_hidden) native_sdk_show_window_implicit(win);
3776
3850
  return 1;
3777
3851
  }
3778
3852
 
@@ -3943,6 +4017,7 @@ int native_sdk_gtk_focus_window(native_sdk_gtk_host_t *host, uint64_t window_id)
3943
4017
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3944
4018
  if (!win || !win->gtk_window) return 0;
3945
4019
  native_sdk_cancel_deferred_show(win);
4020
+ win->policy_hidden = 0;
3946
4021
  gtk_window_present(win->gtk_window);
3947
4022
  win->shown = 1;
3948
4023
  native_sdk_emit_window_frame(host, win, 1);
@@ -3953,12 +4028,24 @@ int native_sdk_gtk_show_window(native_sdk_gtk_host_t *host, uint64_t window_id)
3953
4028
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3954
4029
  if (!win || !win->gtk_window) return 0;
3955
4030
  native_sdk_cancel_deferred_show(win);
4031
+ win->policy_hidden = 0;
3956
4032
  if (!win->activate_on_show) gtk_window_unminimize(win->gtk_window);
3957
4033
  native_sdk_show_window_implicit(win);
3958
4034
  native_sdk_emit_window_frame(host, win, 1);
3959
4035
  return 1;
3960
4036
  }
3961
4037
 
4038
+ int native_sdk_gtk_hide_window(native_sdk_gtk_host_t *host, uint64_t window_id) {
4039
+ native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
4040
+ if (!win || !win->gtk_window) return 0;
4041
+ native_sdk_cancel_deferred_show(win);
4042
+ win->policy_hidden = 1;
4043
+ win->shown = 0;
4044
+ gtk_widget_set_visible(GTK_WIDGET(win->gtk_window), FALSE);
4045
+ native_sdk_emit_window_frame(host, win, 1);
4046
+ return 1;
4047
+ }
4048
+
3962
4049
  int native_sdk_gtk_close_window(native_sdk_gtk_host_t *host, uint64_t window_id) {
3963
4050
  native_sdk_gtk_window_t *win = native_sdk_find_window(host, window_id);
3964
4051
  if (!win || !win->gtk_window) return 0;
@@ -4381,7 +4468,7 @@ int native_sdk_gtk_present_gpu_surface_pixels(native_sdk_gtk_host_t *host, uint6
4381
4468
  * placeholder pump — from here on frames exist only on demand. */
4382
4469
  const int first_present = !view->gpu_presented;
4383
4470
  view->gpu_presented = 1;
4384
- if (first_present && win && !win->shown) {
4471
+ if (first_present && win && !win->shown && !win->policy_hidden) {
4385
4472
  native_sdk_cancel_deferred_show(win);
4386
4473
  native_sdk_show_window_implicit(win);
4387
4474
  native_sdk_emit_window_frame(host, win, 1);
@@ -4739,9 +4826,80 @@ int native_sdk_gtk_reveal_path(native_sdk_gtk_host_t *host, const char *path, si
4739
4826
  return 1;
4740
4827
  }
4741
4828
 
4742
- int native_sdk_gtk_show_notification(native_sdk_gtk_host_t *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len) {
4829
+ static void native_sdk_forget_notification_action_for_id(native_sdk_gtk_host_t *host, const char *notification_id) {
4830
+ if (!host || !host->notification_actions ||
4831
+ !host->notification_action_tokens_by_id || !notification_id) return;
4832
+ const char *token = g_hash_table_lookup(
4833
+ host->notification_action_tokens_by_id, notification_id);
4834
+ if (token) g_hash_table_remove(host->notification_actions, token);
4835
+ g_hash_table_remove(host->notification_action_tokens_by_id, notification_id);
4836
+ }
4837
+
4838
+ static char *native_sdk_take_notification_action_command(
4839
+ native_sdk_gtk_host_t *host, const char *token, size_t token_len)
4840
+ {
4841
+ if (!host || !host->notification_actions || !token || token_len == 0) return NULL;
4842
+ native_sdk_gtk_notification_action_t *notification_action =
4843
+ g_hash_table_lookup(host->notification_actions, token);
4844
+ if (!notification_action || !notification_action->command) return NULL;
4845
+
4846
+ char *command = native_sdk_strndup(
4847
+ notification_action->command, strlen(notification_action->command));
4848
+ if (!command) return NULL;
4849
+ if (notification_action->notification_id &&
4850
+ host->notification_action_tokens_by_id) {
4851
+ const char *current_token = g_hash_table_lookup(
4852
+ host->notification_action_tokens_by_id,
4853
+ notification_action->notification_id);
4854
+ if (current_token && strcmp(current_token, token) == 0) {
4855
+ g_hash_table_remove(
4856
+ host->notification_action_tokens_by_id,
4857
+ notification_action->notification_id);
4858
+ }
4859
+ }
4860
+ /* One click consumes the capability before app code runs. App code
4861
+ * may synchronously show or replace another notification. */
4862
+ g_hash_table_remove(host->notification_actions, token);
4863
+ return command;
4864
+ }
4865
+
4866
+ static void native_sdk_notification_action_activated(GSimpleAction *action, GVariant *parameter, gpointer data) {
4867
+ (void)action;
4868
+ native_sdk_gtk_host_t *host = data;
4869
+ if (!host || !parameter || !g_variant_is_of_type(parameter, G_VARIANT_TYPE_STRING)) return;
4870
+ gsize token_len = 0;
4871
+ const char *token = g_variant_get_string(parameter, &token_len);
4872
+ char *command = native_sdk_take_notification_action_command(host, token, token_len);
4873
+ if (!command) return;
4874
+ const size_t command_len = strlen(command);
4875
+ native_sdk_emit(host, (native_sdk_gtk_event_t){
4876
+ .kind = NATIVE_SDK_GTK_EVENT_NOTIFICATION_COMMAND,
4877
+ .window_id = 1,
4878
+ .command_name = command,
4879
+ .command_name_len = command_len,
4880
+ });
4881
+ free(command);
4882
+ }
4883
+
4884
+ static int native_sdk_ensure_notification_action(native_sdk_gtk_host_t *host) {
4885
+ GActionMap *map = G_ACTION_MAP(host->app);
4886
+ if (g_action_map_lookup_action(map, native_sdk_notification_action_name)) return 1;
4887
+ GSimpleAction *action = g_simple_action_new(
4888
+ native_sdk_notification_action_name, G_VARIANT_TYPE_STRING);
4889
+ if (!action) return 0;
4890
+ g_signal_connect(action, "activate", G_CALLBACK(native_sdk_notification_action_activated), host);
4891
+ g_action_map_add_action(map, G_ACTION(action));
4892
+ g_object_unref(action);
4893
+ return 1;
4894
+ }
4895
+
4896
+ int native_sdk_gtk_show_notification(native_sdk_gtk_host_t *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len, const char *notification_id, size_t notification_id_len, const char *action_label, size_t action_label_len, const char *action_command, size_t action_command_len) {
4743
4897
  if (!host || !host->app || !title || title_len == 0) return 0;
4744
- if ((subtitle_len > 0 && !subtitle) || (body_len > 0 && !body)) return 0;
4898
+ if ((subtitle_len > 0 && !subtitle) || (body_len > 0 && !body) ||
4899
+ (notification_id_len > 0 && !notification_id) ||
4900
+ (action_label_len > 0 && !action_label) ||
4901
+ (action_command_len > 0 && !action_command) ||
4902
+ ((action_label_len == 0) != (action_command_len == 0))) return 0;
4745
4903
  char *title_copy = native_sdk_strndup(title, title_len);
4746
4904
  if (!title_copy) return 0;
4747
4905
 
@@ -4771,7 +4929,59 @@ int native_sdk_gtk_show_notification(native_sdk_gtk_host_t *host, const char *ti
4771
4929
  free(message);
4772
4930
  }
4773
4931
 
4774
- g_application_send_notification(G_APPLICATION(host->app), NULL, notification);
4932
+ char *id_copy = notification_id_len > 0 ? native_sdk_strndup(notification_id, notification_id_len) : NULL;
4933
+ if (notification_id_len > 0 && !id_copy) {
4934
+ g_object_unref(notification);
4935
+ return 0;
4936
+ }
4937
+ if (id_copy) native_sdk_forget_notification_action_for_id(host, id_copy);
4938
+
4939
+ if (action_command_len > 0) {
4940
+ if (!native_sdk_ensure_notification_action(host)) {
4941
+ free(id_copy);
4942
+ g_object_unref(notification);
4943
+ return 0;
4944
+ }
4945
+ char *label_copy = native_sdk_strndup(action_label, action_label_len);
4946
+ char *command_copy = native_sdk_strndup(action_command, action_command_len);
4947
+ char *action_token = native_sdk_random_notification_action_token();
4948
+ native_sdk_gtk_notification_action_t *notification_action =
4949
+ calloc(1, sizeof(native_sdk_gtk_notification_action_t));
4950
+ if (!label_copy || !command_copy || !action_token || !notification_action) {
4951
+ free(label_copy);
4952
+ free(command_copy);
4953
+ g_free(action_token);
4954
+ free(notification_action);
4955
+ free(id_copy);
4956
+ g_object_unref(notification);
4957
+ return 0;
4958
+ }
4959
+ notification_action->command = command_copy;
4960
+ notification_action->notification_id = id_copy
4961
+ ? native_sdk_strndup(id_copy, strlen(id_copy))
4962
+ : NULL;
4963
+ if (id_copy && !notification_action->notification_id) {
4964
+ free(label_copy);
4965
+ g_free(action_token);
4966
+ native_sdk_notification_action_free(notification_action);
4967
+ free(id_copy);
4968
+ g_object_unref(notification);
4969
+ return 0;
4970
+ }
4971
+ g_notification_add_button_with_target_value(notification, label_copy,
4972
+ "app.native-sdk-notification-action", g_variant_new_string(action_token));
4973
+ g_hash_table_insert(host->notification_actions, action_token, notification_action);
4974
+ if (id_copy) {
4975
+ g_hash_table_replace(
4976
+ host->notification_action_tokens_by_id,
4977
+ g_strdup(id_copy),
4978
+ g_strdup(action_token));
4979
+ }
4980
+ free(label_copy);
4981
+ }
4982
+
4983
+ g_application_send_notification(G_APPLICATION(host->app), id_copy, notification);
4984
+ free(id_copy);
4775
4985
  g_object_unref(notification);
4776
4986
  return 1;
4777
4987
  }
@@ -4966,11 +5176,26 @@ static native_sdk_secret_api_t *native_sdk_load_secret_api(void) {
4966
5176
  return &native_sdk_secret_api;
4967
5177
  }
4968
5178
 
4969
- static void native_sdk_secure_free(char *bytes, size_t len) {
5179
+ static void native_sdk_secure_gfree(char *bytes, size_t len) {
4970
5180
  if (!bytes) return;
4971
5181
  volatile char *cursor = bytes;
4972
5182
  for (size_t i = 0; i < len; i++) cursor[i] = 0;
4973
- free(bytes);
5183
+ g_free(bytes);
5184
+ }
5185
+
5186
+ static const char native_sdk_credential_bytes_prefix[] = "native-sdk:bytes:v1:";
5187
+
5188
+ /* libsecret reports a locked collection through GError rather than a
5189
+ * dedicated return value. Keep that condition distinct at the C seam so the
5190
+ * core effect can deliver its closed `locked` outcome instead of flattening
5191
+ * it into `io_failed`. */
5192
+ static int native_sdk_secret_error_locked(const GError *error) {
5193
+ if (!error || !error->message) return 0;
5194
+ char *lower = g_ascii_strdown(error->message, -1);
5195
+ if (!lower) return 0;
5196
+ int locked = strstr(lower, "locked") != NULL;
5197
+ g_free(lower);
5198
+ return locked;
4974
5199
  }
4975
5200
 
4976
5201
  int native_sdk_gtk_credentials_available(native_sdk_gtk_host_t *host) {
@@ -4981,15 +5206,17 @@ int native_sdk_gtk_credentials_available(native_sdk_gtk_host_t *host) {
4981
5206
  int native_sdk_gtk_set_credential(native_sdk_gtk_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len, const char *secret, size_t secret_len) {
4982
5207
  (void)host;
4983
5208
  native_sdk_secret_api_t *api = native_sdk_load_secret_api();
4984
- if (!api || !service || service_len == 0 || !account || account_len == 0 || !secret || secret_len == 0) return 0;
5209
+ if (!api || !service || service_len == 0 || !account || account_len == 0 || (!secret && secret_len > 0)) return 0;
4985
5210
 
4986
5211
  char *service_copy = native_sdk_strndup(service, service_len);
4987
5212
  char *account_copy = native_sdk_strndup(account, account_len);
4988
- char *secret_copy = native_sdk_strndup(secret, secret_len);
5213
+ gchar *encoded = g_base64_encode((const guchar *)(secret ? secret : ""), secret_len);
5214
+ char *secret_copy = encoded ? g_strdup_printf("%s%s", native_sdk_credential_bytes_prefix, encoded) : NULL;
4989
5215
  if (!service_copy || !account_copy || !secret_copy) {
4990
5216
  free(service_copy);
4991
5217
  free(account_copy);
4992
- native_sdk_secure_free(secret_copy, secret_len);
5218
+ if (encoded) native_sdk_secure_gfree(encoded, strlen(encoded));
5219
+ if (secret_copy) native_sdk_secure_gfree(secret_copy, strlen(secret_copy));
4993
5220
  return 0;
4994
5221
  }
4995
5222
 
@@ -4997,32 +5224,35 @@ int native_sdk_gtk_set_credential(native_sdk_gtk_host_t *host, const char *servi
4997
5224
  if (!label) {
4998
5225
  free(service_copy);
4999
5226
  free(account_copy);
5000
- native_sdk_secure_free(secret_copy, secret_len);
5227
+ native_sdk_secure_gfree(encoded, strlen(encoded));
5228
+ native_sdk_secure_gfree(secret_copy, strlen(secret_copy));
5001
5229
  return 0;
5002
5230
  }
5003
5231
 
5004
5232
  GError *error = NULL;
5005
5233
  gboolean ok = api->store_sync(&native_sdk_credential_schema, "default", label, secret_copy, NULL, &error, "service", service_copy, "account", account_copy, NULL);
5234
+ int locked = native_sdk_secret_error_locked(error);
5006
5235
  if (error) g_error_free(error);
5007
5236
 
5008
5237
  g_free(label);
5009
5238
  free(service_copy);
5010
5239
  free(account_copy);
5011
- native_sdk_secure_free(secret_copy, secret_len);
5012
- return ok ? 1 : 0;
5240
+ native_sdk_secure_gfree(encoded, strlen(encoded));
5241
+ native_sdk_secure_gfree(secret_copy, strlen(secret_copy));
5242
+ return locked ? -2 : (ok ? 1 : 0);
5013
5243
  }
5014
5244
 
5015
5245
  size_t native_sdk_gtk_get_credential(native_sdk_gtk_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len, char *buffer, size_t buffer_len) {
5016
5246
  (void)host;
5017
5247
  native_sdk_secret_api_t *api = native_sdk_load_secret_api();
5018
- if (!api || !service || service_len == 0 || !account || account_len == 0 || !buffer) return 0;
5248
+ if (!api || !service || service_len == 0 || !account || account_len == 0 || (!buffer && buffer_len > 0)) return SIZE_MAX - 1;
5019
5249
 
5020
5250
  char *service_copy = native_sdk_strndup(service, service_len);
5021
5251
  char *account_copy = native_sdk_strndup(account, account_len);
5022
5252
  if (!service_copy || !account_copy) {
5023
5253
  free(service_copy);
5024
5254
  free(account_copy);
5025
- return 0;
5255
+ return SIZE_MAX - 1;
5026
5256
  }
5027
5257
 
5028
5258
  GError *error = NULL;
@@ -5030,14 +5260,28 @@ size_t native_sdk_gtk_get_credential(native_sdk_gtk_host_t *host, const char *se
5030
5260
  free(service_copy);
5031
5261
  free(account_copy);
5032
5262
  if (error) {
5263
+ int locked = native_sdk_secret_error_locked(error);
5033
5264
  g_error_free(error);
5034
- return (size_t)-1;
5265
+ return locked ? SIZE_MAX - 2 : SIZE_MAX - 1;
5035
5266
  }
5036
- if (!password) return 0;
5267
+ if (!password) return SIZE_MAX;
5037
5268
 
5038
5269
  size_t password_len = strlen(password);
5270
+ const size_t prefix_len = sizeof(native_sdk_credential_bytes_prefix) - 1;
5271
+ if (password_len >= prefix_len && memcmp(password, native_sdk_credential_bytes_prefix, prefix_len) == 0) {
5272
+ gsize decoded_len = 0;
5273
+ guchar *decoded = g_base64_decode(password + prefix_len, &decoded_len);
5274
+ if (!decoded && decoded_len != 0) {
5275
+ native_sdk_secure_gfree(password, password_len);
5276
+ return SIZE_MAX - 1;
5277
+ }
5278
+ if (decoded_len <= buffer_len && decoded_len > 0) memcpy(buffer, decoded, decoded_len);
5279
+ if (decoded) native_sdk_secure_gfree((char *)decoded, decoded_len);
5280
+ native_sdk_secure_gfree(password, password_len);
5281
+ return decoded_len;
5282
+ }
5039
5283
  if (password_len <= buffer_len && password_len > 0) memcpy(buffer, password, password_len);
5040
- g_free(password);
5284
+ native_sdk_secure_gfree(password, password_len);
5041
5285
  return password_len;
5042
5286
  }
5043
5287
 
@@ -5059,12 +5303,34 @@ int native_sdk_gtk_delete_credential(native_sdk_gtk_host_t *host, const char *se
5059
5303
  free(service_copy);
5060
5304
  free(account_copy);
5061
5305
  if (error) {
5306
+ int locked = native_sdk_secret_error_locked(error);
5062
5307
  g_error_free(error);
5063
- return -1;
5308
+ return locked ? -2 : -1;
5064
5309
  }
5065
5310
  return ok ? 1 : 0;
5066
5311
  }
5067
5312
 
5313
+ size_t native_sdk_gtk_format_local_time(native_sdk_gtk_host_t *host, int64_t timestamp_ms, int style, char *buffer, size_t buffer_len) {
5314
+ (void)host;
5315
+ if (!buffer || buffer_len == 0 || style < 0 || style > 2) return 0;
5316
+ gint64 seconds = timestamp_ms / 1000;
5317
+ if (timestamp_ms < 0 && timestamp_ms % 1000 != 0) seconds -= 1;
5318
+ GDateTime *date = g_date_time_new_from_unix_local(seconds);
5319
+ if (!date) return 0;
5320
+ const char *pattern = style == 0 ? "%x" : (style == 1 ? "%X" : "%x %X");
5321
+ char *text = g_date_time_format(date, pattern);
5322
+ g_date_time_unref(date);
5323
+ if (!text) return 0;
5324
+ size_t len = strlen(text);
5325
+ if (len == 0 || len > buffer_len) {
5326
+ g_free(text);
5327
+ return 0;
5328
+ }
5329
+ memcpy(buffer, text, len);
5330
+ g_free(text);
5331
+ return len;
5332
+ }
5333
+
5068
5334
  /* ---------------------------------------------------------------- audio
5069
5335
  *
5070
5336
  * Backend: the GStreamer PLAYBIN pipeline (gst-1.0). One in-box element
@@ -6317,7 +6583,8 @@ static void native_sdk_alert_done(GObject *source, GAsyncResult *result, gpointe
6317
6583
  }
6318
6584
 
6319
6585
  int native_sdk_gtk_show_message_dialog(native_sdk_gtk_host_t *host, const native_sdk_gtk_message_dialog_opts_t *opts) {
6320
- GtkAlertDialog *dialog = gtk_alert_dialog_new(NULL);
6586
+ /* The constructor requires a non-NULL printf-style format string. */
6587
+ GtkAlertDialog *dialog = gtk_alert_dialog_new("");
6321
6588
  char *title = native_sdk_bytes_to_string(opts->title, opts->title_len);
6322
6589
  char *message = native_sdk_bytes_to_string(opts->message, opts->message_len);
6323
6590
  char *informative = native_sdk_bytes_to_string(opts->informative_text, opts->informative_text_len);
@@ -31,6 +31,7 @@ typedef enum {
31
31
  NATIVE_SDK_GTK_EVENT_AUDIO = 17,
32
32
  NATIVE_SDK_GTK_EVENT_CONTEXT_MENU_ACTION = 18,
33
33
  NATIVE_SDK_GTK_EVENT_VIEW_FOCUSED = 19,
34
+ NATIVE_SDK_GTK_EVENT_NOTIFICATION_COMMAND = 20,
34
35
  } native_sdk_gtk_event_kind_t;
35
36
 
36
37
  typedef struct {
@@ -43,6 +44,7 @@ typedef struct {
43
44
  double y;
44
45
  int open;
45
46
  int focused;
47
+ int hidden;
46
48
  const char *label;
47
49
  size_t label_len;
48
50
  const char *title;
@@ -217,6 +219,7 @@ void native_sdk_gtk_start_timer(native_sdk_gtk_host_t *host, uint64_t timer_id,
217
219
  void native_sdk_gtk_cancel_timer(native_sdk_gtk_host_t *host, uint64_t timer_id);
218
220
  int native_sdk_gtk_focus_window(native_sdk_gtk_host_t *host, uint64_t window_id);
219
221
  int native_sdk_gtk_show_window(native_sdk_gtk_host_t *host, uint64_t window_id);
222
+ int native_sdk_gtk_hide_window(native_sdk_gtk_host_t *host, uint64_t window_id);
220
223
  int native_sdk_gtk_close_window(native_sdk_gtk_host_t *host, uint64_t window_id);
221
224
  /* The real OS minimize verb (gtk_window_minimize), for app-drawn window
222
225
  * controls on chromeless windows. Returns 0 when the window id is
@@ -246,13 +249,14 @@ int native_sdk_gtk_set_webview_layer(native_sdk_gtk_host_t *host, uint64_t windo
246
249
  int native_sdk_gtk_close_webview(native_sdk_gtk_host_t *host, uint64_t window_id, const char *label, size_t label_len);
247
250
  int native_sdk_gtk_open_external_url(native_sdk_gtk_host_t *host, const char *url, size_t url_len);
248
251
  int native_sdk_gtk_reveal_path(native_sdk_gtk_host_t *host, const char *path, size_t path_len);
249
- int native_sdk_gtk_show_notification(native_sdk_gtk_host_t *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len);
252
+ int native_sdk_gtk_show_notification(native_sdk_gtk_host_t *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len, const char *notification_id, size_t notification_id_len, const char *action_label, size_t action_label_len, const char *action_command, size_t action_command_len);
250
253
  int native_sdk_gtk_add_recent_document(native_sdk_gtk_host_t *host, const char *path, size_t path_len);
251
254
  int native_sdk_gtk_clear_recent_documents(native_sdk_gtk_host_t *host);
252
255
  int native_sdk_gtk_credentials_available(native_sdk_gtk_host_t *host);
253
256
  int native_sdk_gtk_set_credential(native_sdk_gtk_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len, const char *secret, size_t secret_len);
254
257
  size_t native_sdk_gtk_get_credential(native_sdk_gtk_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len, char *buffer, size_t buffer_len);
255
258
  int native_sdk_gtk_delete_credential(native_sdk_gtk_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len);
259
+ size_t native_sdk_gtk_format_local_time(native_sdk_gtk_host_t *host, int64_t timestamp_ms, int style, char *buffer, size_t buffer_len);
256
260
  /* Audio playback (GStreamer playbin, runtime-loaded like libsecret).
257
261
  * native_sdk_gtk_audio_available answers whether the library resolved
258
262
  * at runtime; when it did not, every load below answers 3 (no backend)