|
| 1 | +import { FrontendApplicationContribution } from "@theia/core/lib/browser/frontend-application"; |
| 2 | +import { inject, injectable } from "@theia/core/shared/inversify"; |
| 3 | +import * as remote from '@theia/core/electron-shared/@electron/remote'; |
| 4 | +import { SketchesServiceClientImpl } from "../../common/protocol/sketches-service-client-impl"; |
| 5 | +import { SketchesService } from "../../common/protocol/sketches-service"; |
| 6 | +import { Dialog } from "@theia/core/lib/browser/dialogs"; |
| 7 | +import { nls } from "@theia/core/lib/common/nls"; |
| 8 | +import { CommandRegistry } from "@theia/core/lib/common/command"; |
| 9 | +import { ApplicationShell } from "@theia/core/lib/browser/shell/application-shell"; |
| 10 | +import { SaveAsSketch } from "./save-as-sketch"; |
| 11 | +import { WindowService } from "@theia/core/lib/browser/window/window-service"; |
| 12 | + |
| 13 | +@injectable() |
| 14 | +export class ShutdownRoutine implements FrontendApplicationContribution { |
| 15 | + |
| 16 | + @inject(SketchesServiceClientImpl) |
| 17 | + protected readonly sketchServiceClient: SketchesServiceClientImpl; |
| 18 | + |
| 19 | + @inject(SketchesService) |
| 20 | + protected readonly sketchService: SketchesService; |
| 21 | + |
| 22 | + @inject(CommandRegistry) |
| 23 | + protected readonly commandRegistry: CommandRegistry; |
| 24 | + |
| 25 | + @inject(ApplicationShell) |
| 26 | + protected readonly appShell: ApplicationShell; |
| 27 | + |
| 28 | + @inject(WindowService) |
| 29 | + protected readonly windowService: WindowService; |
| 30 | + |
| 31 | + initialize(): void { |
| 32 | + this.setupShutdownRoutine(remote.getCurrentWindow()); |
| 33 | + } |
| 34 | + |
| 35 | + setupShutdownRoutine(electronWindow: Electron.BrowserWindow): void { |
| 36 | + window.addEventListener('beforeunload', event => { |
| 37 | + if (!this.windowService.isSafeToShutDown()) { |
| 38 | + event.preventDefault(); |
| 39 | + event.returnValue = false; |
| 40 | + this.confirmShutdown(electronWindow).then(result => { |
| 41 | + if (result) { |
| 42 | + electronWindow.destroy(); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private async confirmShutdown(window: Electron.BrowserWindow): Promise<boolean> { |
| 50 | + const sketch = await this.sketchServiceClient.currentSketch(); |
| 51 | + if (sketch) { |
| 52 | + const isTemp = await this.sketchService.isTemp(sketch); |
| 53 | + if (isTemp) { |
| 54 | + return this.showTempSketchDialog(window); |
| 55 | + } else if (this.appShell.canSaveAll()) { |
| 56 | + const dialogResult = await remote.dialog.showMessageBox(window, { |
| 57 | + title: nls.localize('theia/core/quitTitle', 'Are you sure you want to quit?'), |
| 58 | + message: nls.localize('theia/core/quitMessage', 'Any unsaved changes will not be saved.'), |
| 59 | + buttons: [ |
| 60 | + Dialog.NO, |
| 61 | + Dialog.YES |
| 62 | + ] |
| 63 | + }); |
| 64 | + return dialogResult.response === 1; |
| 65 | + } |
| 66 | + } |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + private async showTempSketchDialog(window: Electron.BrowserWindow): Promise<boolean> { |
| 71 | + const sketch = await this.sketchServiceClient.currentSketch(); |
| 72 | + if (!sketch) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + const isTemp = await this.sketchService.isTemp(sketch); |
| 76 | + if (!isTemp) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + const messageBoxResult = await remote.dialog.showMessageBox( |
| 80 | + window, |
| 81 | + { |
| 82 | + message: nls.localize('arduino/sketch/saveTempSketch', 'Save your sketch to open it again later.'), |
| 83 | + title: 'Arduino-IDE', |
| 84 | + type: 'question', |
| 85 | + buttons: [ |
| 86 | + Dialog.CANCEL, |
| 87 | + nls.localizeByDefault('Save As...'), |
| 88 | + nls.localizeByDefault("Don't Save"), |
| 89 | + ], |
| 90 | + } |
| 91 | + ) |
| 92 | + const result = messageBoxResult.response; |
| 93 | + if (result === 2) { |
| 94 | + return true; |
| 95 | + } else if (result === 1) { |
| 96 | + return !!(await this.commandRegistry.executeCommand( |
| 97 | + SaveAsSketch.Commands.SAVE_AS_SKETCH.id, |
| 98 | + { |
| 99 | + execOnlyIfTemp: false, |
| 100 | + openAfterMove: false, |
| 101 | + wipeOriginal: true |
| 102 | + } |
| 103 | + )); |
| 104 | + } |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments