Preferences.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.FileMode.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.FileMode.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.FileMode.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.FileMode.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.FileMode.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.FileMode.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.FileMode.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. defaultPath: string;
  283. defaultLanguage: string;
  284. }
  285. class PreferencesFormat {
  286. constructor() {
  287. this.setDefault();
  288. }
  289. setDefault() {
  290. this.recentProjects = [];
  291. this.colorHistory = [ "#000000", "#ffffff", "#00ff00", "#0000ff", "#ff0000", "#ff00ff", "#ffff00", "#668866" ];
  292. this.editorWindow = {
  293. x: 0,
  294. y: 0,
  295. width: 0,
  296. height: 0,
  297. monitor: 0,
  298. maximized: true
  299. };
  300. this.playerWindow = {
  301. x: 0,
  302. y: 0,
  303. width: 0,
  304. height: 0,
  305. monitor: 0,
  306. maximized: false
  307. };
  308. this.codeEditor = {
  309. theme: "vs-dark",
  310. fontSize: 12,
  311. fontFamily: "",
  312. showInvisibles: false,
  313. useSoftTabs: true,
  314. tabSize: 4
  315. };
  316. this.uiData = {
  317. skinPath: "AtomicEditor/editor/skin/",
  318. defaultSkinPath: "AtomicEditor/resources/default_skin/",
  319. fontFile: "AtomicEditor/resources/vera.ttf",
  320. fontName: "Vera",
  321. fontSize: 12
  322. };
  323. this.editorBuildData = {
  324. lastEditorBuildSHA: "Unversioned Build"
  325. };
  326. var fileSystem = Atomic.getFileSystem();
  327. var userDocuments = fileSystem.userDocumentsDir;
  328. if (Atomic.platform == "MacOSX") userDocuments += "Documents/";
  329. userDocuments += "AtomicProjects";
  330. this.editorFeatures = {
  331. closePlayerLog: true,
  332. defaultPath: userDocuments,
  333. defaultLanguage: "JavaScript"
  334. };
  335. }
  336. /**
  337. * Run through a provided prefs block and verify that all the sections are present. If any
  338. * are missing, add the defaults in
  339. * @param {PreferencesFormat} prefs
  340. * @return boolean returns true if any missing defaults were updated
  341. */
  342. applyMissingDefaults(prefs: PreferencesFormat) {
  343. let updatedMissingDefaults = false;
  344. if (!prefs.recentProjects) {
  345. prefs.recentProjects = this.recentProjects;
  346. updatedMissingDefaults = true;
  347. }
  348. if (!prefs.colorHistory) {
  349. prefs.colorHistory = this.colorHistory;
  350. updatedMissingDefaults = true;
  351. }
  352. if (!prefs.editorWindow) {
  353. prefs.editorWindow = this.editorWindow;
  354. updatedMissingDefaults = true;
  355. }
  356. if (!prefs.playerWindow) {
  357. prefs.playerWindow = this.playerWindow;
  358. updatedMissingDefaults = true;
  359. }
  360. if (!prefs.codeEditor) {
  361. prefs.codeEditor = this.codeEditor;
  362. updatedMissingDefaults = true;
  363. }
  364. if (!prefs.uiData) {
  365. prefs.uiData = this.uiData;
  366. updatedMissingDefaults = true;
  367. }
  368. if (!prefs.editorBuildData) {
  369. prefs.editorBuildData = this.editorBuildData;
  370. updatedMissingDefaults = true;
  371. }
  372. if (!prefs.editorFeatures) {
  373. prefs.editorFeatures = this.editorFeatures;
  374. updatedMissingDefaults = true;
  375. }
  376. if (!prefs.editorFeatures.defaultPath) {
  377. prefs.editorFeatures.defaultPath = this.editorFeatures.defaultPath;
  378. updatedMissingDefaults = true;
  379. }
  380. return updatedMissingDefaults;
  381. }
  382. recentProjects: string[];
  383. editorWindow: WindowData;
  384. playerWindow: WindowData;
  385. codeEditor: MonacoEditorSettings;
  386. uiData: UserInterfaceData;
  387. editorBuildData: EditorBuildData;
  388. colorHistory: string[];
  389. editorFeatures: EditorFeatures;
  390. }
  391. export = Preferences;