2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Refactor main.js into separate files, and put static files such as preload and login.html into app/src/static

This commit is contained in:
Jia Hao 2016-01-23 10:09:47 +08:00
parent 0e68fd61dc
commit 90ee346914
10 changed files with 186 additions and 130 deletions

View File

@ -3,6 +3,5 @@
"targetUrl": "http:\/\/www.google.com",
"badge": false,
"width": 1280,
"height": 800,
"showDevTools": false
"height": 800
}

View File

@ -0,0 +1,21 @@
var electron = require('electron');
var BrowserWindow = electron.BrowserWindow;
var ipcMain = electron.ipcMain;
function createLoginWindow(loginCallback) {
var loginWindow = new BrowserWindow({
width: 300,
height: 400,
frame: false,
resizable: false
});
loginWindow.loadURL('file://' + __dirname + '/static/login/login.html');
ipcMain.once('login-message', function(event, usernameAndPassword) {
loginCallback(usernameAndPassword[0], usernameAndPassword[1]);
loginWindow.close();
});
return loginWindow;
}
module.exports = createLoginWindow;

View File

@ -0,0 +1,108 @@
var path = require('path');
var electron = require('electron');
var helpers = require('./../../helpers/helpers');
var createMenu = require('./../menu/menu');
var BrowserWindow = electron.BrowserWindow;
var shell = electron.shell;
var isOSX = helpers.isOSX;
var linkIsInternal = helpers.linkIsInternal;
const ZOOM_INTERVAL = 0.1;
/**
*
* @param {{}} options AppArgs from nativefier.json
* @param {electron.app.quit} onAppQuit
* @param {electron.app.dock.setBadge} setDockBadge
* @returns {electron.BrowserWindow}
*/
function createMainWindow(options, onAppQuit, setDockBadge) {
console.log(__dirname);
var mainWindow = new BrowserWindow(
{
width: options.width || 1280,
height: options.height || 800,
'web-preferences': {
javascript: true,
plugins: true,
nodeIntegration: false,
preload: path.join(__dirname, 'static', 'preload.js')
}
}
);
var currentZoom = 1;
var onZoomIn = function () {
currentZoom += ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom);
};
var onZoomOut = function () {
currentZoom -= ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom);
};
createMenu(options.nativefierVersion, onAppQuit, onZoomIn, onZoomOut);
if (options.userAgent) {
mainWindow.webContents.setUserAgent(options.userAgent);
}
mainWindow.webContents.on('did-finish-load', function () {
mainWindow.webContents.send('params', JSON.stringify(options));
});
if (options.badge || options.counter) {
mainWindow.on('page-title-updated', function () {
if (!isOSX() || mainWindow.isFocused()) {
return;
}
if (options.counter) {
var itemCountRegex = /[\(](\d*?)[\)]/;
var match = itemCountRegex.exec(mainWindow.getTitle());
console.log(mainWindow.getTitle(), match);
if (match) {
setDockBadge(match[1]);
}
return;
}
setDockBadge('●');
});
}
mainWindow.webContents.on('new-window', function (event, urlToGo) {
if (linkIsInternal(options.targetUrl, urlToGo)) {
return;
}
event.preventDefault();
shell.openExternal(urlToGo);
});
mainWindow.loadURL(options.targetUrl);
// if the window is focused, clear the badge
mainWindow.on('focus', function () {
if (!isOSX()) {
return;
}
if (options.badge || options.counter) {
setDockBadge('');
}
});
mainWindow.on('close', (e) => {
if (isOSX()) {
// this is called when exiting from clicking the cross button on the window
e.preventDefault();
mainWindow.hide();
}
});
return mainWindow;
}
module.exports = createMainWindow;

View File

@ -2,7 +2,14 @@ var electron = require('electron');
var Menu = electron.Menu;
var shell = electron.shell;
module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZoomOut) {
/**
*
* @param {string} nativefierVersion
* @param {electron.app.quit} onQuit should be from app.quit
* @param {function} onZoomIn
* @param {function} onZoomOut
*/
function createMenu(nativefierVersion, onQuit, onZoomIn, onZoomOut) {
if (Menu.getApplicationMenu())
return;
@ -51,57 +58,57 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click: function(item, focusedWindow) {
click: function (item, focusedWindow) {
if (focusedWindow)
focusedWindow.reload();
}
},
{
label: 'Toggle Full Screen',
accelerator: (function() {
accelerator: (function () {
if (process.platform == 'darwin')
return 'Ctrl+Command+F';
else
return 'F11';
})(),
click: function(item, focusedWindow) {
click: function (item, focusedWindow) {
if (focusedWindow)
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
},
{
label: 'Zoom In',
accelerator: (function() {
accelerator: (function () {
if (process.platform == 'darwin')
return 'Command+=';
else
return 'Ctrl+=';
})(),
click: function() {
click: function () {
onZoomIn();
}
},
{
label: 'Zoom Out',
accelerator: (function() {
accelerator: (function () {
if (process.platform == 'darwin')
return 'Command+-';
else
return 'Ctrl+-';
})(),
click: function() {
click: function () {
onZoomOut();
}
},
{
label: 'Toggle Window Developer Tools',
accelerator: (function() {
accelerator: (function () {
if (process.platform == 'darwin')
return 'Alt+Command+I';
else
return 'Ctrl+Shift+I';
})(),
click: function(item, focusedWindow) {
click: function (item, focusedWindow) {
if (focusedWindow)
focusedWindow.toggleDevTools();
}
@ -130,11 +137,15 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo
submenu: [
{
label: `Built with Nativefier v${nativefierVersion}`,
click: function() { shell.openExternal('https://github.com/jiahaog/nativefier') }
click: function () {
shell.openExternal('https://github.com/jiahaog/nativefier')
}
},
{
label: 'Report an Issue',
click: function() { shell.openExternal('https://github.com/jiahaog/nativefier/issues') }
click: function () {
shell.openExternal('https://github.com/jiahaog/nativefier/issues')
}
}
]
}
@ -172,7 +183,9 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo
{
label: 'Quit',
accelerator: 'Command+Q',
click: function() { onQuit(); }
click: function () {
onQuit();
}
},
]
});
@ -189,4 +202,6 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
};
}
module.exports = createMenu;

View File

@ -0,0 +1,17 @@
var wurl = require('wurl');
var os = require('os');
function isOSX() {
return os.platform() === 'darwin';
}
function linkIsInternal(currentUrl, newUrl) {
var currentDomain = wurl('domain', currentUrl);
var newDomain = wurl('domain', newUrl);
return currentDomain === newDomain;
}
module.exports = {
isOSX: isOSX,
linkIsInternal: linkIsInternal
};

View File

@ -3,24 +3,22 @@
*/
var fs = require('fs');
var os = require('os');
var path = require('path');
var electron = require('electron');
var wurl = require('wurl');
var createMainWindow = require('./components/mainWindow/mainWindow');
var createLoginWindow = require('./components/login/loginWindow');
var helpers = require('./helpers/helpers');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var shell = electron.shell;
var ipcMain = electron.ipcMain;
var buildMenu = require('./components/menu/menu');
var isOSX = helpers.isOSX;
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
const ZOOM_INTERVAL = 0.1;
var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
var mainWindow;
app.on('window-all-closed', function () {
if (!isOSX()) {
app.quit();
@ -49,112 +47,11 @@ app.on('before-quit', () => {
});
app.on('ready', function () {
var currentZoom = 1;
var mainWindow = new BrowserWindow(
{
width: appArgs.width || 1280,
height: appArgs.height || 800,
'web-preferences': {
javascript: true,
plugins: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
}
}
);
var onZoomIn = function () {
currentZoom += ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom);
};
var onZoomOut = function () {
currentZoom -= ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom);
};
buildMenu(mainWindow, appArgs.nativefierVersion, app.quit, onZoomIn, onZoomOut);
if (appArgs.userAgent) {
mainWindow.webContents.setUserAgent(appArgs.userAgent);
}
mainWindow.webContents.on('did-finish-load', function () {
mainWindow.webContents.send('params', JSON.stringify(appArgs));
});
if (appArgs.badge || appArgs.counter) {
mainWindow.on('page-title-updated', function () {
if (!isOSX() || mainWindow.isFocused()) {
return;
}
if (appArgs.counter) {
var itemCountRegex = /[\(](\d*?)[\)]/;
var match = itemCountRegex.exec(mainWindow.getTitle());
console.log(mainWindow.getTitle(), match);
if (match) {
app.dock.setBadge(match[1]);
}
return;
}
app.dock.setBadge('●');
});
}
mainWindow.webContents.on('new-window', function (event, urlToGo) {
if (linkIsInternal(appArgs.targetUrl, urlToGo)) {
return;
}
event.preventDefault();
shell.openExternal(urlToGo);
});
mainWindow.loadURL(appArgs.targetUrl);
// if the window is focused, clear the badge
mainWindow.on('focus', function () {
if (!isOSX()) {
return;
}
if (appArgs.badge || appArgs.counter) {
app.dock.setBadge('');
}
});
mainWindow.on('close', (e) => {
if (isOSX()) {
// this is called when exiting from clicking the cross button on the window
e.preventDefault();
mainWindow.hide();
}
});
mainWindow = createMainWindow(appArgs, app.quit, app.dock.setBadge);
});
app.on('login', function(event, webContents, request, authInfo, callback) {
// for http authentication
event.preventDefault();
var loginWindow = new BrowserWindow({
width: 300,
height: 400,
frame: false,
resizable: false
});
loginWindow.loadURL('file://' + __dirname + '/components/login/login.html');
ipcMain.once('login-message', function(event, usernameAndPassword) {
callback(usernameAndPassword[0], usernameAndPassword[1]);
loginWindow.close();
});
createLoginWindow(callback);
});
function isOSX() {
return os.platform() === 'darwin';
}
function linkIsInternal(currentUrl, newUrl) {
var currentDomain = wurl('domain', currentUrl);
var newDomain = wurl('domain', newUrl);
return currentDomain === newDomain;
}

View File

@ -17,7 +17,6 @@ ipc.on('params', function (event, message) {
ipc.on('change-zoom', function (event, message) {
console.log('new zom', message);
webFrame.setZoomFactor(message);
});