This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(electron): remeber window position (fixes #121)
- Loading branch information
1 parent
fa32666
commit 7062cac
Showing
2 changed files
with
106 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { BrowserWindow, BrowserWindowConstructorOptions } from 'electron'; | ||
import { fromEvent } from 'rxjs/observable/fromEvent'; | ||
import { merge } from 'rxjs/observable/merge'; | ||
import { debounceTime, takeUntil } from 'rxjs/operators'; | ||
|
||
import { FileDataStore, IDataStore } from './server/datastore'; | ||
|
||
interface IState { | ||
fullscreen: boolean; | ||
maximized: boolean; | ||
options: Partial<BrowserWindowConstructorOptions>; | ||
} | ||
|
||
/** | ||
* ElectronWindowState stores and restores window position information. | ||
*/ | ||
export class ElectronWindowState { | ||
/** | ||
* Where window position preferences are stored in the IDataStore. | ||
*/ | ||
public static readonly storeKey = 'window-position'; | ||
|
||
constructor(private readonly store: IDataStore = new FileDataStore()) {} | ||
|
||
/** | ||
* Loads the stored window configuration. | ||
*/ | ||
public async restore(): Promise<Partial<BrowserWindowConstructorOptions>> { | ||
return (await this.loadState()).options; | ||
} | ||
|
||
/** | ||
* Watches for position changes in the window, saving preferences when | ||
* the window is moved or closed. | ||
*/ | ||
public async watch(window: BrowserWindow) { | ||
const state = await this.loadState(); | ||
if (state.maximized) { | ||
window.maximize(); | ||
} | ||
if (state.fullscreen) { | ||
window.setFullScreen(true); | ||
} | ||
|
||
merge(fromEvent(window, 'resize'), fromEvent(window, 'move')) | ||
.pipe(takeUntil(fromEvent(window, 'closed')), debounceTime(500)) | ||
.subscribe(async () => this.saveState(window)); | ||
} | ||
|
||
private async saveState(window: BrowserWindow): Promise<void> { | ||
await this.store.saveGlobal<IState>(ElectronWindowState.storeKey, { | ||
fullscreen: window.isFullScreen(), | ||
maximized: window.isMaximized(), | ||
options: window.getBounds(), | ||
}); | ||
} | ||
|
||
private async loadState(): Promise<IState> { | ||
return this.store.loadGlobal<IState>(ElectronWindowState.storeKey, { | ||
fullscreen: false, | ||
maximized: false, | ||
options: {}, | ||
}); | ||
} | ||
} |
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