Skip to content

Commit 7c2cd71

Browse files
committed
Build command pattern with ipc
1 parent bf8c605 commit 7c2cd71

File tree

7 files changed

+72
-23
lines changed

7 files changed

+72
-23
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<body>
101101
<div id="title-bar">
102102
<div class="window-control">
103-
<button>
103+
<button id="menu-btn">
104104
<i class="fa fa-bars" aria-hidden="true"></i>
105105
</button>
106106
</div>

ipc/client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { ipcRenderer } = require('electron');
2+
3+
module.exports = function(commandId, args) {
4+
ipcRenderer.send('main:command', { commandId, args });
5+
}

ipc/consumer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const {app, Menu, BrowserWindow, shell} = require('electron');
2+
const path = require('path');
3+
4+
const { createWindow } = require('../window');
5+
6+
module.exports = function(commandId, args={}) {
7+
switch(commandId) {
8+
case 'createWindow':
9+
createWindow(args);
10+
break;
11+
case 'refreshWindow':
12+
const win = BrowserWindow.getFocusedWindow();
13+
win.webContents.send('web:refresh');
14+
break;
15+
case 'learnMore':
16+
shell.openExternal('https://hackmd.io');
17+
break;
18+
default:
19+
break;
20+
}
21+
}

ipc/server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { ipcMain } = require('electron');
2+
const consumer = require('./consumer');
3+
4+
ipcMain.on('main:command', (event, { commandId, args }) => {
5+
consumer(commandId, args);
6+
});

main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const path = require('path');
55
const menu = require('./menu');
66
const { createWindow } = require('./window');
77

8+
require('./ipc/server');
9+
810
let mainWin;
911

1012
function initializeApp () {

menu.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
const {app, Menu, BrowserWindow} = require('electron');
2-
const path = require('path');
3-
const { createWindow } = require('./window');
1+
const { ipcMain, ipcRenderer } = require('electron');
2+
const consumer = require('./ipc/consumer');
3+
4+
const Menu = require('electron').Menu || require('electron').remote.Menu;
5+
const path = require('path') || require('electron').remote.require('path');
6+
const os = require('os') || require('electron').remote.require('os');
7+
8+
const isMainProcess = typeof ipcMain !== 'undefined';
9+
10+
function exec(commandId, args={}) {
11+
if (isMainProcess) {
12+
consumer(commandId, args);
13+
} else {
14+
ipcRenderer.send('main:command', { commandId, args });
15+
}
16+
}
417

518
const template = [
619
{
@@ -10,14 +23,14 @@ const template = [
1023
label: 'New File',
1124
accelerator: 'CmdOrCtrl+N',
1225
click () {
13-
createWindow({url: `file://${path.join(__dirname, 'index.html?target=https://hackmd.io/new')}`});
26+
exec('createWindow', {url: `file://${path.join(__dirname, 'index.html?target=https://hackmd.io/new')}`})
1427
}
1528
},
1629
{
1730
label: 'New Window',
1831
accelerator: 'CmdOrCtrl+Shift+N',
1932
click () {
20-
createWindow({url: `file://${path.join(__dirname, 'index.html')}`});
33+
exec('createWindow', {url: `file://${path.join(__dirname, 'index.html')}`})
2134
}
2235
},
2336
{
@@ -70,8 +83,7 @@ const template = [
7083
label: 'Refresh',
7184
accelerator: 'CmdOrCtrl+R',
7285
click () {
73-
const win = BrowserWindow.getFocusedWindow();
74-
win.webContents.send('web:refresh');
86+
exec('refreshWindow');
7587
}
7688
},
7789
{
@@ -90,13 +102,15 @@ const template = [
90102
submenu: [
91103
{
92104
label: 'Learn More',
93-
click () { require('electron').shell.openExternal('https://hackmd.io') }
105+
click () {
106+
exec('learnMore');
107+
}
94108
}
95109
]
96110
}
97111
]
98112

99-
if (process.platform === 'darwin') {
113+
if (os.platform() === 'darwin') {
100114
template.unshift({
101115
label: app.getName(),
102116
submenu: [

renderer.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
const fs = require('electron').remote.require('fs');
2-
const os = require('electron').remote.require('os');
3-
const path = require('electron').remote.require('path');
4-
const {ipcRenderer} = require('electron');
5-
const {BrowserWindow} = require('electron').remote;
6-
const {clipboard} = require('electron');
7-
const { createWindow } = require('./window');
1+
const { ipcRenderer, remote, clipboard } = require('electron');
2+
const { BrowserWindow } = remote;
3+
4+
const fs = remote.require('fs');
5+
const os = remote.require('os');
6+
const path = remote.require('path');
7+
8+
const ipcClient = require('./ipc/client');
9+
10+
const menu = require('./menu');
811

912
const SERVER_URL = 'https://hackmd.io';
1013

1114
const isMac = os.platform() === 'darwin';
1215

13-
const winOption = {
14-
width: 1024,
15-
height: 768
16-
}
17-
1816
onload = () => {
1917
/* inject mac specific styles */
2018
if (isMac) {
@@ -71,6 +69,9 @@ onload = () => {
7169
new Notification('URL copied', { title: 'URL copied', body: webview.getURL() });
7270
}
7371

72+
document.querySelector('button#menu-btn').onclick = () => {
73+
menu.popup(require('electron').remote.getCurrentWindow());
74+
}
7475
// webview.openDevTools();
7576
});
7677

@@ -81,6 +82,6 @@ onload = () => {
8182

8283
/* handle _target=blank pages */
8384
webview.addEventListener('new-window', (event) => {
84-
createWindow({url: `file://${path.join(__dirname, `index.html?target=${event.url}`)}`});
85+
ipcClient('createWindow', { url: `file://${path.join(__dirname, `index.html?target=${event.url}`)}` });
8586
});
8687
}

0 commit comments

Comments
 (0)