-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
144 lines (127 loc) · 3.88 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require('dotenv').config({ path: __dirname + '/../.env' })
import electron from 'electron'
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const Tray = electron.Tray
const ipcMain = electron.ipcMain
import Promise from 'bluebird'
const fs = Promise.promisifyAll(require('fs'))
const mkdirp = Promise.promisifyAll(require('mkdirp'))
import path from 'path'
import TweetRec from './lib/tweet-rec'
global.tweetRec = new TweetRec()
tweetRec.distDir = path.join(app.getPath('appData'), app.getName(), 'csv')
var mainWindow = null
var appIcon = null
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 320,
height: 480,
minWidth: 320,
minHeight: 480,
useContentSize: true
})
mainWindow.loadURL('file://' + __dirname + '/index.html')
mainWindow.on('closed', () => {
mainWindow = null
})
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('update-state', store.getState())
})
}
const showWindow = () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
} else {
createWindow()
}
}
const setupMenu = () => {
let template = [
{
label: 'Twitter',
submenu: [
{ label: 'Auth', click: () => store.dispatch(actions.showAuth()) }
]
},
{
label: 'Window',
role: 'window',
submenu: [
{ label: 'Minimize', accelerator: 'CmdOrCtrl+M', role: 'minimize' },
{ label: 'Close', accelerator: 'CmdOrCtrl+W', role: 'close' },
{ type: 'separator' },
{ label: 'Bring All to Front', role: 'front' }
]
},
{
label: 'Help',
role: 'help',
submenu: [
{ label: 'Website', click: () => electron.shell.openExternal('https://github.com/midnightSuyama/tweet-rec') }
]
}
]
if (process.platform === 'darwin') {
template.unshift({
submenu: [
{ label: `About ${app.getName()}`, role: 'about' },
{ type: 'separator' },
{ label: 'Services', role: 'services', submenu: [] },
{ type: 'separator' },
{ label: `Hide ${app.getName()}`, accelerator: 'Command+H', role: 'hide' },
{ label: 'Hide Others', accelerator: 'Command+Alt+H', role: 'hideothers' },
{ label: 'Show All', role: 'unhide' },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'Command+Q', click: () => app.quit() }
]
})
}
let menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
const setupTray = () => {
let image = path.join(__dirname, '..', 'res', (process.platform === 'darwin' ? 'tray-22.png' : 'tray-32.png'))
appIcon = new Tray(image)
appIcon.setToolTip(app.getName())
let template = [
{ label: 'Show', click: () => showWindow() },
{ type: 'separator' },
{ label: 'Quit', click: () => app.quit() }
]
let menu = Menu.buildFromTemplate(template)
appIcon.setContextMenu(menu)
}
var shouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
showWindow()
})
if (shouldQuit) app.quit()
app.on('ready', () => {
createWindow()
setupMenu()
setupTray()
}).on('activate', () => {
showWindow()
}).on('window-all-closed', () => {})
// redux
import store from './store/configureStore'
import * as actions from './actions'
const stateCachePath = path.join(app.getPath('cache'), app.getName(), 'state.json')
store.subscribe(() => {
if (mainWindow) {
mainWindow.webContents.send('update-state', store.getState())
}
mkdirp.mkdirpAsync(path.dirname(stateCachePath)).then(() => {
fs.writeFile(stateCachePath, JSON.stringify(store.getState()))
})
})
ipcMain.on('dispatch-action', (event, action) => {
store.dispatch(action)
})
fs.readFileAsync(stateCachePath).then(JSON.parse).then(state => {
state.schedules.forEach(schedule => {
store.dispatch(actions.addSchedule(schedule))
})
})