Skip to content

Commit a872ad9

Browse files
committed
change IPC module(Raw buffer -> Socket) & set window behaviour for Windows
1 parent 2499a05 commit a872ad9

8 files changed

Lines changed: 179 additions & 94 deletions

File tree

atom-lib/finder-window.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@ const electron = require('electron')
22
const BrowserWindow = electron.BrowserWindow
33
const path = require('path')
44

5-
var finderWindow = new BrowserWindow({
5+
var config = {
66
width: 640,
77
height: 400,
88
show: false,
99
frame: false,
1010
resizable: false,
1111
'zoom-factor': 1.0,
12-
'always-on-top': true,
1312
'web-preferences': {
1413
'overlay-scrollbars': true,
1514
'skip-taskbar': true
1615
},
1716
'standard-window': false
18-
})
17+
}
18+
19+
if (process.platform === 'darwin') {
20+
config['always-on-top'] = true
21+
}
22+
23+
var finderWindow = new BrowserWindow(config)
1924

2025
var url = path.resolve(__dirname, '../browser/finder/index.html')
2126

2227
finderWindow.loadURL('file://' + url)
2328

24-
finderWindow.on('blur', function () {
25-
finderWindow.hide()
26-
})
27-
28-
finderWindow.setVisibleOnAllWorkspaces(true)
29+
if (process.platform === 'darwin') {
30+
finderWindow.setVisibleOnAllWorkspaces(true)
31+
}
2932

3033
module.exports = finderWindow

browser/finder/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
<script>
3131
const electron = require('electron')
3232
electron.webFrame.setZoomLevelLimits(1, 1)
33-
var scriptUrl = process.env.BOOST_ENV === 'development'
33+
const _ = require('lodash')
34+
var scriptUrl = _.find(electron.remote.process.argv, a => a === '--hot')
3435
? 'http://localhost:8080/assets/finder.js'
3536
: '../../compiled/finder.js'
3637
var scriptEl=document.createElement('script')

browser/finder/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import dataStore from 'boost/dataStore'
1313
const electron = require('electron')
1414
const { remote, clipboard, ipcRenderer } = electron
1515

16-
var hideFinder = remote.getGlobal('hideFinder')
16+
function hideFinder () {
17+
ipcRenderer.send('hide-finder')
18+
}
1719

1820
function notify (...args) {
1921
return new window.Notification(...args)

browser/main/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@
5858
electron.webFrame.setZoomLevelLimits(1, 1)
5959
var version = electron.remote.app.getVersion()
6060
document.title = 'Boost' + ((version == null || version.length === 0) ? ' DEV' : '')
61-
var scriptUrl = process.env.BOOST_ENV === 'development'
61+
const _ = require('lodash')
62+
var scriptUrl = _.find(electron.remote.process.argv, a => a === '--hot')
6263
? 'http://localhost:8080/assets/main.js'
6364
: '../../compiled/main.js'
64-
var scriptEl=document.createElement('script')
65+
var scriptEl = document.createElement('script')
6566
scriptEl.setAttribute("type","text/javascript")
6667
scriptEl.setAttribute("src", scriptUrl)
6768
document.getElementsByTagName("head")[0].appendChild(scriptEl)

finder.js

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,84 @@ const Tray = electron.Tray
44
const Menu = electron.Menu
55
const MenuItem = electron.MenuItem
66
const ipcMain = electron.ipcMain
7+
const path = require('path')
78

8-
process.stdin.setEncoding('utf8')
9+
const nodeIpc = require('node-ipc')
910

10-
console.log = function () {
11-
process.stdout.write(JSON.stringify({
12-
type: 'log',
13-
data: JSON.stringify(Array.prototype.slice.call(arguments).join(' '))
14-
}), 'utf-8')
11+
var finderWindow = null
12+
var isFinderLoaded = false
13+
14+
function hideFinder () {
15+
if (!isFinderLoaded) return false
16+
17+
if (process.platform === 'darwin') {
18+
Menu.sendActionToFirstResponder('hide:')
19+
}
20+
if (process.platform === 'win32') {
21+
finderWindow.minimize()
22+
}
23+
finderWindow.hide()
24+
}
25+
26+
function showFinder () {
27+
if (!isFinderLoaded) return false
28+
29+
if (!finderWindow.isVisible()) {
30+
finderWindow.show()
31+
}
32+
if (process.platform === 'win32') {
33+
finderWindow.minimize()
34+
finderWindow.restore()
35+
}
1536
}
1637

38+
nodeIpc.config.id = 'finder'
39+
nodeIpc.config.retry = 1500
40+
nodeIpc.config.silent = true
41+
42+
nodeIpc.connectTo(
43+
'main',
44+
path.join(app.getPath('userData'), 'boost.service'),
45+
function () {
46+
nodeIpc.of.main.on(
47+
'connect',
48+
function () {
49+
nodeIpc.log('<< ## connected to world ##'.rainbow, nodeIpc.config.delay)
50+
}
51+
)
52+
nodeIpc.of.main.on(
53+
'disconnect',
54+
function(){
55+
nodeIpc.log('<< disconnected from main'.notice)
56+
}
57+
)
58+
nodeIpc.of.main.on(
59+
'message',
60+
function (payload) {
61+
switch (payload.type) {
62+
case 'open-finder':
63+
showFinder()
64+
break
65+
}
66+
}
67+
)
68+
}
69+
)
70+
1771
function emit (type, data) {
18-
process.stdout.write(JSON.stringify({
72+
var payload = {
1973
type: type,
20-
data: JSON.stringify(data)
21-
}), 'utf-8')
74+
data: data
75+
}
76+
nodeIpc.of.main.emit('message', payload)
2277
}
2378

24-
var finderWindow
79+
var appQuit = false
2580
app.on('ready', function () {
26-
app.dock.hide()
81+
if (process.platform === 'darwin') {
82+
app.dock.hide()
83+
}
84+
2785
var appIcon = new Tray(__dirname + '/resources/tray-icon.png')
2886
appIcon.setToolTip('Boost')
2987

@@ -43,7 +101,7 @@ app.on('ready', function () {
43101
trayMenu.append(new MenuItem({
44102
label: 'Open Finder window',
45103
click: function () {
46-
finderWindow.show()
104+
showFinder()
47105
}
48106
}))
49107
trayMenu.append(new MenuItem({
@@ -53,29 +111,30 @@ app.on('ready', function () {
53111
}
54112
}))
55113
appIcon.setContextMenu(trayMenu)
56-
57-
process.stdin.on('data', function (payload) {
58-
try {
59-
payload = JSON.parse(payload)
60-
} catch (e) {
61-
console.log('Not parsable payload : ', payload)
62-
return
63-
}
64-
console.log('from main >> ', payload.type)
65-
switch (payload.type) {
66-
case 'open-finder':
67-
finderWindow.show()
68-
break
69-
}
114+
appIcon.on('click', function (e) {
115+
e.preventDefault()
116+
appIcon.popUpContextMenu(trayMenu)
70117
})
71118

72119
ipcMain.on('copy-finder', function () {
73120
emit('copy-finder')
74121
})
122+
123+
ipcMain.on('hide-finder', function () {
124+
hideFinder()
125+
})
126+
127+
isFinderLoaded = true
75128
})
76129

77-
global.hideFinder = function () {
78-
Menu.sendActionToFirstResponder('hide:')
79-
}
130+
finderWindow.on('blur', function () {
131+
hideFinder()
132+
})
133+
134+
finderWindow.on('close', function (e) {
135+
if (appQuit) return true
136+
e.preventDefault()
137+
hideFinder()
138+
})
80139
})
81140

lib/dataStore.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,22 @@ function getLocalPath () {
1313
}
1414

1515
function forgeInitialRepositories () {
16-
return [{
16+
let defaultRepo = {
1717
key: keygen(),
1818
name: 'local',
1919
type: 'userData',
2020
user: {
21-
name: remote.getGlobal('process').env.USER
21+
name: 'New user'
2222
}
23-
}]
23+
}
24+
25+
if (process.platform === 'darwin') {
26+
defaultRepo.user.name = remote.process.env.USER
27+
} else if (process.platform === 'win32') {
28+
defaultRepo.user.name = remote.process.env.USERNAME
29+
}
30+
31+
return [defaultRepo]
2432
}
2533

2634
function getRepositories () {

main.js

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const autoUpdater = electron.autoUpdater
77
const jetpack = require('fs-jetpack')
88
const path = require('path')
99
const ChildProcess = require('child_process')
10-
electron.crashReporter.start()
10+
const _ = require('lodash')
11+
// electron.crashReporter.start()
1112

1213
var mainWindow = null
1314
var finderProcess
@@ -61,6 +62,44 @@ autoUpdater
6162
}
6263
})
6364

65+
66+
const nodeIpc = require('node-ipc')
67+
var isNodeIpcReady = false
68+
nodeIpc.config.id = 'node'
69+
nodeIpc.config.retry= 1500
70+
nodeIpc.config.silent = true
71+
72+
nodeIpc.serve(
73+
path.join(app.getPath('userData'), 'boost.service'),
74+
function(){
75+
isNodeIpcReady = true
76+
nodeIpc.server.on(
77+
'message',
78+
function (data, socket){
79+
console.log('>>', data)
80+
format(data)
81+
}
82+
)
83+
}
84+
)
85+
86+
87+
function format (payload) {
88+
switch (payload.type) {
89+
case 'show-main-window':
90+
mainWindow.minimize()
91+
mainWindow.restore()
92+
break
93+
case 'copy-finder':
94+
mainWindow.webContents.send('copy-finder')
95+
break
96+
case 'quit-app':
97+
appQuit = true
98+
app.quit()
99+
break
100+
}
101+
}
102+
64103
app.on('ready', function () {
65104
app.on('before-quit', function () {
66105
if (finderProcess) finderProcess.kill()
@@ -93,14 +132,26 @@ app.on('ready', function () {
93132
e.preventDefault()
94133
mainWindow.hide()
95134
})
135+
136+
function emitToFinder (type, data) {
137+
if (!isNodeIpcReady) {
138+
console.log('server is not ready')
139+
}
140+
var payload = {
141+
type: type,
142+
data: data
143+
}
144+
nodeIpc.server.broadcast('message', payload)
145+
}
146+
96147
mainWindow.webContents.on('did-finish-load', function () {
97148
if (finderProcess == null) {
149+
var finderArgv = [path.resolve(__dirname, 'finder.js'), '--finder']
150+
if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
98151
finderProcess = ChildProcess
99-
.execFile(process.execPath, [path.resolve(__dirname, 'finder.js'), '--finder'])
100-
finderProcess.stdout.setEncoding('utf8')
101-
finderProcess.stderr.setEncoding('utf8')
102-
finderProcess.stdout.on('data', format)
103-
finderProcess.stderr.on('data', errorFormat)
152+
.execFile(process.execPath, finderArgv)
153+
154+
nodeIpc.server.start()
104155
}
105156

106157
if (update != null) {
@@ -115,48 +166,6 @@ app.on('ready', function () {
115166
mainWindow.show()
116167
})
117168

118-
function format (payload) {
119-
// console.log('from finder >> ', payload)
120-
try {
121-
payload = JSON.parse(payload)
122-
} catch (e) {
123-
console.log('Not parsable payload : ', payload)
124-
return
125-
}
126-
switch (payload.type) {
127-
case 'log':
128-
console.log('FINDER(stdout): ' + payload.data)
129-
break
130-
case 'show-main-window':
131-
mainWindow.show()
132-
break
133-
case 'copy-finder':
134-
mainWindow.webContents.send('copy-finder')
135-
break
136-
case 'request-data':
137-
mainWindow.webContents.send('request-data')
138-
break
139-
case 'quit-app':
140-
appQuit = true
141-
app.quit()
142-
break
143-
}
144-
}
145-
function errorFormat (output) {
146-
console.error('FINDER(stderr):' + output)
147-
}
148-
149-
function emitToFinder (type, data) {
150-
if (!finderProcess) {
151-
console.log('finder process is not ready')
152-
return
153-
}
154-
var payload = {
155-
type: type,
156-
data: data
157-
}
158-
finderProcess.stdin.write(JSON.stringify(payload), 'utf-8')
159-
}
160169

161170
var userDataPath = app.getPath('userData')
162171
if (!jetpack.cwd(userDataPath).exists('keymap.json')) {

0 commit comments

Comments
 (0)