Preferences.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. class Preferences {
  23. private static Ctor = (() => {
  24. new Preferences();
  25. })();
  26. private fileSystem: Atomic.FileSystem;
  27. private static instance: Preferences;
  28. private _prefs: PreferencesFormat;
  29. private _cachedProjectPreferences: Object = null;
  30. constructor() {
  31. this.fileSystem = Atomic.getFileSystem();
  32. Preferences.instance = this;
  33. }
  34. registerRecentProject(path: string): void {
  35. var index = this._prefs.recentProjects.indexOf(path);
  36. if (index >= 0) {
  37. this._prefs.recentProjects.splice(index, 1);
  38. }
  39. this._prefs.recentProjects.unshift(path);
  40. this.updateRecentProjects(true);
  41. }
  42. updateRecentProjects(write: boolean = false): void {
  43. for (var i = 0; i < this._prefs.recentProjects.length; i++) {
  44. var path = this._prefs.recentProjects[i];
  45. if (!this.fileSystem.exists(path)) {
  46. this._prefs.recentProjects.splice(i, 1);
  47. write = true;
  48. }
  49. }
  50. if (write)
  51. this.write();
  52. }
  53. deleteRecentProjects(): void {
  54. this._prefs.recentProjects.length = 0;
  55. this.write();
  56. }
  57. addColorHistory(path: string): void {
  58. var index = this._prefs.colorHistory.indexOf(path); // search array for entry
  59. if (index >= 0) { // if its in there,
  60. this._prefs.colorHistory.splice(index, 1); // REMOVE it.
  61. }
  62. this._prefs.colorHistory.unshift(path); // now add it to beginning of array
  63. this.updateColorHistory(true); // update and write out
  64. }
  65. updateColorHistory(write: boolean = false): void {
  66. var len = this._prefs.colorHistory.length; // we only need indexes 0-7 now
  67. var over = 8;
  68. if ( len >= over ) // have MOaR than we need
  69. this._prefs.colorHistory.splice( over, len - over ); // remove the excess
  70. if (write)
  71. this.write();
  72. }
  73. getPreferencesFullPath(): string {
  74. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  75. filePath += "prefs.json";
  76. return filePath;
  77. }
  78. read(): void {
  79. var filePath = this.getPreferencesFullPath();
  80. var jsonFile;
  81. //check if file doesn't exist, create default json
  82. if (!this.fileSystem.fileExists(filePath)) {
  83. this.useDefaultConfig();
  84. this.write();
  85. return;
  86. }
  87. //Read file
  88. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  89. var prefs = null;
  90. try {
  91. if (jsonFile.isOpen())
  92. prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
  93. } catch (e) {
  94. prefs = null;
  95. }
  96. if (prefs) {
  97. const defaultPrefs = new PreferencesFormat();
  98. const shouldWrite = defaultPrefs.applyMissingDefaults(prefs);
  99. this._prefs = prefs;
  100. if (shouldWrite) {
  101. this.write();
  102. }
  103. } else {
  104. console.log("Editor preference file missing or invalid, regenerating default configuration");
  105. this.useDefaultConfig();
  106. this.write();
  107. }
  108. }
  109. write(): boolean {
  110. var filePath = this.getPreferencesFullPath();
  111. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  112. if (!jsonFile.isOpen()) return false;
  113. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  114. }
  115. saveEditorWindowData(windowData: WindowData) {
  116. this._prefs.editorWindow = windowData;
  117. this.write();
  118. }
  119. savePlayerWindowData(windowData: WindowData) {
  120. this._prefs.playerWindow = windowData;
  121. this.write();
  122. }
  123. useDefaultConfig(): void {
  124. this._prefs = new PreferencesFormat();
  125. }
  126. get cachedProjectPreferences(): any {
  127. return this._cachedProjectPreferences;
  128. }
  129. get cachedApplicationPreferences(): PreferencesFormat {
  130. return this._prefs;
  131. }
  132. get editorWindow(): WindowData {
  133. return this._prefs.editorWindow;
  134. }
  135. get playerWindow(): WindowData {
  136. return this._prefs.playerWindow;
  137. }
  138. get recentProjects(): string[] {
  139. return this._prefs.recentProjects;
  140. }
  141. get colorHistory(): string[] {
  142. return this._prefs.colorHistory;
  143. }
  144. get uiData(): UserInterfaceData {
  145. return this._prefs.uiData;
  146. }
  147. get editorBuildData(): EditorBuildData {
  148. return this._prefs.editorBuildData;
  149. }
  150. get editorFeatures(): EditorFeatures {
  151. return this._prefs.editorFeatures;
  152. }
  153. static getInstance(): Preferences {
  154. return Preferences.instance;
  155. }
  156. /**
  157. * Load up the user preferences for the project
  158. */
  159. loadUserPrefs() {
  160. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  161. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  162. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  163. try {
  164. let prefs = JSON.parse(prefsFile.readText());
  165. this._cachedProjectPreferences = prefs;
  166. } finally {
  167. prefsFile.close();
  168. }
  169. }
  170. }
  171. /**
  172. * Return a preference value or the provided default from the user settings file located in the project
  173. * @param {string} settingsGroup name of the group these settings should fall under
  174. * @param {string} preferenceName name of the preference to retrieve
  175. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  176. * @return {number|boolean|string}
  177. */
  178. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: number): number;
  179. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: string): string;
  180. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: boolean): boolean;
  181. getUserPreference(settingsGroup: string, preferenceName: string, defaultValue?: any): any {
  182. // Cache the settings so we don't keep going out to the file
  183. if (this._cachedProjectPreferences == null) {
  184. this.loadUserPrefs();
  185. }
  186. if (this._cachedProjectPreferences && this._cachedProjectPreferences[settingsGroup]) {
  187. return this._cachedProjectPreferences[settingsGroup][preferenceName] || defaultValue;
  188. }
  189. // if all else fails
  190. return defaultValue;
  191. }
  192. /**
  193. * Sets a user preference value in the user settings file located in the project
  194. * @param {string} settingsGroup name of the group the preference lives under
  195. * @param {string} preferenceName name of the preference to set
  196. * @param {number | boolean | string} value value to set
  197. */
  198. setUserPreference(settingsGroup: string, preferenceName: string, value: number | boolean | string) {
  199. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  200. let prefs = {};
  201. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  202. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  203. try {
  204. prefs = JSON.parse(prefsFile.readText());
  205. } finally {
  206. prefsFile.close();
  207. }
  208. }
  209. prefs[settingsGroup] = prefs[settingsGroup] || {};
  210. prefs[settingsGroup][preferenceName] = value;
  211. let saveFile = new Atomic.File(prefsFileLoc, Atomic.FILE_WRITE);
  212. try {
  213. saveFile.writeString(JSON.stringify(prefs, null, " "));
  214. } finally {
  215. saveFile.flush();
  216. saveFile.close();
  217. }
  218. // Cache the update
  219. this._cachedProjectPreferences = prefs;
  220. }
  221. /**
  222. * Sets a group of user preference values in the user settings file located in the project. Elements in the
  223. * group will merge in with existing group preferences. Use this method if setting a bunch of settings
  224. * at once.
  225. * @param {string} settingsGroup name of the group the preference lives under
  226. * @param {string} groupPreferenceValues an object literal containing all of the preferences for the group.
  227. */
  228. setUserPreferenceGroup(settingsGroup: string, groupPreferenceValues: Object) {
  229. const prefsFileLoc = ToolCore.toolSystem.project.userPrefsFullPath;
  230. let prefs = {};
  231. if (Atomic.fileSystem.fileExists(prefsFileLoc)) {
  232. let prefsFile = new Atomic.File(prefsFileLoc, Atomic.FILE_READ);
  233. try {
  234. prefs = JSON.parse(prefsFile.readText());
  235. } finally {
  236. prefsFile.close();
  237. }
  238. }
  239. prefs[settingsGroup] = prefs[settingsGroup] || {};
  240. for (let preferenceName in groupPreferenceValues) {
  241. prefs[settingsGroup][preferenceName] = groupPreferenceValues[preferenceName];
  242. }
  243. let saveFile = new Atomic.File(prefsFileLoc, Atomic.FILE_WRITE);
  244. try {
  245. saveFile.writeString(JSON.stringify(prefs, null, " "));
  246. } finally {
  247. saveFile.flush();
  248. saveFile.close();
  249. }
  250. // Cache the update
  251. this._cachedProjectPreferences = prefs;
  252. }
  253. }
  254. interface WindowData {
  255. x: number;
  256. y: number;
  257. width: number;
  258. height: number;
  259. monitor: number;
  260. maximized: boolean;
  261. }
  262. interface MonacoEditorSettings {
  263. theme: string;
  264. fontSize: number;
  265. fontFamily: string;
  266. showInvisibles: boolean;
  267. useSoftTabs: boolean;
  268. tabSize: number;
  269. }
  270. interface UserInterfaceData {
  271. skinPath: string;
  272. defaultSkinPath: string;
  273. fontFile: string;
  274. fontName: string;
  275. fontSize: number;
  276. }
  277. interface EditorBuildData {
  278. lastEditorBuildSHA: string;
  279. }
  280. interface EditorFeatures {
  281. closePlayerLog: boolean;
  282. defaultLanguage: string;
  283. }
  284. class PreferencesFormat {
  285. constructor() {
  286. this.setDefault();
  287. }
  288. setDefault() {
  289. this.recentProjects = [];
  290. this.colorHistory = [ "#000000", "#ffffff", "#00ff00", "#0000ff", "#ff0000", "#ff00ff", "#ffff00", "#668866" ];
  291. this.editorWindow = {
  292. x: 0,
  293. y: 0,
  294. width: 0,
  295. height: 0,
  296. monitor: 0,
  297. maximized: true
  298. };
  299. this.playerWindow = {
  300. x: 0,
  301. y: 0,
  302. width: 0,
  303. height: 0,
  304. monitor: 0,
  305. maximized: false
  306. };
  307. this.codeEditor = {
  308. theme: "vs-dark",
  309. fontSize: 12,
  310. fontFamily: "",
  311. showInvisibles: false,
  312. useSoftTabs: true,
  313. tabSize: 4
  314. };
  315. this.uiData = {
  316. skinPath: "AtomicEditor/editor/skin/",
  317. defaultSkinPath: "AtomicEditor/resources/default_skin/",
  318. fontFile: "AtomicEditor/resources/vera.ttf",
  319. fontName: "Vera",
  320. fontSize: 12
  321. };
  322. this.editorBuildData = {
  323. lastEditorBuildSHA: "Unversioned Build"
  324. };
  325. this.editorFeatures = {
  326. closePlayerLog: true,
  327. defaultLanguage: "JavaScript"
  328. };
  329. }
  330. /**
  331. * Run through a provided prefs block and verify that all the sections are present. If any
  332. * are missing, add the defaults in
  333. * @param {PreferencesFormat} prefs
  334. * @return boolean returns true if any missing defaults were updated
  335. */
  336. applyMissingDefaults(prefs: PreferencesFormat) {
  337. let updatedMissingDefaults = false;
  338. if (!prefs.recentProjects) {
  339. prefs.recentProjects = this.recentProjects;
  340. updatedMissingDefaults = true;
  341. }
  342. if (!prefs.colorHistory) {
  343. prefs.colorHistory = this.colorHistory;
  344. updatedMissingDefaults = true;
  345. }
  346. if (!prefs.editorWindow) {
  347. prefs.editorWindow = this.editorWindow;
  348. updatedMissingDefaults = true;
  349. }
  350. if (!prefs.playerWindow) {
  351. prefs.playerWindow = this.playerWindow;
  352. updatedMissingDefaults = true;
  353. }
  354. if (!prefs.codeEditor) {
  355. prefs.codeEditor = this.codeEditor;
  356. updatedMissingDefaults = true;
  357. }
  358. if (!prefs.uiData) {
  359. prefs.uiData = this.uiData;
  360. updatedMissingDefaults = true;
  361. }
  362. if (!prefs.editorBuildData) {
  363. prefs.editorBuildData = this.editorBuildData;
  364. updatedMissingDefaults = true;
  365. }
  366. if (!prefs.editorFeatures) {
  367. prefs.editorFeatures = this.editorFeatures;
  368. updatedMissingDefaults = true;
  369. }
  370. return updatedMissingDefaults;
  371. }
  372. recentProjects: string[];
  373. editorWindow: WindowData;
  374. playerWindow: WindowData;
  375. codeEditor: MonacoEditorSettings;
  376. uiData: UserInterfaceData;
  377. editorBuildData: EditorBuildData;
  378. colorHistory: string[];
  379. editorFeatures: EditorFeatures;
  380. }
  381. export = Preferences;