Skip to content

Commit

Permalink
Plug WindowStateKeeper in createInterface method.
Browse files Browse the repository at this point in the history
Storing window attributes and using them on electron app startup.
  • Loading branch information
dafuga committed May 24, 2018
1 parent b0babce commit 3683e79
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
13 changes: 10 additions & 3 deletions app/main/basic/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { BrowserWindow, ipcMain } from 'electron';
import { windowStateKeeper } from '../shared/windowStateKeeper'

const path = require('path');
const log = require('electron-log');

const createInterface = (resourcePath, route = '/', closable = true) => {
const createInterface = (resourcePath, route = '/', closable = true, store) => {
log.info('ui: creating');

const uiStateKeeper = windowStateKeeper(store);

const ui = new BrowserWindow({
closable,
width: 960,
height: 540,
x: uiStateKeeper.x,
y: uiStateKeeper.y,
width: uiStateKeeper.width,
height: uiStateKeeper.height,
show: false,
frame: true,
fullscreenable: false,
Expand All @@ -18,6 +23,8 @@ const createInterface = (resourcePath, route = '/', closable = true) => {
alwaysOnTop: false
});

uiStateKeeper.track(ui);

ui.loadURL(`file://${path.join(resourcePath, 'renderer/basic/index.html')}#${route}`);

ui.webContents.on('did-finish-load', () => {
Expand Down
25 changes: 1 addition & 24 deletions app/main/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { configureStore } from '../shared/store/main/configureStore';
import { configureLocalization } from './shared/i18n';

import { createInterface } from './basic';
import { windowStateKeeper } from './shared/windowStateKeeper'

const path = require('path');
const log = require('electron-log');
Expand Down Expand Up @@ -88,8 +87,6 @@ app.on('ready', async () => {

initMenu();
}

createMainWindow();
});

// debug event logging
Expand All @@ -103,7 +100,7 @@ app.on('will-quit', () => { log.info('app: will-quit'); });
app.on('quit', () => { log.info('app: quit'); });

const initManager = (route = '/', closable = true) => {
ui = createInterface(resourcePath, route, closable);
ui = createInterface(resourcePath, route, closable, store);
ui.on('close', () => {
ui = null;
});
Expand All @@ -115,24 +112,4 @@ const showManager = () => {
}
};

function createMainWindow() {
// Get window state
const mainWindowStateKeeper = windowStateKeeper(store);

// Creating the window
const windowOptions = {
title: 'Main Window',
x: mainWindowStateKeeper.x,
y: mainWindowStateKeeper.y,
width: mainWindowStateKeeper.width,
height: mainWindowStateKeeper.height,
};

mainWindow = new BrowserWindow(windowOptions);
// Track window state
mainWindowStateKeeper.track(mainWindow);
// Load content
return mainWindow;
}

global.showManager = showManager;
8 changes: 4 additions & 4 deletions app/main/shared/windowStateKeeper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const appConfig = require('electron-settings');
import {setSetting} from '../../shared/actions/settings'

function windowStateKeeper(store) {
export function windowStateKeeper(store) {
let window, windowState;

function setBounds() {
// Restore from appConfig
// Restore from store
if (store.getState().settings.setupData) {
windowState = store.getState().settings.setupData;
return;
Expand All @@ -23,7 +23,7 @@ function windowStateKeeper(store) {
windowState = window.getBounds();
}
windowState.isMaximized = window.isMaximized();
appConfig.set(`windowState.${windowName}`, windowState);
store.dispatch(setSetting('setupData', windowState));
}

function track(win) {
Expand Down
5 changes: 3 additions & 2 deletions app/shared/reducers/settings.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as types from '../actions/types';

const initialState = {
account: '',
node: 'http://eos.jesta.us'
account: 'lioninjungle',
node: 'http://dev.cryptolions.io:38888'
};

export default function settings(state = initialState, action) {
console.log(JSON.stringify(state))
switch (action.type) {
case types.SET_SETTING: {
return Object.assign({}, state, action.payload);
Expand Down

0 comments on commit 3683e79

Please sign in to comment.