-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
261 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import { ipcMain } from 'electron'; | ||
import { readUserSettings, updateUserSettings } from '../storage'; | ||
import { PersistenStorage } from '../storage'; | ||
import { MainChannels } from '/common/constants'; | ||
|
||
export function listenForSettingMessages() { | ||
ipcMain.handle(MainChannels.GET_USER_SETTINGS, (e: Electron.IpcMainInvokeEvent) => { | ||
return readUserSettings(); | ||
return PersistenStorage.USER_SETTINGS.read(); | ||
}); | ||
|
||
ipcMain.handle(MainChannels.UPDATE_USER_SETTINGS, (e: Electron.IpcMainInvokeEvent, args) => { | ||
updateUserSettings(args); | ||
PersistenStorage.USER_SETTINGS.update(args); | ||
}); | ||
|
||
ipcMain.handle(MainChannels.GET_PROJECT_SETTINGS, (e: Electron.IpcMainInvokeEvent) => { | ||
return PersistenStorage.PROJECT_SETTINGS.read(); | ||
}); | ||
|
||
ipcMain.handle(MainChannels.UPDATE_PROJECT_SETTINGS, (e: Electron.IpcMainInvokeEvent, args) => { | ||
PersistenStorage.PROJECT_SETTINGS.update(args); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,33 @@ | ||
import { app } from 'electron'; | ||
import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
import { UserSettings } from '/common/models/settings'; | ||
import { ProjectSettings, UserSettings } from '/common/models/settings'; | ||
|
||
const path = app.getPath('userData'); | ||
const settingsPath = `${path}/user-settings.json`; | ||
export class PersistenStorage<T> { | ||
public readonly FILE_PATH: string; | ||
static readonly USER_SETTINGS = new PersistenStorage<UserSettings>('user-settings'); | ||
static readonly PROJECT_SETTINGS = new PersistenStorage<ProjectSettings>('project-settings'); | ||
|
||
export function updateUserSettings(settings: UserSettings) { | ||
const userSettings = readUserSettings(); | ||
writeUserSettings({ ...userSettings, ...settings }); | ||
} | ||
private constructor(public readonly fileName: string) { | ||
const APP_PATH = app.getPath('userData'); | ||
this.FILE_PATH = `${APP_PATH}/${fileName}.json`; | ||
} | ||
|
||
export function writeUserSettings(settings: UserSettings) { | ||
const userData = JSON.stringify(settings); | ||
writeFileSync(settingsPath, userData); | ||
} | ||
read(): T { | ||
if (!existsSync(this.FILE_PATH)) { | ||
return {} as T; | ||
} | ||
|
||
export function readUserSettings(): UserSettings { | ||
if (!existsSync(settingsPath)) { | ||
return {}; | ||
const content = readFileSync(this.FILE_PATH, 'utf8'); | ||
return JSON.parse(content || '') as T; | ||
} | ||
|
||
const content = readFileSync(settingsPath, 'utf8'); | ||
return JSON.parse(content || '') as UserSettings; | ||
write(value: T) { | ||
const data = JSON.stringify(value); | ||
writeFileSync(this.FILE_PATH, data); | ||
} | ||
|
||
update(value: T) { | ||
const existingValue = this.read(); | ||
this.write({ ...existingValue, ...value }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { debounce } from 'lodash'; | ||
import { makeAutoObservable } from 'mobx'; | ||
import { nanoid } from 'nanoid'; | ||
import { DefaultSettings, MainChannels } from '/common/constants'; | ||
import { | ||
FrameSettings, | ||
ProjectSettings, | ||
RectDimension, | ||
RectPosition, | ||
} from '/common/models/settings'; | ||
|
||
export class CanvasManager { | ||
private zoomScale: number = DefaultSettings.SCALE; | ||
private panPosition: RectPosition = DefaultSettings.POSITION; | ||
private idToFrame: Map<string, FrameSettings> = new Map(); | ||
|
||
constructor() { | ||
makeAutoObservable(this); | ||
this.restoreSettings(); | ||
} | ||
|
||
get scale() { | ||
return this.zoomScale; | ||
} | ||
|
||
set scale(value: number) { | ||
this.zoomScale = value; | ||
this.saveSettings(); | ||
} | ||
|
||
get position() { | ||
return this.panPosition; | ||
} | ||
|
||
set position(value: RectPosition) { | ||
this.panPosition = value; | ||
this.saveSettings(); | ||
} | ||
|
||
get frames() { | ||
return Array.from(this.idToFrame.values()); | ||
} | ||
|
||
saveFrame( | ||
id: string, | ||
newSettings: { | ||
url?: string; | ||
position?: RectPosition; | ||
dimension?: RectDimension; | ||
}, | ||
) { | ||
let frame = this.idToFrame.get(id); | ||
if (!frame) { | ||
return; | ||
} | ||
|
||
frame = { ...frame, ...newSettings }; | ||
this.idToFrame.set(id, frame); | ||
this.saveSettings(); | ||
} | ||
|
||
restoreSettings() { | ||
window.api.invoke(MainChannels.GET_PROJECT_SETTINGS).then((res) => { | ||
const settings: ProjectSettings = res as ProjectSettings; | ||
this.scale = settings.scale || DefaultSettings.SCALE; | ||
this.position = settings.position || DefaultSettings.POSITION; | ||
this.idToFrame = this.getFrameMap( | ||
settings.frames && settings.frames.length | ||
? settings.frames | ||
: [this.getDefaultFrame()], | ||
); | ||
}); | ||
} | ||
|
||
getFrameMap(frames: FrameSettings[]): Map<string, FrameSettings> { | ||
const map = new Map<string, FrameSettings>(); | ||
frames.forEach((frame) => { | ||
map.set(frame.id, frame); | ||
}); | ||
return map; | ||
} | ||
|
||
getDefaultFrame(): FrameSettings { | ||
return { | ||
id: nanoid(), | ||
url: DefaultSettings.URL, | ||
position: DefaultSettings.FRAME_POSITION, | ||
dimension: DefaultSettings.FRAME_DIMENSION, | ||
}; | ||
} | ||
|
||
saveSettings = debounce(this.undebouncedSaveSettings, 1000); | ||
|
||
private undebouncedSaveSettings() { | ||
const settings: ProjectSettings = { | ||
scale: this.zoomScale, | ||
position: this.panPosition, | ||
frames: Array.from(this.frames.values()), | ||
}; | ||
window.api.invoke( | ||
MainChannels.UPDATE_PROJECT_SETTINGS, | ||
JSON.parse(JSON.stringify(settings)), | ||
); | ||
} | ||
|
||
private clearSettings() { | ||
const settings: ProjectSettings = {}; | ||
window.api.invoke( | ||
MainChannels.UPDATE_PROJECT_SETTINGS, | ||
JSON.parse(JSON.stringify(settings)), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.