-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
290 lines (244 loc) · 8.07 KB
/
main.js
File metadata and controls
290 lines (244 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const {
app,
BaseWindow,
WebContentsView,
ipcMain,
globalShortcut
} = require('electron')
const path = require('path')
// ─── State ───────────────────────────────────────────────────────────
let mainWindow = null
const panes = [] // Array of { id, view, partition }
let activePaneIndex = 0 // Which pane has focus
let dividerRatio = 0.5 // 0.0–1.0, where the split sits
const TOOLBAR_HEIGHT = 44
const CLAUDE_URL = 'https://claude.ai'
const MIN_PANE_RATIO = 0.2
const MAX_PANE_RATIO = 0.8
// No custom user-agent — just let Electron be Electron, like the official Claude Desktop app
// ─── Window Creation ─────────────────────────────────────────────────
function createMainWindow () {
mainWindow = new BaseWindow({
width: 1400,
height: 900,
minWidth: 800,
minHeight: 500,
title: 'Claude SplitView',
backgroundColor: '#1a1a2e',
show: false
})
// Toolbar view (the thin bar at the top with controls)
const toolbarView = new WebContentsView({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
})
mainWindow.contentView.addChildView(toolbarView)
toolbarView.webContents.loadFile(path.join(__dirname, 'renderer', 'index.html'))
// Store toolbar reference for layout calculations
mainWindow.toolbarView = toolbarView
// Create the initial two panes
createPane('pane-1')
createPane('pane-2')
// Initial layout
updateLayout()
// Recalculate layout on window resize
mainWindow.on('resize', () => updateLayout())
// Show window once toolbar has loaded
toolbarView.webContents.on('did-finish-load', () => {
mainWindow.show()
focusPane(0)
})
mainWindow.on('closed', () => {
mainWindow = null
})
}
// ─── Pane Management ─────────────────────────────────────────────────
function createPane (partitionName) {
// No custom partition — use default session just like the official Claude Desktop app.
// All panes share one session, so you log in once and all panes are authenticated.
const view = new WebContentsView({
webPreferences: {
contextIsolation: true,
sandbox: false
}
})
mainWindow.contentView.addChildView(view)
view.webContents.loadURL(CLAUDE_URL)
// Handle popups: let auth popups open naturally (preserves window.opener
// so claude.ai's OAuth callback can communicate back to the pane).
// Non-auth links open in the system browser.
view.webContents.setWindowOpenHandler(({ url }) => {
try {
const hostname = new URL(url).hostname
const isAuth = ['accounts.google.com', 'appleid.apple.com', 'claude.ai',
'auth.claude.ai', 'anthropic.com'].some(d => hostname.includes(d))
if (isAuth) {
// Let Electron open it as a real popup — this preserves window.opener
return {
action: 'allow',
overrideBrowserWindowOptions: {
width: 500,
height: 700,
title: 'Sign in to Claude',
autoHideMenuBar: true
}
}
}
} catch {}
require('electron').shell.openExternal(url)
return { action: 'deny' }
})
const pane = {
id: partitionName,
view
}
panes.push(pane)
// Notify toolbar of pane count change
sendToToolbar('pane-count-changed', panes.length)
return pane
}
// No custom auth window needed — Electron handles OAuth popups natively
// with { action: 'allow' }, preserving window.opener for the callback
// ─── Pane Removal / Focus ────────────────────────────────────────────
function removePane (index) {
if (panes.length <= 1) return
const pane = panes[index]
mainWindow.contentView.removeChildView(pane.view)
pane.view.webContents.close()
panes.splice(index, 1)
if (activePaneIndex >= panes.length) {
activePaneIndex = panes.length - 1
}
dividerRatio = 1 / panes.length
updateLayout()
focusPane(activePaneIndex)
sendToToolbar('pane-count-changed', panes.length)
}
function focusPane (index) {
if (index < 0 || index >= panes.length) return
activePaneIndex = index
panes[index].view.webContents.focus()
sendToToolbar('active-pane-changed', index)
}
// ─── Layout Engine ───────────────────────────────────────────────────
function updateLayout () {
if (!mainWindow) return
const bounds = mainWindow.getContentBounds()
const width = bounds.width
const height = bounds.height
if (mainWindow.toolbarView) {
mainWindow.toolbarView.setBounds({
x: 0,
y: 0,
width: width,
height: TOOLBAR_HEIGHT
})
}
const availableHeight = height - TOOLBAR_HEIGHT
const paneCount = panes.length
if (paneCount === 1) {
panes[0].view.setBounds({
x: 0,
y: TOOLBAR_HEIGHT,
width: width,
height: availableHeight
})
} else if (paneCount === 2) {
const leftWidth = Math.floor(width * dividerRatio)
const rightWidth = width - leftWidth
panes[0].view.setBounds({
x: 0,
y: TOOLBAR_HEIGHT,
width: leftWidth - 2,
height: availableHeight
})
panes[1].view.setBounds({
x: leftWidth + 2,
y: TOOLBAR_HEIGHT,
width: rightWidth - 2,
height: availableHeight
})
} else {
const paneWidth = Math.floor(width / paneCount)
panes.forEach((pane, i) => {
pane.view.setBounds({
x: i * paneWidth + (i > 0 ? 2 : 0),
y: TOOLBAR_HEIGHT,
width: paneWidth - 4,
height: availableHeight
})
})
}
}
// ─── IPC Handlers ────────────────────────────────────────────────────
function sendToToolbar (channel, data) {
if (mainWindow && mainWindow.toolbarView) {
mainWindow.toolbarView.webContents.send(channel, data)
}
}
ipcMain.on('add-pane', () => {
if (panes.length >= 4) return
const newId = `pane-${Date.now()}`
createPane(newId)
dividerRatio = 1 / panes.length
updateLayout()
focusPane(panes.length - 1)
})
ipcMain.on('remove-pane', (_event, index) => {
removePane(index)
})
ipcMain.on('close-active-pane', () => {
removePane(activePaneIndex)
})
ipcMain.on('focus-pane', (_event, index) => {
focusPane(index)
})
ipcMain.on('update-split', (_event, ratio) => {
dividerRatio = Math.max(MIN_PANE_RATIO, Math.min(MAX_PANE_RATIO, ratio))
updateLayout()
})
ipcMain.on('reset-split', () => {
dividerRatio = 0.5
updateLayout()
})
ipcMain.on('reload-pane', (_event, index) => {
if (panes[index]) {
panes[index].view.webContents.loadURL(CLAUDE_URL)
}
})
ipcMain.handle('get-pane-count', () => panes.length)
ipcMain.handle('get-active-pane', () => activePaneIndex)
// ─── App Lifecycle ───────────────────────────────────────────────────
app.whenReady().then(() => {
createMainWindow()
// Keyboard shortcuts
for (let i = 1; i <= 4; i++) {
globalShortcut.register(`CommandOrControl+${i}`, () => focusPane(i - 1))
}
globalShortcut.register('CommandOrControl+N', () => {
if (panes.length < 4) {
const newId = `pane-${Date.now()}`
createPane(newId)
dividerRatio = 1 / panes.length
updateLayout()
focusPane(panes.length - 1)
}
})
globalShortcut.register('CommandOrControl+W', () => {
removePane(activePaneIndex)
})
globalShortcut.register('CommandOrControl+\\', () => {
dividerRatio = 0.5
updateLayout()
})
})
app.on('window-all-closed', () => {
globalShortcut.unregisterAll()
app.quit()
})
app.on('will-quit', () => {
globalShortcut.unregisterAll()
})