-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
140 lines (136 loc) · 3.94 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
const { app, Menu, Tray, BrowserWindow, Notification } = require("electron");
const path = require("path");
var sudo = require("sudo-prompt");
var options = {
name: "Electron",
};
if (app.getAppPath().slice(-8) == "app.asar") {
run_path = path.resolve(__dirname, "..");
} else {
run_path = path.resolve(__dirname, "");
}
let tray = null;
app.whenReady().then(() => {
tray = new Tray(`${run_path}/assets/icon.png`);
const contextMenu = Menu.buildFromTemplate([
{
label: "均衡",
type: "radio",
click: () => {
run(0, `sudo ./run.sh 00`);
},
},
{
label: "性能",
type: "radio",
click: function () {
run(0, `sudo ./run.sh 01`);
},
},
{
label: "省电",
type: "radio",
click: function () {
run(0, `sudo ./run.sh 02`);
},
},
{
type: "separator",
},
{
label: "快速充电",
type: "checkbox",
click: () => {
if (contextMenu.items[4].checked) {
run(1, `sudo ./run.sh 11`);
} else {
run(1, `sudo ./run.sh 10`);
}
},
},
{
label: "电池保护",
type: "checkbox",
click: function () {
if (contextMenu.items[5].checked) {
run(2, `sudo ./run.sh 21`);
} else {
run(2, `sudo ./run.sh 20`);
}
},
},
{
type: "separator",
},
{
label: "退出",
click: () => {
app.quit();
},
},
]);
tray.setToolTip("This is my application.");
tray.setTitle("hi");
tray.setContextMenu(contextMenu);
function run(n, c) {
sudo.exec(
`cd ${run_path}&&${c}`,
options,
function (error, stdout, stderr) {
if (error) show("失败");
check(n, `${stdout}`);
}
);
}
function check(n, x) {
x = x.match(/[0-9]x[0-9]/)?.[0];
switch (n) {
case 0:
switch (x) {
case "0x0":
contextMenu.items[0].checked = true;
show("均衡模式开启", "00");
break;
case "0x1":
contextMenu.items[1].checked = true;
show("性能模式开启", "01");
break;
case "0x2":
contextMenu.items[2].checked = true;
show("省电模式开启", "02");
break;
}
break;
case 1:
if (x == "0x0") {
contextMenu.items[4].checked = false;
show("快速充电关闭", "10");
}
if (x == "0x1") {
contextMenu.items[4].checked = true;
show("快速充电开启", "11");
contextMenu.items[5].checked = false;
}
break;
case 2:
if (x == "0x0") {
contextMenu.items[5].checked = false;
show("电池保护关闭", "20");
}
if (x == "0x1") {
contextMenu.items[5].checked = true;
show("电池保护开启", "21");
contextMenu.items[4].checked = false;
}
break;
}
tray.setContextMenu(contextMenu);
}
});
function show(t, icon = null) {
new Notification({
title: t,
icon: `${run_path}/assets/${icon}.png`,
}).show();
console.log(t);
}