| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- class Preferences {
- private static Ctor = (() => {
- new Preferences();
- })();
- private fileSystem: Atomic.FileSystem;
- private static instance: Preferences;
- private _prefs: PreferencesFormat;
- constructor() {
- this.fileSystem = Atomic.getFileSystem();
- Preferences.instance = this;
- }
- registerRecentProject(path: string): void {
- var index = this._prefs.recentProjects.indexOf(path);
- if (index >= 0) {
- this._prefs.recentProjects.splice(index, 1);
- }
- this._prefs.recentProjects.unshift(path);
- this.updateRecentProjects();
- }
- unRegisterRecentProject(path: string): void {
- var index = this._prefs.recentProjects.indexOf(path);
- if (index >= 0) {
- this._prefs.recentProjects.splice(index, 1);
- }
- this.updateRecentProjects();
- }
- updateRecentProjects(): void {
- for (var i in this._prefs.recentProjects) {
- var path = this._prefs.recentProjects[i];
- if (!this.fileSystem.exists(path)) {
- this._prefs.recentProjects.splice(i, 1);
- }
- }
- }
- deleteRecentProjects(): void {
- this._prefs.recentProjects.length = 0;
- }
- getPreferencesFullPath(): string {
- var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
- filePath += "prefs.json";
- return filePath;
- }
- read(): void {
- var filePath = this.getPreferencesFullPath();
- var jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
- if (!jsonFile.isOpen()) return;
- var prefs = <PreferencesFormat> JSON.parse(jsonFile.readText());
- if (prefs) {
- if(!prefs.recentProjects) prefs.recentProjects = [""];
- this._prefs = prefs;
- }
- }
- write(): void {
- var filePath = this.getPreferencesFullPath();
- var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
- if (!jsonFile.isOpen()) return;
- var graphics = Atomic.getGraphics();
- var pos, width, height;
- if (graphics && !graphics.getFullscreen()) {
- pos = graphics.getWindowPosition();
- width = graphics.getWidth();
- height = graphics.getHeight();
- }
- this._prefs.window = { x: pos[0], y: pos[1], width: width, height: height };
- jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
- }
- get recentProjects(): [string] {
- return this._prefs.recentProjects;
- }
- get androidSDKPath(): string {
- return Atomic.addTrailingSlash(this._prefs.androidSDKPath);
- }
- set androidSDKPath(path: string) {
- this._prefs.androidSDKPath = path;
- this.write()
- }
- get jdkRootPath(): string {
- return Atomic.addTrailingSlash(this._prefs.jdkRootPath);
- }
- set jdkRootPath(path: string) {
- this._prefs.jdkRootPath = path;
- this.write();
- }
- get antPath(): string {
- return Atomic.addTrailingSlash(this._prefs.antPath);
- }
- set antPath(path: string) {
- this._prefs.antPath = path;
- this.write();
- }
- static getInstance(): Preferences {
- return Preferences.instance;
- }
- }
- class PreferencesFormat {
- recentProjects: [string];
- androidSDKPath: string;
- jdkRootPath: string;
- antPath: string;
- window: { x: number, y: number, width: number, height: number };
- }
- export = Preferences;
|