1+ import * as fs from 'node:fs'
12import * as path from 'node:path'
23import { effect , stop } from '@vue/reactivity'
34import 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
124126function 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
144166const webContentsViews = new Set < WebContentsView > ( )
145167
146168function 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}
0 commit comments