Preferences.ts 3.3 KB

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