Skip to content

Commit 16bcd86

Browse files
committed
Refactor main process and add exception handler for Socket server
1 parent be3c519 commit 16bcd86

5 files changed

Lines changed: 275 additions & 116 deletions

File tree

atom-lib/finder-window.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const app = electron.app
66
const ipcMain = electron.ipcMain
77
const Tray = electron.Tray
88
const path = require('path')
9-
const nodeIpc = require('node-ipc')
9+
const nodeIpc = require('@rokt33r/node-ipc')
1010

1111
var isFinderLoaded = false
1212

@@ -18,6 +18,13 @@ nodeIpc.connectTo(
1818
'main',
1919
path.join(app.getPath('userData'), 'boost.service'),
2020
function () {
21+
nodeIpc.of.main.on(
22+
'error',
23+
function (err) {
24+
nodeIpc.log('<< ## err ##'.rainbow, nodeIpc.config.delay)
25+
nodeIpc.log(err)
26+
}
27+
)
2128
nodeIpc.of.main.on(
2229
'connect',
2330
function () {

atom-lib/main-window.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const electron = require('electron')
2+
const app = electron.app
23
const BrowserWindow = electron.BrowserWindow
34
const path = require('path')
45

@@ -20,4 +21,9 @@ mainWindow.webContents.on('new-window', function (e) {
2021
e.preventDefault()
2122
})
2223

24+
app.on('activate', function () {
25+
if (mainWindow == null) return null
26+
mainWindow.show()
27+
})
28+
2329
module.exports = mainWindow

hotkey.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const electron = require('electron')
2+
const app = electron.app
3+
const Menu = electron.Menu
4+
const ipc = electron.ipcMain
5+
const globalShortcut = electron.globalShortcut
6+
const jetpack = require('fs-jetpack')
7+
const path = require('path')
8+
const mainWindow = require('./atom-lib/main-window')
9+
const nodeIpc = require('@rokt33r/node-ipc')
10+
11+
var userDataPath = app.getPath('userData')
12+
if (!jetpack.cwd(userDataPath).exists('keymap.json')) {
13+
jetpack.cwd(userDataPath).file('keymap.json', {content: '{}'})
14+
}
15+
try {
16+
global.keymap = JSON.parse(jetpack.cwd(userDataPath).read('keymap.json', 'utf-8'))
17+
} catch (err) {
18+
jetpack.cwd(userDataPath).file('keymap.json', {content: '{}'})
19+
global.keymap = {}
20+
}
21+
if (global.keymap.toggleFinder == null) global.keymap.toggleFinder = 'ctrl+tab+shift'
22+
var toggleFinderKey = global.keymap.toggleFinder
23+
24+
try {
25+
globalShortcut.register(toggleFinderKey, function () {
26+
emitToFinder('open-finder')
27+
mainWindow.webContents.send('open-finder', {})
28+
})
29+
} catch (err) {
30+
console.log(err.name)
31+
}
32+
33+
ipc.on('hotkeyUpdated', function (event, newKeymap) {
34+
console.log('got new keymap')
35+
console.log(newKeymap)
36+
globalShortcut.unregisterAll()
37+
global.keymap = newKeymap
38+
jetpack.cwd(userDataPath).file('keymap.json', {content: JSON.stringify(global.keymap)})
39+
40+
var toggleFinderKey = global.keymap.toggleFinder != null ? global.keymap.toggleFinder : 'ctrl+tab+shift'
41+
try {
42+
globalShortcut.register(toggleFinderKey, function () {
43+
emitToFinder('open-finder')
44+
mainWindow.webContents.send('open-finder', {})
45+
})
46+
mainWindow.webContents.send('APP_SETTING_DONE', {})
47+
} catch (err) {
48+
console.error(err)
49+
mainWindow.webContents.send('APP_SETTING_ERROR', {
50+
message: 'Failed to apply hotkey: Invalid format'
51+
})
52+
}
53+
})
54+
55+
56+
function emitToFinder (type, data) {
57+
var payload = {
58+
type: type,
59+
data: data
60+
}
61+
nodeIpc.server.broadcast('message', payload)
62+
}

0 commit comments

Comments
 (0)