Skip to content

Commit ef9ca3a

Browse files
committed
fix: add file if available
1 parent f5ccbf1 commit ef9ca3a

File tree

5 files changed

+43
-39
lines changed

5 files changed

+43
-39
lines changed

src/main/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { app } from 'electron'
33
import * as commas from '../api/core-main'
44
import { handleA11yMessages } from './lib/a11y'
55
import { handleAddonMessages, loadAddons, loadCustomJS } from './lib/addon'
6-
import { getLastWindow, hasWindow, send } from './lib/frame'
76
import { handleI18nMessages, loadTranslations } from './lib/i18n'
87
import { createApplicationMenu, createDockMenu, handleMenuMessages, registerGlobalShortcuts } from './lib/menu'
98
import { handleMessages } from './lib/message'
109
import { handleSettingsMessages } from './lib/settings'
1110
import { handleTerminalMessages } from './lib/terminal'
1211
import { handleThemeMessages } from './lib/theme'
13-
import { createDefaultWindow, createWindow, handleWindowMessages, openFile } from './lib/window'
12+
import { createDefaultWindow, createWindow, handleWindowMessages, openFile, openURL } from './lib/window'
1413

1514
declare module '@commas/api/modules/app' {
1615
export interface Events {
@@ -66,12 +65,7 @@ app.on('will-finish-launching', () => {
6665
})
6766
app.on('open-url', async (event, url) => {
6867
event.preventDefault()
69-
await app.whenReady()
70-
if (!hasWindow()) {
71-
createWindow()
72-
}
73-
const frame = getLastWindow()
74-
send(frame.webContents, 'open-url', url)
68+
openURL(url)
7569
})
7670
})
7771

src/main/lib/message.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ declare module '@commas/electron-ipc' {
3030
'read-file': typeof readFile,
3131
'show-file': (file: string) => void,
3232
'preview-file': (file: string) => void,
33-
'add-file': (file: string) => void,
3433
'open-path': (uri: string) => void,
3534
'open-url': (uri: string) => void,
3635
'save-file': (name: string, content: Buffer | string) => Promise<void>,
@@ -214,14 +213,6 @@ function handleMessages() {
214213
if (!frame) return
215214
frame.previewFile(file)
216215
})
217-
ipcMain.handle('add-file', async (event, file) => {
218-
const stat = await fs.promises.stat(file)
219-
if (stat.isDirectory()) {
220-
send(event.sender, 'add-directory', file)
221-
} else {
222-
send(event.sender, 'add-file', file)
223-
}
224-
})
225216
ipcMain.handle('open-path', (event, uri) => {
226217
shell.openPath(uri)
227218
})

src/main/lib/window.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'node:fs'
12
import * as path from 'node:path'
23
import { effect, stop } from '@vue/reactivity'
34
import type { BrowserWindowConstructorOptions, Rectangle } from 'electron'
@@ -17,6 +18,7 @@ declare module '@commas/electron-ipc' {
1718
'global-main:open-window': (context?: Partial<TerminalContext>) => void,
1819
}
1920
export interface Commands {
21+
'open-file': (file: string) => void,
2022
'create-web-contents': (rect: Rectangle & { borderRadius?: number }) => number,
2123
'destroy-web-contents': (id: number) => void,
2224
'navigate-web-contents': (id: number, url: string) => void,
@@ -119,31 +121,55 @@ async function createWindow(args?: Partial<TerminalContext>) {
119121
return frame
120122
}
121123

122-
let cwd: string
124+
let defaultArgs: Parameters<typeof createWindow>[0]
123125

124126
function createDefaultWindow() {
125-
createWindow({ cwd })
127+
createWindow(defaultArgs)
126128
}
127129

128-
async function openFile(file: string) {
129-
await whenSettingsReady()
130-
const settings = useSettings()
131-
if (!app.isReady()) {
132-
cwd = file
133-
return
130+
async function openFile(file: string, frame?: BrowserWindow | null) {
131+
const stat = await fs.promises.stat(file)
132+
const isDirectory = stat.isDirectory()
133+
const args = isDirectory ? { cwd: file } : undefined
134+
if (!frame) {
135+
if (!app.isReady()) {
136+
defaultArgs = args
137+
if (args) return
138+
}
139+
await app.whenReady()
140+
await whenSettingsReady()
141+
const settings = useSettings()
142+
if (settings['terminal.external.openPathIn'] === 'new-window' || !hasWindow()) {
143+
frame = await createWindow(args)
144+
if (args) return
145+
} else {
146+
frame = getLastWindow()
147+
}
134148
}
135-
if (settings['terminal.external.openPathIn'] === 'new-window' || !hasWindow()) {
136-
createWindow({ cwd: file })
137-
return
149+
if (isDirectory) {
150+
send(frame.webContents, 'open-tab', args)
151+
} else {
152+
send(frame.webContents, 'add-file', file)
138153
}
139-
const frame = getLastWindow()
140-
send(frame.webContents, 'open-tab', { cwd: file })
154+
frame.show()
155+
}
156+
157+
async function openURL(url: string, frame?: BrowserWindow | null) {
158+
if (!frame) {
159+
await app.whenReady()
160+
frame = hasWindow() ? getLastWindow() : await createWindow()
161+
}
162+
send(frame.webContents, 'open-url', url)
141163
frame.show()
142164
}
143165

144166
const webContentsViews = new Set<WebContentsView>()
145167

146168
function handleWindowMessages() {
169+
ipcMain.handle('open-file', async (event, file) => {
170+
const frame = BrowserWindow.fromWebContents(event.sender)
171+
openFile(file, frame)
172+
})
147173
globalHandler.handle('global-main:open-window', (arg?: Partial<TerminalContext> | BrowserWindow) => {
148174
// Convert BrowserWindow from menu
149175
const context = arg && 'contentView' in arg ? undefined : arg
@@ -193,5 +219,6 @@ export {
193219
createWindow,
194220
createDefaultWindow,
195221
openFile,
222+
openURL,
196223
handleWindowMessages,
197224
}

src/renderer/compositions/shell.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ declare module '@commas/electron-ipc' {
1212
'toggle-tab-list': () => void,
1313
'before-quit': () => void,
1414
'add-file': (file: string) => void,
15-
'add-directory': (directory: string) => void,
1615
}
1716
export interface GlobalCommands {
1817
'global-renderer:open-file': (file: string) => void,
1918
'global-renderer:open-directory': (directory: string) => void,
2019
'global-renderer:show-directory': (directory: string) => void,
2120
'global-renderer:open-url': (url: string) => void,
2221
'global-renderer:add-file': (file: string) => void,
23-
'global-renderer:add-directory': (directory: string) => void,
2422
}
2523
}
2624

@@ -137,7 +135,7 @@ export function openFileExternally(file: string) {
137135
}
138136

139137
export async function addFile(file: string) {
140-
return ipcRenderer.invoke('add-file', file)
138+
return ipcRenderer.invoke('open-file', file)
141139
}
142140

143141
export function showDirectory(file: string) {
@@ -184,9 +182,6 @@ export function handleShellMessages() {
184182
globalHandler.invoke('global-renderer:add-file', file)
185183
}
186184
})
187-
ipcRenderer.on('add-directory', (event, directory) => {
188-
globalHandler.invoke('global-renderer:add-directory', directory)
189-
})
190185
globalHandler.handle('global-renderer:open-file', (file) => {
191186
return showFileExternally(file)
192187
})

src/renderer/compositions/terminal.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,9 +1123,6 @@ export function handleTerminalMessages() {
11231123
?? []
11241124
return count ? commands.slice(0 - count) : commands
11251125
})
1126-
globalHandler.handle('global-renderer:add-directory', (directory) => {
1127-
return createTerminalTab({ cwd: directory })
1128-
})
11291126
globalHandler.handle('global-renderer:add-file', (file) => {
11301127
// pass
11311128
})

0 commit comments

Comments
 (0)