gloomberb 0.19.6 → 0.20.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 (209) hide show
  1. package/LICENSE +0 -4
  2. package/package.json +6 -5
  3. package/patches/electrobun-devkit@2.0.5.patch +408 -0
  4. package/src/api-client/bond-trades.ts +59 -0
  5. package/src/api-client/crypto-markets.ts +20 -2
  6. package/src/api-client/data.ts +21 -0
  7. package/src/api-client/fund-facts.ts +23 -1
  8. package/src/api-client/index.ts +2 -0
  9. package/src/api-client/types/account.ts +86 -0
  10. package/src/api-client/types/assist.ts +32 -0
  11. package/src/api-client/types/auth.ts +63 -0
  12. package/src/api-client/types/billing.ts +54 -0
  13. package/src/api-client/types/chat.ts +180 -0
  14. package/src/api-client/types/congress.ts +107 -0
  15. package/src/api-client/types/credit.ts +110 -0
  16. package/src/api-client/types/econ.ts +158 -0
  17. package/src/api-client/types/equity-diagnostic.ts +73 -0
  18. package/src/api-client/types/filings-and-calls.ts +356 -0
  19. package/src/api-client/types/index.ts +25 -0
  20. package/src/api-client/types/jobs.ts +147 -0
  21. package/src/api-client/types/market.ts +122 -0
  22. package/src/api-client/types/news.ts +91 -0
  23. package/src/api-client/types/notes-and-theses.ts +172 -0
  24. package/src/api-client/types/quotes.ts +28 -0
  25. package/src/api-client/types/scanners.ts +125 -0
  26. package/src/api-client/types/screener.ts +65 -0
  27. package/src/api-client/types/search.ts +107 -0
  28. package/src/api-client/types/sync.ts +14 -0
  29. package/src/api-client/types/team-collections.ts +43 -0
  30. package/src/api-client/types/team-views.ts +32 -0
  31. package/src/api-client/types/teams.ts +135 -0
  32. package/src/api-client/types/ticker-data.ts +57 -0
  33. package/src/api-client/types/tweets.ts +59 -0
  34. package/src/api-client/types/world-venues.ts +23 -0
  35. package/src/app/runtime/desktop-deeplink.ts +41 -6
  36. package/src/app/runtime/plugin-seed.ts +119 -0
  37. package/src/app/runtime/startup.ts +45 -9
  38. package/src/app/star-prompt/status-line.tsx +1 -1
  39. package/src/app.tsx +2 -7
  40. package/src/architecture/ui-tokens-allowlist.json +0 -8
  41. package/src/cli/board-coins.ts +38 -5
  42. package/src/cli/commands/crypto-hints.ts +8 -1
  43. package/src/cli/commands/market.ts +29 -26
  44. package/src/cli/commands/overview.ts +13 -6
  45. package/src/cli/commands/ticker.ts +78 -18
  46. package/src/cli/company-name-hint.ts +19 -0
  47. package/src/cli/desktop-pane-shot-browser.ts +69 -3
  48. package/src/cli/entry.ts +10 -4
  49. package/src/cli/glossary.ts +25 -0
  50. package/src/cli/helpers.ts +4 -2
  51. package/src/cli/not-a-ticker.ts +26 -1
  52. package/src/cli/pane-functions/catalog.ts +3 -1
  53. package/src/cli/pane-functions/freshness.ts +45 -29
  54. package/src/cli/pane-functions/market-session.ts +14 -5
  55. package/src/cli/restore-plugins.ts +164 -51
  56. package/src/components/chart/composite/use-composite-chart-keyboard.ts +11 -2
  57. package/src/components/chart-table/header.tsx +10 -1
  58. package/src/components/chart-table/layout.ts +14 -5
  59. package/src/components/command-bar/panel/index.tsx +13 -0
  60. package/src/components/command-bar/panel/layout.ts +7 -0
  61. package/src/components/command-bar/panel/runtime.ts +1 -0
  62. package/src/components/command-bar/panel/types.ts +2 -0
  63. package/src/components/data-table/export.ts +4 -1
  64. package/src/components/layout/detached-pane-shell.tsx +10 -37
  65. package/src/components/layout/double-escape-close.ts +90 -0
  66. package/src/components/layout/header.tsx +3 -2
  67. package/src/components/layout/market-summary.ts +6 -3
  68. package/src/components/layout/pane/footer/index.tsx +2 -6
  69. package/src/components/layout/pane/footer/model.ts +6 -12
  70. package/src/components/layout/shell/active-drag.ts +53 -11
  71. package/src/components/layout/shell/drag/magnets.ts +128 -19
  72. package/src/components/layout/shell/pane/actions.ts +6 -2
  73. package/src/components/layout/shell/pane/management-shortcuts.ts +13 -51
  74. package/src/components/layout/status-bar.tsx +4 -78
  75. package/src/components/layout/status-widget-space.ts +5 -5
  76. package/src/components/onboarding/portfolio-step/positions-panel.tsx +1 -1
  77. package/src/components/ui/data-table/index.tsx +5 -1
  78. package/src/components/ui/data-table/render-cell.ts +19 -0
  79. package/src/components/ui/delayed-mark.tsx +59 -7
  80. package/src/components/ui/icon.tsx +1 -8
  81. package/src/components/ui/menu.tsx +6 -1
  82. package/src/i18n/es.ts +6 -3
  83. package/src/i18n/ja.ts +6 -3
  84. package/src/i18n/ko.ts +6 -3
  85. package/src/i18n/zh-cn.ts +6 -3
  86. package/src/i18n/zh-tw.ts +6 -3
  87. package/src/market-data/fund-facts.ts +7 -1
  88. package/src/market-data/market/change-basis.ts +20 -0
  89. package/src/market-data/market/format.ts +62 -2
  90. package/src/market-data/market/futures-week.ts +3 -1
  91. package/src/plugins/builtin/chart-composer/headless.ts +57 -17
  92. package/src/plugins/builtin/chart-composer/session-range.ts +82 -3
  93. package/src/plugins/builtin/chat/content/composer.tsx +21 -27
  94. package/src/plugins/builtin/chat/content/index.tsx +31 -3
  95. package/src/plugins/builtin/chat/content/shortcuts.ts +4 -2
  96. package/src/plugins/builtin/chat/content/snapshot.ts +0 -3
  97. package/src/plugins/builtin/cloud/auth-actions.tsx +51 -23
  98. package/src/plugins/builtin/cloud/trial-offer-status-widget.tsx +3 -3
  99. package/src/plugins/builtin/cloud/upgrade-status-widget.tsx +32 -25
  100. package/src/plugins/builtin/congress-trades/model.ts +2 -1
  101. package/src/plugins/builtin/congress-trades/pane.tsx +3 -0
  102. package/src/plugins/builtin/correlation/compute.ts +8 -2
  103. package/src/plugins/builtin/correlation/headless.ts +10 -9
  104. package/src/plugins/builtin/correlation/history.ts +30 -0
  105. package/src/plugins/builtin/correlation/matrix/model.ts +10 -10
  106. package/src/plugins/builtin/correlation/relationship/model.ts +3 -6
  107. package/src/plugins/builtin/correlation/relationship/pane.tsx +1 -2
  108. package/src/plugins/builtin/crypto-board/client.ts +6 -1
  109. package/src/plugins/builtin/crypto-board/defi-view.tsx +12 -1
  110. package/src/plugins/builtin/crypto-board/defi.ts +44 -7
  111. package/src/plugins/builtin/crypto-board/headless.ts +83 -12
  112. package/src/plugins/builtin/crypto-board/model.ts +30 -6
  113. package/src/plugins/builtin/crypto-board/pane.tsx +13 -2
  114. package/src/plugins/builtin/custom-view/index.tsx +1 -1
  115. package/src/plugins/builtin/custom-view/loader.ts +85 -8
  116. package/src/plugins/builtin/custom-view/view-spec.ts +28 -0
  117. package/src/plugins/builtin/equity-screener/criterion-editor.tsx +13 -4
  118. package/src/plugins/builtin/equity-screener/headless.ts +33 -1
  119. package/src/plugins/builtin/equity-screener/model.ts +58 -15
  120. package/src/plugins/builtin/equity-screener/pane.tsx +36 -6
  121. package/src/plugins/builtin/executives/format.ts +5 -4
  122. package/src/plugins/builtin/executives/pane.tsx +1 -1
  123. package/src/plugins/builtin/help/index.tsx +55 -1
  124. package/src/plugins/builtin/insider/model.ts +30 -2
  125. package/src/plugins/builtin/jobs/model.ts +2 -2
  126. package/src/plugins/builtin/mna/model.ts +2 -2
  127. package/src/plugins/builtin/notes/files.ts +2 -0
  128. package/src/plugins/builtin/notes/owner.tsx +20 -1
  129. package/src/plugins/builtin/notes/quick-notes-pane.tsx +22 -4
  130. package/src/plugins/builtin/notes/store.ts +6 -1
  131. package/src/plugins/builtin/perps/headless.ts +8 -2
  132. package/src/plugins/builtin/portfolio-list/header.tsx +1 -1
  133. package/src/plugins/builtin/research/corporate-actions-pane.tsx +29 -25
  134. package/src/plugins/builtin/risk-factors/index.tsx +2 -28
  135. package/src/plugins/builtin/risk-factors/lock-note.ts +58 -0
  136. package/src/plugins/builtin/risk-factors/pane.tsx +10 -3
  137. package/src/plugins/builtin/scanner/hilo-model.ts +4 -3
  138. package/src/plugins/builtin/sec/fund-facts.ts +45 -11
  139. package/src/plugins/builtin/sec/headless.ts +30 -1
  140. package/src/plugins/builtin/sec/index.tsx +26 -5
  141. package/src/plugins/builtin/series-inventory.ts +1 -0
  142. package/src/plugins/builtin/shared/earnings-report.ts +7 -2
  143. package/src/plugins/builtin/shared/pro-lock-note.ts +5 -1
  144. package/src/plugins/builtin/shared/use-quote-board.ts +85 -46
  145. package/src/plugins/builtin/short-interest/headless.ts +1 -1
  146. package/src/plugins/builtin/short-interest/model.ts +15 -6
  147. package/src/plugins/builtin/short-interest/types.ts +2 -0
  148. package/src/plugins/builtin/thirteenf/overlap-pane.tsx +3 -0
  149. package/src/plugins/builtin/thirteenf/pane.tsx +3 -0
  150. package/src/plugins/builtin/ticker-detail/overview/model.ts +19 -9
  151. package/src/plugins/builtin/ticker-detail/overview-tab.tsx +4 -4
  152. package/src/plugins/builtin/time-sales/headless.ts +23 -7
  153. package/src/plugins/builtin/time-sales/index.tsx +2 -2
  154. package/src/plugins/builtin/trace-bonds/detail.tsx +198 -0
  155. package/src/plugins/builtin/trace-bonds/headless.ts +62 -4
  156. package/src/plugins/builtin/trace-bonds/history.ts +155 -0
  157. package/src/plugins/builtin/trace-bonds/pane.tsx +64 -5
  158. package/src/plugins/extracted-plugins.ts +49 -24
  159. package/src/plugins/installer.ts +234 -32
  160. package/src/plugins/plugin-names.ts +11 -2
  161. package/src/plugins/running-installs.ts +34 -0
  162. package/src/plugins/seed-progress.ts +72 -0
  163. package/src/remote/controller.ts +9 -2
  164. package/src/remote/schema.ts +2 -1
  165. package/src/renderers/cloudflare/http-proxy.ts +67 -8
  166. package/src/renderers/cloudflare/proxy-session.ts +1 -1
  167. package/src/renderers/cloudflare/worker.ts +29 -2
  168. package/src/renderers/dom/desktop/icons.tsx +0 -2
  169. package/src/renderers/dom/styles.css +0 -3
  170. package/src/renderers/electrobun/bun/desktop/backend.ts +14 -0
  171. package/src/renderers/electrobun/bun/desktop/host-requests.ts +14 -7
  172. package/src/renderers/electrobun/bun/desktop/initialization.ts +24 -14
  173. package/src/renderers/electrobun/bun/desktop/install-records.ts +59 -0
  174. package/src/renderers/electrobun/bun/desktop/installer-folder.ts +208 -592
  175. package/src/renderers/electrobun/bun/desktop/macos-objc.ts +14 -0
  176. package/src/renderers/electrobun/bun/desktop/update.ts +1 -1
  177. package/src/renderers/electrobun/bun/index.ts +24 -13
  178. package/src/renderers/electrobun/bun/window/context-menu-thread.ts +84 -0
  179. package/src/renderers/electrobun/bun/window/create.ts +23 -3
  180. package/src/renderers/electrobun/bun/window/electrobun-window-host.ts +4 -4
  181. package/src/renderers/electrobun/bun/window/events.ts +4 -4
  182. package/src/renderers/electrobun/bun/window/frame.ts +2 -1
  183. package/src/renderers/electrobun/bun/window/native-call-thread.ts +224 -0
  184. package/src/renderers/electrobun/bun/window/native-min-size.ts +322 -0
  185. package/src/renderers/electrobun/bun/window/native-timing.ts +10 -0
  186. package/src/renderers/electrobun/bun/window/script-for-webview.ts +15 -0
  187. package/src/renderers/electrobun/bun/window/webview-script-thread.ts +82 -0
  188. package/src/renderers/electrobun/shared/protocol.ts +13 -2
  189. package/src/renderers/electrobun/view/backend-rpc.ts +8 -1
  190. package/src/renderers/electrobun/view/cli-pane-shot-frame.tsx +1 -1
  191. package/src/renderers/electrobun/view/main.tsx +4 -3
  192. package/src/renderers/opentui/start.tsx +3 -0
  193. package/src/sources/gloomberb-cloud/index.ts +5 -7
  194. package/src/sources/provider-router/batches.ts +18 -3
  195. package/src/sources/provider-router/financials.ts +18 -4
  196. package/src/telemetry/stall-reports.ts +21 -4
  197. package/src/types/data-provider.ts +14 -0
  198. package/src/types/financials.ts +15 -3
  199. package/src/types/headless.ts +2 -0
  200. package/src/utils/double-escape-close.ts +38 -16
  201. package/src/utils/format.ts +23 -3
  202. package/src/utils/main-thread-stall.ts +21 -5
  203. package/src/utils/perf-marks.ts +88 -9
  204. package/src/utils/price-earnings.ts +18 -0
  205. package/src/utils/reported-money.ts +9 -2
  206. package/src/utils/sec.ts +34 -3
  207. package/patches/electrobun@1.18.1.patch +0 -633
  208. package/src/api-client/types.ts +0 -2441
  209. package/src/plugins/seed-notice.ts +0 -20
@@ -24,6 +24,8 @@ function loadObjc() {
24
24
  const runtime = dlopen(LIBOBJC, {
25
25
  objc_getClass: { args: [FFIType.ptr], returns: ID },
26
26
  sel_registerName: { args: [FFIType.ptr], returns: FFIType.ptr },
27
+ objc_autoreleasePoolPush: { args: [], returns: FFIType.ptr },
28
+ objc_autoreleasePoolPop: { args: [FFIType.ptr], returns: FFIType.void },
27
29
  }).symbols;
28
30
  const msgSend = <const Args extends FFIType[], Returns extends FFIType>(args: Args, returns: Returns) =>
29
31
  dlopen(LIBOBJC, { objc_msgSend: { args: [ID, FFIType.ptr, ...args], returns } }).symbols.objc_msgSend;
@@ -31,11 +33,23 @@ function loadObjc() {
31
33
  return {
32
34
  getClass: (name: string) => runtime.objc_getClass(cString(name)),
33
35
  selector: (name: string) => runtime.sel_registerName(cString(name)),
36
+ /** Runs `run` in an autorelease pool of its own, for the objects class methods return: Bun's thread may have none. */
37
+ withAutoreleasePool: <T>(run: () => T): T => {
38
+ const pool = runtime.objc_autoreleasePoolPush();
39
+ try {
40
+ return run();
41
+ } finally {
42
+ runtime.objc_autoreleasePoolPop(pool);
43
+ }
44
+ },
34
45
  send: msgSend([], ID),
35
46
  sendVoid: msgSend([], FFIType.void),
36
47
  sendObject: msgSend([ID], ID),
37
48
  sendCString: msgSend([FFIType.ptr], ID),
38
49
  sendBytes: msgSend([FFIType.ptr, FFIType.u64], ID),
50
+ /** A message taking an NSSize or NSPoint: two doubles, passed as two on arm64 and x86-64 alike. */
51
+ sendDoubles: msgSend([FFIType.f64, FFIType.f64], ID),
52
+ sendObjects: msgSend([ID, ID], ID),
39
53
  sendOnMainThread: msgSend([FFIType.ptr, ID, FFIType.bool], FFIType.void),
40
54
  };
41
55
  }
@@ -40,7 +40,7 @@ type ElectrobunUpdater = typeof Electrobun.Updater;
40
40
  /**
41
41
  * The part of Electrobun's updater this flow uses. Tests pass a fake, because
42
42
  * the real module cannot be loaded or exercised outside the packaged app.
43
- * `setUpdateSource` is this app's patch (patches/electrobun@*.patch).
43
+ * `setUpdateSource` is this app's patch (patches/electrobun-devkit@*.patch).
44
44
  */
45
45
  export type ElectrobunUpdaterApi = Pick<
46
46
  ElectrobunUpdater,
@@ -1,4 +1,4 @@
1
- import Electrobun, { ApplicationMenu, Utils } from "electrobun/bun";
1
+ import Electrobun, { ApplicationMenu, Updater, Utils } from "electrobun/bun";
2
2
  import { homedir } from "os";
3
3
  import { debugLog } from "../../../utils/debug-log";
4
4
  import { setConfigStoreHost } from "../../../data/config/store";
@@ -13,6 +13,8 @@ import { installProcessCrashListeners } from "../../../telemetry/crash-reports-n
13
13
  import { renameLegacyMacApp } from "./desktop/app-name";
14
14
  import { scheduleDesktopRelaunch } from "./desktop/relaunch";
15
15
  import { settleInstallerFolder } from "./desktop/installer-folder";
16
+ import { ensureInstallRecords } from "./desktop/install-records";
17
+ import { runWebviewScriptsOffThread } from "./window/webview-script-thread";
16
18
 
17
19
  console.log = (...args) => console.error(...args);
18
20
  console.info = (...args) => console.error(...args);
@@ -30,24 +32,21 @@ setConfigStoreHost(nodeConfigStoreHost);
30
32
  // before the config, and its off switch, has been read.
31
33
  installProcessCrashListeners();
32
34
 
33
- // An install the Windows installer made lives in the installer's folder,
34
- // where its shortcuts, the gloom command and its uninstall entry lead.
35
- // Electrobun's updater installs an update into a folder of its own and opens
36
- // it from there: this build moves into the installer's folder and opens again
37
- // from it before any window shows.
38
- const installerFolderMove = await settleInstallerFolder({
35
+ // Before any window opens: on Windows each message to a window is a script
36
+ // whose native call waits on the thread that runs the windows.
37
+ runWebviewScriptsOffThread();
38
+
39
+ // A Windows install from before Electrobun 2 lives in the installer's folder.
40
+ // Its last update put this build in Electrobun's folder, the only one
41
+ // Electrobun 2 updates: the installer's folder is retired in the background.
42
+ void settleInstallerFolder({
39
43
  platform: process.platform,
40
44
  execPath: process.execPath,
41
45
  homeDir: homedir(),
42
46
  localAppData: process.env.LOCALAPPDATA,
43
47
  }).catch((error: unknown) => {
44
- console.error("[desktop] could not move the app into the installer's folder", error instanceof Error ? error.message : String(error));
45
- return null;
48
+ console.error("[desktop] could not retire the installer's folder", error instanceof Error ? error.message : String(error));
46
49
  });
47
- if (installerFolderMove?.kind === "relaunching") {
48
- Utils.quit();
49
- await new Promise<never>(() => {});
50
- }
51
50
 
52
51
  // An install from before the rename runs from Gloomberb.app, where its updater
53
52
  // put this build. It takes the new name and opens again from there before any
@@ -70,6 +69,18 @@ if (appRename?.kind === "renamed") {
70
69
  console.error(`[desktop] kept the app's old name: ${appRename.reason}`);
71
70
  }
72
71
 
72
+ // A Mac app that has not updated yet has no folder for Electrobun's install
73
+ // records, which its launcher otherwise fails to write at every start. The
74
+ // records name the bundle the app runs from, so they are written once it has
75
+ // its new name: a Gloomberb.app being renamed above opens again as Gloom.app
76
+ // and writes them from there.
77
+ void ensureInstallRecords({
78
+ platform: process.platform,
79
+ execPath: process.execPath,
80
+ channel: () => Updater.localInfo.channel(),
81
+ channelRoot: () => Updater.appDataFolder(),
82
+ });
83
+
73
84
  function readOpenUrlEvent(event: unknown): string | null {
74
85
  const data = event && typeof event === "object" ? (event as { data?: unknown }).data : null;
75
86
  if (!data || typeof data !== "object") return null;
@@ -0,0 +1,84 @@
1
+ import { ContextMenu } from "electrobun/bun";
2
+ import { reportCrash } from "../../../../telemetry/crash-reports";
3
+ import { debugLog } from "../../../../utils/debug-log";
4
+ import { nativeWrapperLibraryPath, startNativeCallThread, type NativeCallThread, type NativeCallThreadFailure } from "./native-call-thread";
5
+ import { measureNativeCall } from "./native-timing";
6
+
7
+ const menuLog = debugLog.createLogger("context-menu-thread");
8
+
9
+ /**
10
+ * Past this a menu's call counts as stuck: a person picks or dismisses a
11
+ * menu well within it, and Windows closes one when the app loses focus.
12
+ */
13
+ const MENU_CALL_LIMIT_MS = 60_000;
14
+ /** A menu asked for longer ago than this is not shown late. */
15
+ const REPLAY_WITHIN_MS = 5_000;
16
+ /**
17
+ * After this many stuck threads in a row, menus open on the app's thread. A
18
+ * menu that closes starts the count again: one left open past the limit,
19
+ * or open while the computer slept, is not a thread that cannot show menus.
20
+ */
21
+ const MAX_STUCK_THREADS = 3;
22
+
23
+ let installed = false;
24
+ let reported = false;
25
+
26
+ /**
27
+ * Once per process, and only once it happens: menus waiting on the app's
28
+ * thread again is the freeze this thread removes, so error tracking hears
29
+ * of it, and the menus themselves count as stalls (host-requests.ts).
30
+ */
31
+ function reportMenuThreadFailure(failure: NativeCallThreadFailure | "unavailable"): void {
32
+ if (reported) return;
33
+ reported = true;
34
+ reportCrash(
35
+ { name: "ContextMenuThread", message: `Context menus could not open off the app's thread: ${failure}` },
36
+ { kind: "plugin", plugin: "stall-monitor" },
37
+ );
38
+ }
39
+
40
+ /**
41
+ * On Windows a context menu's native call waits on the thread that runs the
42
+ * windows until the menu closes, which is as long as the person takes to
43
+ * pick from it. It goes to a thread of its own, started with the first menu.
44
+ * Where that thread cannot run, menus open on this thread as before, those
45
+ * it took and never began included; a stuck one is replaced by a new thread.
46
+ */
47
+ export function runContextMenusOffThread(platform: string = process.platform): void {
48
+ if (platform !== "win32" || installed) return;
49
+ installed = true;
50
+ /** Undefined: started with the next menu. Null: menus open on this thread. */
51
+ let thread: NativeCallThread | null | undefined;
52
+ let stuckThreads = 0;
53
+ const encoder = new TextEncoder();
54
+
55
+ const start = (): NativeCallThread | null => {
56
+ const started = startNativeCallThread(nativeWrapperLibraryPath(), "showContextMenu", {
57
+ callLimitMs: MENU_CALL_LIMIT_MS,
58
+ onCallDone: () => {
59
+ stuckThreads = 0;
60
+ },
61
+ onFailure: (failure, unstarted) => {
62
+ reportMenuThreadFailure(failure);
63
+ stuckThreads += failure === "stuck" ? 1 : 0;
64
+ thread = failure === "stuck" && stuckThreads < MAX_STUCK_THREADS ? undefined : null;
65
+ const now = performance.now();
66
+ for (const call of unstarted) {
67
+ if (now - call.takenAt <= REPLAY_WITHIN_MS) call.replay();
68
+ else menuLog.warn("A context menu asked for too long ago was not shown", { ageMs: Math.round(now - call.takenAt) });
69
+ }
70
+ },
71
+ });
72
+ if (!started) reportMenuThreadFailure("unavailable");
73
+ return started;
74
+ };
75
+
76
+ const show = (menuConfig: string, handler: number, showHere: () => void): boolean => {
77
+ if (thread === undefined) thread = start();
78
+ if (!thread) return false;
79
+ return thread.call(encoder.encode(`${menuConfig}\0`), handler, () => {
80
+ if (!show(menuConfig, handler, showHere)) measureNativeCall("context-menu.show", showHere);
81
+ });
82
+ };
83
+ ContextMenu.setContextMenuRunner(show);
84
+ }
@@ -1,4 +1,6 @@
1
1
  import { BrowserWindow } from "electrobun/bun";
2
+ import { readFileSync } from "fs";
3
+ import { resolve } from "path";
2
4
  import { measurePerf } from "../../../../utils/perf-marks";
3
5
  import { trackWindowAttention } from "./attention";
4
6
  import {
@@ -10,6 +12,7 @@ import {
10
12
  type WindowResizeEvent,
11
13
  } from "./events";
12
14
  import type { WindowFrame, WindowMinimumSize } from "./frame";
15
+ import { applyNativeMinimumSize } from "./native-min-size";
13
16
  import { applyWindowsCustomChrome } from "./windows-custom-chrome";
14
17
  import { applyWindowsWindowIcon } from "./windows-icons";
15
18
  import { fitPageToClientArea } from "./windows-native";
@@ -33,10 +36,24 @@ function desktopTitleBarStyle(): DesktopTitleBarStyle {
33
36
  return process.platform === "linux" ? "hidden" : "hiddenInset";
34
37
  }
35
38
 
39
+ let chromiumShipped: boolean | undefined;
40
+
41
+ /** Whether this build ships Chromium (Resources/build.json); a build that does not say is taken to. */
42
+ function shipsChromium(): boolean {
43
+ if (chromiumShipped !== undefined) return chromiumShipped;
44
+ try {
45
+ const build = JSON.parse(readFileSync(resolve("../Resources/build.json"), "utf8")) as { availableRenderers?: unknown };
46
+ chromiumShipped = !Array.isArray(build.availableRenderers) || build.availableRenderers.includes("cef");
47
+ } catch {
48
+ chromiumShipped = true;
49
+ }
50
+ return chromiumShipped;
51
+ }
52
+
36
53
  function desktopWindowRenderer(): DesktopWindowRenderer {
37
54
  if (process.platform === "win32") {
38
55
  const hostArchitecture = process.env.PROCESSOR_ARCHITEW6432 ?? process.env.PROCESSOR_ARCHITECTURE;
39
- return hostArchitecture?.toLowerCase() === "arm64" ? "native" : "cef";
56
+ return hostArchitecture?.toLowerCase() === "arm64" || !shipsChromium() ? "native" : "cef";
40
57
  }
41
58
  return "native";
42
59
  }
@@ -82,7 +99,8 @@ interface AppWindowOptions {
82
99
 
83
100
  /**
84
101
  * Opens a window on the app view with this platform's chrome and icon, keeps
85
- * its cached frame current as it moves, and holds it to `minSize` on resize.
102
+ * its cached frame current as it moves, and holds it to `minSize`: the system
103
+ * does while it is resized, and a resize that still ends under it is undone.
86
104
  */
87
105
  export function createAppWindow({ title, frame, rpc, minSize, onFrameChange, preload }: AppWindowOptions): BrowserWindow {
88
106
  // Opening a window waits on the native layer, the webview's creation included.
@@ -100,11 +118,13 @@ export function createAppWindow({ title, frame, rpc, minSize, onFrameChange, pre
100
118
  }));
101
119
  applyWindowsWindowIcon(title);
102
120
  applyWindowsCustomChrome(title);
121
+ applyNativeMinimumSize(window, minSize);
103
122
  if (process.platform === "win32") {
104
123
  loadAgainWhenTakenForCtrlClick(window);
105
124
  // The page exists once it has loaded; until then there is nothing to fit.
106
125
  window.webview.on("dom-ready", () => {
107
- fitPageToClientArea(window.ptr);
126
+ const handle = window.ptr;
127
+ if (handle) fitPageToClientArea(handle);
108
128
  });
109
129
  }
110
130
  updateWindowFrameCache(window, frame, minSize);
@@ -10,9 +10,9 @@ import { getWindowFrame, updateWindowFrameCache } from "./events";
10
10
  import type { FocusableElectrobunWindow } from "./focus";
11
11
 
12
12
  /**
13
- * Electrobun 1.18 has no event for displays coming and going, nor for a window
14
- * being minimized: both are read on a timer, this often while there is more
15
- * than one window...
13
+ * Electrobun (2.0.5, as 1.18) has no event for displays coming and going, nor
14
+ * for a window being minimized: both are read on a timer, this often while
15
+ * there is more than one window...
16
16
  */
17
17
  const POLL_MS = 3_000;
18
18
  /**
@@ -90,7 +90,7 @@ function read(call: () => boolean): boolean {
90
90
  }
91
91
  }
92
92
 
93
- /** The desktop window host on Electrobun 1.18: windows, their frames and events, and the displays. */
93
+ /** The desktop window host on Electrobun: windows, their frames and events, and the displays. */
94
94
  export class ElectrobunWindowHost implements DesktopWindowHost {
95
95
  readonly canPlaceWindows: boolean;
96
96
  /** Windows has no way into full screen with Electrobun's frameless windows. */
@@ -52,10 +52,10 @@ type FramedWindow = Pick<BrowserWindow, "frame" | "getFrame" | "isMinimized" | "
52
52
 
53
53
  /**
54
54
  * Electrobun's Windows events do not carry the window's frame: a resize
55
- * reports the client area's size at 0,0, a move the client area's corner as
56
- * unsigned 16-bit values, and both come while the window is minimized. There
57
- * the frame is read from the window itself, and a minimized window keeps the
58
- * frame it had.
55
+ * reports the client area's size rather than the window's (2.0.5; 1.18 put it
56
+ * at 0,0 too, and a move gave the client area's corner as unsigned 16-bit
57
+ * values), and both come while the window is minimized. There the frame is
58
+ * read from the window itself, and a minimized window keeps the frame it had.
59
59
  */
60
60
  export function applyNativeWindowFrame(window: FramedWindow, minimumSize?: WindowMinimumSize): WindowFrame | null {
61
61
  if (window.isMinimized()) return null;
@@ -12,7 +12,8 @@ export interface WindowMinimumSize {
12
12
 
13
13
  export const DEFAULT_WINDOW_FRAME: WindowFrame = { x: 64, y: 48, width: 1440, height: 920 };
14
14
  const DEFAULT_WINDOWS_WINDOW_FRAME: WindowFrame = { x: 32, y: 32, width: 960, height: 680 };
15
- // Emergency fallback until Electrobun exposes stable native minimum-size handling:
15
+ // Held by the system while a window is resized (native-min-size.ts), and by
16
+ // the resize handlers where that does not apply: Electrobun has no minimum size,
16
17
  // https://github.com/blackboardsh/electrobun/issues/188
17
18
  export const MAIN_WINDOW_MIN_SIZE: WindowMinimumSize = { width: 640, height: 400 };
18
19
  export const DETACHED_WINDOW_MIN_SIZE: WindowMinimumSize = { width: 360, height: 240 };
@@ -0,0 +1,224 @@
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { debugLog } from "../../../../utils/debug-log";
4
+
5
+ const threadLog = debugLog.createLogger("native-call-thread");
6
+
7
+ /**
8
+ * Electrobun's native library, where its SDK opens it for this process: the
9
+ * working folder, else beside the running Bun. Another thread opens the same
10
+ * one, already loaded.
11
+ */
12
+ export function nativeWrapperLibraryPath(): string {
13
+ const name = `libNativeWrapper.${process.platform === "win32" ? "dll" : process.platform === "darwin" ? "dylib" : "so"}`;
14
+ const candidates = [join(process.cwd(), name), join(dirname(process.argv0 || process.execPath), name), join(dirname(process.execPath), name)];
15
+ return candidates.find((path) => existsSync(path)) ?? candidates[0]!;
16
+ }
17
+
18
+ /**
19
+ * Runs on a thread of its own: opens the library once, then makes each call
20
+ * it is sent, one at a time and in order, saying when each starts and ends.
21
+ * The bytes stay alive in the message until the call returns.
22
+ */
23
+ const WORKER_SOURCE = `
24
+ import { dlopen, ptr } from "bun:ffi";
25
+ let call = null;
26
+ let pointerFirst = false;
27
+ self.onmessage = (event) => {
28
+ const message = event.data;
29
+ if (message.type === "open") {
30
+ try {
31
+ pointerFirst = message.pointerFirst === true;
32
+ call = dlopen(message.library, { [message.symbol]: { args: ["ptr", "ptr"], returns: "void" } }).symbols[message.symbol];
33
+ postMessage({ type: "ready" });
34
+ } catch (error) {
35
+ postMessage({ type: "failed", message: String(error && error.message || error) });
36
+ }
37
+ return;
38
+ }
39
+ if (message.type === "call") {
40
+ if (!call) return;
41
+ postMessage({ type: "started", id: message.id });
42
+ const startedAt = performance.now();
43
+ try {
44
+ if (pointerFirst) call(message.pointer || null, ptr(message.bytes));
45
+ else call(ptr(message.bytes), message.pointer || null);
46
+ postMessage({ type: "done", id: message.id, ms: performance.now() - startedAt });
47
+ } catch (error) {
48
+ postMessage({ type: "failed", id: message.id, message: String(error && error.message || error) });
49
+ }
50
+ }
51
+ };
52
+ `;
53
+
54
+ /**
55
+ * Why the thread stopped taking calls: the library would not open on it, the
56
+ * thread ended, or a call ran past `callLimitMs` and everything behind it
57
+ * would have waited for good.
58
+ */
59
+ export type NativeCallThreadFailure = "open-failed" | "ended" | "stuck";
60
+
61
+ /** A call the thread took and never began. */
62
+ export interface UnstartedNativeCall {
63
+ /** When it was taken (performance.now()). */
64
+ takenAt: number;
65
+ /** What the caller gave `call` to make it another way. */
66
+ replay(): void;
67
+ }
68
+
69
+ export interface NativeCallThreadOptions {
70
+ /** Each call that returned, and how long it took. */
71
+ onCallDone?(ms: number): void;
72
+ /**
73
+ * The thread takes no more calls. `unstarted`: those it took and never
74
+ * began, oldest first, for the caller to make another way. A call that
75
+ * began and had not returned is not among them: it may be showing.
76
+ */
77
+ onFailure?(failure: NativeCallThreadFailure, unstarted: UnstartedNativeCall[]): void;
78
+ /** A call running this long counts the thread as stuck. */
79
+ callLimitMs?: number;
80
+ /** `symbol(pointer, bytes)` rather than `symbol(bytes, pointer)`. */
81
+ pointerFirst?: boolean;
82
+ }
83
+
84
+ export interface NativeCallThread {
85
+ /**
86
+ * Queues `symbol(bytes, pointer)` (or `symbol(pointer, bytes)`) and returns at once. False when the
87
+ * thread cannot take it, and the caller makes the call itself; `replay`
88
+ * makes it another way if the thread stops before beginning it.
89
+ */
90
+ call(bytes: Uint8Array, pointer: number | null, replay: () => void): boolean;
91
+ close(): void;
92
+ }
93
+
94
+ interface WorkerMessage {
95
+ type: "ready" | "failed" | "started" | "done";
96
+ id?: number;
97
+ ms?: number;
98
+ message?: string;
99
+ }
100
+
101
+ interface OwedCall extends UnstartedNativeCall {
102
+ started: boolean;
103
+ }
104
+
105
+ /**
106
+ * A native call that waits on another thread, made from a thread of its own
107
+ * so this one keeps running. On Windows, Electrobun's native calls wait for
108
+ * the thread that runs the windows to answer them. That thread answers a
109
+ * context menu's only once the menu closes, and a script for a window (every
110
+ * message the app sends one) whenever it gets to it: made here, the app
111
+ * froze for as long as a menu was open or that thread was busy.
112
+ *
113
+ * `symbol` takes a pointer to the bytes and one pointer, in either order.
114
+ * Null where there is no Worker to run it on.
115
+ */
116
+ export function startNativeCallThread(
117
+ library: string,
118
+ symbol: string,
119
+ options: NativeCallThreadOptions = {},
120
+ ): NativeCallThread | null {
121
+ if (typeof Worker === "undefined" || typeof Blob === "undefined" || typeof URL.createObjectURL !== "function") return null;
122
+ const source = URL.createObjectURL(new Blob([WORKER_SOURCE], { type: "application/javascript" }));
123
+ let worker: Worker;
124
+ try {
125
+ worker = new Worker(source);
126
+ } catch (error) {
127
+ threadLog.warn("Could not start a thread for native calls", { error: String(error) });
128
+ URL.revokeObjectURL(source);
129
+ return null;
130
+ }
131
+ (worker as { unref?: () => void }).unref?.();
132
+ let stopped = false;
133
+ let nextId = 1;
134
+ const owed = new Map<number, OwedCall>();
135
+ let stuckTimer: ReturnType<typeof setTimeout> | null = null;
136
+
137
+ const clearStuckTimer = () => {
138
+ if (stuckTimer) clearTimeout(stuckTimer);
139
+ stuckTimer = null;
140
+ };
141
+
142
+ const stop = () => {
143
+ stopped = true;
144
+ clearStuckTimer();
145
+ try {
146
+ // A thread inside a native call ends once the call returns.
147
+ worker.terminate();
148
+ } catch {
149
+ // Intentional ignore: a thread that cannot be stopped is already gone.
150
+ }
151
+ URL.revokeObjectURL(source);
152
+ };
153
+
154
+ const fail = (failure: NativeCallThreadFailure, reason: string) => {
155
+ if (stopped) return;
156
+ const unstarted = [...owed.values()].filter((call) => !call.started).map(({ takenAt, replay }) => ({ takenAt, replay }));
157
+ threadLog.warn("The thread for native calls stopped", { failure, reason, unstarted: unstarted.length, running: owed.size - unstarted.length });
158
+ owed.clear();
159
+ stop();
160
+ options.onFailure?.(failure, unstarted);
161
+ };
162
+
163
+ worker.onmessage = (event: MessageEvent<WorkerMessage>) => {
164
+ const message = event.data;
165
+ if (message.type === "failed") {
166
+ if (message.id === undefined) {
167
+ fail("open-failed", message.message ?? "could not open the library");
168
+ return;
169
+ }
170
+ owed.delete(message.id);
171
+ clearStuckTimer();
172
+ threadLog.warn("A native call failed on its thread", { message: message.message });
173
+ return;
174
+ }
175
+ if (message.type === "started" && message.id !== undefined) {
176
+ const call = owed.get(message.id);
177
+ if (call) call.started = true;
178
+ const limitMs = options.callLimitMs;
179
+ if (limitMs !== undefined) {
180
+ clearStuckTimer();
181
+ stuckTimer = setTimeout(() => fail("stuck", `a call ran past ${limitMs} ms`), limitMs);
182
+ (stuckTimer as { unref?: () => void }).unref?.();
183
+ }
184
+ return;
185
+ }
186
+ if (message.type === "done" && message.id !== undefined) {
187
+ owed.delete(message.id);
188
+ clearStuckTimer();
189
+ options.onCallDone?.(message.ms ?? 0);
190
+ }
191
+ };
192
+ worker.onerror = (event) => {
193
+ fail("ended", event instanceof ErrorEvent ? event.message : "the thread failed");
194
+ };
195
+ worker.addEventListener("close", () => fail("ended", "the thread ended"));
196
+
197
+ try {
198
+ worker.postMessage({ type: "open", library, symbol, pointerFirst: options.pointerFirst === true });
199
+ } catch (error) {
200
+ stop();
201
+ threadLog.warn("Could not start a thread for native calls", { error: String(error) });
202
+ return null;
203
+ }
204
+
205
+ return {
206
+ call(bytes, pointer, replay) {
207
+ if (stopped) return false;
208
+ const id = nextId++;
209
+ try {
210
+ worker.postMessage({ type: "call", id, bytes, pointer });
211
+ } catch (error) {
212
+ fail("ended", error instanceof Error ? error.message : String(error));
213
+ return false;
214
+ }
215
+ owed.set(id, { takenAt: performance.now(), replay, started: false });
216
+ return true;
217
+ },
218
+ close() {
219
+ if (stopped) return;
220
+ owed.clear();
221
+ stop();
222
+ },
223
+ };
224
+ }