@corelauncher/rod 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@corelauncher/rod",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "Webview for bun based on tauri's tao and wry.",
5
5
  "keywords": [
6
6
  "corelauncher",
@@ -12,6 +12,7 @@ interface EventLoopEvents {
12
12
  window_focused: (id: number, focused: boolean) => void;
13
13
  window_moved: (id: number, position: Position) => void;
14
14
  window_resized: (id: number, size: Size) => void;
15
+ tray_clicked: (id: number) => void;
15
16
  }
16
17
 
17
18
  export default class EventLoop extends TypedEmitter<EventLoopEvents> {
@@ -47,7 +48,11 @@ export default class EventLoop extends TypedEmitter<EventLoopEvents> {
47
48
  width: data.width,
48
49
  height: data.height,
49
50
  });
51
+ case "tray_clicked":
52
+ return this.emit("tray_clicked", data.id);
50
53
  }
54
+
55
+ throw new Error(`Unknown event type: ${event}`);
51
56
  },
52
57
  {
53
58
  args: [FFIType.cstring, FFIType.cstring],
@@ -1,5 +1,6 @@
1
- import type { WebViewOptions, WindowOptions } from "../types";
1
+ import type { TrayOptions, WebViewOptions, WindowOptions } from "../types";
2
2
  import EventLoop from "./eventloop";
3
+ import Tray from "./tray";
3
4
  import WebView from "./webview";
4
5
 
5
6
  let idIndex = 1;
@@ -7,9 +8,11 @@ let idIndex = 1;
7
8
  export default class Rod {
8
9
  eventLoop: EventLoop;
9
10
  private webviews: WebView[];
11
+ private trays: Tray[];
10
12
  constructor() {
11
13
  this.eventLoop = new EventLoop();
12
14
  this.webviews = [];
15
+ this.trays = [];
13
16
 
14
17
  this.eventLoop.on("window_close_requested", (id) => {
15
18
  const webview = this.retrieveWebViewById(id);
@@ -34,6 +37,12 @@ export default class Rod {
34
37
  if (!webview) return;
35
38
  webview.emit("resized", size);
36
39
  });
40
+
41
+ this.eventLoop.on("tray_clicked", (id) => {
42
+ const tray = this.trays.find((t) => t.id === id);
43
+ if (!tray) return;
44
+ tray.emit("click");
45
+ });
37
46
  }
38
47
 
39
48
  private generateId() {
@@ -47,6 +56,7 @@ export default class Rod {
47
56
  createWebView(options: WebViewOptions & WindowOptions = {}) {
48
57
  const id = this.generateId();
49
58
  const webview = new WebView(this.eventLoop, id, options);
59
+ this.webviews.push(webview);
50
60
 
51
61
  webview.on("destroyed", () => {
52
62
  this.webviews = this.webviews.filter((wv) => wv.id !== webview.id);
@@ -54,4 +64,27 @@ export default class Rod {
54
64
 
55
65
  return webview;
56
66
  }
67
+
68
+ createTray(options: TrayOptions) {
69
+ const id = this.generateId();
70
+ const tray = new Tray(id, options);
71
+ this.trays.push(tray);
72
+
73
+ tray.on("destroyed", () => {
74
+ this.trays = this.trays.filter((t) => t.id !== tray.id);
75
+ });
76
+
77
+ return tray;
78
+ }
79
+
80
+ destroy() {
81
+ this.webviews.forEach((webview) => {
82
+ webview.destroy();
83
+ });
84
+ this.trays.forEach((tray) => {
85
+ tray.destroy();
86
+ });
87
+
88
+ this.eventLoop.destroy();
89
+ }
57
90
  }
@@ -0,0 +1,37 @@
1
+ import type { Pointer } from "bun:ffi";
2
+ import { TypedEmitter } from "tiny-typed-emitter";
3
+ import { rod_tray_create, rod_tray_destroy } from "../ffi";
4
+ import type { TrayOptions } from "../types";
5
+ import { transformTrayOptions } from "../utilities/options";
6
+ import { encodeString } from "../utilities/strings";
7
+
8
+ interface TrayEvents {
9
+ click: () => void;
10
+ destroyed: () => void;
11
+ }
12
+
13
+ export default class Tray extends TypedEmitter<TrayEvents> {
14
+ id: number;
15
+ protected trayPtr: Pointer;
16
+ constructor(id: number, options: TrayOptions) {
17
+ super();
18
+
19
+ this.id = id;
20
+
21
+ const trayPtr = rod_tray_create(
22
+ this.id,
23
+ encodeString(JSON.stringify(transformTrayOptions(options))),
24
+ );
25
+ if (!trayPtr) throw new Error("Failed to create Tray");
26
+ this.trayPtr = trayPtr;
27
+ }
28
+
29
+ destroy() {
30
+ if (!this.trayPtr) return;
31
+
32
+ rod_tray_destroy(this.trayPtr);
33
+ this.trayPtr = null as unknown as Pointer;
34
+
35
+ this.emit("destroyed");
36
+ }
37
+ }
@@ -0,0 +1,21 @@
1
+ import type { Pointer } from "bun:ffi";
2
+ import { resolve } from "node:path";
3
+ import { rod_webcontext_create, rod_webcontext_destroy } from "../ffi";
4
+ import { encodeString } from "../utilities/strings";
5
+
6
+ export default class WebContext {
7
+ readonly path: string;
8
+ readonly webcontextPtr: Pointer;
9
+
10
+ constructor(path: string) {
11
+ this.path = resolve(path);
12
+
13
+ const webcontextPtr = rod_webcontext_create(encodeString(this.path));
14
+ if (!webcontextPtr) throw new Error("Failed to create WebContext");
15
+ this.webcontextPtr = webcontextPtr;
16
+ }
17
+
18
+ destroy() {
19
+ rod_webcontext_destroy(this.webcontextPtr);
20
+ }
21
+ }
@@ -16,9 +16,11 @@ import type { WebViewOptions, WindowOptions } from "../types";
16
16
  import { transformWebViewOptions } from "../utilities/options";
17
17
  import { encodeString } from "../utilities/strings";
18
18
  import type EventLoop from "./eventloop";
19
+ import WebContext from "./webcontext";
19
20
  import Window from "./window";
20
21
 
21
22
  export default class WebView extends Window {
23
+ private webcontext: WebContext;
22
24
  protected webviewPtr: Pointer;
23
25
  constructor(
24
26
  eventLoop: EventLoop,
@@ -27,8 +29,11 @@ export default class WebView extends Window {
27
29
  ) {
28
30
  super(eventLoop.eventloopPtr, id, options);
29
31
 
32
+ this.webcontext = new WebContext(options.dataDirectory || "./rod_data");
33
+
30
34
  const webviewPtr = rod_webview_create(
31
35
  this.windowPtr,
36
+ this.webcontext.webcontextPtr,
32
37
  encodeString(JSON.stringify(transformWebViewOptions(options))),
33
38
  );
34
39
  if (!webviewPtr) throw new Error("Failed to create WebView");
@@ -78,6 +83,7 @@ export default class WebView extends Window {
78
83
  rod_webview_destroy(this.webviewPtr);
79
84
  this.webviewPtr = null as unknown as Pointer;
80
85
 
86
+ this.webcontext.destroy();
81
87
  super.destroy();
82
88
  }
83
89
  }
package/src-ts/ffi.ts CHANGED
@@ -65,6 +65,10 @@ const {
65
65
  // window actions
66
66
  rod_window_start_drag,
67
67
 
68
+ // webcontext
69
+ rod_webcontext_create,
70
+ rod_webcontext_destroy,
71
+
68
72
  // webview
69
73
  rod_webview_create,
70
74
  rod_webview_destroy,
@@ -83,6 +87,10 @@ const {
83
87
  rod_webview_close_devtools,
84
88
  rod_webview_reload,
85
89
  rod_webview_clear_all_browsing_data,
90
+
91
+ // tray
92
+ rod_tray_create,
93
+ rod_tray_destroy,
86
94
  },
87
95
  } = dlopen(library.default, {
88
96
  // event loop
@@ -259,9 +267,19 @@ const {
259
267
  returns: FFIType.void,
260
268
  },
261
269
 
270
+ // webcontext
271
+ rod_webcontext_create: {
272
+ args: [FFIType.ptr, FFIType.cstring],
273
+ returns: FFIType.ptr,
274
+ },
275
+ rod_webcontext_destroy: {
276
+ args: [FFIType.ptr],
277
+ returns: FFIType.void,
278
+ },
279
+
262
280
  // webview
263
281
  rod_webview_create: {
264
- args: [FFIType.ptr, FFIType.cstring],
282
+ args: [FFIType.ptr, FFIType.ptr, FFIType.cstring],
265
283
  returns: FFIType.ptr,
266
284
  },
267
285
  rod_webview_destroy: {
@@ -304,6 +322,16 @@ const {
304
322
  args: [FFIType.ptr],
305
323
  returns: FFIType.void,
306
324
  },
325
+
326
+ // tray
327
+ rod_tray_create: {
328
+ args: [FFIType.u16, FFIType.cstring],
329
+ returns: FFIType.ptr,
330
+ },
331
+ rod_tray_destroy: {
332
+ args: [FFIType.ptr],
333
+ returns: FFIType.void,
334
+ },
307
335
  });
308
336
 
309
337
  export {
@@ -354,6 +382,9 @@ export {
354
382
  rod_window_set_visible_on_all_workspaces,
355
383
  // window actions
356
384
  rod_window_start_drag,
385
+ // webcontext
386
+ rod_webcontext_create,
387
+ rod_webcontext_destroy,
357
388
  // webview
358
389
  rod_webview_create,
359
390
  rod_webview_destroy,
@@ -369,4 +400,7 @@ export {
369
400
  rod_webview_close_devtools,
370
401
  rod_webview_reload,
371
402
  rod_webview_clear_all_browsing_data,
403
+ // tray
404
+ rod_tray_create,
405
+ rod_tray_destroy,
372
406
  };
package/src-ts/types.ts CHANGED
@@ -7,6 +7,7 @@ export type WebViewOptions = {
7
7
  html?: string;
8
8
  url?: string;
9
9
  incognito?: boolean;
10
+ dataDirectory?: string;
10
11
  };
11
12
 
12
13
  export type WindowOptions = {
@@ -32,6 +33,12 @@ export type WindowOptions = {
32
33
  visibleOnAllWorkspaces?: boolean;
33
34
  };
34
35
 
36
+ export type TrayOptions = {
37
+ iconPath?: string;
38
+ tooltip?: string;
39
+ title?: string;
40
+ };
41
+
35
42
  export type Size = {
36
43
  width: number;
37
44
  height: number;
@@ -1,4 +1,5 @@
1
- import type { WebViewOptions, WindowOptions } from "../types";
1
+ import { resolve } from "node:path";
2
+ import type { TrayOptions, WebViewOptions, WindowOptions } from "../types";
2
3
 
3
4
  export function transformWebViewOptions(options: WebViewOptions) {
4
5
  return {
@@ -37,3 +38,11 @@ export function transformWindowOptions(options: WindowOptions) {
37
38
  visible_on_all_workspaces: options.visibleOnAllWorkspaces,
38
39
  };
39
40
  }
41
+
42
+ export function transformTrayOptions(options: TrayOptions) {
43
+ return {
44
+ icon_path: options.iconPath ? resolve(options.iconPath) : undefined,
45
+ tooltip: options.tooltip,
46
+ title: options.title,
47
+ };
48
+ }
Binary file