electrobun 0.0.2 → 0.0.4

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 (76) hide show
  1. package/.colab.json +1 -0
  2. package/README.md +14 -12
  3. package/bun.lockb +0 -0
  4. package/dist/bsdiff +0 -0
  5. package/dist/bspatch +0 -0
  6. package/dist/webview +0 -0
  7. package/docs-old/architecture.md +46 -0
  8. package/documentation/README.md +41 -0
  9. package/documentation/babel.config.js +3 -0
  10. package/documentation/blog/2024-08-20-electrobun.md +22 -0
  11. package/documentation/blog/authors.yml +8 -0
  12. package/documentation/blog/tags.yml +0 -0
  13. package/documentation/docs/apis/Application Icons.md +9 -0
  14. package/documentation/docs/apis/Bundled Assets.md +95 -0
  15. package/documentation/docs/apis/browser/DraggableRegions.md +36 -0
  16. package/documentation/docs/apis/browser/Electrobun Webview Tag.md +200 -0
  17. package/documentation/docs/apis/browser/Electroview Class.md +158 -0
  18. package/documentation/docs/apis/browser/GlobalProperties.md +11 -0
  19. package/documentation/docs/apis/browser/index.md +25 -0
  20. package/documentation/docs/apis/bun/ApplicationMenu.md +141 -0
  21. package/documentation/docs/apis/bun/BrowserView.md +513 -0
  22. package/documentation/docs/apis/bun/BrowserWindow.md +423 -0
  23. package/documentation/docs/apis/bun/ContextMenu.md +50 -0
  24. package/documentation/docs/apis/bun/Events.md +50 -0
  25. package/documentation/docs/apis/bun/PATHS.md +17 -0
  26. package/documentation/docs/apis/bun/Tray.md +115 -0
  27. package/documentation/docs/apis/bun/Updater.md +74 -0
  28. package/documentation/docs/apis/bun/Utils.md +51 -0
  29. package/documentation/docs/apis/bun/index.md +26 -0
  30. package/documentation/docs/apis/cli/Electrobun.config.md +97 -0
  31. package/documentation/docs/apis/cli/cli args.md +76 -0
  32. package/documentation/docs/guides/Architecture/Events.md +19 -0
  33. package/documentation/docs/guides/Architecture/IPC and Isolation.md +20 -0
  34. package/documentation/docs/guides/Architecture/Overview.md +140 -0
  35. package/documentation/docs/guides/Architecture/Updates.md +7 -0
  36. package/documentation/docs/guides/Architecture/Webview Tag.md +5 -0
  37. package/documentation/docs/guides/Compatability.md +8 -0
  38. package/documentation/docs/guides/Getting Started/Creating UI.md +147 -0
  39. package/documentation/docs/guides/Getting Started/Distributing.md +116 -0
  40. package/documentation/docs/guides/Getting Started/Getting Started.md +7 -0
  41. package/documentation/docs/guides/Getting Started/Hello World.md +93 -0
  42. package/documentation/docs/guides/Getting Started/What is Electrobun.md +39 -0
  43. package/documentation/docs/guides/Guides/Build UI with React +0 -0
  44. package/documentation/docs/guides/Guides/Build UI with Solidjs +0 -0
  45. package/documentation/docs/guides/Guides/Build a Web Browser +0 -0
  46. package/documentation/docs/guides/Guides/Bun <-> Browser RPC +0 -0
  47. package/documentation/docs/guides/Guides/Using Tailwind +0 -0
  48. package/documentation/docusaurus.config.ts +153 -0
  49. package/documentation/package-lock.json +14530 -0
  50. package/documentation/package.json +47 -0
  51. package/documentation/sidebars.ts +32 -0
  52. package/documentation/src/components/HomepageFeatures/index.tsx +70 -0
  53. package/documentation/src/components/HomepageFeatures/styles.module.css +11 -0
  54. package/documentation/src/css/custom.css +30 -0
  55. package/documentation/src/pages/index.module.css +23 -0
  56. package/documentation/src/pages/index.tsx +137 -0
  57. package/documentation/static/.nojekyll +0 -0
  58. package/documentation/static/img/electrobun-logo-256.png +0 -0
  59. package/documentation/static/img/electrobun-logo-32.png +0 -0
  60. package/documentation/tsconfig.json +7 -0
  61. package/package.json +11 -6
  62. package/src/browser/index.ts +7 -2
  63. package/src/browser/webviewtag.ts +149 -17
  64. package/src/bun/core/BrowserView.ts +19 -2
  65. package/src/bun/proc/zig.ts +1 -0
  66. package/src/cli/build/electrobun +0 -0
  67. package/src/cli/index.ts +3 -1
  68. package/src/extractor/zig-out/bin/extractor +0 -0
  69. package/src/launcher/zig-out/bin/launcher +0 -0
  70. package/docs/architecture.md +0 -84
  71. /package/{docs → docs-old}/api/bun-api.md +0 -0
  72. /package/{docs → docs-old}/api/view-api.md +0 -0
  73. /package/{docs → docs-old}/electrobun-config.md +0 -0
  74. /package/{docs → docs-old}/getting-started.md +0 -0
  75. /package/{docs → docs-old}/node_modules/.cache/webpack/client-development-en/0.pack +0 -0
  76. /package/{docs → docs-old}/node_modules/.cache/webpack/client-development-en/index.pack +0 -0
@@ -0,0 +1,513 @@
1
+ > Create and control browser views (sometimes referred to as webviews).
2
+
3
+ :::info
4
+ Instead of creating BrowserViews directly from the bun process, you would use the [BrowserWindow](/docs/apis/bun/BrowserWindow) class which automatically creates a default BrowserView that fills the window, and then use [WebviewTags](/docs/apis/browser/Electrobun%20Webview%20Tag) within your html to create nested BrowserViews from the browser context.
5
+ :::
6
+
7
+ ```typescript
8
+ // Most use cases: Access webview created by BrowserWindow or WebviewTag
9
+ import { BrowserView } from "electrobun/bun";
10
+ const webview = BrowserView.getById(id);
11
+
12
+ // or
13
+
14
+ const win = new BrowserWindow(/*....*/);
15
+
16
+ const webview = win.webview;
17
+
18
+ // or
19
+
20
+ // advnaced use cases: Create BrowserView directly
21
+ import { BrowserWindow } from "electrobun/bun";
22
+
23
+ const webview = new BrowserView({
24
+ url: "views://mainview/index.html",
25
+ frame: {
26
+ width: 1800,
27
+ height: 600,
28
+ x: 2000,
29
+ y: 2000,
30
+ },
31
+ });
32
+ ```
33
+
34
+ :::note
35
+ While you can create a BrowserView directly in bun it will only render when you add it to a window.
36
+ :::
37
+
38
+ ## Constructor Options
39
+
40
+ ### frame
41
+
42
+ Set the Webview's dimensions relative to the window. The default webview created via `new BrowserWindow()` will be stretched to cover the window's dimensions automatically.
43
+
44
+ ```
45
+ const webview = new BrowserView({
46
+ frame: {
47
+ width: 1800,
48
+ height: 600,
49
+ x: 2000,
50
+ y: 2000,
51
+ },
52
+ });
53
+ ```
54
+
55
+ ### url
56
+
57
+ Set the initial url for the window's default BrowserView to navigate to when it opens.
58
+
59
+ ```
60
+ // Use any url on the internet
61
+ const webview = new BrowserView({
62
+ url: "https://electrobun.dev",
63
+ });
64
+
65
+ // or use the views:// url scheme to load local
66
+ // content that you've bundled with your app.
67
+
68
+ const webview = new BrowserView({
69
+ url: "views://mainview/index.html",
70
+ });
71
+ ```
72
+
73
+ ### html
74
+
75
+ Set an html string for the window's default BrowserView to load when it opens. Anything that would be valid in an html file including javascript and css can be used.
76
+
77
+ Use this instead of setting the `url` property.
78
+
79
+ ```
80
+ const htmlString = "<html><head></head><body><h1>hello world</h1></body></html>";
81
+
82
+ const webview = new BrowserView({
83
+ html: htmlString,
84
+ });
85
+
86
+ ```
87
+
88
+ ### partition
89
+
90
+ Partitions allow you to separate the browser session. Things like cookies and so on. For example if you have two BrowserViews with the same partition and log into gmail in one, the other will also be logged into gmail. If you use two different partitions then you could log into a different gmail account in each BrowserView.
91
+
92
+ ```
93
+ // ephemeral partition. If you close and reopen your app
94
+ // even if you use the same partition name it will not
95
+ // have persisted.
96
+ const webview = new BrowserView({
97
+ partition: "partition1",
98
+ });
99
+
100
+ // To make partitions persistent just prefix it with `persist:`
101
+ const webview = new BrowserView({
102
+ partition: "persist:partition1",
103
+ });
104
+ ```
105
+
106
+ ### preload
107
+
108
+ Set a preload script for the window's default BrowserView to render after html is parsed but before any other javascript is executed. The preload script will be run after any navigation before the page's scripts are run.
109
+
110
+ You can use either inline javascript or a url.
111
+
112
+ ```
113
+ // Use any url on the internet
114
+ const webview = new BrowserView({
115
+ preload: "https://electrobun.dev/some/remote/file.js",
116
+ });
117
+
118
+ // or use the views:// preload scheme to load local
119
+ // content that you've bundled with your app.
120
+
121
+ const webview = new BrowserView({
122
+ preload: "views://somebundledview/preloadscript.js",
123
+ });
124
+
125
+ // or use inline javascript
126
+
127
+ const webview = new BrowserView({
128
+ preload: "document.body.innerHTML = 'Hello world'; console.log('hello console')",
129
+ });
130
+
131
+ ```
132
+
133
+ ### rpc
134
+
135
+ The RPC property allows you to establish RPC (remote procedure calls) between the bun process and this window's default BrowserView. In other words it lets you define functions that execute in the bun process that are callable and return a value back to the browser process and visa versa.
136
+
137
+ These RPC functions are asynchronous.
138
+
139
+ ```typescript title="src/shared/types.ts"
140
+ export type MyWebviewRPCType = {
141
+ // functions that execute in the main process
142
+ bun: RPCSchema<{
143
+ requests: {
144
+ someBunFunction: {
145
+ params: {
146
+ a: number;
147
+ b: number;
148
+ };
149
+ response: number;
150
+ };
151
+ };
152
+ messages: {
153
+ logToBun: {
154
+ msg: string;
155
+ };
156
+ };
157
+ }>;
158
+ // functions that execute in the browser context
159
+ webview: RPCSchema<{
160
+ requests: {
161
+ someWebviewFunction: {
162
+ params: {
163
+ a: number;
164
+ b: number;
165
+ };
166
+ response: number;
167
+ };
168
+ };
169
+ messages: {
170
+ logToWebview: {
171
+ msg: string;
172
+ };
173
+ };
174
+ }>;
175
+ };
176
+ ```
177
+
178
+ ```typescript title="src/bun/index.ts"
179
+ import { BrowserView } from "electrobun/bun";
180
+ import { type MyWebviewRPCType } from "../shared/types";
181
+
182
+ // Create an RPC object for the bun handlers with the shared type
183
+ const myWebviewRPC = BrowserView.defineRPC<MyWebviewRPC>({
184
+ maxRequestTime: 5000,
185
+ handlers: {
186
+ requests: {
187
+ someBunFunction: ({ a, b }) => {
188
+ console.log(`browser asked me to do math with: ${a} and ${b}`);
189
+ return a + b;
190
+ },
191
+ },
192
+ // When the browser sends a message we can handle it
193
+ // in the main bun process
194
+ messages: {
195
+ "*": (messageName, payload) => {
196
+ console.log("global message handler", messageName, payload);
197
+ },
198
+ logToBun: ({ msg }) => {
199
+ console.log("Log to bun: ", msg);
200
+ },
201
+ },
202
+ },
203
+ });
204
+
205
+ // Pass the RPC object to the BrowserWindow, which will set it
206
+ // on the window's default BrowserView
207
+ const webview = new BrowserView({
208
+ title: "my window",
209
+ url: "views://mainview/index.html",
210
+ frame: {
211
+ width: 1800,
212
+ height: 600,
213
+ x: 2000,
214
+ y: 2000,
215
+ },
216
+ rpc: myWebviewRPC,
217
+ });
218
+
219
+ // ... later on
220
+
221
+ // Note: These RPC methods will inherit types from the shared type
222
+
223
+ // Call a browser function from bun
224
+ const answer = await webview.rpc.someWebviewFunction(4, 6);
225
+
226
+ // Send a message to the BrowserView from bun
227
+ webview.rpc.logToBrowser("my message");
228
+ ```
229
+
230
+ :::info
231
+ The above code snippet shows defining the bun process rpc handlers and calling the browser process handlers from bun. To see how to handle the Browser context code take a look at the [Browser API](/docs/apis/browser/Electroview%20Class)
232
+ :::
233
+
234
+ ### syncRpc
235
+
236
+ :::warning
237
+ The `SyncRpc` api is blocking. Calling `syncRpc` methods from the browser will completely block the browser thread and halt javascript while waiting for the bun process to respond.
238
+ :::
239
+
240
+ :::info
241
+ Really the only time you may want to use the `syncRpc` api instead of the regular async `rpc` api is when you're migrating from Electron to Electrobun and you had a lot of browser code with the node integration enabled. Using the `syncRpc` api can save you lots of time on the initial refactor/migration.
242
+
243
+ It's strongly advised to later follow up and migrate `syncRpc` methods to async `rpc` later on.
244
+ :::
245
+
246
+ ```typescript title="src/bun/index.ts"
247
+ const win = new BrowserWindow({
248
+ title: "my url window",
249
+ url: "views://mainview/index.html",
250
+ frame: {
251
+ width: 1800,
252
+ height: 600,
253
+ x: 2000,
254
+ y: 2000,
255
+ },
256
+ syncRpc: {
257
+ someSyncBunMethod: ({ a, b }) => {
258
+ console.log("doing sync math in bun", a, b);
259
+ return a + b;
260
+ },
261
+ },
262
+ });
263
+ ```
264
+
265
+ To see how to call syncRpc methods from the browser take a look at the [Browser API](/docs/apis/browser/Electroview%20Class)
266
+
267
+ ## Static Methods
268
+
269
+ ### BrowserView.getAll
270
+
271
+ Get a list of references to all BrowserViews. This includes the default Browserviews created via `new BrowserWindow`, Browserviews created as nested OOPIFs via [WebviewTags](/docs/apis/browser/Electrobun%20Webview%20Tag), and BrowserViews that you create manually via `new BrowserView()` for advanced use cases.
272
+
273
+ ```typescript title="/src/bun/index.ts"
274
+ import { BrowserView } from "electrobun/bun";
275
+
276
+ const webviews = BrowserView.getAll();
277
+ ```
278
+
279
+ ### BrowserView.getById
280
+
281
+ Get a specific BrowserView by id. This includes the default Browserviews created via `new BrowserWindow`, Browserviews created as nested OOPIFs via [WebviewTags](/docs/apis/browser/Electrobun%20Webview%20Tag), and BrowserViews that you create manually via `new BrowserView()` for advanced use cases.
282
+
283
+ ```typescript title="/src/bun/index.ts"
284
+ import { BrowserWindow, BrowserView } from "electrobun/bun";
285
+
286
+ const win = new BrowserWindow({
287
+ title: "my url window",
288
+ url: "views://mainview/index.html",
289
+ frame: {
290
+ width: 1800,
291
+ height: 600,
292
+ x: 2000,
293
+ y: 2000,
294
+ },
295
+ });
296
+
297
+ const webview = BrowserView.getById(win.webview.id);
298
+ ```
299
+
300
+ ### BrowserView.defineRPC
301
+
302
+ Whenever you create a BrowserWindow with async RPC you'll use this static method to create an RPC instance.
303
+
304
+ ```typescript title="src/shared/types.ts"
305
+ export type MyWebviewRPCType = {
306
+ // functions that execute in the main process
307
+ bun: RPCSchema<{
308
+ requests: {
309
+ someBunFunction: {
310
+ params: {
311
+ a: number;
312
+ b: number;
313
+ };
314
+ response: number;
315
+ };
316
+ };
317
+ messages: {
318
+ logToBun: {
319
+ msg: string;
320
+ };
321
+ };
322
+ }>;
323
+ // functions that execute in the browser context
324
+ webview: RPCSchema<{
325
+ requests: {
326
+ someWebviewFunction: {
327
+ params: {
328
+ a: number;
329
+ b: number;
330
+ };
331
+ response: number;
332
+ };
333
+ };
334
+ messages: {
335
+ logToWebview: {
336
+ msg: string;
337
+ };
338
+ };
339
+ }>;
340
+ };
341
+ ```
342
+
343
+ ```typescript title="src/bun/index.ts"
344
+ import { BrowserWindow, BrowserView } from "electrobun/bun";
345
+ import { type MyWebviewRPCType } from "../shared/types";
346
+
347
+ // Create an RPC object for the bun handlers with the shared type
348
+ const myWebviewRPC = BrowserView.defineRPC<MyWebviewRPC>({
349
+ maxRequestTime: 5000,
350
+ handlers: {
351
+ requests: {
352
+ someBunFunction: ({ a, b }) => {
353
+ console.log(`browser asked me to do math with: ${a} and ${b}`);
354
+ return a + b;
355
+ },
356
+ },
357
+ // When the browser sends a message we can handle it
358
+ // in the main bun process
359
+ messages: {
360
+ "*": (messageName, payload) => {
361
+ console.log("global message handler", messageName, payload);
362
+ },
363
+ logToBun: ({ msg }) => {
364
+ console.log("Log to bun: ", msg);
365
+ },
366
+ },
367
+ },
368
+ });
369
+ ```
370
+
371
+ ## Methods
372
+
373
+ ### executeJavascript
374
+
375
+ Execute arbitrary js in the webview. Unlike a `preload` script that you would typically set as a [BrowserWindow](/docs/apis/bun/BrowserWindow) configuratino option, `executeJavascript()` can be called at any time.
376
+
377
+ ```typescript title="/src/bun/index.ts"
378
+ webview.executeJavascript('document.body.innerHTML += "hello"');
379
+ ```
380
+
381
+ ### loadURL
382
+
383
+ Load a url into the webview. This will navigate the webview and trigger navigation events.
384
+
385
+ ```typescript title="/src/bun/index.ts"
386
+ webview.loadURL("https://electrobun.dev");
387
+
388
+ // or
389
+
390
+ webview.loadURL("views://mainview/somepage.html");
391
+ ```
392
+
393
+ ### loadHTML
394
+
395
+ Load html directly into the webview. This will completely replace any content that was previously loaded and trigger navigation events.
396
+
397
+ ```typescript title="/src/bun/index.ts"
398
+ const htmlString =
399
+ "<html><head></head><body><h1>hello world</h1></body></html>";
400
+
401
+ webview.loadHTML({
402
+ html: htmlString,
403
+ });
404
+ ```
405
+
406
+ ### on(name, handler)
407
+
408
+ Subscribe to BrowserWindow events (see below)
409
+
410
+ ## Properties
411
+
412
+ ### id
413
+
414
+ This is the webview's id.
415
+
416
+ ### hostWebviewId
417
+
418
+ This is only used for BrowserViews created using the [WebviewTag](/docs/apis/browser/Electrobun%20Webview%20Tag) as a nested OOPIF. It's the id of the parent BrowserView.
419
+
420
+ ### rpc
421
+
422
+ Once you've configured async rpc for a webview (typically via new BrowserWindow and Webview.defineRPC()) you'll use the rpc property to access the generated typed request and message methods.
423
+
424
+ ```typescript title="/src/bun/index.ts"
425
+ // ... configure BrowserWindow with BrowserView.defineRPC and new BrowserWindow()
426
+
427
+ // Call a browser function from bun
428
+ const answer = await webview.rpc.someWebviewFunction(4, 6);
429
+
430
+ // Send a message to the BrowserView from bun
431
+ webview.rpc.logToBrowser("my message");
432
+ ```
433
+
434
+ ## Events
435
+
436
+ ### will-navigate
437
+
438
+ When a webview is going to navigate. This is cancellable. Global events are fired first so you could cancel it globally, then on a specific Webview check if it was globally cancelled and reverse the decision.
439
+
440
+ ```
441
+ event.data = {
442
+ windowId: string,
443
+ url: string,
444
+ webviewId: string
445
+ }
446
+ ```
447
+
448
+ ```
449
+ Electrobun.events.on("will-navigate", (e) => {
450
+ console.log(
451
+ "example global will navigate handler",
452
+ e.data.url,
453
+ e.data.webviewId
454
+ );
455
+
456
+
457
+ e.response = { allow: false };
458
+ });
459
+
460
+ webview.on("will-navigate", (e) => {
461
+ console.log(
462
+ "example webview will navigate handler",
463
+ e.data.url,
464
+ e.data.webviewId
465
+ );
466
+ if (e.responseWasSet && e.response.allow === false) {
467
+ e.response.allow = true;
468
+
469
+ // Note: since allow is the default you could clear the
470
+ // response instead which would also have the effect of
471
+ // overriding the global decision
472
+ // e.clearResponse();
473
+ }
474
+ });
475
+ ```
476
+
477
+ ### did-navigate
478
+
479
+ After a webview navigates
480
+
481
+ ```
482
+ event.data = {
483
+ detail: string // the url
484
+ }
485
+ ```
486
+
487
+ ### did-navigate-in-page
488
+
489
+ After an in-page navigation
490
+
491
+ ```
492
+ event.data = {
493
+ detail: string // the url
494
+ }
495
+ ```
496
+
497
+ ### did-commit-navigation
498
+
499
+ The webview has started to receive content for the main frame after a navigation.
500
+
501
+ ```
502
+ event.data = {
503
+ detail: string // the url
504
+ }
505
+ ```
506
+
507
+ ### dom-ready
508
+
509
+ The the dom ready event is fired from the browser context.
510
+
511
+ ### newWindowOpen
512
+
513
+ The browser context is attempting to open a new window. For example a popup or a user right clicked and selected "open in new window"