forked from Sn8z/Poddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
201 lines (180 loc) · 5.24 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const electron = require("electron");
const app = electron.app;
const Menu = electron.Menu;
const ipcMain = electron.ipcMain;
const BrowserWindow = electron.BrowserWindow;
const globalShortcut = electron.globalShortcut;
const nativeImage = electron.nativeImage;
const windowStateKeeper = require("electron-window-state");
const path = require("path");
var log = require("electron-log");
//Global reference to window object;
var mainWindow = null;
//Launch options
const options = {
debug: false
};
const argv = process.argv.slice(1);
log.info("Flags:");
log.info(argv);
for (const arg of argv) {
if (arg === ".") {
continue;
} else if (arg === "--debug" || arg === "-d") {
log.info("Setting debug to true.");
options.debug = true;
} else {
log.info(arg + " is not a valid flag.");
}
}
//Fix for correctly naming the app...
app.setPath('userData', app.getPath('userData').replace("Poddr", "poddr"));
//Allow actions before user have interacted with the document
app.commandLine.appendSwitch('--autoplay-policy','no-user-gesture-required');
//Quit when all windows are closed
app.on("window-all-closed", function () {
log.info("Exiting Poddr.");
app.quit();
});
ipcMain.on("quit-app", function () {
app.quit();
});
ipcMain.on("raise-app", function () {
mainWindow.show();
mainWindow.focus();
});
//When app is rdy, create window
app.once("ready", function () {
if (process.platform == "linux") {
function registerBindings(desktopEnv, session) {
session.getInterface(
`org.${desktopEnv}.SettingsDaemon`,
`/org/${desktopEnv}/SettingsDaemon/MediaKeys`,
`org.${desktopEnv}.SettingsDaemon.MediaKeys`,
function (error, iface) {
if (error) {
log.info(desktopEnv);
log.info(error);
} else {
iface.on("MediaPlayerKeyPressed", (n, keyName) => {
switch (keyName) {
case "Next":
log.info("next");
return;
case "Previous":
log.info("prev");
return;
case "Play":
mainWindow.webContents.send("toggle-play", "playpause");
return;
case "Stop":
log.info("stop");
return;
default:
return;
}
});
iface.GrabMediaPlayerKeys(
0,
`org.${desktopEnv}.SettingsDaemon.MediaKeys`
);
}
}
);
}
try {
log.info("Registering mediakey bindings.");
var DBus = require("dbus");
var session = DBus.getBus("session");
registerBindings("gnome", session);
registerBindings("mate", session);
} catch (e) {
log.error(e);
}
} else {
//Global shortcut for Play/Pause toggle, player.js listens for the toggle-play event
globalShortcut.register("MediaPlayPause", function () {
mainWindow.webContents.send("toggle-play", "playpause");
});
}
//default window size
let mainWindowState = windowStateKeeper({
defaultWidth: 1200,
defaultHeight: 900
});
let icon = path.join(__dirname, "/app/images/icon.png");
mainWindow = new BrowserWindow({
name: "Poddr",
minWidth: 640,
minHeight: 600,
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
frame: false,
show: false,
webPreferences: {
nodeIntegration: true
},
backgroundColor: "#0f0f0f",
icon: nativeImage.createFromPath(icon)
});
mainWindowState.manage(mainWindow);
mainWindow.on("ready-to-show", function () {
mainWindow.show();
mainWindow.focus();
});
mainWindow.loadURL("file://" + __dirname + "/app/index.html");
//Devtools
if (options.debug) {
log.info("Enabling DevTools.");
mainWindow.webContents.openDevTools({ mode: "detach" });
}
mainWindow.on("closed", function () {
mainWindow = null;
});
//Context & application menus
if (process.platform == "darwin") {
var menuTemplate = [
{
label: "Poddr",
submenu: [
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
}
}
]
},
{
label: "Edit",
submenu: [
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{
label: "Select All",
accelerator: "CmdOrCtrl+A",
selector: "selectAll:"
}
]
}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
}
contextMenuTemplate = [
{ label: "Cut", accelerator: "CmdOrCtrl+X", role: "cut" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", role: "copy" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", role: "paste" },
{ label: "Select all", accelerator: "CmdOrCtrl+A", role: "selectall" }
];
mainWindow.webContents.on("context-menu", function (e, params) {
Menu.buildFromTemplate(contextMenuTemplate).popup(
mainWindow,
params.x,
params.y
);
});
});