Skip to content

Commit d84baf5

Browse files
committed
refacotor: put apis properly
1 parent a6256ca commit d84baf5

4 files changed

Lines changed: 64 additions & 42 deletions

File tree

src-electron/main-app-ipc.js

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1+
/**
2+
* IPC handlers for electronAppAPI
3+
* Preload location: contextBridge.exposeInMainWorld('electronAppAPI', { ... })
4+
*/
5+
16
const { app, ipcMain } = require('electron');
27
const { spawn } = require('child_process');
38
const readline = require('readline');
4-
const path = require('path');
5-
const fs = require('fs');
69
const { productName } = require('./package.json');
710

811
let processInstanceId = 0;
912
// Map of instanceId -> { process, terminated }
1013
const spawnedProcesses = new Map();
1114

12-
// In-memory key-value store shared across all windows (mirrors Tauri's put_item/get_all_items)
13-
// Used for multi-window storage synchronization
14-
const sharedStorageMap = new Map();
15-
1615
function waitForTrue(fn, timeout) {
1716
return new Promise((resolve) => {
1817
const startTime = Date.now();
@@ -122,38 +121,6 @@ function registerAppIpcHandlers() {
122121
ipcMain.handle('get-app-name', () => {
123122
return productName;
124123
});
125-
126-
// Set zoom factor on the webview (mirrors Tauri's zoom_window)
127-
ipcMain.handle('zoom-window', (event, scaleFactor) => {
128-
event.sender.setZoomFactor(scaleFactor);
129-
});
130-
131-
// In-memory storage for multi-window sync (mirrors Tauri's put_item/get_all_items)
132-
ipcMain.handle('put-item', (event, key, value) => {
133-
sharedStorageMap.set(key, value);
134-
});
135-
136-
ipcMain.handle('get-all-items', () => {
137-
return Object.fromEntries(sharedStorageMap);
138-
});
139-
140-
// Get path to phnode binary
141-
ipcMain.handle('get-phnode-path', () => {
142-
const phNodePath = path.resolve(__dirname, 'bin', 'phnode');
143-
if (!fs.existsSync(phNodePath)) {
144-
throw new Error(`phnode binary does not exist: ${phNodePath}`);
145-
}
146-
return phNodePath;
147-
});
148-
149-
// Get path to src-node (for development)
150-
ipcMain.handle('get-src-node-path', () => {
151-
const srcNodePath = path.resolve(__dirname, '..', '..', 'phoenix', 'src-node');
152-
if (!fs.existsSync(srcNodePath)) {
153-
throw new Error(`src-node path does not exist: ${srcNodePath}`);
154-
}
155-
return srcNodePath;
156-
});
157124
}
158125

159126
module.exports = {

src-electron/main-fs-ipc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* IPC handlers for electronFSAPI
3+
* Preload location: contextBridge.exposeInMainWorld('electronFSAPI', { ... })
4+
*/
5+
16
const { ipcMain, dialog, BrowserWindow } = require('electron');
27
const path = require('path');
38
const fsp = require('fs/promises');

src-electron/main.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
const { app, BrowserWindow, protocol } = require('electron');
1+
const { app, BrowserWindow, protocol, Menu, ipcMain } = require('electron');
22
const path = require('path');
3+
const fs = require('fs');
34

45
const { registerAppIpcHandlers, terminateAllProcesses } = require('./main-app-ipc');
56
const { registerFsIpcHandlers, getAppDataDir } = require('./main-fs-ipc');
67

8+
// In-memory key-value store shared across all windows (mirrors Tauri's put_item/get_all_items)
9+
// Used for multi-window storage synchronization
10+
const sharedStorageMap = new Map();
11+
712
let mainWindow;
813

914
async function createWindow() {
@@ -21,9 +26,6 @@ async function createWindow() {
2126
// Load the test page from the http-server
2227
mainWindow.loadURL('http://localhost:8000/src/');
2328

24-
// Open DevTools for debugging
25-
mainWindow.webContents.openDevTools();
26-
2729
mainWindow.on('closed', () => {
2830
mainWindow = null;
2931
});
@@ -39,12 +41,57 @@ async function gracefulShutdown(exitCode = 0) {
3941
registerAppIpcHandlers();
4042
registerFsIpcHandlers();
4143

44+
/**
45+
* IPC handlers for electronAPI
46+
* Preload location: contextBridge.exposeInMainWorld('electronAPI', { ... })
47+
*/
48+
49+
// Set zoom factor on the webview (mirrors Tauri's zoom_window)
50+
ipcMain.handle('zoom-window', (event, scaleFactor) => {
51+
event.sender.setZoomFactor(scaleFactor);
52+
});
53+
54+
// In-memory storage for multi-window sync (mirrors Tauri's put_item/get_all_items)
55+
ipcMain.handle('put-item', (event, key, value) => {
56+
sharedStorageMap.set(key, value);
57+
});
58+
59+
ipcMain.handle('get-all-items', () => {
60+
return Object.fromEntries(sharedStorageMap);
61+
});
62+
63+
// Toggle DevTools
64+
ipcMain.handle('toggle-dev-tools', (event) => {
65+
event.sender.toggleDevTools();
66+
});
67+
68+
// Get path to phnode binary
69+
ipcMain.handle('get-phnode-path', () => {
70+
const phNodePath = path.resolve(__dirname, 'bin', 'phnode');
71+
if (!fs.existsSync(phNodePath)) {
72+
throw new Error(`phnode binary does not exist: ${phNodePath}`);
73+
}
74+
return phNodePath;
75+
});
76+
77+
// Get path to src-node (for development)
78+
ipcMain.handle('get-src-node-path', () => {
79+
const srcNodePath = path.resolve(__dirname, '..', '..', 'phoenix', 'src-node');
80+
if (!fs.existsSync(srcNodePath)) {
81+
throw new Error(`src-node path does not exist: ${srcNodePath}`);
82+
}
83+
return srcNodePath;
84+
});
85+
4286
// Handle quit request from renderer
4387
app.on('quit-requested', (exitCode) => {
4488
gracefulShutdown(exitCode);
4589
});
4690

4791
app.whenReady().then(async () => {
92+
// Remove default menu bar
93+
Menu.setApplicationMenu(null);
94+
4895
// Register asset:// protocol for serving local files from appLocalData/assets/
4996
const appDataDir = getAppDataDir();
5097
const assetsDir = path.join(appDataDir, 'assets');

src-electron/preload.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ contextBridge.exposeInMainWorld('electronAPI', {
7171
putItem: (key, value) => ipcRenderer.invoke('put-item', key, value),
7272
getAllItems: () => ipcRenderer.invoke('get-all-items'),
7373

74+
// Toggle DevTools
75+
toggleDevTools: () => ipcRenderer.invoke('toggle-dev-tools'),
76+
7477
// Path to phnode binary (src-electron/bin/phnode)
7578
getPhNodePath: () => ipcRenderer.invoke('get-phnode-path'),
7679

0 commit comments

Comments
 (0)