-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added keyboard shortcuts for creating and closing tabs.
- Loading branch information
Showing
4 changed files
with
201 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const { app, Menu } = require('electron'); | ||
|
||
const createMenu = (actions) => { | ||
const template = [ | ||
{ | ||
label: "Edit", | ||
submenu: [ | ||
{ | ||
label: 'New Tab', | ||
accelerator: 'CmdOrCtrl+T', | ||
click: actions.createTab | ||
}, | ||
{ | ||
label: 'Close Tab', | ||
accelerator: 'CmdOrCtrl+W', | ||
click: actions.closeTab | ||
}, | ||
{ role: "undo" }, | ||
{ role: "redo" }, | ||
{ type: "separator" }, | ||
{ role: "cut" }, | ||
{ role: "copy" }, | ||
{ role: "paste" }, | ||
{ role: "pasteandmatchstyle" }, | ||
{ role: "delete" }, | ||
{ role: "selectall" } | ||
] | ||
}, | ||
{ | ||
label: "View", | ||
submenu: [ | ||
{ role: "reload" }, | ||
{ role: "forcereload" }, | ||
{ role: "toggledevtools" }, | ||
{ type: "separator" }, | ||
{ role: "togglefullscreen" } | ||
] | ||
}, | ||
{ | ||
role: "window", | ||
submenu: [ | ||
{ role: "minimize" }, | ||
{ role: "close" } | ||
] | ||
} | ||
]; | ||
|
||
if (process.platform === "darwin") { | ||
template.unshift({ | ||
label: app.getName(), | ||
submenu: [ | ||
{ role: "about" }, | ||
{ type: "separator" }, | ||
{ role: "services", submenu: [] }, | ||
{ type: "separator" }, | ||
{ role: "hide" }, | ||
{ role: "hideothers" }, | ||
{ role: "unhide" }, | ||
{ type: "separator" }, | ||
{ role: "quit" } | ||
] | ||
}); | ||
|
||
// Edit menu | ||
template[1].submenu.push( | ||
{ type: "separator" }, | ||
{ | ||
label: "Speech", | ||
submenu: [{ role: "startspeaking" }, { role: "stopspeaking" }] | ||
} | ||
); | ||
|
||
// Window menu | ||
template[3].submenu = [ | ||
{ role: "close" }, | ||
{ role: "minimize" }, | ||
{ type: "separator" }, | ||
{ role: "front" } | ||
]; | ||
} | ||
|
||
const menu = Menu.buildFromTemplate(template); | ||
Menu.setApplicationMenu(menu); | ||
}; | ||
|
||
module.exports = { | ||
createMenu | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const { BrowserWindow, protocol } = require('electron'); | ||
const path = require('path'); | ||
const url = require('url'); | ||
const fs = require('fs'); | ||
const mime = require('mime-types'); | ||
|
||
const { createMenu } = require('./menu'); | ||
|
||
let instance = null; | ||
|
||
const actions = { | ||
createTab: () => { | ||
console.log('Create tab.'); | ||
instance.webContents.send('create-tab', true); | ||
}, | ||
closeTab: () => { | ||
console.log('Close tab.'); | ||
instance.webContents.send('close-tab', true); | ||
} | ||
}; | ||
|
||
const createWindow = () => { | ||
|
||
/** | ||
* Using a custom buffer protocol, instead of a file protocol because of restrictions with the file protocol. | ||
*/ | ||
protocol.registerBufferProtocol('altair', (request, callback) => { | ||
let url = request.url.substr(7); | ||
|
||
// In windows, the url comes as altair://c/Users/Jackie/Documents/projects/altair/dist/index.html | ||
// We also need to strip out the //c from the path | ||
// TODO - Find a better way of handling this | ||
if (process.platform === 'win32') { | ||
url = request.url.substr(10); | ||
} | ||
|
||
// Maybe this could work? | ||
// console.log('::>>', path.join(__dirname, '../dist', path.basename(request.url))); | ||
|
||
fs.readFile(path.normalize(`${url}`), 'utf8', function (err, data) { | ||
if (err) { | ||
return console.log('Error loading file to buffer.', err); | ||
} | ||
|
||
// Load the data from the file into a buffer and pass it to the callback | ||
// Using the mime package to get the mime type for the file, based on the file name | ||
callback({ mimeType: mime.lookup(url), data: new Buffer(data) }); | ||
// console.log(data); | ||
}); | ||
// callback({path: path.normalize(`${__dirname}/${url}`)}) | ||
}, (error) => { | ||
if (error) console.error('Failed to register protocol') | ||
}); | ||
|
||
// Create the browser window. | ||
instance = new BrowserWindow({ | ||
width: 1280, | ||
height: 800, | ||
webPreferences: { | ||
webSecurity: false | ||
}, | ||
// titleBarStyle: 'hidden-inset' | ||
}); | ||
|
||
// Populate the application menu | ||
createMenu(actions); | ||
|
||
// and load the index.html of the app. | ||
instance.loadURL(url.format({ | ||
pathname: path.join(__dirname, '../dist/index.html'), | ||
protocol: 'altair:', | ||
slashes: true | ||
})); | ||
// instance.loadURL('altair://' + __dirname + '/../dist/index.html'); | ||
|
||
// Open the DevTools. | ||
// instance.webContents.openDevTools(); | ||
|
||
// Prevent the app from navigating away from the app | ||
instance.webContents.on('will-navigate', (e, url) => e.preventDefault()); | ||
|
||
// Emitted when the window is closed. | ||
instance.on('closed', () => { | ||
// Dereference the window object, usually you would store windows | ||
// in an array if your app supports multi windows, this is the time | ||
// when you should delete the corresponding element. | ||
instance = null; | ||
}) | ||
}; | ||
|
||
module.exports = { | ||
instance, | ||
createWindow, | ||
actions | ||
}; |
Oops, something went wrong.