Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 910db23

Browse files
committed
Restore window size and position. Fixes #224.
1 parent 4d9d869 commit 910db23

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/views/1_ApplicationComponent.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {ipcRenderer} from "electron";
77
import {KeyCode} from "../Enums";
88
import {remote} from "electron";
99
import * as css from "./css/main";
10+
import {restoreWindowBounds, saveWindowBounds} from "./ViewUtils";
1011

1112
interface State {
1213
sessions: Session[];
@@ -18,21 +19,31 @@ export default class ApplicationComponent extends React.Component<{}, State> {
1819

1920
constructor(props: {}) {
2021
super(props);
22+
const focusedWindow = remote.BrowserWindow.getFocusedWindow();
23+
24+
restoreWindowBounds(focusedWindow);
2125

2226
this.addTab();
2327
this.state = {sessions: this.activeTab.sessions};
2428

25-
$(window).resize(() => {
26-
for (const tab of this.tabs) {
27-
tab.updateAllSessionsDimensions();
28-
}
29-
});
29+
focusedWindow
30+
.on("move", () => saveWindowBounds(focusedWindow))
31+
.on("resize", () => {
32+
saveWindowBounds(focusedWindow);
33+
34+
for (const tab of this.tabs) {
35+
tab.updateAllSessionsDimensions();
36+
}
37+
});
3038

3139
ipcRenderer.on("change-working-directory", (event: Electron.IpcRendererEvent, directory: string) =>
3240
this.activeTab.activeSession().directory = directory
3341
);
3442

35-
window.onbeforeunload = () => this.closeAllTabs();
43+
window.onbeforeunload = () => {
44+
focusedWindow.removeAllListeners();
45+
this.closeAllTabs();
46+
}
3647
}
3748

3849
handleKeyDown(event: KeyboardEvent) {

src/views/ViewUtils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,19 @@ export function getCaretPosition(element: any): number {
7575
}
7676
return caretOffset;
7777
}
78+
79+
const windowBoundsKey = "windowBounds";
80+
81+
export function saveWindowBounds(browserWindow: Electron.BrowserWindow) {
82+
const bounds = browserWindow.getBounds();
83+
84+
localStorage.setItem(windowBoundsKey, JSON.stringify(bounds));
85+
}
86+
87+
export function restoreWindowBounds(browserWindow: Electron.BrowserWindow) {
88+
const windowBounds: Electron.Rectangle = JSON.parse(localStorage.getItem(windowBoundsKey));
89+
90+
if (windowBounds) {
91+
browserWindow.setBounds(windowBounds);
92+
}
93+
}

0 commit comments

Comments
 (0)