Skip to content

Instantly share code, notes, and snippets.

@astec-kte
Created Jul 19, 2021
Embed
What would you like to do?
electron-app-sexy
const { dialog } = require('electron');
const { autoUpdater } = require('electron-updater');
autoUpdater.autoDownload = true;
autoUpdater.on('error', (error) => {
dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString());
});
autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
title: 'Found Updates',
message: 'Found updates, do you want update now?',
buttons: ['Sure', 'No']
}, (buttonIndex) => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate();
}
});
});
autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({
title: 'No Updates',
message: 'Current version is up-to-date.'
});
});
autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({
title: 'Install Updates',
message: 'Updates downloaded, application will be quit for update...'
}, () => {
setImmediate(() => autoUpdater.quitAndInstall());
});
});
function checkForUpdates (menuItem, focusedWindow, event) {
autoUpdater.checkForUpdates();
}
module.exports.checkForUpdates = checkForUpdates;
module.exports = {
init: () => {
const dragAndDrop = document.getElementById('drag-and-drop');
dragAndDrop.ondragover = () => {
return false;
};
dragAndDrop.ondragleave = () => {
return false;
};
dragAndDrop.ondragend = () => {
return false;
};
dragAndDrop.ondrop = (e) => {
e.preventDefault();
const paths = [];
for (let file of e.dataTransfer.files) {
paths.push(file.path);
}
// create html from file paths
const fileItems = paths.reduce((html, file) => {
html += `<li class="file-item">${file}</li>`;
return html;
}, '');
// put File paths items into fileList element
const fileList = document.getElementById('fileList');
fileList.innerHTML = fileItems;
return false;
};
},
};
const { crashReporter } = require('electron')
crashReporter.start({ submitURL: 'https://your-domain.com/url-to-submit' })
import {Menu, shell, app} from 'electron';
module.exports = {
create: (mainWindow) => {
const menu = Menu.buildFromTemplate([
{
label: 'Menu',
submenu: [
{
label: 'Preview notification',
click() {
mainWindow.webContents.send('notify-user', 'Thanks for clicking!');
}
},
{
label: 'Exit',
click(){
app.quit();
}
}
]
}
]);
Menu.setApplicationMenu(menu);
}
};
module.exports = {
notify: (message) => {
const notification = {
title: 'Electron App',
body: message,
icon: 'assets/icons/check-ok.ico'
};
new window.Notification(notification.title, notification);
}
};
import { Menu, Tray, nativeImage } from 'electron';
let tray = null;
const trayIcon = nativeImage.createFromPath('assets/icons/electron-logo-icon.png').resize({
width: 18,
height: 18,
});
module.exports = {
setTray: () => {
tray = new Tray(trayIcon);
const contextMenu = Menu.buildFromTemplate([
{ label: 'Tray item 1', type: 'radio' },
{ label: 'Tray item 2', type: 'radio' },
{ label: 'Tray item 3', type: 'radio', checked: true }
]);
tray.setToolTip('This text comes from tray module.');
// Need to call to set Context Menu.
tray.setContextMenu(contextMenu);
},
destroy: () => {
if (tray) {
tray.destroy();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment