| 5 | export type WindowStoreData = Record<string, {state: WindowState, opened?: boolean}>; |
| 6 | |
| 7 | export default class WindowStore { |
| 8 | #windowCreator: WindowCreator; |
| 9 | #windowStates: Record<string, WindowState> = {}; |
| 10 | #windows: Record<string, BaseWindow> = {}; |
| 11 | |
| 12 | constructor(windowCreator: WindowCreator) { |
| 13 | this.#windowCreator = windowCreator; |
| 14 | } |
| 15 | |
| 16 | deserialize(data: WindowStoreData) { |
| 17 | if (typeof data != 'object') // accepts empty data |
| 18 | data = {}; |
| 19 | this.#windowStates = {}; |
| 20 | for (const id in data) { |
| 21 | const wd = data[id]; |
| 22 | this.#windowStates[id] = wd.state; |
| 23 | if (wd.opened) |
| 24 | this.showWindow(id); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | serialize() { |
| 29 | const data: WindowStoreData = {}; |
| 30 | for (const id in this.#windowStates) { |
| 31 | data[id] = { |
| 32 | state: this.#windowStates[id], |
| 33 | opened: !!this.#windows[id], |
| 34 | }; |
| 35 | } |
| 36 | return data; |
| 37 | } |
| 38 | |
| 39 | // Try get window with |id|. |
| 40 | getWindow(id: string) { |
| 41 | return this.#windows[id]; |
| 42 | } |
| 43 | |
| 44 | // Get or create window with |id|. The window is created lazily and will |
| 45 | // be destroyed when closed. |
| 46 | getOrCreateWindow(id: string) { |
| 47 | let win = this.#windows[id]; |
| 48 | if (!win) { |
| 49 | win = this.#windows[id] = this.#windowCreator(id); |
| 50 | if (id in this.#windowStates) { |
| 51 | win.restoreState(this.#windowStates[id]); |
| 52 | } else { |
| 53 | win.restoreState({}); |
| 54 | win.window.center(); |
| 55 | } |
| 56 | win.window.onClose = () => { |
| 57 | this.saveWindowState(id); |
| 58 | // It is possible to cache, but windows are cheap to create in "gui" so |
| 59 | // we just recreate the window every time. |
| 60 | delete this.#windows[id]; |
| 61 | }; |
| 62 | } |
| 63 | return win; |
| 64 | } |
nothing calls this directly
no outgoing calls
no test coverage detected