1+ import { shell } from 'electron' ;
12import { inject , injectable } from 'inversify' ;
23import { CommandRegistry } from '@theia/core/lib/common/command' ;
34import { MenuModelRegistry } from '@theia/core/lib/common/menu' ;
@@ -7,10 +8,19 @@ import { FrontendApplicationContribution } from '@theia/core/lib/browser/fronten
78import { MainMenuManager } from '../../../common/main-menu-manager' ;
89import { ArduinoPreferences } from '../../arduino-preferences' ;
910import { SketchbookWidget } from './sketchbook-widget' ;
10- import { ArduinoMenus } from '../../menu/arduino-menus' ;
11+ import { ArduinoMenus , PlaceholderMenuNode } from '../../menu/arduino-menus' ;
1112import { SketchbookTree } from './sketchbook-tree' ;
1213import { SketchbookCommands } from './sketchbook-commands' ;
1314import { WorkspaceService } from '../../theia/workspace/workspace-service' ;
15+ import { ContextMenuRenderer , RenderContextMenuOptions } from '@theia/core/lib/browser' ;
16+ import { Disposable , DisposableCollection } from '@theia/core/lib/common/disposable' ;
17+ import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl' ;
18+ 19+ 20+ export const SKETCHBOOK__CONTEXT = [ 'arduino-sketchbook--context' ] ;
21+ 22+ // `Open Folder`, `Open in New Window`
23+ export const SKETCHBOOK__CONTEXT__MAIN_GROUP = [ ...SKETCHBOOK__CONTEXT , '0_main' ] ;
1424
1525@injectable ( )
1626export class SketchbookWidgetContribution extends AbstractViewContribution < SketchbookWidget > implements FrontendApplicationContribution {
@@ -27,6 +37,17 @@ export class SketchbookWidgetContribution extends AbstractViewContribution<Sketc
2737 @inject ( WorkspaceService )
2838 protected readonly workspaceService : WorkspaceService ;
2939
40+ @inject ( MenuModelRegistry )
41+ protected readonly menuRegistry : MenuModelRegistry ;
42+ 43+ @inject ( SketchesServiceClientImpl )
44+ protected readonly sketchServiceClient : SketchesServiceClientImpl ;
45+ 46+ @inject ( ContextMenuRenderer )
47+ protected readonly contextMenuRenderer : ContextMenuRenderer ;
48+ 49+ protected readonly toDisposeBeforeNewContextMenu = new DisposableCollection ( ) ;
50+ 3051 constructor ( ) {
3152 super ( {
3253 widgetId : 'arduino-sketchbook-widget' ,
@@ -64,16 +85,59 @@ export class SketchbookWidgetContribution extends AbstractViewContribution<Sketc
6485 isEnabled : ( ) => this . arduinoPreferences [ 'arduino.sketchbook.showAllFiles' ] ,
6586 isVisible : ( ) => this . arduinoPreferences [ 'arduino.sketchbook.showAllFiles' ]
6687 } ) ;
67- registry . registerCommand ( SketchbookCommands . OPEN , {
68- execute : arg => this . workspaceService . open ( arg . node . uri , { preserveWindow : true } ) ,
69- isEnabled : arg => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node ) ,
70- isVisible : arg => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node )
71- } ) ;
7288 registry . registerCommand ( SketchbookCommands . OPEN_NEW_WINDOW , {
7389 execute : arg => this . workspaceService . open ( arg . node . uri ) ,
7490 isEnabled : arg => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node ) ,
7591 isVisible : arg => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node )
7692 } ) ;
93+ 94+ registry . registerCommand ( SketchbookCommands . REVEAL_IN_FINDER , {
95+ execute : ( arg ) => {
96+ shell . openPath ( arg . node . id ) ;
97+ } ,
98+ isEnabled : ( arg ) => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node ) ,
99+ isVisible : ( arg ) => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node ) ,
100+ } ) ;
101+ 102+ 103+ registry . registerCommand ( SketchbookCommands . OPEN_SKETCHBOOK_CONTEXT_MENU , {
104+ isEnabled : ( arg ) => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node ) ,
105+ isVisible : ( arg ) => ! ! arg && 'node' in arg && SketchbookTree . SketchDirNode . is ( arg . node ) ,
106+ execute : async ( arg ) => {
107+ // cleanup previous context menu entries
108+ this . toDisposeBeforeNewContextMenu . dispose ( ) ;
109+ const container = arg . event . target ;
110+ if ( ! container ) {
111+ return ;
112+ }
113+ 114+ // disable the "open sketch" command for the current sketch.
115+ // otherwise make the command clickable
116+ const currentSketch = await this . sketchServiceClient . currentSketch ( ) ;
117+ if ( currentSketch && currentSketch . uri === arg . node . uri . toString ( ) ) {
118+ const placeholder = new PlaceholderMenuNode ( SKETCHBOOK__CONTEXT__MAIN_GROUP , SketchbookCommands . OPEN_NEW_WINDOW . label ! ) ;
119+ this . menuRegistry . registerMenuNode ( SKETCHBOOK__CONTEXT__MAIN_GROUP , placeholder ) ;
120+ this . toDisposeBeforeNewContextMenu . push ( Disposable . create ( ( ) => this . menuRegistry . unregisterMenuNode ( placeholder . id ) ) ) ;
121+ } else {
122+ this . menuRegistry . registerMenuAction ( SKETCHBOOK__CONTEXT__MAIN_GROUP , {
123+ commandId : SketchbookCommands . OPEN_NEW_WINDOW . id ,
124+ label : SketchbookCommands . OPEN_NEW_WINDOW . label ,
125+ } ) ;
126+ this . toDisposeBeforeNewContextMenu . push ( Disposable . create ( ( ) => this . menuRegistry . unregisterMenuAction ( SketchbookCommands . OPEN_NEW_WINDOW ) ) ) ;
127+ }
128+ 129+ 130+ const options : RenderContextMenuOptions = {
131+ menuPath : SKETCHBOOK__CONTEXT ,
132+ anchor : {
133+ x : container . getBoundingClientRect ( ) . left ,
134+ y : container . getBoundingClientRect ( ) . top + container . offsetHeight
135+ } ,
136+ args : arg
137+ }
138+ this . contextMenuRenderer . render ( options ) ;
139+ }
140+ } ) ;
77141 }
78142
79143 registerMenus ( registry : MenuModelRegistry ) : void {
@@ -88,6 +152,13 @@ export class SketchbookWidgetContribution extends AbstractViewContribution<Sketc
88152 label : 'Hide sketch files' ,
89153 order : '2'
90154 } ) ;
155+ 156+ registry . registerMenuAction ( SKETCHBOOK__CONTEXT__MAIN_GROUP , {
157+ commandId : SketchbookCommands . REVEAL_IN_FINDER . id ,
158+ label : SketchbookCommands . REVEAL_IN_FINDER . label ,
159+ order : '0'
160+ } ) ;
161+ 91162 }
92163
93164}
0 commit comments