2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 10:38: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", "targetUrl": "http:\/\/www.google.com",
"badge": false, "badge": false,
"width": 1280, "width": 1280,
"height": 800, "height": 800
"showDevTools": false
} }

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 Menu = electron.Menu;
var shell = electron.shell; 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()) if (Menu.getApplicationMenu())
return; return;
@ -51,57 +58,57 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo
{ {
label: 'Reload', label: 'Reload',
accelerator: 'CmdOrCtrl+R', accelerator: 'CmdOrCtrl+R',
click: function(item, focusedWindow) { click: function (item, focusedWindow) {
if (focusedWindow) if (focusedWindow)
focusedWindow.reload(); focusedWindow.reload();
} }
}, },
{ {
label: 'Toggle Full Screen', label: 'Toggle Full Screen',
accelerator: (function() { accelerator: (function () {
if (process.platform == 'darwin') if (process.platform == 'darwin')
return 'Ctrl+Command+F'; return 'Ctrl+Command+F';
else else
return 'F11'; return 'F11';
})(), })(),
click: function(item, focusedWindow) { click: function (item, focusedWindow) {
if (focusedWindow) if (focusedWindow)
focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
} }
}, },
{ {
label: 'Zoom In', label: 'Zoom In',
accelerator: (function() { accelerator: (function () {
if (process.platform == 'darwin') if (process.platform == 'darwin')
return 'Command+='; return 'Command+=';
else else
return 'Ctrl+='; return 'Ctrl+=';
})(), })(),
click: function() { click: function () {
onZoomIn(); onZoomIn();
} }
}, },
{ {
label: 'Zoom Out', label: 'Zoom Out',
accelerator: (function() { accelerator: (function () {
if (process.platform == 'darwin') if (process.platform == 'darwin')
return 'Command+-'; return 'Command+-';
else else
return 'Ctrl+-'; return 'Ctrl+-';
})(), })(),
click: function() { click: function () {
onZoomOut(); onZoomOut();
} }
}, },
{ {
label: 'Toggle Window Developer Tools', label: 'Toggle Window Developer Tools',
accelerator: (function() { accelerator: (function () {
if (process.platform == 'darwin') if (process.platform == 'darwin')
return 'Alt+Command+I'; return 'Alt+Command+I';
else else
return 'Ctrl+Shift+I'; return 'Ctrl+Shift+I';
})(), })(),
click: function(item, focusedWindow) { click: function (item, focusedWindow) {
if (focusedWindow) if (focusedWindow)
focusedWindow.toggleDevTools(); focusedWindow.toggleDevTools();
} }
@ -130,11 +137,15 @@ module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZo
submenu: [ submenu: [
{ {
label: `Built with Nativefier v${nativefierVersion}`, 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', 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', label: 'Quit',
accelerator: 'Command+Q', 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); var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu); 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 fs = require('fs');
var os = require('os');
var path = require('path'); var path = require('path');
var electron = require('electron'); var electron = require('electron');
var createMainWindow = require('./components/mainWindow/mainWindow');
var wurl = require('wurl'); var createLoginWindow = require('./components/login/loginWindow');
var helpers = require('./helpers/helpers');
var app = electron.app; var app = electron.app;
var BrowserWindow = electron.BrowserWindow; var isOSX = helpers.isOSX;
var shell = electron.shell;
var ipcMain = electron.ipcMain;
var buildMenu = require('./components/menu/menu');
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json'); 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 appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
var mainWindow;
app.on('window-all-closed', function () { app.on('window-all-closed', function () {
if (!isOSX()) { if (!isOSX()) {
app.quit(); app.quit();
@ -49,112 +47,11 @@ app.on('before-quit', () => {
}); });
app.on('ready', function () { app.on('ready', function () {
var currentZoom = 1; mainWindow = createMainWindow(appArgs, app.quit, app.dock.setBadge);
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();
}
});
}); });
app.on('login', function(event, webContents, request, authInfo, callback) { app.on('login', function(event, webContents, request, authInfo, callback) {
// for http authentication
event.preventDefault(); event.preventDefault();
var loginWindow = new BrowserWindow({ createLoginWindow(callback);
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();
});
}); });
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) { ipc.on('change-zoom', function (event, message) {
console.log('new zom', message);
webFrame.setZoomFactor(message); webFrame.setZoomFactor(message);
}); });