Skip to content

Commit

Permalink
feat: open file with quick look on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Nov 4, 2024
1 parent 4c26883 commit 3116dbb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/lib/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare module '@commas/electron-ipc' {
'stop-finding': (type: Parameters<WebContents['stopFindInPage']>[0]) => void,
'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,
Expand Down Expand Up @@ -208,6 +209,11 @@ function handleMessages() {
ipcMain.handle('show-file', (event, file) => {
shell.showItemInFolder(file)
})
ipcMain.handle('preview-file', (event, file) => {
const frame = BrowserWindow.fromWebContents(event.sender)
if (!frame) return
frame.previewFile(file)
})
ipcMain.handle('add-file', async (event, file) => {
const stat = await fs.promises.stat(file)
if (stat.isDirectory()) {
Expand Down
51 changes: 51 additions & 0 deletions src/renderer/compositions/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,58 @@ export async function confirmClosing() {
return response === 0
}

/**
* {@link https://en.wikipedia.org/wiki/Quick_Look#Supported_file_types_by_default}
*/
const QUICK_LOOK_EXTENSIONS = [
'.aac',
'.psd',
'.aiff',
'.icns',
'.avi',
'.bmp',
'.dae',
'.c4d',
'.gif',
// '.html',
'.ichat',
'.jpg',
'.jpeg',
'.jp2',
'.xls',
'.xlsx',
'.ppt',
'.pptx',
'.doc',
'.docx',
'.midi',
'.mp3',
'.mp4',
'.mpo',
'.pdf',
'.pictureclipping',
'.pict',
'.pct',
'.pic',
'.png',
'.mov',
'.movie',
'.qt',
'.rtf',
// '.svg',
// '.txt',
'.textclipping',
'.tiff',
'.wav',
]

export function openFile(file: string) {
if (process.platform === 'darwin') {
const ext = path.extname(file).toLowerCase()
if (QUICK_LOOK_EXTENSIONS.includes(ext)) {
return ipcRenderer.invoke('preview-file', file)
}
}
return globalHandler.invoke('global-renderer:open-file', file)
}

Expand Down

0 comments on commit 3116dbb

Please sign in to comment.