11import { inject , injectable } from 'inversify' ;
2+ import { toArray } from '@phosphor/algorithm' ;
23import { 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' ;
38import { ArduinoMenus } from '../menu/arduino-menus' ;
4- import { SketchContribution , Command , CommandRegistry , MenuModelRegistry , KeybindingRegistry , URI } from './contribution' ;
59import { 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' ;
811
12+ /**
13+ * Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
14+ */
915@injectable ( )
10- export class CloseSketch extends SketchContribution {
16+ export class Close extends SketchContribution {
1117
1218 @inject ( EditorManager )
1319 protected readonly editorManager : EditorManager ;
1420
21+ protected shell : ApplicationShell ;
22+
23+ onStart ( app : FrontendApplication ) : void {
24+ this . shell = app . shell ;
25+ }
26+
1527 registerCommands ( registry : CommandRegistry ) : void {
16- registry . registerCommand ( CloseSketch . Commands . CLOSE_SKETCH , {
28+ registry . registerCommand ( Close . Commands . CLOSE , {
1729 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).
1848 const sketch = await this . sketchServiceClient . currentSketch ( ) ;
1949 if ( ! sketch ) {
2050 return ;
@@ -48,15 +78,15 @@ export class CloseSketch extends SketchContribution {
4878
4979 registerMenus ( registry : MenuModelRegistry ) : void {
5080 registry . registerMenuAction ( ArduinoMenus . FILE__SKETCH_GROUP , {
51- commandId : CloseSketch . Commands . CLOSE_SKETCH . id ,
81+ commandId : Close . Commands . CLOSE . id ,
5282 label : 'Close' ,
5383 order : '5'
5484 } ) ;
5585 }
5686
5787 registerKeybindings ( registry : KeybindingRegistry ) : void {
5888 registry . registerKeybinding ( {
59- command : CloseSketch . Commands . CLOSE_SKETCH . id ,
89+ command : Close . Commands . CLOSE . id ,
6090 keybinding : 'CtrlCmd+W'
6191 } ) ;
6292 }
@@ -80,10 +110,10 @@ export class CloseSketch extends SketchContribution {
80110
81111}
82112
83- export namespace CloseSketch {
113+ export namespace Close {
84114 export namespace Commands {
85- export const CLOSE_SKETCH : Command = {
86- id : 'arduino-close-sketch '
115+ export const CLOSE : Command = {
116+ id : 'arduino-close'
87117 } ;
88118 }
89119}
0 commit comments