Preferences.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = new Atomic.File(filePath, Atomic.FILE_READ);
  46. if (!jsonFile.isOpen()) return;
  47. var prefs = <PreferencesFormat> JSON.parse(jsonFile.readText());
  48. if (prefs) {
  49. if(!prefs.recentProjects) prefs.recentProjects = [""];
  50. this._prefs = prefs;
  51. }
  52. }
  53. write(): void {
  54. var filePath = this.getPreferencesFullPath();
  55. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  56. if (!jsonFile.isOpen()) return;
  57. var graphics = Atomic.getGraphics();
  58. var pos, width, height;
  59. if (graphics && !graphics.getFullscreen()) {
  60. pos = graphics.getWindowPosition();
  61. width = graphics.getWidth();
  62. height = graphics.getHeight();
  63. }
  64. this._prefs.window = { x: pos[0], y: pos[1], width: width, height: height };
  65. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  66. }
  67. get recentProjects(): [string] {
  68. return this._prefs.recentProjects;
  69. }
  70. get androidSDKPath(): string {
  71. return Atomic.addTrailingSlash(this._prefs.androidSDKPath);
  72. }
  73. set androidSDKPath(path: string) {
  74. this._prefs.androidSDKPath = path;
  75. this.write()
  76. }
  77. get jdkRootPath(): string {
  78. return Atomic.addTrailingSlash(this._prefs.jdkRootPath);
  79. }
  80. set jdkRootPath(path: string) {
  81. this._prefs.jdkRootPath = path;
  82. this.write();
  83. }
  84. get antPath(): string {
  85. return Atomic.addTrailingSlash(this._prefs.antPath);
  86. }
  87. set antPath(path: string) {
  88. this._prefs.antPath = path;
  89. this.write();
  90. }
  91. static getInstance(): Preferences {
  92. return Preferences.instance;
  93. }
  94. }
  95. class PreferencesFormat {
  96. recentProjects: [string];
  97. androidSDKPath: string;
  98. jdkRootPath: string;
  99. antPath: string;
  100. window: { x: number, y: number, width: number, height: number };
  101. }
  102. export = Preferences;