Skip to content

Commit

Permalink
Update app.js (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jun 14, 2024
1 parent f56be8e commit 4954cd4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 48 deletions.
71 changes: 42 additions & 29 deletions source/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,27 @@ app.Application = class {
this._dropPaths(event.sender, paths);
event.returnValue = null;
});
electron.ipcMain.on('show-save-dialog', (event, options) => {
electron.ipcMain.on('show-save-dialog', async (event, options) => {
const owner = event.sender.getOwnerBrowserWindow();
event.returnValue = electron.dialog.showSaveDialogSync(owner, options);
const argument = {};
try {
const { filePath, canceled } = await electron.dialog.showSaveDialog(owner, options);
argument.filePath = filePath;
argument.canceled = canceled;
} catch (error) {
argument.error = error.message;
}
event.sender.send('show-save-dialog-complete', argument);
});
electron.ipcMain.on('execute', (event, data) => {
electron.ipcMain.on('execute', async (event, data) => {
const owner = event.sender.getOwnerBrowserWindow();
this.execute(data.name, data.value || null, owner);
event.returnValue = null;
const argument = {};
try {
argument.value = await this.execute(data.name, data.value || null, owner);
} catch (error) {
argument.error = error.message;
}
event.sender.send('execute-complete', argument);
});

electron.app.on('will-finish-launching', () => {
Expand Down Expand Up @@ -222,7 +235,7 @@ app.Application = class {
}
}

_export() {
async _export() {
const view = this._views.activeView;
if (view && view.path) {
let defaultPath = 'Untitled';
Expand All @@ -241,17 +254,17 @@ app.Application = class {
{ name: 'SVG', extensions: ['svg'] }
]
};
const selectedFile = electron.dialog.showSaveDialogSync(owner, options);
if (selectedFile) {
view.execute('export', { 'file': selectedFile });
const { filePath, canceled } = await electron.dialog.showSaveDialog(owner, options);
if (filePath && !canceled) {
view.execute('export', { 'file': filePath });
}
}
}

execute(command, value, window) {
async execute(command, value, window) {
switch (command) {
case 'open': this._open(value); break;
case 'export': this._export(); break;
case 'export': await this._export(); break;
case 'close': window.close(); break;
case 'quit': electron.app.quit(); break;
case 'reload': this._reload(); break;
Expand Down Expand Up @@ -397,7 +410,7 @@ app.Application = class {
id: 'file.export',
label: '&Export...',
accelerator: 'CmdOrCtrl+Shift+E',
click: () => this.execute('export', null)
click: async () => await this.execute('export', null)
},
{ type: 'separator' },
{ role: 'close' },
Expand All @@ -423,32 +436,32 @@ app.Application = class {
id: 'edit.cut',
label: 'Cu&t',
accelerator: 'CmdOrCtrl+X',
click: () => this.execute('cut', null),
click: async () => await this.execute('cut', null),
},
{
id: 'edit.copy',
label: '&Copy',
accelerator: 'CmdOrCtrl+C',
click: () => this.execute('copy', null),
click: async () => await this.execute('copy', null),
},
{
id: 'edit.paste',
label: '&Paste',
accelerator: 'CmdOrCtrl+V',
click: () => this.execute('paste', null),
click: async () => await this.execute('paste', null),
},
{
id: 'edit.select-all',
label: 'Select &All',
accelerator: 'CmdOrCtrl+A',
click: () => this.execute('selectall', null),
click: async () => await this.execute('selectall', null),
},
{ type: 'separator' },
{
id: 'edit.find',
label: '&Find...',
accelerator: 'CmdOrCtrl+F',
click: () => this.execute('find', null),
click: async () => await this.execute('find', null),
}
]
});
Expand All @@ -459,60 +472,60 @@ app.Application = class {
{
id: 'view.toggle-attributes',
accelerator: 'CmdOrCtrl+D',
click: () => this.execute('toggle', 'attributes'),
click: async () => await this.execute('toggle', 'attributes'),
},
{
id: 'view.toggle-weights',
accelerator: 'CmdOrCtrl+I',
click: () => this.execute('toggle', 'weights'),
click: async () => await this.execute('toggle', 'weights'),
},
{
id: 'view.toggle-names',
accelerator: 'CmdOrCtrl+U',
click: () => this.execute('toggle', 'names'),
click: async () => await this.execute('toggle', 'names'),
},
{
id: 'view.toggle-direction',
accelerator: 'CmdOrCtrl+K',
click: () => this.execute('toggle', 'direction')
click: async () => await this.execute('toggle', 'direction')
},
{
id: 'view.toggle-mousewheel',
accelerator: 'CmdOrCtrl+M',
click: () => this.execute('toggle', 'mousewheel'),
click: async () => await this.execute('toggle', 'mousewheel'),
},
{ type: 'separator' },
{
id: 'view.reload',
label: '&Reload',
accelerator: darwin ? 'Cmd+R' : 'F5',
click: () => this._reload(),
click: async () => await this._reload(),
},
{ type: 'separator' },
{
id: 'view.reset-zoom',
label: 'Actual &Size',
accelerator: 'Shift+Backspace',
click: () => this.execute('reset-zoom', null),
click: async () => await this.execute('reset-zoom', null),
},
{
id: 'view.zoom-in',
label: 'Zoom &In',
accelerator: 'Shift+Up',
click: () => this.execute('zoom-in', null),
click: async () => await this.execute('zoom-in', null),
},
{
id: 'view.zoom-out',
label: 'Zoom &Out',
accelerator: 'Shift+Down',
click: () => this.execute('zoom-out', null),
click: async () => await this.execute('zoom-out', null),
},
{ type: 'separator' },
{
id: 'view.show-properties',
label: '&Properties...',
accelerator: 'CmdOrCtrl+Enter',
click: () => this.execute('show-properties', null),
click: async () => await this.execute('show-properties', null),
}
]
};
Expand All @@ -537,15 +550,15 @@ app.Application = class {
const helpSubmenu = [
{
label: 'Report &Issue',
click: () => this.execute('report-issue', null)
click: async () => await this.execute('report-issue', null)
}
];

if (!darwin) {
helpSubmenu.push({ type: 'separator' });
helpSubmenu.push({
label: `&About ${electron.app.name}`,
click: () => this.execute('about', null)
click: async () => await this.execute('about', null)
});
}

Expand Down
2 changes: 1 addition & 1 deletion source/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ host.BrowserHost = class {
this.document.body.removeChild(element);
}

execute(name /*, value */) {
async execute(name /*, value */) {
switch (name) {
case 'open': {
const openFileDialog = this._element('open-file-dialog');
Expand Down
38 changes: 29 additions & 9 deletions source/electron.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ host.ElectronHost = class {
electron.ipcRenderer.sendSync('update-window-state', {});
const openFileButton = this._element('open-file-button');
if (openFileButton) {
openFileButton.addEventListener('click', () => {
this.execute('open');
openFileButton.addEventListener('click', async () => {
await this.execute('open');
});
}
this.document.addEventListener('dragover', (e) => {
Expand Down Expand Up @@ -250,11 +250,22 @@ host.ElectronHost = class {
}

async save(name, extension, defaultPath) {
return electron.ipcRenderer.sendSync('show-save-dialog', {
title: 'Export Tensor',
defaultPath,
buttonLabel: 'Export',
filters: [{ name, extensions: [extension] }]
return new Promise((resolve, reject) => {
electron.ipcRenderer.once('show-save-dialog-complete', (event, data) => {
if (data.error) {
reject(new Error(data.error));
} else if (data.canceled) {
resolve(null);
} else {
resolve(data.filePath);
}
});
electron.ipcRenderer.send('show-save-dialog', {
title: 'Export Tensor',
defaultPath,
buttonLabel: 'Export',
filters: [{ name, extensions: [extension] }]
});
});
}

Expand All @@ -281,8 +292,17 @@ host.ElectronHost = class {
}
}

execute(name, value) {
electron.ipcRenderer.send('execute', { name, value });
async execute(name, value) {
return new Promise((resolve, reject) => {
electron.ipcRenderer.once('execute-complete', (event, data) => {
if (data.error) {
reject(new Error(data.error));
} else {
resolve(data.value);
}
});
electron.ipcRenderer.send('execute', { name, value });
});
}

async request(file, encoding, basename) {
Expand Down
18 changes: 9 additions & 9 deletions source/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ view.View = class {
this._menu = new view.Menu(this._host);
this._menu.add({
accelerator: platform === 'darwin' ? 'Ctrl+Cmd+F' : 'F11',
execute: () => this._host.execute('fullscreen')
execute: async () => await this._host.execute('fullscreen')
});
this._menu.add({
accelerator: 'Backspace',
Expand All @@ -90,25 +90,25 @@ view.View = class {
file.add({
label: '&Open...',
accelerator: 'CmdOrCtrl+O',
execute: () => this._host.execute('open')
execute: async () => await this._host.execute('open')
});
if (this._host.type === 'Electron') {
this._recents = file.group('Open &Recent');
file.add({
label: '&Export...',
accelerator: 'CmdOrCtrl+Shift+E',
execute: () => this._host.execute('export'),
execute: async () => await this._host.execute('export'),
enabled: () => this.activeGraph
});
file.add({
label: platform === 'darwin' ? '&Close Window' : '&Close',
accelerator: 'CmdOrCtrl+W',
execute: () => this._host.execute('close'),
execute: async () => await this._host.execute('close'),
});
file.add({
label: platform === 'win32' ? 'E&xit' : '&Quit',
accelerator: platform === 'win32' ? '' : 'CmdOrCtrl+Q',
execute: () => this._host.execute('quit'),
execute: async () => await this._host.execute('quit'),
});
} else {
file.add({
Expand Down Expand Up @@ -167,7 +167,7 @@ view.View = class {
view.add({
label: '&Reload',
accelerator: platform === 'darwin' ? 'CmdOrCtrl+R' : 'F5',
execute: () => this._host.execute('reload'),
execute: async () => await this._host.execute('reload'),
enabled: () => this.activeGraph
});
view.add({});
Expand Down Expand Up @@ -202,17 +202,17 @@ view.View = class {
view.add({
label: '&Developer Tools...',
accelerator: 'CmdOrCtrl+Alt+I',
execute: () => this._host.execute('toggle-developer-tools')
execute: async () => await this._host.execute('toggle-developer-tools')
});
}
const help = this._menu.group('&Help');
help.add({
label: 'Report &Issue',
execute: () => this._host.execute('report-issue')
execute: async () => await this._host.execute('report-issue')
});
help.add({
label: `&About ${this._host.environment('name')}`,
execute: () => this._host.execute('about')
execute: async () => await this._host.execute('about')
});
}
await this._host.start();
Expand Down

0 comments on commit 4954cd4

Please sign in to comment.