From dbd4336c880e12a9a43778c719a03bb5a3d96241 Mon Sep 17 00:00:00 2001 From: Hary Pirajan Mahalingam Date: Wed, 18 Nov 2020 18:30:06 +0100 Subject: [PATCH] Added support for opening in browser. (#6) --- main.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 114 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index ce40eec..6373afc 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,8 @@ // Modules to control application life and create native browser window -const { app, BrowserWindow } = require('electron') +const { app, BrowserWindow, Menu, MenuItem } = require('electron') const path = require('path') +const isMac = process.platform === 'darwin' +const shell = require('electron').shell function createWindow() { createSpecificWindow('https:///gitpod.idi.ntnu.no') @@ -18,20 +20,123 @@ function createSpecificWindow(urlToLoad) { } }) - // and load the index.html of the app. - mainWindow.loadURL(urlToLoad) - + const template = [ + // { role: 'appMenu' } + ...(isMac ? [{ + label: app.name, + submenu: [ + { role: 'about' }, + { type: 'separator' }, + { role: 'services' }, + { type: 'separator' }, + { role: 'hide' }, + { role: 'hideothers' }, + { role: 'unhide' }, + { type: 'separator' }, + { role: 'quit' } + ] + }] : []), + // { role: 'fileMenu' } + { + label: 'File', + submenu: [ + isMac ? { role: 'close' } : { role: 'quit' } + ] + }, + // { role: 'editMenu' } + { + label: 'Edit', + submenu: [ + { role: 'undo' }, + { role: 'redo' }, + { type: 'separator' }, + { role: 'cut' }, + { role: 'copy' }, + { role: 'paste' }, + ...(isMac ? [ + { role: 'pasteAndMatchStyle' }, + { role: 'delete' }, + { role: 'selectAll' }, + { type: 'separator' }, + { + label: 'Speech', + submenu: [ + { role: 'startSpeaking' }, + { role: 'stopSpeaking' } + ] + } + ] : [ + { role: 'delete' }, + { type: 'separator' }, + { role: 'selectAll' } + ]) + ] + }, + // { role: 'viewMenu' } + { + label: 'View', + submenu: [ + { role: 'reload' }, + { role: 'forceReload' }, + { role: 'toggleDevTools' }, + { type: 'separator' }, + { role: 'resetZoom' }, + { role: 'zoomIn' }, + { role: 'zoomOut' }, + { type: 'separator' }, + { role: 'togglefullscreen' } + ] + }, + // { role: 'windowMenu' } + { + label: 'Window', + submenu: [ + { role: 'minimize' }, + { role: 'zoom' }, + { + label: 'Open in system browser', + click() { + shell.openExternal(urlToLoad) + } + }, + ...(isMac ? [ + { type: 'separator' }, + { role: 'front' }, + { type: 'separator' }, + { role: 'window' } + ] : [ + { role: 'close' } + ]) + ] + }, + { + role: 'help', + submenu: [ + { + label: 'Learn More', + click: async () => { + const { shell } = require('electron') + await shell.openExternal('https://electronjs.org') + } + } + ] + } + ] + const menu = Menu.buildFromTemplate(template) + Menu.setApplicationMenu(menu) + // and load the index.html of the app. + mainWindow.loadURL(urlToLoad) - mainWindow.on('resize', (event) =>{ + mainWindow.on('resize', (event) => { updateCSS(mainWindow) }); - mainWindow.webContents.on('did-finish-load', () => { + mainWindow.webContents.on('did-finish-load', () => { updateCSS(mainWindow) - }) + }) mainWindow.webContents.on('new-window', (event, url) => { event.preventDefault() createSpecificWindow(url) @@ -47,11 +152,11 @@ function createSpecificWindow(urlToLoad) { function updateCSS(window) { var zoomFactor = window.webContents.getZoomFactor() - var theiaPadding = 60/zoomFactor + var theiaPadding = 60 / zoomFactor if (window.isFullScreen()) { window.webContents.insertCSS('#theia-top-panel { padding-left: 0px; -webkit-app-region: drag; } header { -webkit-app-region: drag; }') } else { - window.webContents.insertCSS('#theia-top-panel { padding-left: '+theiaPadding+'px; -webkit-app-region: drag; } header { -webkit-app-region: drag; }') + window.webContents.insertCSS('#theia-top-panel { padding-left: ' + theiaPadding + 'px; -webkit-app-region: drag; } header { -webkit-app-region: drag; }') } }