Skip to content

Commit

Permalink
Merge pull request #46 from hardchor/reminders#21
Browse files Browse the repository at this point in the history
Reminders#21
  • Loading branch information
hardchor committed Jun 4, 2016
2 parents ae59a43 + 66ca06a commit 143b90a
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 136 deletions.
84 changes: 45 additions & 39 deletions app/main/createMainWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,59 @@ import path from 'path';

const mainHtml = path.join(__dirname, '../renderer/assets/html/main.html');

export default function createWindow() {
let browserWindow = null;

function showWindow() {
browserWindow.maximize();
browserWindow.show();
browserWindow.focus();
}

export default function createWindow({ uri = '/' } = {}) {
if (browserWindow !== null) {
if (!browserWindow.webContents.isLoading()) {
showWindow();
}
return browserWindow;
}

let menu;
let template;

const window = new BrowserWindow({
browserWindow = new BrowserWindow({
show: false,
width: 1024,
height: 728,
});

function handleRedirect(e, url) {
if (url !== window.webContents.getURL()) {
if (url !== browserWindow.webContents.getURL()) {
e.preventDefault();
shell.openExternal(url);
}
}

window.maximize();
browserWindow.loadURL(`file://${mainHtml}#${uri}`);

window.loadURL(`file://${mainHtml}`);
browserWindow.on('closed', () => {
browserWindow = null;
});

window.webContents.on('did-finish-load', () => {
window.show();
window.focus();
browserWindow.webContents.on('did-finish-load', () => {
showWindow();
});
window.webContents.on('will-navigate', handleRedirect);
window.webContents.on('new-window', handleRedirect);
browserWindow.webContents.on('will-navigate', handleRedirect);
browserWindow.webContents.on('new-window', handleRedirect);

if (process.env.NODE_ENV === 'development') {
window.openDevTools();
browserWindow.openDevTools();
}

if (process.platform === 'darwin') {
template = [{
label: 'Electron',
submenu: [{
label: 'About ElectronReact',
label: 'About Timesheets',
selector: 'orderFrontStandardAboutPanel:',
}, {
type: 'separator',
Expand Down Expand Up @@ -103,25 +119,25 @@ export default function createWindow() {
label: 'Reload',
accelerator: 'Command+R',
click() {
window.webContents.reload();
browserWindow.webContents.reload();
},
}, {
label: 'Toggle Full Screen',
accelerator: 'Ctrl+Command+F',
click() {
window.setFullScreen(!window.isFullScreen());
browserWindow.setFullScreen(!browserWindow.isFullScreen());
},
}, {
label: 'Toggle Developer Tools',
accelerator: 'Alt+Command+I',
click() {
window.toggleDevTools();
browserWindow.toggleDevTools();
},
}] : [{
label: 'Toggle Full Screen',
accelerator: 'Ctrl+Command+F',
click() {
window.setFullScreen(!window.isFullScreen());
browserWindow.setFullScreen(!browserWindow.isFullScreen());
},
}],
}, {
Expand All @@ -145,22 +161,17 @@ export default function createWindow() {
submenu: [{
label: 'Learn More',
click() {
shell.openExternal('http://electron.atom.io');
shell.openExternal('http://burgiblog.com/timesheets/');
},
}, {
label: 'Documentation',
click() {
shell.openExternal('https://github.com/atom/electron/tree/master/docs#readme');
},
}, {
label: 'Community Discussions',
click() {
shell.openExternal('https://discuss.atom.io/c/electron');
shell.openExternal('https://github.com/hardchor/timesheets#readme');
},
}, {
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/atom/electron/issues');
shell.openExternal('https://github.com/hardchor/timesheets/issues');
},
}],
}];
Expand All @@ -177,7 +188,7 @@ export default function createWindow() {
label: '&Close',
accelerator: 'Ctrl+W',
click() {
window.close();
browserWindow.close();
},
}],
}, {
Expand All @@ -186,54 +197,49 @@ export default function createWindow() {
label: '&Reload',
accelerator: 'Ctrl+R',
click() {
window.webContents.reload();
browserWindow.webContents.reload();
},
}, {
label: 'Toggle &Full Screen',
accelerator: 'F11',
click() {
window.setFullScreen(!window.isFullScreen());
browserWindow.setFullScreen(!browserWindow.isFullScreen());
},
}, {
label: 'Toggle &Developer Tools',
accelerator: 'Alt+Ctrl+I',
click() {
window.toggleDevTools();
browserWindow.toggleDevTools();
},
}] : [{
label: 'Toggle &Full Screen',
accelerator: 'F11',
click() {
window.setFullScreen(!window.isFullScreen());
browserWindow.setFullScreen(!browserWindow.isFullScreen());
},
}],
}, {
label: 'Help',
submenu: [{
label: 'Learn More',
click() {
shell.openExternal('http://electron.atom.io');
shell.openExternal('http://burgiblog.com/timesheets/');
},
}, {
label: 'Documentation',
click() {
shell.openExternal('https://github.com/atom/electron/tree/master/docs#readme');
},
}, {
label: 'Community Discussions',
click() {
shell.openExternal('https://discuss.atom.io/c/electron');
shell.openExternal('https://github.com/hardchor/timesheets#readme');
},
}, {
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/atom/electron/issues');
shell.openExternal('https://github.com/hardchor/timesheets/issues');
},
}],
}];
menu = Menu.buildFromTemplate(template);
window.setMenu(menu);
browserWindow.setMenu(menu);
}

return window;
return browserWindow;
}
57 changes: 42 additions & 15 deletions app/main/createMenuBarWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,57 @@ import Positioner from 'electron-positioner';

const menuBarHtml = path.join(__dirname, '../renderer/assets/html/menubar.html');

export default function createMenuBar(trayBounds) {
const window = new BrowserWindow({
let browserWindow = null;
let cachedTrayBounds;

function positionWindow() {
let windowPosition = 'topRight';
if (cachedTrayBounds) {
windowPosition = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter';
}
const positioner = new Positioner(browserWindow);
const { x, y } = positioner.calculate(windowPosition, cachedTrayBounds);

browserWindow.setPosition(x, y);
}

function showWindow() {
browserWindow.show();
browserWindow.focus();
}

export default function createMenuBar({ trayBounds, uri = '/' } = {}) {
if (trayBounds) cachedTrayBounds = trayBounds;

if (browserWindow !== null) {
positionWindow();
if (!browserWindow.webContents.isLoading()) {
showWindow();
}
return browserWindow;
}

browserWindow = new BrowserWindow({
show: false,
width: 338,
height: 600,
frame: false,
show: false,
});

window.loadURL(`file://${menuBarHtml}`);
browserWindow.loadURL(`file://${menuBarHtml}#${uri}`);

window.webContents.on('did-finish-load', () => {
// Default the window to the right if `trayPos` bounds are undefined or null.
const windowPosition = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter';
const positioner = new Positioner(window);
const { x, y } = positioner.calculate(windowPosition, trayBounds);
browserWindow.on('closed', () => {
browserWindow = null;
});

window.setPosition(x, y);
window.show();
window.focus();
browserWindow.webContents.on('did-finish-load', () => {
positionWindow();
showWindow();
});

window.on('blur', () => {
window.hide();
browserWindow.on('blur', () => {
browserWindow.hide();
});

return window;
return browserWindow;
}
58 changes: 21 additions & 37 deletions app/main/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,29 @@
import path from 'path';
import { app, ipcMain, Tray } from 'electron';
import os from 'os';
import { app, ipcMain, dialog } from 'electron';
import pify from 'pify';
import jsonStorage from 'electron-json-storage';
import createMainWindow from './createMainWindow';
import createMenuBarWindow from './createMenuBarWindow';
import configureStore from '../shared/store/configureStore';
import osxAutoUpdater from './osxAutoUpdater';
import tray from './tray';
import osxAutoUpdater from './tasks/osxAutoUpdater';
import reminder from './tasks/reminder';

// we have to do this to allow remote-loading of the current state :()
// we have to do this to ease remote-loading of the initial state :(
global.state = {};

const storage = pify(jsonStorage);
const trayIcon = path.join(__dirname, '../renderer/assets/images/logo.png');

let mainWindow = null;
let menuBarWindow = null;

if (process.env.NODE_ENV === 'development') {
require('electron-debug')(); // eslint-disable-line global-require
}

function doCreateMainWindow() {
mainWindow = createMainWindow();
mainWindow.on('closed', () => {
mainWindow = null;
});
}

function doCreateMenuBarWindow(bounds) {
menuBarWindow = createMenuBarWindow(bounds);

menuBarWindow.on('closed', () => {
menuBarWindow = null;
});

menuBarWindow.on('blur', () => {
menuBarWindow.close();
});
}

async function start() {
// set-up menu bar
const appIcon = new Tray(trayIcon);
appIcon.setToolTip('Timesheets');
const appIcon = tray();

global.state = await storage.get('state').catch(err => console.log('Error getting state', err));
global.state = await storage.get('state');
const store = configureStore(global.state, 'main');

store.subscribe(async () => {
Expand All @@ -65,26 +44,31 @@ async function start() {
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
doCreateMainWindow();
}
createMainWindow();
});

appIcon.on('click', (event, bounds) => {
doCreateMenuBarWindow(bounds);
appIcon.on('click', (event, trayBounds) => {
createMenuBarWindow({ trayBounds });
});

// init
doCreateMainWindow();
createMainWindow();

// auto-updating
setTimeout(() => {
osxAutoUpdater(store);
if (process.env.NODE_ENV === 'production') {
if (os.platform() === 'darwin') {
osxAutoUpdater(store);
}
}
}, 5000);

reminder(store);
}

app.on('ready', () => {
start()
.catch((err) => {
console.log(err);
dialog.showErrorBox('There\'s been an error', err.message);
});
});
Loading

0 comments on commit 143b90a

Please sign in to comment.