1
1
import { inject , injectable } from 'inversify' ;
2
+ import { toArray } from '@phosphor/algorithm' ;
2
3
import { remote } from 'electron' ;
4
+ import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor' ;
5
+ import { EditorManager } from '@theia/editor/lib/browser/editor-manager' ;
6
+ import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell' ;
7
+ import { FrontendApplication } from '@theia/core/lib/browser/frontend-application' ;
3
8
import { ArduinoMenus } from '../menu/arduino-menus' ;
4
- import { SketchContribution , Command , CommandRegistry , MenuModelRegistry , KeybindingRegistry , URI } from './contribution' ;
5
9
import { SaveAsSketch } from './save-as-sketch' ;
6
- import { EditorManager } from '@theia/editor/lib/browser' ;
7
- import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor' ;
10
+ import { SketchContribution , Command , CommandRegistry , MenuModelRegistry , KeybindingRegistry , URI } from './contribution' ;
8
11
12
+ /**
13
+ * Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
14
+ */
9
15
@injectable ( )
10
- export class CloseSketch extends SketchContribution {
16
+ export class Close extends SketchContribution {
11
17
12
18
@inject ( EditorManager )
13
19
protected readonly editorManager : EditorManager ;
14
20
21
+ protected shell : ApplicationShell ;
22
+
23
+ onStart ( app : FrontendApplication ) : void {
24
+ this . shell = app . shell ;
25
+ }
26
+
15
27
registerCommands ( registry : CommandRegistry ) : void {
16
- registry . registerCommand ( CloseSketch . Commands . CLOSE_SKETCH , {
28
+ registry . registerCommand ( Close . Commands . CLOSE , {
17
29
execute : async ( ) => {
30
+
31
+ // Close current editor if closeable.
32
+ const { currentEditor } = this . editorManager ;
33
+ if ( currentEditor && currentEditor . title . closable ) {
34
+ currentEditor . close ( ) ;
35
+ return ;
36
+ }
37
+
38
+ // Close current widget from the main area if possible.
39
+ const { currentWidget } = this . shell ;
40
+ if ( currentWidget ) {
41
+ const currentWidgetInMain = toArray ( this . shell . mainPanel . widgets ( ) ) . find ( widget => widget === currentWidget ) ;
42
+ if ( currentWidgetInMain && currentWidgetInMain . title . closable ) {
43
+ return currentWidgetInMain . close ( ) ;
44
+ }
45
+ }
46
+
47
+ // Close the sketch (window).
18
48
const sketch = await this . sketchServiceClient . currentSketch ( ) ;
19
49
if ( ! sketch ) {
20
50
return ;
@@ -48,15 +78,15 @@ export class CloseSketch extends SketchContribution {
48
78
49
79
registerMenus ( registry : MenuModelRegistry ) : void {
50
80
registry . registerMenuAction ( ArduinoMenus . FILE__SKETCH_GROUP , {
51
- commandId : CloseSketch . Commands . CLOSE_SKETCH . id ,
81
+ commandId : Close . Commands . CLOSE . id ,
52
82
label : 'Close' ,
53
83
order : '5'
54
84
} ) ;
55
85
}
56
86
57
87
registerKeybindings ( registry : KeybindingRegistry ) : void {
58
88
registry . registerKeybinding ( {
59
- command : CloseSketch . Commands . CLOSE_SKETCH . id ,
89
+ command : Close . Commands . CLOSE . id ,
60
90
keybinding : 'CtrlCmd+W'
61
91
} ) ;
62
92
}
@@ -80,10 +110,10 @@ export class CloseSketch extends SketchContribution {
80
110
81
111
}
82
112
83
- export namespace CloseSketch {
113
+ export namespace Close {
84
114
export namespace Commands {
85
- export const CLOSE_SKETCH : Command = {
86
- id : 'arduino-close-sketch '
115
+ export const CLOSE : Command = {
116
+ id : 'arduino-close'
87
117
} ;
88
118
}
89
119
}
0 commit comments