-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
125 lines (106 loc) · 3.81 KB
/
main.js
File metadata and controls
125 lines (106 loc) · 3.81 KB
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
import {ConfigJSON} from "./src/modules/config-json"
const electron = require('electron');
const app = electron.app; // Module to control application life.
app.commandLine.appendSwitch("--ignore-certificate-errors");
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const Menu = electron.Menu;
const shell = electron.shell;
const ipc = electron.ipcMain;
const localConfig = new ConfigJSON(`${process.cwd()}/data.json`);
//TODO: Add remote config
//TODO: Add tray icon
Menu.setApplicationMenu(null);
// Report crashes to our server.
//electron.crashReporter.start({companyName: 'raf924', submitURL: 'http://127.0.0.1'});
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
let mainWindow = null;
let webContents = null;
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
ipc.on("join", function (event, data) {
event.sender.send("openChannel", data);
});
ipc.on("set", function (event, args) {
//TODO: Add a remote config
localConfig.set(args.prop, args.value);
localConfig.save();
});
ipc.on("get", function (event, prop, async) {
let value = localConfig.get(prop);
if (async) {
event.sender.send(prop, value || "");
} else {
event.returnValue = value || "";
}
});
ipc.on("close", function (event) {
mainWindow.close();
});
ipc.on("minimize", function (event) {
mainWindow.minimize();
});
ipc.on("fullscreen", function (event) {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize()
} else {
mainWindow.maximize();
}
});
app.on('ready', function () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: localConfig.get("windowWidth"),
height: localConfig.get("windowHeight"),
frame: false,
icon: "./static/img/icon128.png",
minWidth: 400,
minHeight: 400
});
mainWindow.setTitle("Chatron");
Menu.setApplicationMenu(null);
// and load the index.html of the app.
let htmlFile = `file://${__dirname}/../platforms/browser/www/index.html`;
mainWindow.loadURL(htmlFile);
webContents = mainWindow.webContents;
webContents.on("did-finish-load", function () {
webContents.executeJavaScript("document.dispatchEvent(new Event('deviceready'));");
});
mainWindow.flashFrame(true);
// Open the devtools.
process.argv.forEach(function (item) {
if (item === "dev") {
mainWindow.webContents.openDevTools({mode: "undocked"});
}
});
mainWindow.on('resize', function () {
localConfig.set("windowHeight", mainWindow.height);
localConfig.set("windowWidth", mainWindow.width);
});
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
webContents.on("new-window", function (e, url, frameName, disposition) {
if (url.match(/(https:\/\/hack\.chat)|(file:\/\/.+\?.+)/i) != null) {
e.preventDefault();
this.send("openChannel", JSON.stringify({channel: url.split("?")[1]}));
} else {
if (!localConfig.get("openInside")) {
e.preventDefault();
shell.openExternal(url);
}
}
});
});