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

Commit

Permalink
Restore window bounds on load instead of changing them later.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-shatskyi committed May 31, 2016
1 parent ddc1c89 commit 32747c8
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const executors: Dictionary<(i: Job, a: string[]) => void> = {
let fullPath: string;

if (!args.length) {
fullPath = homeDirectory();
fullPath = homeDirectory;
} else {
const enteredPath = args[0];

Expand Down
2 changes: 1 addition & 1 deletion src/CommandExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ShellExecutionStrategy extends CommandExecutionStrategy {

class WindowsShellExecutionStrategy extends CommandExecutionStrategy {
static async canExecute(job: Job) {
return isWindows();
return isWindows;
}

startExecution() {
Expand Down
2 changes: 1 addition & 1 deletion src/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Environment {

get pwd(): string {
if (!this.get("PWD")) {
this.pwd = homeDirectory();
this.pwd = homeDirectory;
}

return this.get("PWD");
Expand Down
10 changes: 4 additions & 6 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import PluginManager from "./PluginManager";
import {Status} from "./Enums";
import ApplicationComponent from "./views/1_ApplicationComponent";
import Environment from "./Environment";
import {homeDirectory, normalizeDirectory, writeFileCreatingParents} from "./utils/Common";
import {homeDirectory, normalizeDirectory, writeFileCreatingParents, stateFilePath} from "./utils/Common";
import {remote} from "electron";
import * as Path from "path";

export default class Session extends EmitterWithUniqueID {
jobs: Array<Job> = [];
readonly environment = new Environment();
history: typeof History;
historicalCurrentDirectoriesStack: string[] = [];
private readonly stateFileName = Path.join(homeDirectory(), ".black-screen", "state");
// The value of the dictionary is the default value used if there is no serialized data.
private readonly serializableProperties: Dictionary<string> = {
directory: `String:${homeDirectory()}`,
directory: `String:${homeDirectory}`,
history: `History:[]`,
};

Expand Down Expand Up @@ -101,7 +99,7 @@ export default class Session extends EmitterWithUniqueID {
values[key] = Serializer.serialize((<any>this)[key])
);

writeFileCreatingParents(this.stateFileName, JSON.stringify(values)).then(
writeFileCreatingParents(stateFilePath, JSON.stringify(values)).then(
() => void 0,
(error: any) => { if (error) throw error; }
);
Expand All @@ -123,7 +121,7 @@ export default class Session extends EmitterWithUniqueID {

private readSerialized(): Dictionary<any> {
try {
return JSON.parse(readFileSync(this.stateFileName).toString());
return JSON.parse(readFileSync(stateFilePath).toString());
} catch (error) {
return this.serializableProperties;
}
Expand Down
25 changes: 22 additions & 3 deletions src/main/Main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {app, ipcMain, nativeImage, BrowserWindow, screen} from "electron";
import menu from "./Menu";
import {readFileSync} from "fs";
import {windowBoundsFilePath} from "../utils/Common";


let browserWindow: Electron.BrowserWindow = undefined;
Expand All @@ -16,7 +18,7 @@ app.on("mainWindow-all-closed", () => process.platform === "darwin" || app.quit(
ipcMain.on("quit", app.quit);

function getMainWindow(): Electron.BrowserWindow {
const workAreaSize = screen.getPrimaryDisplay().workAreaSize;
const bounds = windowBounds();

if (!browserWindow) {
let options: Electron.BrowserWindowOptions = {
Expand All @@ -28,8 +30,10 @@ function getMainWindow(): Electron.BrowserWindow {
resizable: true,
minWidth: 500,
minHeight: 300,
width: workAreaSize.width,
height: workAreaSize.height,
width: bounds.width,
height: bounds.height,
x: bounds.x,
y: bounds.y,
show: false,
};
browserWindow = new BrowserWindow(options);
Expand All @@ -48,3 +52,18 @@ function getMainWindow(): Electron.BrowserWindow {

return browserWindow;
}

function windowBounds(): Electron.Bounds {
try {
return JSON.parse(readFileSync(windowBoundsFilePath).toString());
} catch (error) {
const workAreaSize = screen.getPrimaryDisplay().workAreaSize;

return {
width: workAreaSize.width,
height: workAreaSize.height,
x: 0,
y: 0,
};
}
}
2 changes: 1 addition & 1 deletion src/plugins/RVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PluginManager from "../PluginManager";
import * as Path from "path";
import {homeDirectory, exists, readFile} from "../utils/Common";

const rvmDirectory = Path.join(homeDirectory(), ".rvm");
const rvmDirectory = Path.join(homeDirectory, ".rvm");
const rubyVersionFileName = ".ruby-version";
const gemSetNameFileName = ".ruby-gemset";

Expand Down
17 changes: 8 additions & 9 deletions src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,19 @@ export const {executablesInPaths} = new class {
};
};

export function homeDirectory(): string {
return process.env[(isWindows()) ? "USERPROFILE" : "HOME"];
}
export const isWindows = process.platform === "win32";
export const homeDirectory = process.env[(isWindows) ? "USERPROFILE" : "HOME"];

export function resolveDirectory(pwd: string, directory: string): string {
return normalizeDirectory(resolveFile(pwd, directory));
}

export function resolveFile(pwd: string, file: string): string {
return Path.resolve(pwd, file.replace(/^~/, homeDirectory()));
return Path.resolve(pwd, file.replace(/^~/, homeDirectory));
}

export function userFriendlyPath(path: string): string {
return path.replace(homeDirectory(), "~");
return path.replace(homeDirectory, "~");
}

export async function filterAsync<T>(values: T[], asyncPredicate: (t: T) => Promise<boolean>): Promise<T[]> {
Expand Down Expand Up @@ -215,10 +214,6 @@ function pluralFormOf(word: string) {
}
}

export function isWindows(): boolean {
return process.platform === "win32";
}

export function groupWhen<T>(grouper: (a: T, b: T) => boolean, row: T[]): T[][] {
if (row.length === 0) return [];
if (row.length === 1) return [row];
Expand Down Expand Up @@ -390,3 +385,7 @@ export function mapObject<T, R>(object: Dictionary<T>, mapper: (key: string, val

return result;
}

const baseConfigDirectory = Path.join(homeDirectory, ".black-screen");
export const stateFilePath = Path.join(baseConfigDirectory, "state");
export const windowBoundsFilePath = Path.join(baseConfigDirectory, "windowBounds");
5 changes: 1 addition & 4 deletions src/views/1_ApplicationComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ipcRenderer} from "electron";
import {KeyCode} from "../Enums";
import {remote} from "electron";
import * as css from "./css/main";
import {restoreWindowBounds, saveWindowBounds} from "./ViewUtils";
import {saveWindowBounds} from "./ViewUtils";

interface State {
sessions: Session[];
Expand All @@ -21,9 +21,6 @@ export default class ApplicationComponent extends React.Component<{}, State> {
super(props);
const focusedWindow = remote.BrowserWindow.getFocusedWindow();

restoreWindowBounds(focusedWindow);
setTimeout(() => this.recalculateDimensions(), 500);

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

Expand Down
18 changes: 5 additions & 13 deletions src/views/ViewUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {KeyCode} from "../Enums";
import * as _ from "lodash";
import {writeFileCreatingParents, windowBoundsFilePath} from "../utils/Common";

export function stopBubblingUp(event: Event): Event {
event.stopPropagation();
Expand Down Expand Up @@ -76,18 +77,9 @@ export function getCaretPosition(element: any): number {
return caretOffset;
}

const windowBoundsKey = "windowBounds";

export function saveWindowBounds(browserWindow: Electron.BrowserWindow) {
const bounds = browserWindow.getBounds();

localStorage.setItem(windowBoundsKey, JSON.stringify(bounds));
}

export function restoreWindowBounds(browserWindow: Electron.BrowserWindow) {
const windowBounds: Electron.Rectangle = JSON.parse(localStorage.getItem(windowBoundsKey));

if (windowBounds) {
browserWindow.setBounds(windowBounds, false);
}
writeFileCreatingParents(windowBoundsFilePath, JSON.stringify(browserWindow.getBounds())).then(
() => void 0,
(error: any) => { if (error) throw error; }
);
}

0 comments on commit 32747c8

Please sign in to comment.