custom menu, adds RPC recreation on page reload

This commit is contained in:
Paul Frazee 2015-07-08 14:43:20 -05:00
parent d57f2ea1ba
commit 6cdcc86c86
3 changed files with 152 additions and 32 deletions

View File

@ -14,19 +14,16 @@ app.on('ready', function ready () {
// register sbot plugins
sbot.use(require('phoenix-api'))
// setup blobs
// setup electron
var blobs = require('./lib/blobs')(sbot, app.getPath('userDesktop'))
// register electron protocols
require('protocol').registerProtocol('blob', blobs.protocol)
// open the web app
var mainWindow = windows.open(
'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'),
sbot,
blobs,
{ width: 1000, height: 720 }
)
require('./lib/menu')(mainWindow)
// mainWindow.openDevTools()
// setup menu

110
app/lib/menu.js Normal file
View File

@ -0,0 +1,110 @@
var Menu = require('menu')
module.exports = function (window) {
var template = [
{
label: 'Patchwork',
submenu: [
{
label: 'About Patchwork',
selector: 'orderFrontStandardAboutPanel:'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
selector: 'terminate:'
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'Command+Z',
selector: 'undo:'
},
{
label: 'Redo',
accelerator: 'Shift+Command+Z',
selector: 'redo:'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'Command+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'Command+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'Command+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'Command+A',
selector: 'selectAll:'
}
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'Command+R',
click: function() {
window.resetRpc()
window.reload()
}
},
{
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click: function() {
window.toggleDevTools()
}
},
]
},
{
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:'
},
{
label: 'Close',
accelerator: 'Command+W',
selector: 'performClose:'
},
{
type: 'separator'
},
{
label: 'Bring All to Front',
selector: 'arrangeInFront:'
}
]
},
{
label: 'Help',
submenu: []
}
]
menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}

View File

@ -12,35 +12,48 @@ module.exports = function (window, sbot, params) {
api[k] = sbot[k] // copy over the plugin APIs
}
// create rpc object
var rpc = muxrpc(null, sbot.manifest, serialize)(api)
rpc.authorized = { id: sbot.feed.id, role: 'master' }
rpc.permissions({allow: null, deny: null})
function serialize (stream) { return stream }
// setup rpc stream over ipc
var rpcStream = rpc.createStream()
var ipcPush = pushable()
ipc.on('muxrpc-ssb', function (e, msg) {
if (e.sender == window.webContents) {
if (msg.bvalue) {
msg.value = new Buffer(msg.bvalue, 'base64')
delete msg.bvalue
// add rpc APIs to window
window.createRpc = function () {
// create rpc object
var rpc = window.rpc = muxrpc(null, sbot.manifest, serialize)(api)
rpc.authorized = { id: sbot.feed.id, role: 'master' }
rpc.permissions({allow: null, deny: null})
function serialize (stream) { return stream }
// start the stream
window.rpcStream = rpc.createStream()
var ipcPush = pushable()
ipc.on('muxrpc-ssb', function (e, msg) {
if (e.sender == window.webContents) {
if (msg.bvalue) {
msg.value = new Buffer(msg.bvalue, 'base64')
delete msg.bvalue
}
ipcPush.push(msg)
}
ipcPush.push(msg)
}
})
pull(ipcPush, rpcStream, pull.drain(
function (msg) {
if (msg.value && Buffer.isBuffer(msg.value)) {
// convert buffers to base64
msg.bvalue = msg.value.toString('base64')
delete msg.value
}
window.webContents.send('muxrpc-ssb', msg)
},
function (err) { if (err) { console.error(err) } }
))
})
pull(ipcPush, window.rpcStream, pull.drain(
function (msg) {
if (msg.value && Buffer.isBuffer(msg.value)) {
// convert buffers to base64
msg.bvalue = msg.value.toString('base64')
delete msg.value
}
window.webContents.send('muxrpc-ssb', msg)
},
function (err) { if (err) { console.error(err) } }
))
}
window.resetRpc = function () {
console.log('close rpc')
window.rpcStream.source(true)
window.rpc.close()
window.createRpc()
}
// setup default stream
window.createRpc()
// setup helper messages
ipc.on('fetch-manifest', function(e) {