Skip to content

Commit

Permalink
fix: add file if available
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Nov 4, 2024
1 parent f5ccbf1 commit ef9ca3a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
10 changes: 2 additions & 8 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { app } from 'electron'
import * as commas from '../api/core-main'
import { handleA11yMessages } from './lib/a11y'
import { handleAddonMessages, loadAddons, loadCustomJS } from './lib/addon'
import { getLastWindow, hasWindow, send } from './lib/frame'
import { handleI18nMessages, loadTranslations } from './lib/i18n'
import { createApplicationMenu, createDockMenu, handleMenuMessages, registerGlobalShortcuts } from './lib/menu'
import { handleMessages } from './lib/message'
import { handleSettingsMessages } from './lib/settings'
import { handleTerminalMessages } from './lib/terminal'
import { handleThemeMessages } from './lib/theme'
import { createDefaultWindow, createWindow, handleWindowMessages, openFile } from './lib/window'
import { createDefaultWindow, createWindow, handleWindowMessages, openFile, openURL } from './lib/window'

declare module '@commas/api/modules/app' {
export interface Events {
Expand Down Expand Up @@ -66,12 +65,7 @@ app.on('will-finish-launching', () => {
})
app.on('open-url', async (event, url) => {
event.preventDefault()
await app.whenReady()
if (!hasWindow()) {
createWindow()
}
const frame = getLastWindow()
send(frame.webContents, 'open-url', url)
openURL(url)
})
})

Expand Down
9 changes: 0 additions & 9 deletions src/main/lib/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ declare module '@commas/electron-ipc' {
'read-file': typeof readFile,
'show-file': (file: string) => void,
'preview-file': (file: string) => void,
'add-file': (file: string) => void,
'open-path': (uri: string) => void,
'open-url': (uri: string) => void,
'save-file': (name: string, content: Buffer | string) => Promise<void>,
Expand Down Expand Up @@ -214,14 +213,6 @@ function handleMessages() {
if (!frame) return
frame.previewFile(file)
})
ipcMain.handle('add-file', async (event, file) => {
const stat = await fs.promises.stat(file)
if (stat.isDirectory()) {
send(event.sender, 'add-directory', file)
} else {
send(event.sender, 'add-file', file)
}
})
ipcMain.handle('open-path', (event, uri) => {
shell.openPath(uri)
})
Expand Down
53 changes: 40 additions & 13 deletions src/main/lib/window.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'node:fs'
import * as path from 'node:path'
import { effect, stop } from '@vue/reactivity'
import type { BrowserWindowConstructorOptions, Rectangle } from 'electron'
Expand All @@ -17,6 +18,7 @@ declare module '@commas/electron-ipc' {
'global-main:open-window': (context?: Partial<TerminalContext>) => void,
}
export interface Commands {
'open-file': (file: string) => void,
'create-web-contents': (rect: Rectangle & { borderRadius?: number }) => number,
'destroy-web-contents': (id: number) => void,
'navigate-web-contents': (id: number, url: string) => void,
Expand Down Expand Up @@ -119,31 +121,55 @@ async function createWindow(args?: Partial<TerminalContext>) {
return frame
}

let cwd: string
let defaultArgs: Parameters<typeof createWindow>[0]

function createDefaultWindow() {
createWindow({ cwd })
createWindow(defaultArgs)
}

async function openFile(file: string) {
await whenSettingsReady()
const settings = useSettings()
if (!app.isReady()) {
cwd = file
return
async function openFile(file: string, frame?: BrowserWindow | null) {
const stat = await fs.promises.stat(file)
const isDirectory = stat.isDirectory()
const args = isDirectory ? { cwd: file } : undefined
if (!frame) {
if (!app.isReady()) {
defaultArgs = args
if (args) return
}
await app.whenReady()
await whenSettingsReady()
const settings = useSettings()
if (settings['terminal.external.openPathIn'] === 'new-window' || !hasWindow()) {
frame = await createWindow(args)
if (args) return
} else {
frame = getLastWindow()
}
}
if (settings['terminal.external.openPathIn'] === 'new-window' || !hasWindow()) {
createWindow({ cwd: file })
return
if (isDirectory) {
send(frame.webContents, 'open-tab', args)
} else {
send(frame.webContents, 'add-file', file)
}
const frame = getLastWindow()
send(frame.webContents, 'open-tab', { cwd: file })
frame.show()
}

async function openURL(url: string, frame?: BrowserWindow | null) {
if (!frame) {
await app.whenReady()
frame = hasWindow() ? getLastWindow() : await createWindow()
}
send(frame.webContents, 'open-url', url)
frame.show()
}

const webContentsViews = new Set<WebContentsView>()

function handleWindowMessages() {
ipcMain.handle('open-file', async (event, file) => {
const frame = BrowserWindow.fromWebContents(event.sender)
openFile(file, frame)
})
globalHandler.handle('global-main:open-window', (arg?: Partial<TerminalContext> | BrowserWindow) => {
// Convert BrowserWindow from menu
const context = arg && 'contentView' in arg ? undefined : arg
Expand Down Expand Up @@ -193,5 +219,6 @@ export {
createWindow,
createDefaultWindow,
openFile,
openURL,
handleWindowMessages,
}
7 changes: 1 addition & 6 deletions src/renderer/compositions/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ declare module '@commas/electron-ipc' {
'toggle-tab-list': () => void,
'before-quit': () => void,
'add-file': (file: string) => void,
'add-directory': (directory: string) => void,
}
export interface GlobalCommands {
'global-renderer:open-file': (file: string) => void,
'global-renderer:open-directory': (directory: string) => void,
'global-renderer:show-directory': (directory: string) => void,
'global-renderer:open-url': (url: string) => void,
'global-renderer:add-file': (file: string) => void,
'global-renderer:add-directory': (directory: string) => void,
}
}

Expand Down Expand Up @@ -137,7 +135,7 @@ export function openFileExternally(file: string) {
}

export async function addFile(file: string) {
return ipcRenderer.invoke('add-file', file)
return ipcRenderer.invoke('open-file', file)
}

export function showDirectory(file: string) {
Expand Down Expand Up @@ -184,9 +182,6 @@ export function handleShellMessages() {
globalHandler.invoke('global-renderer:add-file', file)
}
})
ipcRenderer.on('add-directory', (event, directory) => {
globalHandler.invoke('global-renderer:add-directory', directory)
})
globalHandler.handle('global-renderer:open-file', (file) => {
return showFileExternally(file)
})
Expand Down
3 changes: 0 additions & 3 deletions src/renderer/compositions/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,9 +1123,6 @@ export function handleTerminalMessages() {
?? []
return count ? commands.slice(0 - count) : commands
})
globalHandler.handle('global-renderer:add-directory', (directory) => {
return createTerminalTab({ cwd: directory })
})
globalHandler.handle('global-renderer:add-file', (file) => {
// pass
})
Expand Down

0 comments on commit ef9ca3a

Please sign in to comment.