File tree Expand file tree Collapse file tree 5 files changed +36
-7
lines changed Expand file tree Collapse file tree 5 files changed +36
-7
lines changed Original file line number Diff line number Diff line change @@ -4,12 +4,12 @@ const path = require('path')
44
55async function openFolderDialog ( win ) {
66 // https://stackoverflow.com/questions/46027287/electron-open-folder-dialog
7- let dir = await dialog . showOpenDialog ( win , { properties : [ 'openDirectory' ] } )
7+ const dir = await dialog . showOpenDialog ( win , { properties : [ 'openDirectory' ] } )
88 return dir . filePaths [ 0 ] || null
99}
1010
1111function listFolder ( folder ) {
12- files = fs . readdirSync ( path . resolve ( folder ) )
12+ let files = fs . readdirSync ( path . resolve ( folder ) )
1313 // Filter out directories
1414 files = files . filter ( f => {
1515 let filePath = path . resolve ( folder , f )
@@ -38,7 +38,7 @@ function ilistFolder(folder) {
3838
3939function getAllFiles ( dirPath , arrayOfFiles ) {
4040 // https://coderrocketfuel.com/article/recursively-list-all-the-files-in-a-directory-using-node-js
41- files = ilistFolder ( dirPath )
41+ let files = ilistFolder ( dirPath )
4242 arrayOfFiles = arrayOfFiles || [ ]
4343 files . forEach ( function ( file ) {
4444 const p = path . join ( dirPath , file . path )
Original file line number Diff line number Diff line change 66 getAllFiles
77} = require ( './helpers.js' )
88
9- module . exports = function registerIPCHandlers ( win , ipcMain ) {
9+ module . exports = function registerIPCHandlers ( win , ipcMain , app ) {
1010 ipcMain . handle ( 'open-folder' , async ( event ) => {
1111 console . log ( 'ipcMain' , 'open-folder' )
1212 const folder = await openFolderDialog ( win )
@@ -107,4 +107,15 @@ module.exports = function registerIPCHandlers(win, ipcMain) {
107107
108108 win . setMinimumSize ( minWidth , minHeight )
109109 } )
110+
111+ ipcMain . handle ( 'confirm-close' , ( ) => {
112+ console . log ( 'ipcMain' , 'confirm-close' )
113+ app . exit ( )
114+ } )
115+
116+ win . on ( 'close' , ( event ) => {
117+ console . log ( 'BrowserWindow' , 'close' )
118+ event . preventDefault ( )
119+ win . webContents . send ( 'check-before-close' )
120+ } )
110121}
Original file line number Diff line number Diff line change @@ -23,11 +23,17 @@ function createWindow () {
2323 // and load the index.html of the app.
2424 win . loadFile ( 'ui/arduino/index.html' )
2525
26- registerIPCHandlers ( win , ipcMain )
26+ registerIPCHandlers ( win , ipcMain , app )
2727 registerMenu ( win )
28+
29+ app . on ( 'activate' , ( ) => {
30+ if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( )
31+ } )
32+ // app.on('window-all-closed', () => {
33+ // if (process.platform !== 'darwin') app.quit()
34+ // })
2835}
2936
3037
3138// TODO: Loading splash screen
32-
3339app . whenReady ( ) . then ( createWindow )
Original file line number Diff line number Diff line change @@ -151,9 +151,12 @@ const Disk = {
151151const Window = {
152152 setWindowSize : ( minWidth , minHeight ) => {
153153 ipcRenderer . invoke ( 'set-window-size' , minWidth , minHeight )
154- }
154+ } ,
155+ beforeClose : ( callback ) => ipcRenderer . on ( 'check-before-close' , callback ) ,
156+ confirmClose : ( ) => ipcRenderer . invoke ( 'confirm-close' )
155157}
156158
159+
157160contextBridge . exposeInMainWorld ( 'BridgeSerial' , Serial )
158161contextBridge . exposeInMainWorld ( 'BridgeDisk' , Disk )
159162contextBridge . exposeInMainWorld ( 'BridgeWindow' , Window )
Original file line number Diff line number Diff line change @@ -1303,6 +1303,15 @@ async function store(state, emitter) {
13031303 emitter . emit ( 'render' )
13041304 } )
13051305
1306+ win . beforeClose ( async ( ) => {
1307+ const hasChanges = ! ! state . openFiles . find ( f => f . parentFolder && f . hasChanges )
1308+ if ( hasChanges ) {
1309+ const response = await confirm ( 'You may have unsaved changes. Are you sure you want to proceed?' , 'Yes' , 'Cancel' )
1310+ if ( ! response ) return false
1311+ }
1312+ await win . confirmClose ( )
1313+ } )
1314+
13061315 function createFile ( args ) {
13071316 const {
13081317 source,
You can’t perform that action at this time.
0 commit comments