Skip to content

Commit 7fa597a

Browse files
committed
Add menu with common actions
1 parent 5cc4b5e commit 7fa597a

File tree

5 files changed

+194
-14
lines changed

5 files changed

+194
-14
lines changed

main.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
const { app, BrowserWindow } = require('electron');
1+
const { app, BrowserWindow, Menu } = require('electron');
22
const os = require('os');
33
const fs = require('fs');
44
const path = require('path');
5+
const menu = require('./menu');
6+
const { createWindow } = require('./window');
57

68
let mainWin;
79

8-
const winOption = {
9-
width: 1024,
10-
height: 768
11-
}
12-
13-
const isDarwin = os.platform() === 'darwin';
14-
1510
function initializeApp () {
16-
mainWin = new BrowserWindow(
17-
(isDarwin
18-
? Object.assign({}, winOption, {titleBarStyle: 'hidden'})
19-
: winOption)
20-
);
11+
Menu.setApplicationMenu(menu);
2112

22-
mainWin.loadURL(`file://${path.join(__dirname, 'index.html')}`);
13+
mainWin = createWindow({url: `file://${path.join(__dirname, 'index.html')}`});
2314
}
2415

2516
app.on('ready', () => {

menu.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
const {app, Menu, BrowserWindow} = require('electron');
2+
const path = require('path');
3+
const { createWindow } = require('./window');
4+
5+
const template = [
6+
{
7+
label: 'File',
8+
submenu: [
9+
{
10+
label: 'New File',
11+
accelerator: 'CmdOrCtrl+N',
12+
click () {
13+
createWindow({url: `file://${path.join(__dirname, 'index.html?target=https://hackmd.io/new')}`});
14+
}
15+
},
16+
{
17+
label: 'New Window',
18+
accelerator: 'CmdOrCtrl+Shift+N',
19+
click () {
20+
createWindow({url: `file://${path.join(__dirname, 'index.html')}`});
21+
}
22+
},
23+
{
24+
type: 'separator'
25+
}
26+
]
27+
},
28+
{
29+
label: 'Edit',
30+
submenu: [
31+
{
32+
role: 'undo'
33+
},
34+
{
35+
role: 'redo'
36+
},
37+
{
38+
type: 'separator'
39+
},
40+
{
41+
role: 'cut'
42+
},
43+
{
44+
role: 'copy'
45+
},
46+
{
47+
role: 'paste'
48+
},
49+
{
50+
role: 'pasteandmatchstyle'
51+
},
52+
{
53+
role: 'delete'
54+
},
55+
{
56+
role: 'selectall'
57+
}
58+
]
59+
},
60+
{
61+
role: 'window',
62+
submenu: [
63+
{
64+
role: 'minimize'
65+
},
66+
{
67+
role: 'close'
68+
},
69+
{
70+
label: 'Refresh',
71+
accelerator: 'CmdOrCtrl+R',
72+
click () {
73+
const win = BrowserWindow.getFocusedWindow();
74+
win.webContents.send('web:refresh');
75+
}
76+
},
77+
{
78+
type: 'separator'
79+
},
80+
{
81+
role: 'togglefullscreen'
82+
},
83+
{
84+
role: 'toggledevtools'
85+
},
86+
]
87+
},
88+
{
89+
role: 'help',
90+
submenu: [
91+
{
92+
label: 'Learn More',
93+
click () { require('electron').shell.openExternal('https://hackmd.io') }
94+
}
95+
]
96+
}
97+
]
98+
99+
if (process.platform === 'darwin') {
100+
template.unshift({
101+
label: app.getName(),
102+
submenu: [
103+
{
104+
role: 'about'
105+
},
106+
{
107+
type: 'separator'
108+
},
109+
{
110+
role: 'services',
111+
submenu: []
112+
},
113+
{
114+
type: 'separator'
115+
},
116+
{
117+
role: 'hide'
118+
},
119+
{
120+
role: 'hideothers'
121+
},
122+
{
123+
role: 'unhide'
124+
},
125+
{
126+
type: 'separator'
127+
},
128+
{
129+
role: 'quit'
130+
}
131+
]
132+
})
133+
// Edit menu.
134+
template[2].submenu.push(
135+
{
136+
type: 'separator'
137+
},
138+
{
139+
label: 'Speech',
140+
submenu: [
141+
{
142+
role: 'startspeaking'
143+
},
144+
{
145+
role: 'stopspeaking'
146+
}
147+
]
148+
}
149+
)
150+
}
151+
152+
module.exports = Menu.buildFromTemplate(template);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "hackmd-desktop",
3+
"productName": "HackMD Desktop",
34
"version": "0.0.1",
45
"description": "HackMD desktop client",
56
"main": "main.js",

renderer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('electron').remote.require('fs');
22
const os = require('electron').remote.require('os');
33
const path = require('electron').remote.require('path');
4+
const {ipcRenderer} = require('electron');
45
const {BrowserWindow} = require('electron').remote;
56
const {clipboard} = require('electron');
67

@@ -27,6 +28,7 @@ onload = () => {
2728
webview.addEventListener('dom-ready', function(){
2829
// set webview title
2930
document.querySelector('#navbar-container .title').innerHTML = webview.getTitle();
31+
document.querySelector('title').innerHTML = webview.getTitle();
3032

3133
// set dark theme if in home page
3234
if (webview.getURL().split('?')[0].split('#')[0].match(/https:\/\/hackmd.io\/$/)) {
@@ -64,6 +66,11 @@ onload = () => {
6466
// webview.openDevTools();
6567
});
6668

69+
/* handle ipc actions */
70+
ipcRenderer.on('web:refresh', (event, message) => {
71+
webview.loadURL(webview.getURL());
72+
});
73+
6774
/* handle _target=blank pages */
6875
webview.addEventListener('new-window', (event) => {
6976
new BrowserWindow(

window.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const {BrowserWindow} = require('electron');
2+
const os = require('os');
3+
4+
const winOption = {
5+
width: 1100,
6+
height: 768,
7+
minWidth: 522,
8+
minHeight: 400
9+
}
10+
11+
const isDarwin = os.platform() === 'darwin';
12+
13+
function createWindow (opts={}) {
14+
const win = new BrowserWindow(
15+
(isDarwin
16+
? Object.assign({}, winOption, {titleBarStyle: 'hidden'})
17+
: winOption)
18+
);
19+
20+
if (opts.hasOwnProperty('url')) {
21+
win.loadURL(opts.url)
22+
}
23+
24+
return win;
25+
}
26+
27+
module.exports = {
28+
createWindow
29+
};

0 commit comments

Comments
 (0)