Browse Source

Cleaning up the TypeScript plugin menu handling

Shaddock Heath 9 years ago
parent
commit
32c60a84d8

+ 22 - 24
Script/AtomicEditor/hostExtensions/languageExtensions/TypscriptLanguageExtension.ts

@@ -35,7 +35,8 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
      */
      */
     private isTypescriptProject = false;
     private isTypescriptProject = false;
     private serviceRegistry: Editor.HostExtensions.HostServiceLocator = null;
     private serviceRegistry: Editor.HostExtensions.HostServiceLocator = null;
-    private extensionMenu: Atomic.UIMenuItemSource = null;
+
+    /** Reference to the compileOnSaveMenuItem */
     private compileOnSaveMenuItem: Atomic.UIMenuItem;
     private compileOnSaveMenuItem: Atomic.UIMenuItem;
 
 
     /**
     /**
@@ -197,32 +198,28 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
         // got a load, we need to reset the language service
         // got a load, we need to reset the language service
         console.log(`${this.name}: received a project loaded event for project at ${ev.path}`);
         console.log(`${this.name}: received a project loaded event for project at ${ev.path}`);
         this.setTsConfigOnWebView(this.buildTsConfig());
         this.setTsConfigOnWebView(this.buildTsConfig());
-        this.rebuildMenu();
-    }
 
 
-    /**
-     * Rebuilds the plugin menu.  This is needed to toggle the CompileOnSave true or false
-     */
-    rebuildMenu() {
         if (this.isTypescriptProject) {
         if (this.isTypescriptProject) {
-
-            if (!this.extensionMenu) {
-                // First build up an empty menu then manually add the items so we can have reference to them
-                this.extensionMenu = this.serviceRegistry.uiServices.createPluginMenuItemSource("TypeScript", {});
-                this.compileOnSaveMenuItem = new Atomic.UIMenuItem("Compile on Save", `${this.name}.compileonsave`);
-                this.extensionMenu.addItem(this.compileOnSaveMenuItem);
-                this.extensionMenu.addItem(new Atomic.UIMenuItem("Compile Project", `${this.name}.compileproject`));
-            }
-
             const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
             const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
-            if (isCompileOnSave) {
-                this.compileOnSaveMenuItem.string = "Compile on Save: On";
-            } else {
-                this.compileOnSaveMenuItem.string = "Compile on Save: Off";
-            }
+
+            // Build the menu - First build up an empty menu then manually add the items so we can have reference to them
+            const menu = this.serviceRegistry.uiServices.createPluginMenuItemSource("TypeScript", {});
+            this.compileOnSaveMenuItem = new Atomic.UIMenuItem(`Compile on Save: ${isCompileOnSave ? "On" : "Off"}`, `${this.name}.compileonsave`);
+            menu.addItem(this.compileOnSaveMenuItem);
+            menu.addItem(new Atomic.UIMenuItem("Compile Project", `${this.name}.compileproject`));
         }
         }
     }
     }
 
 
+    /**
+     * Called when the project is unloaded
+     */
+    projectUnloaded() {
+        // Clean up
+        this.serviceRegistry.uiServices.removePluginMenuItemSource("TypeScript");
+        this.compileOnSaveMenuItem = null;
+        this.isTypescriptProject = false;
+    }
+
     /*** UIService implementation ***/
     /*** UIService implementation ***/
 
 
     /**
     /**
@@ -235,10 +232,11 @@ export default class TypescriptLanguageExtension implements Editor.HostExtension
         if (extension == this.name) {
         if (extension == this.name) {
             switch (action) {
             switch (action) {
                 case "compileonsave":
                 case "compileonsave":
+                    let isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
                     // Toggle
                     // Toggle
-                    const isCompileOnSave = this.serviceRegistry.projectServices.getUserPreference(this.name, "CompileOnSave", false);
-                    this.serviceRegistry.projectServices.setUserPreference(this.name, "CompileOnSave", !isCompileOnSave);
-                    this.rebuildMenu();
+                    isCompileOnSave = !isCompileOnSave;
+                    this.serviceRegistry.projectServices.setUserPreference(this.name, "CompileOnSave", isCompileOnSave);
+                    this.compileOnSaveMenuItem.string = `Compile on Save: ${isCompileOnSave ? "On" : "Off"}`;
                     return true;
                     return true;
                 case "compileproject":
                 case "compileproject":
                     this.doFullCompile();
                     this.doFullCompile();