mirror of
https://github.com/scratchfoundation/scratch-desktop.git
synced 2024-12-22 21:52:31 -05:00
Merge pull request #27 from cwillisf/native-save-dialog
Override browser save dialog with a native save dialog
This commit is contained in:
commit
94822649f8
2 changed files with 121 additions and 0 deletions
102
src/main/FileFilters.js
Normal file
102
src/main/FileFilters.js
Normal file
|
@ -0,0 +1,102 @@
|
|||
const saveFilters = {
|
||||
JPEG: {
|
||||
name: 'JPEG Image',
|
||||
extensions: ['jpg', 'jpeg']
|
||||
},
|
||||
MP3: {
|
||||
name: 'MP3 Sound',
|
||||
extensions: ['mp3']
|
||||
},
|
||||
PNG: {
|
||||
name: 'PNG Image',
|
||||
extensions: ['png']
|
||||
},
|
||||
SB: {
|
||||
name: 'Scratch 1 Project',
|
||||
extensions: ['sb']
|
||||
},
|
||||
SB2: {
|
||||
name: 'Scratch 2 Project',
|
||||
extensions: ['sb2']
|
||||
},
|
||||
SB3: {
|
||||
name: 'Scratch 3 Project',
|
||||
extensions: ['sb3']
|
||||
},
|
||||
Sprite2: {
|
||||
name: 'Scratch 2 Sprite',
|
||||
extensions: ['sprite2']
|
||||
},
|
||||
Sprite3: {
|
||||
name: 'Scratch 3 Sprite',
|
||||
extensions: ['sprite3']
|
||||
},
|
||||
SVG: {
|
||||
name: 'SVG Image',
|
||||
extensions: ['svg']
|
||||
},
|
||||
WAV: {
|
||||
name: 'WAV Sound',
|
||||
extensions: ['wav']
|
||||
}
|
||||
};
|
||||
|
||||
const loadFilters = {
|
||||
...saveFilters,
|
||||
AllBitmaps: {
|
||||
name: 'All Bitmaps',
|
||||
extensions: [
|
||||
...saveFilters.JPEG.extensions,
|
||||
...saveFilters.PNG.extensions
|
||||
]
|
||||
},
|
||||
AllImages: {
|
||||
name: 'All Images',
|
||||
extensions: [
|
||||
...saveFilters.JPEG.extensions,
|
||||
...saveFilters.PNG.extensions,
|
||||
...saveFilters.SVG.extensions
|
||||
]
|
||||
},
|
||||
AllProjects: {
|
||||
name: 'All Scratch Projects',
|
||||
extensions: [
|
||||
...saveFilters.SB3.extensions,
|
||||
...saveFilters.SB2.extensions,
|
||||
...saveFilters.SB.extensions
|
||||
]
|
||||
},
|
||||
AllSounds: {
|
||||
name: 'All Sounds',
|
||||
extensions: [
|
||||
...saveFilters.MP3.extensions,
|
||||
...saveFilters.WAV.extensions
|
||||
]
|
||||
},
|
||||
AllSprites: {
|
||||
name: 'All Sprites',
|
||||
extensions: [
|
||||
...saveFilters.Sprite3.extensions,
|
||||
...saveFilters.Sprite2.extensions
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
const filtersByExtension = Object.values(saveFilters).reduce((result, filter) => {
|
||||
for (const extension of filter.extensions) {
|
||||
result[extension] = filter;
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
|
||||
const getFilterForExtension = extNameNoDot =>
|
||||
filtersByExtension[extNameNoDot] || {
|
||||
name: `${extNameNoDot.toUpperCase()} Files`,
|
||||
extensions: [extNameNoDot]
|
||||
};
|
||||
|
||||
export {
|
||||
saveFilters,
|
||||
loadFilters,
|
||||
getFilterForExtension
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
import {BrowserWindow, Menu, app, dialog} from 'electron';
|
||||
import * as path from 'path';
|
||||
import {format as formatUrl} from 'url';
|
||||
import {getFilterForExtension} from './FileFilters';
|
||||
import telemetry from './ScratchDesktopTelemetry';
|
||||
import MacOSMenu from './MacOSMenu';
|
||||
|
||||
|
@ -54,6 +55,24 @@ const createMainWindow = () => {
|
|||
}));
|
||||
}
|
||||
|
||||
webContents.session.on('will-download', (ev, item) => {
|
||||
const itemPath = item.getFilename();
|
||||
const baseName = path.basename(itemPath);
|
||||
const extName = path.extname(baseName);
|
||||
if (extName) {
|
||||
const extNameNoDot = extName.replace(/^\./, '');
|
||||
const options = {
|
||||
filters: [getFilterForExtension(extNameNoDot)]
|
||||
};
|
||||
const userChosenPath = dialog.showSaveDialog(window, options);
|
||||
if (userChosenPath) {
|
||||
item.setSavePath(userChosenPath);
|
||||
} else {
|
||||
item.cancel();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
webContents.on('will-prevent-unload', ev => {
|
||||
const choice = dialog.showMessageBox(window, {
|
||||
type: 'question',
|
||||
|
|
Loading…
Reference in a new issue