electrobun 0.0.19-beta.7 → 0.0.19-beta.71
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/BUILD.md +90 -0
- package/bin/electrobun.cjs +165 -0
- package/debug.js +5 -0
- package/dist/api/browser/builtinrpcSchema.ts +19 -0
- package/dist/api/browser/index.ts +409 -0
- package/dist/api/browser/rpc/webview.ts +79 -0
- package/dist/api/browser/stylesAndElements.ts +3 -0
- package/dist/api/browser/webviewtag.ts +534 -0
- package/dist/api/bun/core/ApplicationMenu.ts +66 -0
- package/dist/api/bun/core/BrowserView.ts +349 -0
- package/dist/api/bun/core/BrowserWindow.ts +191 -0
- package/dist/api/bun/core/ContextMenu.ts +67 -0
- package/dist/api/bun/core/Paths.ts +5 -0
- package/dist/api/bun/core/Socket.ts +181 -0
- package/dist/api/bun/core/Tray.ts +107 -0
- package/dist/api/bun/core/Updater.ts +395 -0
- package/dist/api/bun/core/Utils.ts +48 -0
- package/dist/api/bun/events/ApplicationEvents.ts +14 -0
- package/dist/api/bun/events/event.ts +29 -0
- package/dist/api/bun/events/eventEmitter.ts +45 -0
- package/dist/api/bun/events/trayEvents.ts +9 -0
- package/dist/api/bun/events/webviewEvents.ts +16 -0
- package/dist/api/bun/events/windowEvents.ts +12 -0
- package/dist/api/bun/index.ts +45 -0
- package/dist/api/bun/proc/linux.md +43 -0
- package/dist/api/bun/proc/native.ts +1217 -0
- package/dist/api/shared/platform.ts +48 -0
- package/dist/main.js +12 -0
- package/package.json +13 -7
- package/src/cli/index.ts +621 -203
- package/templates/hello-world/README.md +57 -0
- package/templates/hello-world/bun.lock +63 -0
- package/templates/hello-world/electrobun.config +17 -0
- package/templates/hello-world/package.json +16 -0
- package/templates/hello-world/src/bun/index.ts +15 -0
- package/templates/hello-world/src/mainview/index.css +124 -0
- package/templates/hello-world/src/mainview/index.html +47 -0
- package/templates/hello-world/src/mainview/index.ts +5 -0
- package/bin/electrobun +0 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { native, toCString, ffi } from "../proc/native";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import { execSync } from "child_process";
|
|
4
|
+
import electrobunEventEmitter from "../events/eventEmitter";
|
|
5
|
+
import {
|
|
6
|
+
type RPCSchema,
|
|
7
|
+
type RPCRequestHandler,
|
|
8
|
+
type RPCMessageHandlerFn,
|
|
9
|
+
type WildcardRPCMessageHandlerFn,
|
|
10
|
+
type RPCOptions,
|
|
11
|
+
createRPC,
|
|
12
|
+
} from "rpc-anywhere";
|
|
13
|
+
import { Updater } from "./Updater";
|
|
14
|
+
import type { BuiltinBunToWebviewSchema,BuiltinWebviewToBunSchema } from "../../browser/builtinrpcSchema";
|
|
15
|
+
import { rpcPort, sendMessageToWebviewViaSocket } from "./Socket";
|
|
16
|
+
import { randomBytes } from "crypto";
|
|
17
|
+
import {FFIType, type Pointer} from 'bun:ffi';
|
|
18
|
+
|
|
19
|
+
const BrowserViewMap: {
|
|
20
|
+
[id: number]: BrowserView<any>;
|
|
21
|
+
} = {};
|
|
22
|
+
let nextWebviewId = 1;
|
|
23
|
+
|
|
24
|
+
const CHUNK_SIZE = 1024 * 4; // 4KB
|
|
25
|
+
|
|
26
|
+
type BrowserViewOptions<T = undefined> = {
|
|
27
|
+
url: string | null;
|
|
28
|
+
html: string | null;
|
|
29
|
+
preload: string | null;
|
|
30
|
+
renderer: 'native' | 'cef';
|
|
31
|
+
partition: string | null;
|
|
32
|
+
frame: {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
};
|
|
38
|
+
rpc: T;
|
|
39
|
+
hostWebviewId: number;
|
|
40
|
+
autoResize: boolean;
|
|
41
|
+
|
|
42
|
+
windowId: number;
|
|
43
|
+
navigationRules: string | null;
|
|
44
|
+
// renderer:
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
interface ElectrobunWebviewRPCSChema {
|
|
48
|
+
bun: RPCSchema;
|
|
49
|
+
webview: RPCSchema;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const defaultOptions: Partial<BrowserViewOptions> = {
|
|
53
|
+
url: null,
|
|
54
|
+
html: null,
|
|
55
|
+
preload: null,
|
|
56
|
+
renderer: 'native',
|
|
57
|
+
frame: {
|
|
58
|
+
x: 0,
|
|
59
|
+
y: 0,
|
|
60
|
+
width: 800,
|
|
61
|
+
height: 600,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
const hash = await Updater.localInfo.hash();
|
|
68
|
+
// Note: we use the build's hash to separate from different apps and different builds
|
|
69
|
+
// but we also want a randomId to separate different instances of the same app
|
|
70
|
+
const randomId = Math.random().toString(36).substring(7);
|
|
71
|
+
|
|
72
|
+
export class BrowserView<T> {
|
|
73
|
+
id: number = nextWebviewId++;
|
|
74
|
+
ptr: Pointer;
|
|
75
|
+
hostWebviewId?: number;
|
|
76
|
+
windowId: number;
|
|
77
|
+
renderer: 'cef' | 'native';
|
|
78
|
+
url: string | null = null;
|
|
79
|
+
html: string | null = null;
|
|
80
|
+
preload: string | null = null;
|
|
81
|
+
partition: string | null = null;
|
|
82
|
+
autoResize: boolean = true;
|
|
83
|
+
frame: {
|
|
84
|
+
x: number;
|
|
85
|
+
y: number;
|
|
86
|
+
width: number;
|
|
87
|
+
height: number;
|
|
88
|
+
} = {
|
|
89
|
+
x: 0,
|
|
90
|
+
y: 0,
|
|
91
|
+
width: 800,
|
|
92
|
+
height: 600,
|
|
93
|
+
};
|
|
94
|
+
pipePrefix: string;
|
|
95
|
+
inStream: fs.WriteStream;
|
|
96
|
+
outStream: ReadableStream<Uint8Array>;
|
|
97
|
+
secretKey: Uint8Array;
|
|
98
|
+
rpc?: T;
|
|
99
|
+
rpcHandler?: (msg: any) => void;
|
|
100
|
+
navigationRules: string | null;
|
|
101
|
+
|
|
102
|
+
constructor(options: Partial<BrowserViewOptions<T>> = defaultOptions) {
|
|
103
|
+
// const rpc = options.rpc;
|
|
104
|
+
|
|
105
|
+
this.url = options.url || defaultOptions.url || null;
|
|
106
|
+
this.html = options.html || defaultOptions.html || null;
|
|
107
|
+
this.preload = options.preload || defaultOptions.preload || null;
|
|
108
|
+
this.frame = options.frame
|
|
109
|
+
? { ...defaultOptions.frame, ...options.frame }
|
|
110
|
+
: { ...defaultOptions.frame };
|
|
111
|
+
this.rpc = options.rpc;
|
|
112
|
+
this.secretKey = new Uint8Array(randomBytes(32));
|
|
113
|
+
this.partition = options.partition || null;
|
|
114
|
+
// todo (yoav): since collisions can crash the app add a function that checks if the
|
|
115
|
+
// file exists first
|
|
116
|
+
this.pipePrefix = `/private/tmp/electrobun_ipc_pipe_${hash}_${randomId}_${this.id}`;
|
|
117
|
+
this.hostWebviewId = options.hostWebviewId;
|
|
118
|
+
this.windowId = options.windowId;
|
|
119
|
+
this.autoResize = options.autoResize === false ? false : true;
|
|
120
|
+
this.navigationRules = options.navigationRules || null;
|
|
121
|
+
this.renderer = options.renderer || defaultOptions.renderer;
|
|
122
|
+
|
|
123
|
+
BrowserViewMap[this.id] = this;
|
|
124
|
+
this.ptr = this.init();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
init() {
|
|
128
|
+
console.log('browserView init', this.id, this.windowId, this.renderer);
|
|
129
|
+
this.createStreams();
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
console.log('ffi createWEbview')
|
|
133
|
+
// TODO: add a then to this that fires an onReady event
|
|
134
|
+
return ffi.request.createWebview({
|
|
135
|
+
id: this.id,
|
|
136
|
+
windowId: this.windowId,
|
|
137
|
+
renderer: this.renderer,
|
|
138
|
+
rpcPort: rpcPort,
|
|
139
|
+
// todo: consider sending secretKey as base64
|
|
140
|
+
secretKey: this.secretKey.toString(),
|
|
141
|
+
hostWebviewId: this.hostWebviewId || null,
|
|
142
|
+
pipePrefix: this.pipePrefix,
|
|
143
|
+
partition: this.partition,
|
|
144
|
+
url: this.url,
|
|
145
|
+
html: this.html,
|
|
146
|
+
preload: this.preload,
|
|
147
|
+
frame: {
|
|
148
|
+
width: this.frame.width,
|
|
149
|
+
height: this.frame.height,
|
|
150
|
+
x: this.frame.x,
|
|
151
|
+
y: this.frame.y,
|
|
152
|
+
},
|
|
153
|
+
autoResize: this.autoResize,
|
|
154
|
+
navigationRules: this.navigationRules,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
createStreams() {
|
|
161
|
+
if (!this.rpc) {
|
|
162
|
+
this.rpc = BrowserView.defineRPC({
|
|
163
|
+
handlers: { requests: {}, messages: {} },
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this.rpc.setTransport(this.createTransport());
|
|
168
|
+
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
sendMessageToWebviewViaExecute(jsonMessage) {
|
|
172
|
+
const stringifiedMessage =
|
|
173
|
+
typeof jsonMessage === "string"
|
|
174
|
+
? jsonMessage
|
|
175
|
+
: JSON.stringify(jsonMessage);
|
|
176
|
+
// todo (yoav): make this a shared const with the browser api
|
|
177
|
+
const wrappedMessage = `window.__electrobun.receiveMessageFromBun(${stringifiedMessage})`;
|
|
178
|
+
this.executeJavascript(wrappedMessage);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
sendInternalMessageViaExecute(jsonMessage) {
|
|
182
|
+
const stringifiedMessage =
|
|
183
|
+
typeof jsonMessage === "string"
|
|
184
|
+
? jsonMessage
|
|
185
|
+
: JSON.stringify(jsonMessage);
|
|
186
|
+
// todo (yoav): make this a shared const with the browser api
|
|
187
|
+
const wrappedMessage = `window.__electrobun.receiveInternalMessageFromBun(${stringifiedMessage})`;
|
|
188
|
+
this.executeJavascript(wrappedMessage);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Note: the OS has a buffer limit on named pipes. If we overflow it
|
|
192
|
+
// it won't trigger the kevent for zig to read the pipe and we'll be stuck.
|
|
193
|
+
// so we have to chunk it
|
|
194
|
+
// TODO: is this still needed after switching from named pipes
|
|
195
|
+
executeJavascript(js: string) {
|
|
196
|
+
ffi.request.evaluateJavascriptWithNoCompletion({id: this.id, js});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
loadURL(url: string) {
|
|
200
|
+
this.url = url;
|
|
201
|
+
native.symbols.loadURLInWebView(this.ptr, toCString(this.url))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
loadHTML(html: string) {
|
|
205
|
+
// Note: CEF doesn't natively support "setting html" so we just return
|
|
206
|
+
// this BrowserView's html when a special views:// url is hit.
|
|
207
|
+
// So we can update that content and ask the webview to load that url
|
|
208
|
+
this.html = html;
|
|
209
|
+
this.loadURL('views://internal/index.html');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
// todo (yoav): move this to a class that also has off, append, prepend, etc.
|
|
214
|
+
// name should only allow browserView events
|
|
215
|
+
// Note: normalize event names to willNavigate instead of ['will-navigate'] to save
|
|
216
|
+
// 5 characters per usage and allow minification to be more effective.
|
|
217
|
+
on(
|
|
218
|
+
name:
|
|
219
|
+
| "will-navigate"
|
|
220
|
+
| "did-navigate"
|
|
221
|
+
| "did-navigate-in-page"
|
|
222
|
+
| "did-commit-navigation"
|
|
223
|
+
| "dom-ready",
|
|
224
|
+
handler
|
|
225
|
+
) {
|
|
226
|
+
const specificName = `${name}-${this.id}`;
|
|
227
|
+
electrobunEventEmitter.on(specificName, handler);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
createTransport = () => {
|
|
231
|
+
const that = this;
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
send(message: any) {
|
|
235
|
+
const sentOverSocket = sendMessageToWebviewViaSocket(that.id, message);
|
|
236
|
+
|
|
237
|
+
if (!sentOverSocket) {
|
|
238
|
+
try {
|
|
239
|
+
const messageString = JSON.stringify(message);
|
|
240
|
+
that.sendMessageToWebviewViaExecute(messageString);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error("bun: failed to serialize message to webview", error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
registerHandler(handler) {
|
|
247
|
+
that.rpcHandler = handler;
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
static getById(id: number) {
|
|
253
|
+
return BrowserViewMap[id];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
static getAll() {
|
|
257
|
+
return Object.values(BrowserViewMap);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
static defineRPC<
|
|
261
|
+
Schema extends ElectrobunWebviewRPCSChema,
|
|
262
|
+
BunSchema extends RPCSchema = Schema["bun"],
|
|
263
|
+
WebviewSchema extends RPCSchema = Schema["webview"]
|
|
264
|
+
>(config: {
|
|
265
|
+
maxRequestTime?: number;
|
|
266
|
+
handlers: {
|
|
267
|
+
requests?: RPCRequestHandler<BunSchema["requests"]>;
|
|
268
|
+
messages?: {
|
|
269
|
+
[key in keyof BunSchema["messages"]]: RPCMessageHandlerFn<
|
|
270
|
+
BunSchema["messages"],
|
|
271
|
+
key
|
|
272
|
+
>;
|
|
273
|
+
} & {
|
|
274
|
+
"*"?: WildcardRPCMessageHandlerFn<BunSchema["messages"]>;
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
}) {
|
|
278
|
+
// Note: RPC Anywhere requires defining the requests that a schema handles and the messages that a schema sends.
|
|
279
|
+
// eg: BunSchema {
|
|
280
|
+
// requests: // ... requests bun handles, sent by webview
|
|
281
|
+
// messages: // ... messages bun sends, handled by webview
|
|
282
|
+
// }
|
|
283
|
+
// In some generlized contexts that makes sense,
|
|
284
|
+
// In the Electrobun context it can feel a bit counter-intuitive so we swap this around a bit. In Electrobun, the
|
|
285
|
+
// webview and bun are known endpoints so we simplify schema definitions by combining them.
|
|
286
|
+
// Schema {
|
|
287
|
+
// bun: BunSchema {
|
|
288
|
+
// requests: // ... requests bun handles, sent by webview,
|
|
289
|
+
// messages: // ... messages bun handles, sent by webview
|
|
290
|
+
// },
|
|
291
|
+
// webview: WebviewSchema {
|
|
292
|
+
// requests: // ... requests webview handles, sent by bun,
|
|
293
|
+
// messages: // ... messages webview handles, sent by bun
|
|
294
|
+
// },
|
|
295
|
+
// }
|
|
296
|
+
// This way from bun, webview.rpc.request.getTitle() and webview.rpc.send.someMessage maps to the schema
|
|
297
|
+
// MySchema.webview.requests.getTitle and MySchema.webview.messages.someMessage
|
|
298
|
+
// and in the webview, Electroview.rpc.request.getFileContents maps to
|
|
299
|
+
// MySchema.bun.requests.getFileContents.
|
|
300
|
+
// electrobun also treats messages as "requests that we don't wait for to complete", and normalizes specifying the
|
|
301
|
+
// handlers for them alongside request handlers.
|
|
302
|
+
|
|
303
|
+
type mixedWebviewSchema = {
|
|
304
|
+
requests: BunSchema["requests"]// & BuiltinWebviewToBunSchema["requests"];
|
|
305
|
+
messages: WebviewSchema["messages"];
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
type mixedBunSchema = {
|
|
309
|
+
messages: BunSchema["messages"];
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const rpcOptions = {
|
|
313
|
+
maxRequestTime: config.maxRequestTime,
|
|
314
|
+
requestHandler: {
|
|
315
|
+
...config.handlers.requests,
|
|
316
|
+
// ...internalRpcHandlers,
|
|
317
|
+
},
|
|
318
|
+
transport: {
|
|
319
|
+
// Note: RPC Anywhere will throw if you try add a message listener if transport.registerHandler is falsey
|
|
320
|
+
registerHandler: () => {},
|
|
321
|
+
},
|
|
322
|
+
} as RPCOptions<mixedWebviewSchema, mixedBunSchema>;
|
|
323
|
+
|
|
324
|
+
const rpc = createRPC<mixedWebviewSchema, mixedBunSchema>(rpcOptions);
|
|
325
|
+
|
|
326
|
+
const messageHandlers = config.handlers.messages;
|
|
327
|
+
if (messageHandlers) {
|
|
328
|
+
// note: this can only be done once there is a transport
|
|
329
|
+
// @ts-ignore - this is due to all the schema mixing we're doing, fine to ignore
|
|
330
|
+
// while types in here are borked, they resolve correctly/bubble up to the defineRPC call site.
|
|
331
|
+
rpc.addMessageListener(
|
|
332
|
+
"*",
|
|
333
|
+
(messageName: keyof BunSchema["messages"], payload) => {
|
|
334
|
+
const globalHandler = messageHandlers["*"];
|
|
335
|
+
if (globalHandler) {
|
|
336
|
+
globalHandler(messageName, payload);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const messageHandler = messageHandlers[messageName];
|
|
340
|
+
if (messageHandler) {
|
|
341
|
+
messageHandler(payload);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return rpc;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { ffi } from "../proc/native";
|
|
2
|
+
import electrobunEventEmitter from "../events/eventEmitter";
|
|
3
|
+
import { BrowserView } from "./BrowserView";
|
|
4
|
+
import { type RPC } from "rpc-anywhere";
|
|
5
|
+
import {FFIType} from 'bun:ffi'
|
|
6
|
+
|
|
7
|
+
let nextWindowId = 1;
|
|
8
|
+
|
|
9
|
+
// todo (yoav): if we default to builtInSchema, we don't want dev to have to define custom handlers
|
|
10
|
+
// for the built-in schema stuff.
|
|
11
|
+
type WindowOptionsType<T = undefined> = {
|
|
12
|
+
title: string;
|
|
13
|
+
frame: {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
};
|
|
19
|
+
url: string | null;
|
|
20
|
+
html: string | null;
|
|
21
|
+
preload: string | null;
|
|
22
|
+
renderer: 'native' | 'cef';
|
|
23
|
+
rpc?: T;
|
|
24
|
+
styleMask?: {};
|
|
25
|
+
// TODO: implement all of them
|
|
26
|
+
titleBarStyle: "hiddenInset" | "default";
|
|
27
|
+
navigationRules: string | null;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const defaultOptions: WindowOptionsType = {
|
|
31
|
+
title: "Electrobun",
|
|
32
|
+
frame: {
|
|
33
|
+
x: 0,
|
|
34
|
+
y: 0,
|
|
35
|
+
width: 800,
|
|
36
|
+
height: 600,
|
|
37
|
+
},
|
|
38
|
+
url: "https://electrobun.dev",
|
|
39
|
+
html: null,
|
|
40
|
+
preload: null,
|
|
41
|
+
renderer: 'native',
|
|
42
|
+
titleBarStyle: "default",
|
|
43
|
+
navigationRules: null,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const BrowserWindowMap = {};
|
|
47
|
+
|
|
48
|
+
// todo (yoav): do something where the type extends the default schema
|
|
49
|
+
// that way we can provide built-in requests/messages and devs can extend it
|
|
50
|
+
|
|
51
|
+
export class BrowserWindow<T> {
|
|
52
|
+
id: number = nextWindowId++;
|
|
53
|
+
ptr: FFIType.ptr;
|
|
54
|
+
title: string = "Electrobun";
|
|
55
|
+
state: "creating" | "created" = "creating";
|
|
56
|
+
url: string | null = null;
|
|
57
|
+
html: string | null = null;
|
|
58
|
+
preload: string | null = null;
|
|
59
|
+
renderer: 'native' | 'cef';
|
|
60
|
+
frame: {
|
|
61
|
+
x: number;
|
|
62
|
+
y: number;
|
|
63
|
+
width: number;
|
|
64
|
+
height: number;
|
|
65
|
+
} = {
|
|
66
|
+
x: 0,
|
|
67
|
+
y: 0,
|
|
68
|
+
width: 800,
|
|
69
|
+
height: 600,
|
|
70
|
+
};
|
|
71
|
+
// todo (yoav): make this an array of ids or something
|
|
72
|
+
webviewId: number;
|
|
73
|
+
|
|
74
|
+
constructor(options: Partial<WindowOptionsType<T>> = defaultOptions) {
|
|
75
|
+
this.title = options.title || "New Window";
|
|
76
|
+
this.frame = options.frame
|
|
77
|
+
? { ...defaultOptions.frame, ...options.frame }
|
|
78
|
+
: { ...defaultOptions.frame };
|
|
79
|
+
this.url = options.url || null;
|
|
80
|
+
this.html = options.html || null;
|
|
81
|
+
this.preload = options.preload || null;
|
|
82
|
+
this.renderer = options.renderer === 'cef' ? 'cef' : 'native';
|
|
83
|
+
this.navigationRules = options.navigationRules || null;
|
|
84
|
+
|
|
85
|
+
this.init(options);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
init({
|
|
89
|
+
rpc,
|
|
90
|
+
styleMask,
|
|
91
|
+
titleBarStyle,
|
|
92
|
+
}: Partial<WindowOptionsType<T>>) {
|
|
93
|
+
|
|
94
|
+
this.ptr = ffi.request.createWindow({
|
|
95
|
+
id: this.id,
|
|
96
|
+
title: this.title,
|
|
97
|
+
url: this.url || "",
|
|
98
|
+
frame: {
|
|
99
|
+
width: this.frame.width,
|
|
100
|
+
height: this.frame.height,
|
|
101
|
+
x: this.frame.x,
|
|
102
|
+
y: this.frame.y,
|
|
103
|
+
},
|
|
104
|
+
styleMask: {
|
|
105
|
+
Borderless: false,
|
|
106
|
+
Titled: true,
|
|
107
|
+
Closable: true,
|
|
108
|
+
Miniaturizable: true,
|
|
109
|
+
Resizable: true,
|
|
110
|
+
UnifiedTitleAndToolbar: false,
|
|
111
|
+
FullScreen: false,
|
|
112
|
+
FullSizeContentView: false,
|
|
113
|
+
UtilityWindow: false,
|
|
114
|
+
DocModalWindow: false,
|
|
115
|
+
NonactivatingPanel: false,
|
|
116
|
+
HUDWindow: false,
|
|
117
|
+
...(styleMask || {}),
|
|
118
|
+
...(titleBarStyle === "hiddenInset"
|
|
119
|
+
? {
|
|
120
|
+
Titled: true,
|
|
121
|
+
FullSizeContentView: true,
|
|
122
|
+
}
|
|
123
|
+
: {}),
|
|
124
|
+
},
|
|
125
|
+
titleBarStyle: titleBarStyle || "default",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
BrowserWindowMap[this.id] = this;
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
// todo (yoav): user should be able to override this and pass in their
|
|
133
|
+
// own webview instance, or instances for attaching to the window.
|
|
134
|
+
const webview = new BrowserView({
|
|
135
|
+
// TODO: decide whether we want to keep sending url/html
|
|
136
|
+
// here, if we're manually calling loadURL/loadHTML below
|
|
137
|
+
// then we can remove it from the api here
|
|
138
|
+
url: this.url,
|
|
139
|
+
html: this.html,
|
|
140
|
+
preload: this.preload,
|
|
141
|
+
// frame: this.frame,
|
|
142
|
+
renderer: this.renderer,
|
|
143
|
+
frame: {
|
|
144
|
+
x: 0,
|
|
145
|
+
y: 0,
|
|
146
|
+
width: this.frame.width,
|
|
147
|
+
height: this.frame.height,
|
|
148
|
+
},
|
|
149
|
+
rpc,
|
|
150
|
+
// todo: we need to send the window here and attach it in one go
|
|
151
|
+
// then the view creation code in objc can toggle between offscreen
|
|
152
|
+
// or on screen views depending on if windowId is null
|
|
153
|
+
// does this mean browserView needs to track the windowId or handle it ephemerally?
|
|
154
|
+
windowId: this.id,
|
|
155
|
+
navigationRules: this.navigationRules,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
console.log('setting webviewId: ', webview.id)
|
|
159
|
+
|
|
160
|
+
this.webviewId = webview.id;
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
get webview() {
|
|
166
|
+
console.log('getting webview for window: ', this.webviewId)
|
|
167
|
+
// todo (yoav): we don't want this to be undefined, so maybe we should just
|
|
168
|
+
// link directly to the browserview object instead of a getter
|
|
169
|
+
return BrowserView.getById(this.webviewId) as BrowserView<T>;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static getById(id: number) {
|
|
173
|
+
return BrowserWindowMap[id];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
setTitle(title: string) {
|
|
177
|
+
this.title = title;
|
|
178
|
+
return ffi.request.setTitle({ winId: this.id, title });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
close() {
|
|
182
|
+
return ffi.request.closeWindow({ winId: this.id });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// todo (yoav): move this to a class that also has off, append, prepend, etc.
|
|
186
|
+
// name should only allow browserWindow events
|
|
187
|
+
on(name, handler) {
|
|
188
|
+
const specificName = `${name}-${this.id}`;
|
|
189
|
+
electrobunEventEmitter.on(specificName, handler);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// TODO: have a context specific menu that excludes role
|
|
2
|
+
import { ffi, type ApplicationMenuItemConfig } from "../proc/native";
|
|
3
|
+
import electrobunEventEmitter from "../events/eventEmitter";
|
|
4
|
+
|
|
5
|
+
export const showContextMenu = (menu: Array<ApplicationMenuItemConfig>) => {
|
|
6
|
+
const menuWithDefaults = menuConfigWithDefaults(menu);
|
|
7
|
+
ffi.request.showContextMenu({
|
|
8
|
+
menuConfig: JSON.stringify(menuWithDefaults),
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const on = (name: "context-menu-clicked", handler) => {
|
|
13
|
+
const specificName = `${name}`;
|
|
14
|
+
electrobunEventEmitter.on(specificName, handler);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// todo: Consolidate Application menu, context menu, and tray menus can all have roles.
|
|
18
|
+
const roleLabelMap = {
|
|
19
|
+
quit: "Quit",
|
|
20
|
+
hide: "Hide",
|
|
21
|
+
hideOthers: "Hide Others",
|
|
22
|
+
showAll: "Show All",
|
|
23
|
+
undo: "Undo",
|
|
24
|
+
redo: "Redo",
|
|
25
|
+
cut: "Cut",
|
|
26
|
+
copy: "Copy",
|
|
27
|
+
paste: "Paste",
|
|
28
|
+
pasteAndMatchStyle: "Paste And Match Style",
|
|
29
|
+
delete: "Delete",
|
|
30
|
+
selectAll: "Select All",
|
|
31
|
+
startSpeaking: "Start Speaking",
|
|
32
|
+
stopSpeaking: "Stop Speaking",
|
|
33
|
+
enterFullScreen: "Enter FullScreen",
|
|
34
|
+
exitFullScreen: "Exit FullScreen",
|
|
35
|
+
toggleFullScreen: "Toggle Full Screen",
|
|
36
|
+
minimize: "Minimize",
|
|
37
|
+
zoom: "Zoom",
|
|
38
|
+
bringAllToFront: "Bring All To Front",
|
|
39
|
+
close: "Close",
|
|
40
|
+
cycleThroughWindows: "Cycle Through Windows",
|
|
41
|
+
showHelp: "Show Help",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const menuConfigWithDefaults = (
|
|
45
|
+
menu: Array<ApplicationMenuItemConfig>
|
|
46
|
+
): Array<ApplicationMenuItemConfig> => {
|
|
47
|
+
return menu.map((item) => {
|
|
48
|
+
if (item.type === "divider" || item.type === "separator") {
|
|
49
|
+
return { type: "divider" };
|
|
50
|
+
} else {
|
|
51
|
+
return {
|
|
52
|
+
label: item.label || roleLabelMap[item.role] || "",
|
|
53
|
+
type: item.type || "normal",
|
|
54
|
+
// application menus can either have an action or a role. not both.
|
|
55
|
+
...(item.role ? { role: item.role } : { action: item.action || "" }),
|
|
56
|
+
// default enabled to true unless explicitly set to false
|
|
57
|
+
enabled: item.enabled === false ? false : true,
|
|
58
|
+
checked: Boolean(item.checked),
|
|
59
|
+
hidden: Boolean(item.hidden),
|
|
60
|
+
tooltip: item.tooltip || undefined,
|
|
61
|
+
...(item.submenu
|
|
62
|
+
? { submenu: menuConfigWithDefaults(item.submenu) }
|
|
63
|
+
: {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
};
|