aboutsummaryrefslogtreecommitdiff
path: root/src/win.js
blob: 9bec3c9c7d61f15080b0250add2ed8d167e3dd3b (plain)
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
const ipcMain = require("electron").ipcMain;

// logs into the dev tools of the renderer
log = (...args) => {
	win.send("log", ...args);
}

// this increments for every alert that's created, the ID is used to
// keep track of popups being opened or closed.
let alert_id = 0;

// sends an alert to the renderer
alert = async (msg) => {
	alert_id++;

	return new Promise((resolve) => {
		ipcMain.once(`alert-closed-${alert_id}`, () => {
			resolve();
		})

		win.send("alert", {
			id: alert_id,
			message: msg
		})
	})
}

// this increments for every confirm alert that's created, the ID is
// used to keep track of popups being opened or closed.
let confirm_id = 0;

// sends an alert to the renderer
confirm = async (msg) => {
	confirm_id++;

	return new Promise((resolve) => {
		ipcMain.once(`confirm-closed-${confirm_id}`, (event, confirmed) => {
			resolve(confirmed);
		})

		win.send("confirm", {
			message: msg,
			id: confirm_id
		})
	})
}

let win = {
	send: () => {},

    log: log,
    alert: alert,
    confirm: confirm
}

let func = () => {
	return win;
}

func.set = (main_window) => {
	win = main_window;

    win.log = log;
    win.alert = alert;
    win.confirm = confirm;
}

module.exports = func;