- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathclose.ts
228 lines (211 loc) · 6.82 KB
/
close.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import{Dialog}from'@theia/core/lib/browser/dialogs';
importtype{FrontendApplication}from'@theia/core/lib/browser/frontend-application';
importtype{OnWillStopAction}from'@theia/core/lib/browser/frontend-application-contribution';
import{ApplicationShell}from'@theia/core/lib/browser/shell/application-shell';
import{nls}from'@theia/core/lib/common/nls';
importtype{MaybePromise}from'@theia/core/lib/common/types';
import{toArray}from'@theia/core/shared/@phosphor/algorithm';
import{inject,injectable}from'@theia/core/shared/inversify';
import{MonacoEditor}from'@theia/monaco/lib/browser/monaco-editor';
import{ArduinoMenus}from'../menu/arduino-menus';
import{CurrentSketch}from'../sketches-service-client-impl';
import{WindowServiceExt}from'../theia/core/window-service-ext';
import{
Command,
CommandRegistry,
KeybindingRegistry,
MenuModelRegistry,
Sketch,
SketchContribution,
URI,
}from'./contribution';
import{SaveAsSketch}from'./save-as-sketch';
/**
* Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
*/
@injectable()
exportclassCloseextendsSketchContribution{
@inject(WindowServiceExt)
privatereadonlywindowServiceExt: WindowServiceExt;
privateshell: ApplicationShell|undefined;
overrideonStart(app: FrontendApplication): MaybePromise<void>{
this.shell=app.shell;
}
overrideregisterCommands(registry: CommandRegistry): void{
registry.registerCommand(Close.Commands.CLOSE,{
execute: ()=>{
// Close current editor if closeable.
const{ currentEditor }=this.editorManager;
if(currentEditor&¤tEditor.title.closable){
currentEditor.close();
return;
}
if(this.shell){
// Close current widget from the main area if possible.
const{ currentWidget }=this.shell;
if(currentWidget){
constcurrentWidgetInMain=toArray(
this.shell.mainPanel.widgets()
).find((widget)=>widget===currentWidget);
if(currentWidgetInMain&¤tWidgetInMain.title.closable){
returncurrentWidgetInMain.close();
}
}
}
returnthis.windowServiceExt.close();
},
});
}
overrideregisterMenus(registry: MenuModelRegistry): void{
registry.registerMenuAction(ArduinoMenus.FILE__SKETCH_GROUP,{
commandId: Close.Commands.CLOSE.id,
label: nls.localize('vscode/editor.contribution/close','Close'),
order: '6',
});
}
overrideregisterKeybindings(registry: KeybindingRegistry): void{
registry.registerKeybinding({
command: Close.Commands.CLOSE.id,
keybinding: 'CtrlCmd+W',
});
}
// `FrontendApplicationContribution#onWillStop`
onWillStop(): OnWillStopAction{
return{
reason: 'save-sketch',
action: ()=>{
returnthis.showSaveSketchDialog();
},
};
}
/**
* If returns with `true`, IDE2 will close. Otherwise, it won't.
*/
privateasyncshowSaveSketchDialog(): Promise<boolean>{
constsketch=awaitthis.isCurrentSketchTemp();
if(!sketch){
// Normal close workflow: if there are dirty editors prompt the user.
if(!this.shell){
console.error(
`Could not get the application shell. Something went wrong.`
);
returntrue;
}
if(this.shell.canSaveAll()){
constprompt=awaitthis.prompt(false);
switch(prompt){
casePrompt.DoNotSave:
returntrue;
casePrompt.Cancel:
returnfalse;
casePrompt.Save: {
awaitthis.shell.saveAll();
returntrue;
}
default:
thrownewError(`Unexpected prompt: ${prompt}`);
}
}
returntrue;
}
// If non of the sketch files were ever touched, do not prompt the save dialog. (#1274)
constwereTouched=awaitPromise.all(
Sketch.uris(sketch).map((uri)=>this.wasTouched(uri))
);
if(wereTouched.every((wasTouched)=>!Boolean(wasTouched))){
returntrue;
}
constprompt=awaitthis.prompt(true);
switch(prompt){
casePrompt.DoNotSave:
returntrue;
casePrompt.Cancel:
returnfalse;
casePrompt.Save: {
// If `save as` was canceled by user, the result will be `undefined`, otherwise the new URI.
constresult=awaitthis.commandService.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
{
execOnlyIfTemp: false,
openAfterMove: false,
wipeOriginal: true,
markAsRecentlyOpened: true,
}
);
return!!result;
}
default:
thrownewError(`Unexpected prompt: ${prompt}`);
}
}
privateasyncprompt(isTemp: boolean): Promise<Prompt>{
const{ response }=awaitthis.dialogService.showMessageBox({
message: nls.localize(
'arduino/sketch/saveSketch',
'Save your sketch to open it again later.'
),
title: nls.localize(
'theia/core/quitTitle',
'Are you sure you want to quit?'
),
type: 'question',
buttons: [
nls.localizeByDefault("Don't Save"),
Dialog.CANCEL,
nls.localizeByDefault(isTemp ? 'Save As...' : 'Save'),
],
defaultId: 2,// `Save`/`Save As...` button index is the default.
});
switch(response){
case0:
returnPrompt.DoNotSave;
case1:
returnPrompt.Cancel;
case2:
returnPrompt.Save;
default:
thrownewError(`Unexpected response: ${response}`);
}
}
privateasyncisCurrentSketchTemp(): Promise<false|Sketch>{
constcurrentSketch=awaitthis.sketchServiceClient.currentSketch();
if(CurrentSketch.isValid(currentSketch)){
constisTemp=awaitthis.sketchesService.isTemp(currentSketch);
if(isTemp){
returncurrentSketch;
}
}
returnfalse;
}
/**
* If the file was ever touched/modified. We get this based on the `version` of the monaco model.
*/
protectedasyncwasTouched(uri: string): Promise<boolean>{
consteditorWidget=awaitthis.editorManager.getByUri(newURI(uri));
if(editorWidget){
const{ editor }=editorWidget;
if(editorinstanceofMonacoEditor){
constversionId=editor.getControl().getModel()?.getVersionId();
if(this.isInteger(versionId)&&versionId>1){
returntrue;
}
}
}
returnfalse;
}
privateisInteger(arg: unknown): arg is number{
returnNumber.isInteger(arg);
}
}
enumPrompt{
Save,
DoNotSave,
Cancel,
}
exportnamespaceClose{
exportnamespaceCommands{
exportconstCLOSE: Command={
id: 'arduino-close',
};
}
}