Add confirm before close dialog to newIDE

This commit is contained in:
Florian Rival
2018-02-15 08:44:40 +01:00
parent 42f91565fa
commit 09cf13d6e2
2 changed files with 63 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ import ProjectManager from '../ProjectManager';
import PlatformSpecificAssetsDialog from '../PlatformSpecificAssetsEditor/PlatformSpecificAssetsDialog';
import LoaderModal from '../UI/LoaderModal';
import EditorBar from '../UI/EditorBar';
import CloseConfirmDialog from '../UI/CloseConfirmDialog';
import ProfileDialog from '../Profile/ProfileDialog';
import Window from '../Utils/Window';
import { showErrorBox } from '../UI/Messages/MessageBox';
@@ -996,6 +997,7 @@ export default class MainFrame extends Component<*, State> {
onClose={() => this.openAboutDialog(false)}
updateStatus={updateStatus}
/>
<CloseConfirmDialog shouldPrompt={!!this.state.currentProject} />
</div>
</Providers>
);

View File

@@ -0,0 +1,61 @@
// @flow
import * as React from 'react';
import optionalRequire from '../Utils/OptionalRequire';
import Window from '../Utils/Window';
const electron = optionalRequire('electron');
type Props = {|
shouldPrompt: boolean,
|};
export default class CloseConfirmDialog extends React.Component<Props, *> {
_delayElectronClose = true;
componentDidMount() {
this._setup(this.props);
}
componentWillReceiveProps(newProps: Props) {
if (newProps.shouldPrompt !== this.props.shouldPrompt)
this._setup(newProps);
}
_setup(props: Props) {
if (Window.isDev()) return; // Don't prevent live-reload in development
const { shouldPrompt } = props;
const message =
'Are you sure you want to quit GDevelop? Any unsaved changes will be lost.';
if (electron) {
window.onbeforeunload = e => {
if (this._delayElectronClose && shouldPrompt) {
//eslint-disable-next-line
const answer = confirm(message);
setTimeout(() => {
if (answer) {
// If answer is positive, re-trigger the close
this._delayElectronClose = false;
electron.remote.getCurrentWindow().close();
}
});
// First, prevents closing the window immediately
e.returnValue = true;
} else {
// Returning undefined will let the window close
}
};
} else if (window) {
if (shouldPrompt) {
window.onbeforeunload = () => message;
} else {
window.onbeforeunload = null;
}
}
}
render() {
return null;
}
}