Preferences.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. class Preferences {
  2. private static Ctor = (() => {
  3. new Preferences();
  4. })();
  5. private fileSystem: Atomic.FileSystem;
  6. private static instance: Preferences;
  7. private _prefs: PreferencesFormat;
  8. constructor() {
  9. this.fileSystem = Atomic.getFileSystem();
  10. Preferences.instance = this;
  11. }
  12. registerRecentProject(path: string): void {
  13. var index = this._prefs.recentProjects.indexOf(path);
  14. if (index >= 0) {
  15. this._prefs.recentProjects.splice(index, 1);
  16. }
  17. this._prefs.recentProjects.unshift(path);
  18. this.updateRecentProjects();
  19. }
  20. unRegisterRecentProject(path: string): void {
  21. var index = this._prefs.recentProjects.indexOf(path);
  22. if (index >= 0) {
  23. this._prefs.recentProjects.splice(index, 1);
  24. }
  25. this.updateRecentProjects();
  26. }
  27. updateRecentProjects(): void {
  28. for (var i in this._prefs.recentProjects) {
  29. var path = this._prefs.recentProjects[i];
  30. if (!this.fileSystem.exists(path)) {
  31. this._prefs.recentProjects.splice(i, 1);
  32. }
  33. }
  34. }
  35. deleteRecentProjects(): void {
  36. this._prefs.recentProjects.length = 0;
  37. }
  38. getPreferencesFullPath(): string {
  39. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  40. filePath += "prefs.json";
  41. return filePath;
  42. }
  43. read(): void {
  44. var filePath = this.getPreferencesFullPath();
  45. var jsonFile;
  46. //check if file doesn't exists, create an empty JSON file
  47. if (!this.fileSystem.fileExists(filePath)) {
  48. jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  49. jsonFile.writeString("{}");
  50. jsonFile.close();
  51. }
  52. //Read file
  53. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  54. var prefs = <PreferencesFormat>JSON.parse(jsonFile.readText());
  55. if (prefs) {
  56. if (!prefs.recentProjects) prefs.recentProjects = [""];
  57. this._prefs = prefs;
  58. }
  59. }
  60. write(): void {
  61. var filePath = this.getPreferencesFullPath();
  62. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  63. if (!jsonFile.isOpen()) return;
  64. var graphics = Atomic.getGraphics();
  65. var pos, width, height;
  66. if (graphics && !graphics.getFullscreen()) {
  67. pos = graphics.getWindowPosition();
  68. width = graphics.getWidth();
  69. height = graphics.getHeight();
  70. }
  71. this._prefs.window = { x: pos[0], y: pos[1], width: width, height: height };
  72. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  73. }
  74. get recentProjects(): [string] {
  75. return this._prefs.recentProjects;
  76. }
  77. static getInstance(): Preferences {
  78. return Preferences.instance;
  79. }
  80. }
  81. class PreferencesFormat {
  82. recentProjects: [string];
  83. window: { x: number, y: number, width: number, height: number };
  84. }
  85. export = Preferences;