-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.js
180 lines (146 loc) · 4.52 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
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
173
174
175
176
177
178
179
180
const {app,Tray,Menu,shell,BrowserWindow} = require("electron"); //electron application stuff
const path = require("path"); //allows for use of path
const url = require("url"); //allows for loadURL and url.format
const iconPath = path.join(__dirname, "/assets/icon.png"); //grab the icon
const fs = require("fs");
const filePath = path.join(__dirname, "/assets/settings.txt");
const electronLocalshortcut = require("electron-localshortcut");
let tray = null; //set the tray to null
let win = null; //set the main window to null
let pref = null;
let abt = null;
var dev = null;
if (process.argv.length === 3) {
if (process.argv.slice(2).toString().toLowerCase() === "dev") {
dev = true;
} else {
dev = false;
}
}
function restart() {
app.relaunch();
app.isQuiting = true;
app.quit();
}
function preferencesWindow() {
pref = new BrowserWindow({
width: 500,
height: 730,
resizable: false
});
pref.setMenu(null); //the about window has no menu
pref.loadURL(url.format({ //loads the webpage for the about window
pathname: path.join(__dirname, "/pages/preferences.html"),
protocol: "file:",
slashes: true
}));
if (dev) {
pref.openDevTools();
}
electronLocalshortcut.register(pref, process.platform === "darwin" ? "Cmd+R" : "Ctrl+R", () => {
restart();
});
electronLocalshortcut.register(pref, process.platform === "darwin" ? "Cmd+Q" : "Ctrl+Q", () => {
app.isQuitting = true;
app.quit();
});
}
function readFile() {
var mySettings = [];
mySettings = fs.readFileSync(filePath, "utf8"); //read in the settings file
mySettings = (mySettings).split(" "); //split up the settings into an array (each index contains a different setting)
return mySettings;
}
app.on("ready", function() {
win = new BrowserWindow({
width: 600,
height: 475,
resizable: false
}); //create main window
win.setMenu(null); //the main window had no menu
win.loadURL(url.format({ //loads the webpage for the main window
pathname: path.join(__dirname, "/pages/index.html"),
protocol: "file:",
slashes: true
}));
//console.log(settings.getAll());
if (dev) {
win.openDevTools(); //starts the application with developer tools open
}
var mySettings = readFile();
win.on("minimize", function(event) { //prevents standard minimize function of a main window
event.preventDefault();
win.hide();
});
win.on("close", function(event) { //prevents the closing of the aplication when the window closes
if (mySettings[0] === "true") {
app.quit();
} else {
if (!app.isQuiting) {
event.preventDefault();
win.hide();
}
}
});
electronLocalshortcut.register(win, process.platform === "darwin" ? "Cmd+P" : "Ctrl+P", () => {
preferencesWindow();
});
electronLocalshortcut.register(win, process.platform === "darwin" ? "Cmd+Q" : "Ctrl+Q", () => {
app.isQuitting = true;
app.quit();
});
electronLocalshortcut.register(win, process.platform === "darwin" ? "Cmd+R" : "Ctrl+R", () => {
restart();
});
tray = new Tray(iconPath); //create a new tray
var contextMenu = Menu.buildFromTemplate([ //start buliding out the menu for the tray
{
label: "Sandman",
click: function() { //makes the main window reappear
win.show();
}
},
{
label: "About",
click: function() { //shows the about window
abt = new BrowserWindow({
width: 500,
height: 625,
resizable: false
});
abt.setMenu(null); //the about window has no menu
abt.loadURL(url.format({ //loads the webpage for the about window
pathname: path.join(__dirname, "/pages/about.html"),
protocol: "file:",
slashes: true
}));
if (dev) {
abt.openDevTools();
}
}
},
{
label: "Preferences",
accelerator: "CommandOrControl+P",
click: function() { //shows the about window
preferencesWindow();
}
},
{
label: "Report a bug...",
click: function() { //shows the about window
shell.openExternal("https://github.com/alexanderepstein/Sandman/issues/new");
}
},
{
label: "Quit",
accelerator: "CommandOrControl+Q",
click: function() { //quit the application
app.isQuiting = true;
app.quit(); //quit called
}
}
]);
tray.setToolTip("Sandman"); //Honestly no clue but itll make the tray say insomnia in some other place
tray.setContextMenu(contextMenu); //attach the menu to the tray
});