Skip to content

Commit

Permalink
Associate .chn with chaiNNer on MacOS (chaiNNer-org#2115)
Browse files Browse the repository at this point in the history
associate .chn with chaiNNer on macOS

- added association of .chn filetype with chainner on macOS
- added file type icon
- added open-file event for macOS

This addresses chaiNNer-org#1062 but does not solve it completely, since file association only works on macOS ATM.
  • Loading branch information
stonerl authored Aug 21, 2023
1 parent 3d763ed commit 64136a2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
4 changes: 2 additions & 2 deletions forge.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ const deletePycFiles = async (directory) => {
const config = {
packagerConfig: {
executableName: process.platform === 'linux' ? 'chainner' : 'chaiNNer',
extraResource: './backend/src/',
extraResource: ['./backend/src/', './src/public/icons/mac/file_chn.icns'],
icon: './src/public/icons/cross_platform/icon',
appBundleId: 'app.chainner',
appCategoryType: 'public.app-category.graphics-design',
extendInfo: { LSMinimumSystemVersion: '11.0.0' },
extendInfo: './src/public/Info.plist',
},
publishers: [
{
Expand Down
50 changes: 32 additions & 18 deletions src/main/gui/main-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import EventSource from 'eventsource';
import { t } from 'i18next';
import { BackendEventMap } from '../../common/Backend';
import { Version, WindowSize } from '../../common/common-types';
import { isMac } from '../../common/env';
import { log } from '../../common/log';
import { BrowserWindowWithSafeIpc, ipcMain } from '../../common/safeIpc';
import { SaveFile, openSaveFile } from '../../common/SaveFile';
Expand Down Expand Up @@ -125,26 +126,39 @@ const registerEventHandlerPreSetup = (
mainWindow.webContents.send('window-blur');
});

// Opening file with chaiNNer
if (args.file) {
const result = openSaveFile(args.file);
ipcMain.handle('get-cli-open', () => result);
if (isMac) {
// Open file with chaiNNer on macOS
app.on('open-file', (event, filePath) => {
(async () => {
const file = filePath;
if (file) {
const result = await openSaveFile(file);
mainWindow.webContents.send('file-open', result);
}
})().catch(log.error);
});
} else {
ipcMain.handle('get-cli-open', () => undefined);
}
// Opening file with chaiNNer on other platforms
if (args.file) {
const result = openSaveFile(args.file);
ipcMain.handle('get-cli-open', () => result);
} else {
ipcMain.handle('get-cli-open', () => undefined);
}

app.on('second-instance', (_event, commandLine) => {
(async () => {
const { file } = parseArgs(commandLine.slice(app.isPackaged ? 2 : 3));
if (file) {
const result = await openSaveFile(file);
mainWindow.webContents.send('file-open', result);
}
// Focus main window if a second instance was attempted
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
})().catch(log.error);
});
app.on('second-instance', (_event, commandLine) => {
(async () => {
const { file } = parseArgs(commandLine.slice(app.isPackaged ? 2 : 3));
if (file) {
const result = await openSaveFile(file);
mainWindow.webContents.send('file-open', result);
}
// Focus main window if a second instance was attempted
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
})().catch(log.error);
});
}
};

const registerEventHandlerPostSetup = (
Expand Down
26 changes: 26 additions & 0 deletions src/public/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSMinimumSystemVersion</key>
<string>11.0.0</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>chn</string>
<string>CHN</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>file_chn.icns</string>
<key>CFBundleTypeName</key>
<string>chaiNNer Chain File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Default</string>
</dict>
</array>
</dict>
</plist>
Binary file added src/public/icons/mac/file_chn.icns
Binary file not shown.
Binary file modified src/public/icons/mac/icon.icns
Binary file not shown.

0 comments on commit 64136a2

Please sign in to comment.