-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
172 lines (146 loc) · 4.01 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const { app, BrowserWindow, globalShortcut, Menu } = require('electron')
const { ipcMain } = require('electron')
const fs = require('fs')
const path = require('path')
let listWindow
let inputWindow
// Menu bar configuration
const isMac = process.platform === 'darwin';
const template = [
...(isMac ? [{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}] : []),
{
label: 'Actions',
submenu: [
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
isMac ? { role: 'close' } : { role: 'quit' }
]
}
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
temp_path = path.join(app.getPath("temp"), 'second-brain')
// create the savestate directory...
if (!fs.existsSync(temp_path)){
fs.mkdir(temp_path, (err) => {
if (err) {
return console.error(err);
} else {
console.log("directory for temp data successfully created")
}
})
}
// ... and file
if (!fs.existsSync(path.join(temp_path, 'savestate'))){
fs.writeFile(path.join(temp_path, 'savestate'), '', (err) => {
if(err) {
console.log(err)
} else {
console.log("savestate file successfully created")
}
})
}
// windows
function renderListWindow(){
if (listWindow == null) {
listWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload_listwindow.js')
},
show: false,
skipTaskbar:true,
icon: path.join(__dirname, 'icons', '2ndbrain_icon_256px.ico')
})
listWindow.loadFile('list-window.html');
listWindow.setResizable(false);
// read and send save state
let savestate = fs.readFileSync(path.join(temp_path,'savestate'), 'utf8')
listWindow.webContents.send("importSaveState", savestate)
}
}
function renderInputWindow(){
inputWindow = new BrowserWindow({
width: 800,
height: 250,
transparent:true,
frame:false,
webPreferences: {
preload: path.join(__dirname, 'preload_inputwindow.js')
}
})
inputWindow.loadFile('input-window.html');
inputWindow.setResizable(false);
}
// utility functions
function exportSaveState(data){
savestateLocation = path.join(temp_path, 'savestate');
//state = JSON.parse(data)
if (fs.existsSync(savestateLocation)){
fs.unlinkSync(savestateLocation, (err) => {
if (err) {
console.log(err);
} else {
console.log("old savestate deleted")
}
})
fs.appendFile(savestateLocation, data/*JSON.stringify(state)*/, (err) => {
if (err) {
console.log(err);
}
})
} else {
console.log("No file exists, and no state could therefore be saved.")
}
}
// Listeners
ipcMain.on("newThought", (event, data) => {
inputWindow.close();
inputWindow = null;
listWindow.webContents.send("todoItem", data);
listWindow.webContents.send("reloadList");
})
ipcMain.on("closeInputWindow", (event) => {
inputWindow.close();
inputWindow = null;
})
ipcMain.on("exportSaveState", (event, data) => {
exportSaveState(data)
})
// application behaviour
app.whenReady().then(() => {
renderListWindow();
})
app.whenReady().then(() => {
globalShortcut.register('Alt+CommandOrControl+I', () => {
if (inputWindow == null){
renderInputWindow()
}
else if (inputWindow.isFocused()) {
inputWindow.close();
inputWindow = null;
}
})
globalShortcut.register('Alt+CommandOrControl+B', () => {
listWindow.minimize();
listWindow.restore();
listWindow.focus();
})
})
app.on('quit', () => {
app.quit();
})