Browse Source

Merge pull request #1435 from AtomicGameEngine/TSH-ATOMIC-DYNAMIC-WEBVIEW-SETTINGS

User and Application settings improvements
JoshEngebretson 8 years ago
parent
commit
123bcb0ae0

+ 32 - 1
Script/AtomicEditor/editor/Editor.ts

@@ -152,7 +152,7 @@ class AtomicEditor extends Atomic.ScriptObject {
 
 
     /**
-     * Sets a user preference value in the user settings file
+     * Sets a user preference value in the project user settings file
      * @param  {string} extensionName name of the extension the preference lives under
      * @param  {string} preferenceName name of the preference to set
      * @param  {number | boolean | string} value value to set
@@ -168,6 +168,37 @@ class AtomicEditor extends Atomic.ScriptObject {
         this.sendEvent(Editor.UserPreferencesChangedNotificationEventData(eventData));
     }
 
+    /**
+     * Return a preference value or the provided default from the global user settings file
+     * @param  {string} groupName name of the section the preference lives under
+     * @param  {string} preferenceName name of the preference to retrieve
+     * @param  {number | boolean | string} defaultValue value to return if pref doesn't exist
+     * @return {number|boolean|string}
+     */
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
+    getApplicationPreference(groupName: string, preferenceName: string, defaultValue?: any): any {
+        return Preferences.getInstance().getApplicationPreference(groupName, preferenceName, defaultValue);
+    }
+
+    /**
+     * Sets a user preference value in the global application settings file
+     * @param  {string} groupName name of the section the preference lives under
+     * @param  {string} preferenceName name of the preference to set
+     * @param  {number | boolean | string} value value to set
+     */
+    setApplicationPreference(groupName: string, preferenceName: string, value: number | boolean | string) {
+        Preferences.getInstance().setApplicationPreference(groupName, preferenceName, value);
+        WebView.WebBrowserHost.setGlobalStringProperty("HOST_Preferences", "ApplicationPreferences", JSON.stringify(Preferences.getInstance().cachedApplicationPreferences, null, 2 ));
+        const eventData: EditorEvents.UserPreferencesChangedEvent = {
+            projectPreferences: JSON.stringify(Preferences.getInstance().cachedProjectPreferences),
+            applicationPreferences: JSON.stringify(Preferences.getInstance().cachedApplicationPreferences)
+        };
+
+        this.sendEvent(EditorEvents.UserPreferencesChangedNotification, eventData);
+    }
+
     /**
      * Sets a group of user preference values in the user settings file located in the project.  Elements in the
      * group will merge in with existing group preferences.  Use this method if setting a bunch of settings

+ 60 - 7
Script/AtomicEditor/editor/Preferences.ts

@@ -235,18 +235,17 @@ class Preferences {
     }
 
     /**
-     * Sets a user preference value in the user settings file located in the project
+     * Sets a preference value in a preferences file that is provided
+     * @param  {string} preferencesFilePath path to the prefs file to update
      * @param  {string} settingsGroup name of the group the preference lives under
      * @param  {string} preferenceName name of the preference to set
      * @param  {number | boolean | string} value value to set
      */
-    setUserPreference(settingsGroup: string, preferenceName: string, value: number | boolean | string) {
-
-        const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
+    setGenericPreference(preferencesFilePath: string, settingsGroup: string, preferenceName: string, value: number | boolean | string): Object {
         let prefs = {};
 
-        if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
-            let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FileMode.FILE_READ);
+        if (Atomic.fileSystem.fileExists(preferencesFilePath)) {
+            let prefsFile = new Atomic.File(preferencesFilePath, Atomic.FileMode.FILE_READ);
             try {
                 prefs = JSON.parse(prefsFile.readText());
             } finally {
@@ -257,7 +256,7 @@ class Preferences {
         prefs[settingsGroup] = prefs[settingsGroup] || {};
         prefs[settingsGroup][preferenceName] = value;
 
-        let saveFile = new Atomic.File(prefsFileLoc, Atomic.FileMode.FILE_WRITE);
+        let saveFile = new Atomic.File(preferencesFilePath, Atomic.FileMode.FILE_WRITE);
         try {
             saveFile.writeString(JSON.stringify(prefs, null, "  "));
         } finally {
@@ -265,10 +264,64 @@ class Preferences {
             saveFile.close();
         }
 
+        // Cache the update
+        return prefs;
+    }
+
+    /**
+     * Sets a user preference value in the user settings file located in the project
+     * @param  {string} settingsGroup name of the group the preference lives under
+     * @param  {string} preferenceName name of the preference to set
+     * @param  {number | boolean | string} value value to set
+     */
+    setUserPreference(settingsGroup: string, preferenceName: string, value: number | boolean | string) {
+
+        const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
+        const prefs = this.setGenericPreference(prefsFileLoc, settingsGroup, preferenceName, value);
+
         // Cache the update
         this._cachedProjectPreferences = prefs;
     }
 
+    /**
+     * Sets an editor preference value in the global user settings file
+     * @param  {string} settingsGroup name of the group the preference lives under
+     * @param  {string} preferenceName name of the preference to set
+     * @param  {number | boolean | string} value value to set
+     */
+    setApplicationPreference(settingsGroup: string, preferenceName: string, value: number | boolean | string) {
+
+        const prefsFileLoc = this.getPreferencesFullPath();
+        const prefs = this.setGenericPreference(prefsFileLoc, settingsGroup, preferenceName, value);
+
+        // Cache the update
+        this._prefs = prefs as PreferencesFormat;
+    }
+
+    /**
+     * Return a preference value or the provided default from the global user settings file located in the project
+     * @param  {string} settingsGroup name of the group these settings should fall under
+     * @param  {string} preferenceName name of the preference to retrieve
+     * @param  {number | boolean | string} defaultValue value to return if pref doesn't exist
+     * @return {number|boolean|string}
+     */
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: any): any {
+
+        // Cache the settings so we don't keep going out to the file
+        if (this._prefs == null) {
+            this.read();
+        }
+
+        if (this._prefs && this._prefs[settingsGroup]) {
+            return this._prefs[settingsGroup][preferenceName] || defaultValue;
+        }
+
+        // if all else fails
+        return defaultValue;
+    }
 
     /**
      * Sets a group of user preference values in the user settings file located in the project.  Elements in the

+ 24 - 1
Script/AtomicEditor/hostExtensions/HostExtensionServices.ts

@@ -139,9 +139,22 @@ export class ProjectServicesProvider extends ServicesProvider<Editor.HostExtensi
         return EditorUI.getEditor().getUserPreference(extensionName, preferenceName, defaultValue);
     }
 
+    /**
+     * Return a preference value or the provided default from the global user settings file
+     * @param  {string} groupName name of the section the preference lives under
+     * @param  {string} preferenceName name of the preference to retrieve
+     * @param  {number | boolean | string} defaultValue value to return if pref doesn't exist
+     * @return {number|boolean|string}
+     */
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
+    getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: any): any {
+        return EditorUI.getEditor().getApplicationPreference(settingsGroup, preferenceName, defaultValue);
+    }
 
     /**
-     * Sets a user preference value in the user settings file
+     * Sets a user preference value in the project user settings file
      * @param  {string} extensionName name of the extension the preference lives under
      * @param  {string} preferenceName name of the preference to set
      * @param  {number | boolean | string} value value to set
@@ -150,6 +163,16 @@ export class ProjectServicesProvider extends ServicesProvider<Editor.HostExtensi
         EditorUI.getEditor().setUserPreference(extensionName, preferenceName, value);
     }
 
+    /**
+     * Sets an editor preference value in the global application settings file
+     * @param  {string} extensionName name of the extension the preference lives under
+     * @param  {string} preferenceName name of the preference to set
+     * @param  {number | boolean | string} value value to set
+     */
+    setApplicationPreference(extensionName: string, preferenceName: string, value: number | boolean | string) {
+        EditorUI.getEditor().setApplicationPreference(extensionName, preferenceName, value);
+    }
+
     /**
      * Sets a group of user preference values in the user settings file located in the project.  Elements in the
      * group will merge in with existing group preferences.  Use this method if setting a bunch of settings

+ 24 - 12
Script/AtomicWebViewEditor/editor/editorCommands.ts

@@ -32,6 +32,28 @@ import ClientExtensionEventNames from "../clientExtensions/ClientExtensionEventN
  */
 export function configure(fileExt: string, filename: string) {
 
+    let monacoEditor = <monaco.editor.IStandaloneCodeEditor>internalEditor.getInternalEditor();
+
+    updateEditorPrefs();
+
+    // give the language extensions the opportunity to configure the editor based upon the file type
+    serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
+        fileExt: fileExt,
+        filename: filename,
+        editor: monacoEditor
+    });
+
+    // Override CMD/CTRL+I since that is going to be used for Format Code and in the editor it is assigned to something else
+    const noOpCommand: monaco.editor.ICommandHandler = () => { };
+    monacoEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_I, noOpCommand, null);
+
+    updateEditorPrefs();
+}
+
+/**
+ * Update the editor prefs
+ */
+export function updateEditorPrefs() {
     // converter to handle new version of the renderWhitespace setting
     const renderWhitespaceAdapter = (setting): "none" | "boundary" | "all" => {
         switch (setting.toLowerCase()) {
@@ -49,18 +71,6 @@ export function configure(fileExt: string, filename: string) {
         fontSize: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontSize", 12),
         fontFamily: serviceLocator.clientServices.getApplicationPreference("codeEditor", "fontFamily", "")
     });
-
-    // give the language extensions the opportunity to configure the editor based upon the file type
-    serviceLocator.sendEvent(ClientExtensionEventNames.ConfigureEditorEvent, {
-        fileExt: fileExt,
-        filename: filename,
-        editor: monacoEditor
-    });
-
-    // Override CMD/CTRL+I since that is going to be used for Format Code and in the editor it is assigned to something else
-    const noOpCommand: monaco.editor.ICommandHandler = () => { };
-    monacoEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_I, noOpCommand, null);
-
 }
 
 /**
@@ -153,6 +163,8 @@ export function editorLoaded() {
  */
 export function preferencesChanged(prefs: Editor.ClientExtensions.PreferencesChangedEventData) {
     serviceLocator.clientServices.setPreferences(prefs.projectPreferences, prefs.applicationPreferences);
+    updateEditorPrefs();
+
     serviceLocator.sendEvent(ClientExtensionEventNames.PreferencesChangedEvent, prefs);
 }
 

+ 20 - 1
Script/TypeScript/EditorWork.d.ts

@@ -194,12 +194,31 @@ declare module Editor.HostExtensions {
         getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
 
         /**
-         * Sets a user preference value in the user settings file
+         * Return a preference value or the provided default from the global user settings file
+         * @param  {string} extensionName name of the section the preference lives under
+         * @param  {string} preferenceName name of the preference to retrieve
+         * @param  {number | boolean | string} defaultValue value to return if pref doesn't exist
+         * @return {number|boolean|string}
+         */
+        getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
+        getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
+        getApplicationPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
+
+        /**
+         * Sets a user preference value in the project settings file
          * @param  {string} extensionName name of the extension the preference lives under
          * @param  {string} preferenceName name of the preference to set
          * @param  {number | boolean | string} value value to set
          */
         setUserPreference(extensionName: string, preferenceName: string, value: number | boolean | string);
+
+        /**
+         * Sets an editor preference value in the global editor settings file
+         * @param  {string} groupName name of the section the preference lives under
+         * @param  {string} preferenceName name of the preference to set
+         * @param  {number | boolean | string} value value to set
+         */
+        setApplicationPreference(groupName: string, preferenceName: string, value: number | boolean | string);
     }
 
     export interface SceneServicesEventListener extends Editor.Extensions.ServiceEventListener {