|
| 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 | +} |
0 commit comments