Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.

Commit eb2c29b

Browse files
committed
v2.0.0b6
1 parent 443d635 commit eb2c29b

34 files changed

+3576
-10170
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
package-lock.json

addons/notifications/addon.js

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
const fs = require('node:fs'),
2+
path = require('node:path'),
3+
notifications = require('electron-custom-notifications');
4+
5+
const PATH_ICON = path.join(__dirname, '..', '..', 'public', 'images', 'logo.png'),
6+
BASE64_ICON = fs.readFileSync(PATH_ICON, 'base64');
7+
8+
let _config = {},
9+
_screen = 0,
10+
_sender = null,
11+
_default = {
12+
settings: {
13+
screen: 0
14+
}
15+
};
16+
17+
function is_numeric(n) {
18+
return (!isNaN(parseFloat(n)) && isFinite(n));
19+
}
20+
21+
function update_interface() {
22+
const screens = notifications.getScreens();
23+
24+
_sender('message', 'config', _config);
25+
_sender('message', 'screens', screens.length);
26+
}
27+
28+
function save_config() {
29+
_sender('manager', 'config', _config);
30+
}
31+
32+
function next_screen(index) {
33+
const screens = notifications.getScreens();
34+
if (typeof(index) === 'undefined') {
35+
_screen = ((_screen + 1) % screens.length);
36+
_config.settings.screen = _screen;
37+
} else {
38+
_screen = ((index < screens.length) ? index : 0);
39+
}
40+
41+
notifications.setScreen(_screen);
42+
}
43+
44+
const functions = {
45+
GetScreens: async () => {
46+
return notifications.getScreens();
47+
},
48+
SetScreen: async index => {
49+
next_screen(index);
50+
},
51+
ShowNotification: async (message, title, icon, timeout) => {
52+
if (_config.default.enabled && message.length) {
53+
if (icon && fs.existsSync(icon)) {
54+
icon = fs.readFileSync(icon, 'base64');
55+
}
56+
57+
notifications.createNotification({
58+
parameters: [
59+
{ key: 'logo', value: (icon || BASE64_ICON) },
60+
{ key: 'title', value: (title || 'Scripts Manager') },
61+
{ key: 'body', value: message }
62+
],
63+
timeout: (timeout > 0) ? timeout : 6000,
64+
});
65+
}
66+
}
67+
};
68+
69+
module.exports = {
70+
init: (origin, config, sender, vars) => {
71+
_sender = sender;
72+
_config = config;
73+
74+
for (const section in _default) {
75+
if (typeof(_config[section]) !== 'object') {
76+
_config[section] = {};
77+
}
78+
79+
for (const name in _default[section]) {
80+
const config_value = _config[section][name];
81+
const default_value = _default[section][name];
82+
const config_type = typeof(config_value);
83+
const default_type = typeof(default_value);
84+
if (config_type !== default_type) {
85+
if (default_type === 'number' && config_type === 'string' && is_numeric(config_value)) {
86+
_config[section][name] = parseFloat(config_value);
87+
} else {
88+
_config[section][name] = default_value;
89+
}
90+
}
91+
}
92+
}
93+
94+
notifications.setContainerWidth(350);
95+
96+
notifications.setGlobalStyles(`
97+
notification {
98+
overflow: hidden;
99+
display: flex;
100+
margin: 10px;
101+
padding: 20px;
102+
background-color: #fff;
103+
border-radius: 12px;
104+
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
105+
}
106+
notification .logo {
107+
margin-right: 20px;
108+
width: 50px;
109+
height: 50px;
110+
background-size: contain;
111+
background-repeat: no-repeat;
112+
background-position: center;
113+
}
114+
notification .content {
115+
font-family: sans-serif;
116+
}
117+
notification .content h1 {
118+
margin-bottom: 5px;
119+
font-weight: bold;
120+
}`);
121+
122+
notifications.setDefaultTemplate(`
123+
<notification id="%id%" class="animated fadeInUp">
124+
<div class="logo" style="background-image: url('data:image/png;base64,%logo%');"></div>
125+
<div class="content">
126+
<h1>%title%</h1>
127+
<p>%body%</p>
128+
</div>
129+
</notification>`);
130+
131+
_screen = _config.settings.screen;
132+
next_screen(_screen);
133+
},
134+
initialized: () => {
135+
_sender('manager', 'menu', [
136+
{ label: 'Next Screen', click : () => {
137+
next_screen();
138+
update_interface();
139+
save_config();
140+
141+
functions.ShowNotification(`Screen: ${_screen + 1}`, 'Screen change');
142+
} }
143+
]);
144+
},
145+
receiver: async (id, name, data) => {
146+
if (id === 'manager') {
147+
if (name === 'show') {
148+
update_interface();
149+
} else if (name === 'enabled') {
150+
_config.default.enabled = data;
151+
}
152+
153+
return;
154+
} else if (id === 'message') {
155+
if (typeof(data) === 'object') {
156+
const name = Object.keys(data)[0];
157+
if (typeof(data[name]) === typeof(_config.settings[name])) {
158+
_config.settings[name] = data[name];
159+
}
160+
save_config();
161+
162+
if (name === 'screen') {
163+
next_screen(data.screen);
164+
165+
if (_config.default.enabled) {
166+
functions.ShowNotification(`Screen: ${_screen + 1}`, 'Screen change');
167+
}
168+
}
169+
}
170+
171+
return;
172+
}
173+
174+
if (_config.default.enabled) {
175+
if (typeof functions[name] === 'function') {
176+
if (Array.isArray(data) && data.length) {
177+
return await functions[name](...data);
178+
} else {
179+
return await functions[name]();
180+
}
181+
}
182+
}
183+
}
184+
}

addons/notifications/config.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[default]
2+
name=Notifications
3+
enabled=false
4+
5+
[settings]
6+
screen=0

addons/notifications/index.html

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'" />
8+
9+
<title>Notifications</title>
10+
11+
<link id="config_stylesheet" rel="stylesheet" />
12+
<style type="text/css">
13+
.has-addons .button {
14+
border-color: #444;
15+
}
16+
</style>
17+
</head>
18+
19+
<body>
20+
<div class="container">
21+
<section class="hero is-small has-background-grey-dark">
22+
<div class="hero-body">
23+
<p class="subtitle is-size-6 has-text-white-ter">Notifications usable via scripts (such as Multi Actions).</p>
24+
</div>
25+
</section>
26+
27+
<div class="m-5">
28+
<div class="columns is-horizontal is-narrow mt-2 mb-5">
29+
<div class="column mt-2">
30+
<label class="label">Screen to notify:</label>
31+
</div>
32+
<div class="column has-text-right">
33+
<div class="is-inline-block screen">
34+
<div class="buttons has-addons"></div>
35+
</div>
36+
</div>
37+
</div>
38+
</div>
39+
</div>
40+
41+
<script type="text/javascript">
42+
let _config = {};
43+
44+
function update_buttons() {
45+
const buttons = document.querySelectorAll('.screen .buttons .button');
46+
for (let i = 0; i < buttons.length; ++i) {
47+
buttons[i].classList.toggle('is-selected', (i == _config.settings.screen));
48+
}
49+
}
50+
51+
window.addEventListener('message', event => {
52+
if (event.origin !== 'null') {
53+
if (event.data.name == 'config') {
54+
_config = event.data.data;
55+
update_buttons();
56+
} else if (event.data.name == 'screens') {
57+
const buttons = document.querySelector('.screen .buttons');
58+
buttons.innerHTML = '';
59+
60+
for (let i = 0; i < event.data.data; ++i) {
61+
const button = document.createElement('button');
62+
button.innerText = (i + 1).toString();
63+
64+
button.classList.add('button');
65+
if (i == _config.settings.screen) {
66+
button.classList.add('is-selected');
67+
}
68+
69+
button.addEventListener('click', event => {
70+
_config.settings.screen = Array.prototype.indexOf.call(buttons.children, button);
71+
update_buttons();
72+
73+
event.target.blur();
74+
window.parent.postMessage({screen: _config.settings.screen}, '*');
75+
});
76+
77+
buttons.appendChild(button);
78+
}
79+
}
80+
}
81+
}, false);
82+
</script>
83+
</body>
84+
85+
</html>

0 commit comments

Comments
 (0)