Skip to content

Commit

Permalink
Added a save check on settings window close
Browse files Browse the repository at this point in the history
  • Loading branch information
AtomicSponge committed Jun 25, 2024
1 parent 31ffbb8 commit 54b30b9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Removed stylus-loader (unnecessary)
- Removed CSS injection plug-in for Vite
- Added exception checking on AppSettings.getData
- Added a save check on settings window close

## v2.0.1
- Removed `close` button from the input window to prevent accidental closure.
Expand Down
1 change: 1 addition & 0 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ declare global {
bufferSize:number
encoding:string
startup:boolean
check:boolean
}

/** Prompt data passed to the Input window */
Expand Down
3 changes: 2 additions & 1 deletion electron/lib/AppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export class AppSettings {
launchMenu: tempData,
bufferSize: AppSettings.#bufferSize,
encoding: AppSettings.#encoding,
startup: AppSettings.#startup
startup: AppSettings.#startup,
check: false
}
}

Expand Down
43 changes: 26 additions & 17 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,33 @@ const settingsEditorWindow = ():void => {

/* Event handler for saving settings */
ipcMain.on('save-settings-data', (_event, data) => {
if (dialog.showMessageBoxSync(<BrowserWindow>settingsWin, {
type: 'question',
title: `${appInfo.name} - Confirm`,
icon: `${appInfo.icon}`,
buttons: [ 'Yes', 'No' ],
message: 'Do you want to save changes?'
}) === 0) {
try {
appSettings.setData(data)
appSettings.save()
resBuff.size = appSettings.bufferSize
{(appSettings.startup) ? autoLauncher.enable() : autoLauncher.disable() }
appTray?.setContextMenu(buildMenu())
settingsWin?.webContents.send('send-settings-data', appSettings.getData())
} catch (error:any) {
dialog.showErrorBox(`${appInfo.name}`,
`Error saving settings!\n\n${error.message}`)
const saveDataPrompt = (data:SettingsIpc, message:string) => {
if (dialog.showMessageBoxSync(<BrowserWindow>settingsWin, {
type: 'question',
title: `${appInfo.name} - Confirm`,
icon: `${appInfo.icon}`,
buttons: [ 'Yes', 'No' ],
message: message
}) === 0) {
try {
appSettings.setData(data)
appSettings.save()
resBuff.size = appSettings.bufferSize
{(appSettings.startup) ? autoLauncher.enable() : autoLauncher.disable() }
appTray?.setContextMenu(buildMenu())
settingsWin?.webContents.send('send-settings-data', appSettings.getData())
} catch (error:any) {
dialog.showErrorBox(`${appInfo.name}`,
`Error saving settings!\n\n${error.message}`)
}
}
}
if (data.check) {
if(!appSettings.compareData(data)) {
saveDataPrompt(data, 'Settings have changed! Do you want to save?')
}
} else {
saveDataPrompt(data, 'Do you want to save changes?')
}
})

Expand Down
13 changes: 8 additions & 5 deletions renderer/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const buildMenuList = ():void => {
}
/** Parse data from the settings window */
const parseData = ():SettingsIpc => {
const parseData = (event:any):SettingsIpc => {
const minVal:ScriptBufferMin = 10
const maxVal:ScriptBufferMax = 500
if(_bufferSize.value < minVal) _bufferSize.value = minVal
Expand All @@ -81,7 +81,8 @@ const parseData = ():SettingsIpc => {
launchMenu: JSON.stringify(_launchMenu.value),
bufferSize: Number(_bufferSize.value),
encoding: _encodingSelect.value,
startup: Boolean(_startup.value)
startup: Boolean(_startup.value),
check: (event.type === 'beforeunload') ? true : false
}
}
Expand All @@ -100,8 +101,8 @@ const resetSettings = ():void => {
buildMenuList()
}
/** Save settings button action */
const saveSettings = ():void => {
window.settingsAPI.saveSettings(parseData())
const saveSettings = (event:any):void => {
window.settingsAPI.saveSettings(parseData(event))
}
/** Add a new item to the launch menu */
Expand Down Expand Up @@ -160,7 +161,9 @@ onMounted(() => {
buildMenuList()
})
//window.onbeforeunload = () => { window.settingsAPI.saveSettings(parseData()) }
window.onbeforeunload = (event:any) => {
window.settingsAPI.saveSettings(parseData(event))
}
})
</script>

Expand Down

0 comments on commit 54b30b9

Please sign in to comment.