Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Add option to warn before quitting (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanglam2807 authored Oct 28, 2020
1 parent 11eb1df commit 4163fca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
47 changes: 34 additions & 13 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,42 @@ if (!gotTheLock) {
});
});

app.on('before-quit', () => {
const win = mainWindow.get();
if (win) {
// https://github.com/atom/electron/issues/444#issuecomment-76492576
win.forceClose = true;
win.setBrowserView(null);
app.on('before-quit', (e) => {
const handleBeforeQuit = () => {
const win = mainWindow.get();
if (win) {
// https://github.com/atom/electron/issues/444#issuecomment-76492576
win.forceClose = true;
win.setBrowserView(null);
}

// https://github.com/webcatalog/webcatalog-app/issues/1141
// the bug seems to only occur when there's BrowserView opened
// so destroy all BrowserViews before exiting
const views = BrowserView.getAllViews();
views.forEach((view) => {
view.destroy();
});
};

const warnBeforeQuitting = getPreference('warnBeforeQuitting');
if (warnBeforeQuitting) {
e.preventDefault();
dialog.showMessageBox(mainWindow.get(), {
type: 'question',
buttons: ['Yes', 'No'],
message: 'Are you sure you want to quit the app?',
cancelId: 1,
defaultId: 1,
}).then(({ response }) => {
if (response === 0) {
handleBeforeQuit();
}
}).catch(console.log); // eslint-disable-line
return;
}

// https://github.com/webcatalog/webcatalog-app/issues/1141
// the bug seems to only occur when there's BrowserView opened
// so destroy all BrowserViews before exiting
const views = BrowserView.getAllViews();
views.forEach((view) => {
view.destroy();
});
handleBeforeQuit();
});

app.on('window-all-closed', () => {
Expand Down
10 changes: 10 additions & 0 deletions public/libs/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getViewBounds = require('./get-view-bounds');
const formatBytes = require('./format-bytes');
const {
setPreference,
getPreference,
} = require('./preferences');

const {
Expand Down Expand Up @@ -126,6 +127,15 @@ const createMenu = async () => {
},
...macMenuItems,
{ type: 'separator' },
{
label: 'Warn Before Quitting',
click: () => {
setPreference('warnBeforeQuitting', !getPreference('warnBeforeQuitting'));
createMenu();
},
type: 'checkbox',
checked: getPreference('warnBeforeQuitting'),
},
{ role: 'quit' },
],
},
Expand Down
1 change: 1 addition & 0 deletions public/libs/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const defaultPreferences = {
titleBar: !shouldShowSidebar, // if sidebar is shown, then hide titleBar
unreadCountBadge: true,
useHardwareAcceleration: true,
warnBeforeQuitting: false,
};

let cachedPreferences = null;
Expand Down
18 changes: 17 additions & 1 deletion src/components/dialog-preferences/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ const Preferences = ({
titleBar,
unreadCountBadge,
useHardwareAcceleration,
warnBeforeQuitting,
}) => {
const appJson = window.remote.getGlobal('appJson');
const utmSource = 'juli_app';
Expand Down Expand Up @@ -1311,7 +1312,7 @@ const Preferences = ({
onClick={requestCheckForUpdates}
>
<ListItemText
primary="Check for Updates"
primary="Check for updates"
/>
<ChevronRightIcon color="action" />
</ListItem>
Expand Down Expand Up @@ -1364,6 +1365,19 @@ const Preferences = ({
<ChevronRightIcon color="action" />
</ListItem>
<Divider />
<ListItem>
<ListItemText primary="Warn before quitting" />
<ListItemSecondaryAction>
<Switch
edge="end"
color="primary"
checked={warnBeforeQuitting}
onChange={(e) => {
requestSetPreference('warnBeforeQuitting', e.target.checked);
}}
/>
</ListItemSecondaryAction>
</ListItem>
<ListItem button onClick={requestQuit}>
<ListItemText primary="Quit" />
<ChevronRightIcon color="action" />
Expand Down Expand Up @@ -1436,6 +1450,7 @@ Preferences.propTypes = {
titleBar: PropTypes.bool.isRequired,
unreadCountBadge: PropTypes.bool.isRequired,
useHardwareAcceleration: PropTypes.bool.isRequired,
warnBeforeQuitting: PropTypes.bool.isRequired,
};

const mapStateToProps = (state) => ({
Expand Down Expand Up @@ -1481,6 +1496,7 @@ const mapStateToProps = (state) => ({
titleBar: state.preferences.titleBar,
unreadCountBadge: state.preferences.unreadCountBadge,
useHardwareAcceleration: state.preferences.useHardwareAcceleration,
warnBeforeQuitting: state.preferences.warnBeforeQuitting,
});

const actionCreators = {
Expand Down

0 comments on commit 4163fca

Please sign in to comment.