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

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Arubinu committed Feb 21, 2023
1 parent d4dca97 commit a8cb8b7
Show file tree
Hide file tree
Showing 31 changed files with 1,367 additions and 6,994 deletions.
140 changes: 102 additions & 38 deletions addons/notifications/addon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const fs = require('node:fs'),
path = require('node:path'),
https = require('node:https'),
temp = require('temp'),
StreamTransform = require('stream').Transform,
notifications = require('electron-custom-notifications');

const PATH_ICON = path.join(__dirname, '..', '..', 'public', 'images', 'logo.png'),
Expand All @@ -8,9 +11,14 @@ const PATH_ICON = path.join(__dirname, '..', '..', 'public', 'images', 'logo.png
let _config = {},
_screen = 0,
_sender = null,
_timeout = 0,
_default = {
settings: {
screen: 0
round: 0,
scale: 100,
anchor: ['bottom', 'right'],
screen: 0,
opacity: 100
}
};

Expand All @@ -26,12 +34,13 @@ function update_interface() {
}

function save_config() {
_sender('manager', 'config', _config);
set_style();
_sender('manager', 'config:override', _config);
}

function next_screen(index) {
const screens = notifications.getScreens();
if (typeof(index) === 'undefined') {
if (typeof index === 'undefined') {
_screen = ((_screen + 1) % screens.length);
_config.settings.screen = _screen;
} else {
Expand All @@ -41,6 +50,73 @@ function next_screen(index) {
notifications.setScreen(_screen);
}

function set_style() {
const round = (Math.max(0, Math.min(100, _config.settings.round)) * .5).toFixed(0),
scale = (Math.max(0, Math.min(100, _config.settings.scale)) / 100).toFixed(2).replace('.00', ''),
anchor = _config.settings.anchor.join(' '),
opacity = (Math.max(0, Math.min(100, _config.settings.opacity)) / 100).toFixed(2).replace('.00', '');

if (anchor.indexOf('left') >= 0) {
notifications.toLeft();
} else if (anchor.indexOf('right') >= 0) {
notifications.toRight();
}

notifications.setGlobalStyles(`
#notification-container {
${(anchor.indexOf('top') >= 0) ? 'top: 0;' : ''}
${(anchor.indexOf('bottom') >= 0) ? 'bottom: 0;' : ''}
${(anchor.indexOf('left') >= 0) ? 'left: 0;' : ''}
${(anchor.indexOf('right') >= 0) ? 'right: 0;' : ''}
opacity: ${opacity};
transform: scale(${scale});
transform-origin: ${anchor};
}
notification {
overflow: hidden;
display: flex;
margin: 10px;
padding: 12px;
background-color: #fff;
border-radius: ${round}%;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
}
notification .logo {
margin-right: 20px;
width: 50px;
height: 50px;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
notification .content {
font-family: sans-serif;
}
notification .content h1 {
margin-bottom: 5px;
font-weight: bold;
}`);
}

function load_file(name, data) {
return new Promise((resolve, reject) => {
https.request(data, response => {
const sdata = new StreamTransform();

response.on('data', chunk => {
sdata.push(chunk);
});

response.on('end', () => {
const file_path = path.join(temp.mkdirSync(), (name + '.' + response.headers['content-type'].split('/')[1]));
fs.writeFileSync(file_path, sdata.read());

resolve(file_path);
});
}).end();
});
}

const functions = {
GetScreens: async () => {
return notifications.getScreens();
Expand All @@ -50,8 +126,16 @@ const functions = {
},
ShowNotification: async (message, title, icon, timeout) => {
if (_config.default.enabled && message.length) {
if (icon && fs.existsSync(icon)) {
icon = fs.readFileSync(icon, 'base64');
if (icon) {
if (icon.indexOf('://') >= 0) {
icon = await load_file('icon', icon);
} else if (!fs.existsSync(icon)) {
icon = false;
}

if (icon) {
icon = fs.readFileSync(icon, 'base64');
}
}

notifications.createNotification({
Expand All @@ -66,21 +150,22 @@ const functions = {
}
};


module.exports = {
init: (origin, config, sender, vars) => {
_sender = sender;
_config = config;

for (const section in _default) {
if (typeof(_config[section]) !== 'object') {
if (typeof _config[section] !== 'object') {
_config[section] = {};
}

for (const name in _default[section]) {
const config_value = _config[section][name];
const default_value = _default[section][name];
const config_type = typeof(config_value);
const default_type = typeof(default_value);
const config_type = typeof config_value;
const default_type = typeof default_value;
if (config_type !== default_type) {
if (default_type === 'number' && config_type === 'string' && is_numeric(config_value)) {
_config[section][name] = parseFloat(config_value);
Expand All @@ -91,34 +176,8 @@ module.exports = {
}
}

set_style();
notifications.setContainerWidth(350);

notifications.setGlobalStyles(`
notification {
overflow: hidden;
display: flex;
margin: 10px;
padding: 20px;
background-color: #fff;
border-radius: 12px;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
}
notification .logo {
margin-right: 20px;
width: 50px;
height: 50px;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
notification .content {
font-family: sans-serif;
}
notification .content h1 {
margin-bottom: 5px;
font-weight: bold;
}`);

notifications.setDefaultTemplate(`
<notification id="%id%" class="animated fadeInUp">
<div class="logo" style="background-image: url('data:image/png;base64,%logo%');"></div>
Expand Down Expand Up @@ -152,9 +211,9 @@ notification .content h1 {

return;
} else if (id === 'message') {
if (typeof(data) === 'object') {
if (typeof data === 'object') {
const name = Object.keys(data)[0];
if (typeof(data[name]) === typeof(_config.settings[name])) {
if (typeof data[name] === typeof _config.settings[name]) {
_config.settings[name] = data[name];
}
save_config();
Expand All @@ -165,6 +224,11 @@ notification .content h1 {
if (_config.default.enabled) {
functions.ShowNotification(`Screen: ${_screen + 1}`, 'Screen change');
}
} else if (_config.default.enabled) {
clearTimeout(_timeout);
_timeout = setTimeout(() => {
functions.ShowNotification(`${name[0].toUpperCase() + name.substring(1)}: ${Array.isArray(data[name]) ? data[name].join(' ') : data[name]}`, 'Notification settings');
}, 2000 );
}
}

Expand All @@ -181,4 +245,4 @@ notification .content h1 {
}
}
}
}
};
5 changes: 5 additions & 0 deletions addons/notifications/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ name=Notifications
enabled=false

[settings]
round=0
scale=100
anchor[]="bottom"
anchor[]="right"
screen=0
opacity=100
Loading

0 comments on commit a8cb8b7

Please sign in to comment.