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 // register sbot plugins
sbot.use(require('phoenix-api')) sbot.use(require('phoenix-api'))
// setup blobs // setup electron
var blobs = require('./lib/blobs')(sbot, app.getPath('userDesktop')) var blobs = require('./lib/blobs')(sbot, app.getPath('userDesktop'))
// register electron protocols
require('protocol').registerProtocol('blob', blobs.protocol) require('protocol').registerProtocol('blob', blobs.protocol)
// open the web app
var mainWindow = windows.open( var mainWindow = windows.open(
'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'), 'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'),
sbot, sbot,
blobs, blobs,
{ width: 1000, height: 720 } { width: 1000, height: 720 }
) )
require('./lib/menu')(mainWindow)
// mainWindow.openDevTools() // mainWindow.openDevTools()
// setup menu // 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 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 // add rpc APIs to window
var rpcStream = rpc.createStream() window.createRpc = function () {
var ipcPush = pushable() // create rpc object
ipc.on('muxrpc-ssb', function (e, msg) { var rpc = window.rpc = muxrpc(null, sbot.manifest, serialize)(api)
if (e.sender == window.webContents) { rpc.authorized = { id: sbot.feed.id, role: 'master' }
if (msg.bvalue) { rpc.permissions({allow: null, deny: null})
msg.value = new Buffer(msg.bvalue, 'base64') function serialize (stream) { return stream }
delete msg.bvalue
// 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, window.rpcStream, pull.drain(
}) function (msg) {
pull(ipcPush, rpcStream, pull.drain( if (msg.value && Buffer.isBuffer(msg.value)) {
function (msg) { // convert buffers to base64
if (msg.value && Buffer.isBuffer(msg.value)) { msg.bvalue = msg.value.toString('base64')
// convert buffers to base64 delete msg.value
msg.bvalue = msg.value.toString('base64') }
delete msg.value window.webContents.send('muxrpc-ssb', msg)
} },
window.webContents.send('muxrpc-ssb', msg) function (err) { if (err) { console.error(err) } }
}, ))
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 // setup helper messages
ipc.on('fetch-manifest', function(e) { ipc.on('fetch-manifest', function(e) {