@corelauncher/rod 1.0.8 → 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.8",
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
+ }
@@ -5,17 +5,17 @@ import { encodeString } from "../utilities/strings";
5
5
 
6
6
  export default class WebContext {
7
7
  readonly path: string;
8
- readonly ptr: Pointer;
8
+ readonly webcontextPtr: Pointer;
9
9
 
10
10
  constructor(path: string) {
11
11
  this.path = resolve(path);
12
12
 
13
- const ptr = rod_webcontext_create(encodeString(this.path));
14
- if (!ptr) throw new Error("Failed to create WebContext");
15
- this.ptr = ptr;
13
+ const webcontextPtr = rod_webcontext_create(encodeString(this.path));
14
+ if (!webcontextPtr) throw new Error("Failed to create WebContext");
15
+ this.webcontextPtr = webcontextPtr;
16
16
  }
17
17
 
18
18
  destroy() {
19
- rod_webcontext_destroy(this.ptr);
19
+ rod_webcontext_destroy(this.webcontextPtr);
20
20
  }
21
21
  }
@@ -33,7 +33,7 @@ export default class WebView extends Window {
33
33
 
34
34
  const webviewPtr = rod_webview_create(
35
35
  this.windowPtr,
36
- this.webcontext.ptr,
36
+ this.webcontext.webcontextPtr,
37
37
  encodeString(JSON.stringify(transformWebViewOptions(options))),
38
38
  );
39
39
  if (!webviewPtr) throw new Error("Failed to create WebView");
package/src-ts/ffi.ts CHANGED
@@ -87,6 +87,10 @@ const {
87
87
  rod_webview_close_devtools,
88
88
  rod_webview_reload,
89
89
  rod_webview_clear_all_browsing_data,
90
+
91
+ // tray
92
+ rod_tray_create,
93
+ rod_tray_destroy,
90
94
  },
91
95
  } = dlopen(library.default, {
92
96
  // event loop
@@ -318,6 +322,16 @@ const {
318
322
  args: [FFIType.ptr],
319
323
  returns: FFIType.void,
320
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
+ },
321
335
  });
322
336
 
323
337
  export {
@@ -386,4 +400,7 @@ export {
386
400
  rod_webview_close_devtools,
387
401
  rod_webview_reload,
388
402
  rod_webview_clear_all_browsing_data,
403
+ // tray
404
+ rod_tray_create,
405
+ rod_tray_destroy,
389
406
  };
package/src-ts/types.ts CHANGED
@@ -33,6 +33,12 @@ export type WindowOptions = {
33
33
  visibleOnAllWorkspaces?: boolean;
34
34
  };
35
35
 
36
+ export type TrayOptions = {
37
+ iconPath?: string;
38
+ tooltip?: string;
39
+ title?: string;
40
+ };
41
+
36
42
  export type Size = {
37
43
  width: number;
38
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