Browse Source

custom menu, adds RPC recreation on page reload

Paul Frazee 5 years ago
parent
commit
6cdcc86c86
3 changed files with 152 additions and 32 deletions
  1. 2
    5
      app/index.js
  2. 110
    0
      app/lib/menu.js
  3. 40
    27
      app/lib/muxrpc-ipc.js

+ 2
- 5
app/index.js View File

@@ -14,19 +14,16 @@ app.on('ready', function ready () {
14 14
     // register sbot plugins
15 15
     sbot.use(require('phoenix-api'))
16 16
     
17
-    // setup blobs
17
+    // setup electron
18 18
     var blobs = require('./lib/blobs')(sbot, app.getPath('userDesktop'))
19
-
20
-    // register electron protocols
21 19
     require('protocol').registerProtocol('blob', blobs.protocol)
22
-
23
-    // open the web app
24 20
     var mainWindow = windows.open(
25 21
       'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'),
26 22
       sbot,
27 23
       blobs,
28 24
       { width: 1000, height: 720 }
29 25
     )
26
+    require('./lib/menu')(mainWindow)
30 27
     // mainWindow.openDevTools()
31 28
 
32 29
     // setup menu

+ 110
- 0
app/lib/menu.js View File

@@ -0,0 +1,110 @@
1
+var Menu = require('menu')
2
+
3
+module.exports = function (window) {
4
+  var template = [
5
+    {
6
+      label: 'Patchwork',
7
+      submenu: [
8
+        {
9
+          label: 'About Patchwork',
10
+          selector: 'orderFrontStandardAboutPanel:'
11
+        },
12
+        {
13
+          type: 'separator'
14
+        },
15
+        {
16
+          label: 'Quit',
17
+          accelerator: 'Command+Q',
18
+          selector: 'terminate:'
19
+        }
20
+      ]
21
+    },
22
+    {
23
+      label: 'Edit',
24
+      submenu: [
25
+        {
26
+          label: 'Undo',
27
+          accelerator: 'Command+Z',
28
+          selector: 'undo:'
29
+        },
30
+        {
31
+          label: 'Redo',
32
+          accelerator: 'Shift+Command+Z',
33
+          selector: 'redo:'
34
+        },
35
+        {
36
+          type: 'separator'
37
+        },
38
+        {
39
+          label: 'Cut',
40
+          accelerator: 'Command+X',
41
+          selector: 'cut:'
42
+        },
43
+        {
44
+          label: 'Copy',
45
+          accelerator: 'Command+C',
46
+          selector: 'copy:'
47
+        },
48
+        {
49
+          label: 'Paste',
50
+          accelerator: 'Command+V',
51
+          selector: 'paste:'
52
+        },
53
+        {
54
+          label: 'Select All',
55
+          accelerator: 'Command+A',
56
+          selector: 'selectAll:'
57
+        }
58
+      ]
59
+    },
60
+    {
61
+      label: 'View',
62
+      submenu: [
63
+        {
64
+          label: 'Reload',
65
+          accelerator: 'Command+R',
66
+          click: function() { 
67
+            window.resetRpc()
68
+            window.reload()
69
+          }
70
+        },
71
+        {
72
+          label: 'Toggle DevTools',
73
+          accelerator: 'Alt+Command+I',
74
+          click: function() { 
75
+            window.toggleDevTools()
76
+          }
77
+        },
78
+      ]
79
+    },
80
+    {
81
+      label: 'Window',
82
+      submenu: [
83
+        {
84
+          label: 'Minimize',
85
+          accelerator: 'Command+M',
86
+          selector: 'performMiniaturize:'
87
+        },
88
+        {
89
+          label: 'Close',
90
+          accelerator: 'Command+W',
91
+          selector: 'performClose:'
92
+        },
93
+        {
94
+          type: 'separator'
95
+        },
96
+        {
97
+          label: 'Bring All to Front',
98
+          selector: 'arrangeInFront:'
99
+        }
100
+      ]
101
+    },
102
+    {
103
+      label: 'Help',
104
+      submenu: []
105
+    }
106
+  ]
107
+
108
+  menu = Menu.buildFromTemplate(template)
109
+  Menu.setApplicationMenu(menu)
110
+}

+ 40
- 27
app/lib/muxrpc-ipc.js View File

@@ -12,35 +12,48 @@ module.exports = function (window, sbot, params) {
12 12
       api[k] = sbot[k] // copy over the plugin APIs
13 13
   }
14 14
 
15
-  // create rpc object
16
-  var rpc = muxrpc(null, sbot.manifest, serialize)(api)
17
-  rpc.authorized = { id: sbot.feed.id, role: 'master' }
18
-  rpc.permissions({allow: null, deny: null})
19
-  function serialize (stream) { return stream }
20 15
 
21
-  // setup rpc stream over ipc
22
-  var rpcStream = rpc.createStream()
23
-  var ipcPush = pushable()
24
-  ipc.on('muxrpc-ssb', function (e, msg) {
25
-    if (e.sender == window.webContents) {
26
-      if (msg.bvalue) {
27
-        msg.value = new Buffer(msg.bvalue, 'base64')
28
-        delete msg.bvalue
29
-      }
30
-      ipcPush.push(msg)
31
-    }
32
-  })
33
-  pull(ipcPush, rpcStream, pull.drain(
34
-    function (msg) { 
35
-      if (msg.value && Buffer.isBuffer(msg.value)) {
36
-        // convert buffers to base64
37
-        msg.bvalue = msg.value.toString('base64')
38
-        delete msg.value
16
+  // add rpc APIs to window
17
+  window.createRpc = function () {
18
+    // create rpc object
19
+    var rpc = window.rpc = muxrpc(null, sbot.manifest, serialize)(api)
20
+    rpc.authorized = { id: sbot.feed.id, role: 'master' }
21
+    rpc.permissions({allow: null, deny: null})
22
+    function serialize (stream) { return stream }
23
+
24
+    // start the stream
25
+    window.rpcStream = rpc.createStream()
26
+    var ipcPush = pushable()
27
+    ipc.on('muxrpc-ssb', function (e, msg) {
28
+      if (e.sender == window.webContents) {
29
+        if (msg.bvalue) {
30
+          msg.value = new Buffer(msg.bvalue, 'base64')
31
+          delete msg.bvalue
32
+        }
33
+        ipcPush.push(msg)
39 34
       }
40
-      window.webContents.send('muxrpc-ssb', msg)
41
-    },
42
-    function (err) { if (err) { console.error(err) } }
43
-  ))
35
+    })
36
+    pull(ipcPush, window.rpcStream, pull.drain(
37
+      function (msg) { 
38
+        if (msg.value && Buffer.isBuffer(msg.value)) {
39
+          // convert buffers to base64
40
+          msg.bvalue = msg.value.toString('base64')
41
+          delete msg.value
42
+        }
43
+        window.webContents.send('muxrpc-ssb', msg)
44
+      },
45
+      function (err) { if (err) { console.error(err) } }
46
+    ))
47
+  }
48
+  window.resetRpc = function () {
49
+    console.log('close rpc')
50
+    window.rpcStream.source(true)
51
+    window.rpc.close()
52
+    window.createRpc()
53
+  }
54
+
55
+  // setup default stream
56
+  window.createRpc()
44 57
 
45 58
   // setup helper messages
46 59
   ipc.on('fetch-manifest', function(e) {

Loading…
Cancel
Save