Skip to content

Commit d7bbfc5

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
init
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent 0c22884 commit d7bbfc5

File tree

2 files changed

+66
-84
lines changed

2 files changed

+66
-84
lines changed

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+1-69
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import {
55
postConstruct,
66
} from '@theia/core/shared/inversify';
77
import * as React from '@theia/core/shared/react';
8-
import { SketchesService } from '../common/protocol';
98
import {
109
MAIN_MENU_BAR,
1110
MenuContribution,
1211
MenuModelRegistry,
1312
} from '@theia/core';
1413
import {
15-
Dialog,
1614
FrontendApplication,
1715
FrontendApplicationContribution,
18-
OnWillStopAction,
1916
} from '@theia/core/lib/browser';
2017
import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution';
2118
import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
@@ -34,14 +31,9 @@ import { EditorCommands, EditorMainMenu } from '@theia/editor/lib/browser';
3431
import { MonacoMenus } from '@theia/monaco/lib/browser/monaco-menu';
3532
import { FileNavigatorCommands } from '@theia/navigator/lib/browser/navigator-contribution';
3633
import { TerminalMenus } from '@theia/terminal/lib/browser/terminal-frontend-contribution';
37-
import {
38-
CurrentSketch,
39-
SketchesServiceClientImpl,
40-
} from '../common/protocol/sketches-service-client-impl';
4134
import { ArduinoPreferences } from './arduino-preferences';
4235
import { BoardsServiceProvider } from './boards/boards-service-provider';
4336
import { BoardsToolBarItem } from './boards/boards-toolbar-item';
44-
import { SaveAsSketch } from './contributions/save-as-sketch';
4537
import { ArduinoMenus } from './menu/arduino-menus';
4638
import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution';
4739
import { ArduinoToolbar } from './toolbar/arduino-toolbar';
@@ -63,18 +55,12 @@ export class ArduinoFrontendContribution
6355
@inject(BoardsServiceProvider)
6456
private readonly boardsServiceProvider: BoardsServiceProvider;
6557

66-
@inject(SketchesService)
67-
private readonly sketchService: SketchesService;
68-
6958
@inject(CommandRegistry)
7059
private readonly commandRegistry: CommandRegistry;
7160

7261
@inject(ArduinoPreferences)
7362
private readonly arduinoPreferences: ArduinoPreferences;
7463

75-
@inject(SketchesServiceClientImpl)
76-
private readonly sketchServiceClient: SketchesServiceClientImpl;
77-
7864
@inject(FrontendApplicationStateService)
7965
private readonly appStateService: FrontendApplicationStateService;
8066

@@ -91,7 +77,7 @@ export class ArduinoFrontendContribution
9177
}
9278
}
9379

94-
async onStart(app: FrontendApplication): Promise<void> {
80+
onStart(app: FrontendApplication): void {
9581
this.arduinoPreferences.onPreferenceChanged((event) => {
9682
if (event.newValue !== event.oldValue) {
9783
switch (event.preferenceName) {
@@ -303,58 +289,4 @@ export class ArduinoFrontendContribution
303289
}
304290
);
305291
}
306-
307-
// TODO: should be handled by `Close` contribution. https://github.com/arduino/arduino-ide/issues/1016
308-
onWillStop(): OnWillStopAction {
309-
return {
310-
reason: 'temp-sketch',
311-
action: () => {
312-
return this.showTempSketchDialog();
313-
},
314-
};
315-
}
316-
317-
private async showTempSketchDialog(): Promise<boolean> {
318-
const sketch = await this.sketchServiceClient.currentSketch();
319-
if (!CurrentSketch.isValid(sketch)) {
320-
return true;
321-
}
322-
const isTemp = await this.sketchService.isTemp(sketch);
323-
if (!isTemp) {
324-
return true;
325-
}
326-
const messageBoxResult = await remote.dialog.showMessageBox(
327-
remote.getCurrentWindow(),
328-
{
329-
message: nls.localize(
330-
'arduino/sketch/saveTempSketch',
331-
'Save your sketch to open it again later.'
332-
),
333-
title: nls.localize(
334-
'theia/core/quitTitle',
335-
'Are you sure you want to quit?'
336-
),
337-
type: 'question',
338-
buttons: [
339-
Dialog.CANCEL,
340-
nls.localizeByDefault('Save As...'),
341-
nls.localizeByDefault("Don't Save"),
342-
],
343-
}
344-
);
345-
const result = messageBoxResult.response;
346-
if (result === 2) {
347-
return true;
348-
} else if (result === 1) {
349-
return !!(await this.commandRegistry.executeCommand(
350-
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
351-
{
352-
execOnlyIfTemp: false,
353-
openAfterMove: false,
354-
wipeOriginal: true,
355-
}
356-
));
357-
}
358-
return false;
359-
}
360292
}

arduino-ide-extension/src/browser/contributions/close.ts

+65-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { inject, injectable } from '@theia/core/shared/inversify';
1+
import { injectable } from '@theia/core/shared/inversify';
22
import * as remote from '@theia/core/electron-shared/@electron/remote';
33
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
4-
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
5-
import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell';
6-
import { FrontendApplication } from '@theia/core/lib/browser/frontend-application';
74
import { ArduinoMenus } from '../menu/arduino-menus';
85
import {
96
SketchContribution,
@@ -14,24 +11,19 @@ import {
1411
URI,
1512
} from './contribution';
1613
import { nls } from '@theia/core/lib/common';
14+
import { Dialog } from '@theia/core/lib/browser/dialogs';
15+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
16+
import { SaveAsSketch } from './save-as-sketch';
17+
import type { OnWillStopAction } from '@theia/core/lib/browser/frontend-application';
1718

1819
/**
1920
* Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
2021
*/
2122
@injectable()
2223
export class Close extends SketchContribution {
23-
@inject(EditorManager)
24-
protected override readonly editorManager: EditorManager;
25-
26-
protected shell: ApplicationShell;
27-
28-
override onStart(app: FrontendApplication): void {
29-
this.shell = app.shell;
30-
}
31-
3224
override registerCommands(registry: CommandRegistry): void {
3325
registry.registerCommand(Close.Commands.CLOSE, {
34-
execute: () => remote.getCurrentWindow().close()
26+
execute: () => remote.getCurrentWindow().close(),
3527
});
3628
}
3729

@@ -50,6 +42,60 @@ export class Close extends SketchContribution {
5042
});
5143
}
5244

45+
// `FrontendApplicationContribution#onWillStop`
46+
onWillStop(): OnWillStopAction {
47+
return {
48+
reason: 'temp-sketch',
49+
action: () => {
50+
return this.showTempSketchDialog();
51+
},
52+
};
53+
}
54+
55+
private async showTempSketchDialog(): Promise<boolean> {
56+
const sketch = await this.sketchServiceClient.currentSketch();
57+
if (!CurrentSketch.isValid(sketch)) {
58+
return true;
59+
}
60+
const isTemp = await this.sketchService.isTemp(sketch);
61+
if (!isTemp) {
62+
return true;
63+
}
64+
const messageBoxResult = await remote.dialog.showMessageBox(
65+
remote.getCurrentWindow(),
66+
{
67+
message: nls.localize(
68+
'arduino/sketch/saveTempSketch',
69+
'Save your sketch to open it again later.'
70+
),
71+
title: nls.localize(
72+
'theia/core/quitTitle',
73+
'Are you sure you want to quit?'
74+
),
75+
type: 'question',
76+
buttons: [
77+
Dialog.CANCEL,
78+
nls.localizeByDefault('Save As...'),
79+
nls.localizeByDefault("Don't Save"),
80+
],
81+
}
82+
);
83+
const result = messageBoxResult.response;
84+
if (result === 2) {
85+
return true;
86+
} else if (result === 1) {
87+
return !!(await this.commandService.executeCommand(
88+
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
89+
{
90+
execOnlyIfTemp: false,
91+
openAfterMove: false,
92+
wipeOriginal: true,
93+
}
94+
));
95+
}
96+
return false;
97+
}
98+
5399
/**
54100
* If the file was ever touched/modified. We get this based on the `version` of the monaco model.
55101
*/
@@ -59,13 +105,17 @@ export class Close extends SketchContribution {
59105
const { editor } = editorWidget;
60106
if (editor instanceof MonacoEditor) {
61107
const versionId = editor.getControl().getModel()?.getVersionId();
62-
if (Number.isInteger(versionId) && versionId! > 1) {
108+
if (this.isInteger(versionId) && versionId > 1) {
63109
return true;
64110
}
65111
}
66112
}
67113
return false;
68114
}
115+
116+
private isInteger(arg: unknown): arg is number {
117+
return Number.isInteger(arg);
118+
}
69119
}
70120

71121
export namespace Close {

0 commit comments

Comments
 (0)