From 0e68fd61dc1e604ed22e749f10ad0a5a80719989 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Sat, 23 Jan 2016 09:27:09 +0800 Subject: [PATCH] Implement changing of zoom which fixes #17 - Take note that the keyboard shortcut for zoom in appears to be 'command + =', due to a bug in atom/electron#1507 --- app/src/components/menu/menu.js | 26 +++++++++++++++++++++++++- app/src/main.js | 19 +++++++++++++++---- app/src/preload.js | 11 ++++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/src/components/menu/menu.js b/app/src/components/menu/menu.js index 8fa3224..07aff01 100644 --- a/app/src/components/menu/menu.js +++ b/app/src/components/menu/menu.js @@ -2,7 +2,7 @@ var electron = require('electron'); var Menu = electron.Menu; var shell = electron.shell; -module.exports = function (mainWindow, nativefierVersion, onQuit) { +module.exports = function (mainWindow, nativefierVersion, onQuit, onZoomIn, onZoomOut) { if (Menu.getApplicationMenu()) return; @@ -69,6 +69,30 @@ module.exports = function (mainWindow, nativefierVersion, onQuit) { focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); } }, + { + label: 'Zoom In', + accelerator: (function() { + if (process.platform == 'darwin') + return 'Command+='; + else + return 'Ctrl+='; + })(), + click: function() { + onZoomIn(); + } + }, + { + label: 'Zoom Out', + accelerator: (function() { + if (process.platform == 'darwin') + return 'Command+-'; + else + return 'Ctrl+-'; + })(), + click: function() { + onZoomOut(); + } + }, { label: 'Toggle Window Developer Tools', accelerator: (function() { diff --git a/app/src/main.js b/app/src/main.js index 915f8ac..7af4a81 100644 --- a/app/src/main.js +++ b/app/src/main.js @@ -17,8 +17,7 @@ var ipcMain = electron.ipcMain; var buildMenu = require('./components/menu/menu'); const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json'); - -var mainWindow = null; +const ZOOM_INTERVAL = 0.1; var appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8')); @@ -50,7 +49,9 @@ app.on('before-quit', () => { }); app.on('ready', function () { - mainWindow = new BrowserWindow( + var currentZoom = 1; + + var mainWindow = new BrowserWindow( { width: appArgs.width || 1280, height: appArgs.height || 800, @@ -63,7 +64,17 @@ app.on('ready', function () { } ); - buildMenu(mainWindow, appArgs.nativefierVersion, app.quit); + 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); diff --git a/app/src/preload.js b/app/src/preload.js index 154a311..410665f 100644 --- a/app/src/preload.js +++ b/app/src/preload.js @@ -2,7 +2,9 @@ Preload file that will be executed in the renderer process */ -var ipc = require('electron').ipcRenderer; +var electron = require('electron'); +var ipc = electron.ipcRenderer; +var webFrame = electron.webFrame; document.addEventListener("DOMContentLoaded", function(event) { // do things @@ -12,3 +14,10 @@ ipc.on('params', function (event, message) { var appArgs = JSON.parse(message); console.log('nativefier.json', appArgs); }); + + +ipc.on('change-zoom', function (event, message) { + console.log('new zom', message); + webFrame.setZoomFactor(message); +}); +