Skip to content

Commit

Permalink
Merge pull request #10 from dylanmcdougall/master
Browse files Browse the repository at this point in the history
Add confirmation dialog on close
  • Loading branch information
amanharwara authored Dec 28, 2018
2 parents 3abebc7 + 7c65710 commit 7de6ce1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
9 changes: 8 additions & 1 deletion src/js/mainWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ function getSetPrefs() {
} else {
localStorage.setItem('toggle-sound-preference', true);
}

if (localStorage.getItem('exit-prompt-preference')) {
soundPref = localStorage.getItem('exit-prompt-preference');
} else {
localStorage.setItem('exit-prompt-preference', true);
}
}

getSetPrefs();
Expand All @@ -53,7 +59,8 @@ ipcRenderer.on('sendPreferencesBool', function(e, bool) {
persistTheme: JSON.parse(localStorage.getItem('persist-theme-preference')),
toggleNotifications: JSON.parse(localStorage.getItem('toggle-notifications-preference')),
toggleSound: JSON.parse(localStorage.getItem('toggle-sound-preference')),
toggleTray: JSON.parse(localStorage.getItem('toggle-tray-preference'))
toggleTray: JSON.parse(localStorage.getItem('toggle-tray-preference')),
showExitPrompt: JSON.parse(localStorage.getItem('exit-prompt-preference'))
});
}
});
Expand Down
20 changes: 18 additions & 2 deletions src/js/prefWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const {
} = remote;
const fs = require('fs');

var persistTheme, toggleNotifications, toggleMessagePreview, toggleSound;
var persistTheme, toggleNotifications, toggleMessagePreview, toggleSound, showExitPrompt;
persistTheme = localStorage.getItem('persist-theme-preference');
toggleNotifications = localStorage.getItem('toggle-notifications-preference');
toggleSound = localStorage.getItem('toggle-sound-preference');
toggleTray = localStorage.getItem('toggle-tray-preference');
showExitPrompt = localStorage.getItem('exit-prompt-preference');

function loadSettings() {
if (document.querySelector('#persist-theme-preference')) {
Expand All @@ -30,6 +31,10 @@ function loadSettings() {
let checked = toggleTray == "true" ? true : false;
document.querySelector('#toggle-tray-preference').checked = checked;
}
if (document.querySelector('#exit-prompt-preference')) {
let checked = showExitPrompt == "true" ? true : false;
document.querySelector('#exit-prompt-preference').checked = checked;
}
}

loadSettings();
Expand All @@ -51,7 +56,8 @@ document.querySelector('#export').addEventListener('click', function(e) {
persistTheme: persistTheme,
toggleNotifications: toggleNotifications,
toggleSound: toggleSound,
toggleTray: toggleTray
toggleTray: toggleTray,
showExitPrompt: showExitPrompt
});

// fileName is a string that contains the path and filename created in the save file dialog.
Expand All @@ -77,6 +83,7 @@ document.querySelector('#import').addEventListener('click', function(e) {
JSONtoggleNotifications = JSONsettings.toggleNotifications;
JSONtoggleSound = JSONsettings.toggleSound;
JSONtoggleTray = JSONsettings.toggleTray;
JSONshowExitPrompt = JSONsettings.showExitPrompt;

if (document.querySelector('#persist-theme-preference')) {
let checked = JSONpersistTheme == "true" ? true : false;
Expand All @@ -94,6 +101,10 @@ document.querySelector('#import').addEventListener('click', function(e) {
let checked = JSONtoggleTray == "true" ? true : false;
document.querySelector('#toggle-tray-preference').checked = checked;
}
if (document.querySelector('#exit-prompt-preference')) {
let checked = JSONshowExitPrompt == "true" ? true : false;
document.querySelector('#exit-prompt-preference').checked = checked;
}
});
});

Expand All @@ -118,6 +129,11 @@ document.querySelector('#save').addEventListener('click', function(e) {
} else {
localStorage.setItem('toggle-tray-preference', false);
}
if (document.querySelector('#exit-prompt-preference').checked) {
localStorage.setItem('exit-prompt-preference', true);
} else {
localStorage.setItem('exit-prompt-preference', false);
}

ipcRenderer.send('settingsChanged', true);
});
44 changes: 31 additions & 13 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ if (!singleInstanceLock) {
}));
const mainMenu = Menu.buildFromTemplate(mainMenuTemp); //Applies the main menu template
Menu.setApplicationMenu(mainMenu); //Sets the main menu

mainWindow.on('close', function (e) { // Confirm dialog when user closes window
if (app.showExitPrompt) {
e.preventDefault();
confirmExit();
}
});
mainWindow.on('closed', function() { //Quits app when main window is closed
mainWindow = null
mainWindow = null;
app.quit();
});

Expand Down Expand Up @@ -85,7 +92,7 @@ if (!singleInstanceLock) {
});

function init() {
var theme, persistTheme, toggleNotifications, toggleSound, toggleTray;
var theme, persistTheme, toggleNotifications, toggleSound, toggleTray, showExitPrompt;
ipcMain.on('preferences', function(e, pref) {
theme = pref.theme || {
name: 'default-theme',
Expand All @@ -98,6 +105,12 @@ if (!singleInstanceLock) {
toggleTray = true;
}

if (typeof pref.showExitPrompt !== "undefined") {
app.showExitPrompt = pref.showExitPrompt;
} else {
app.showExitPrompt = true;
}

if (typeof pref.persistTheme !== "undefined") {
persistTheme = pref.persistTheme;
} else {
Expand Down Expand Up @@ -156,17 +169,7 @@ if (!singleInstanceLock) {
}, {
label: 'Exit',
click() {
dialog.showMessageBox({
type: 'question',
buttons: ["OK", "Cancel"],
title: "Exit",
message: "Are you sure you want to exit?"
}, function(res) {
if (res == 0) {
app.quit();
return;
}
});
app.quit();
}
}]);
if (process.platform !== "darwin") {
Expand Down Expand Up @@ -206,6 +209,21 @@ if (!singleInstanceLock) {
});
}

function confirmExit() {
dialog.showMessageBox({
type: 'question',
buttons: ["OK", "Cancel"],
title: "Exit",
message: "Are you sure you want to exit?"
}, function(res) {
if (res == 0) {
app.showExitPrompt = false;
app.quit();
return;
}
});
}

function createAboutWindow() {
if (typeof aboutWindow == 'object') {
aboutWindow.show();
Expand Down
7 changes: 7 additions & 0 deletions src/windows/prefWindow.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ <h1>Preferences</h1>
<span class="slider"></span>
</label>
</div>
<div class="preference">
<p class="preference-header">Show confirmation dialog on exit</p>
<label class="preference-switch">
<input type="checkbox" id="exit-prompt-preference">
<span class="slider"></span>
</label>
</div>
<div class="preference-group">
<p class="preference-header">Notifications</p>
<ul class="sub-preferences">
Expand Down

0 comments on commit 7de6ce1

Please sign in to comment.