- Save WhatsApp media downloads without overwriting existing files - Show download notifications with open and folder actions - Display the app version and update Linux package metadata
282 lines
5.4 KiB
JavaScript
282 lines
5.4 KiB
JavaScript
const path = require('node:path');
|
|
|
|
const {
|
|
app,
|
|
dialog,
|
|
ipcMain,
|
|
shell
|
|
} = require('electron');
|
|
|
|
const TOAST_TIMEOUT_MS = 5000;
|
|
|
|
const TOAST_CSS = `
|
|
#wad-toast-container {
|
|
position: fixed;
|
|
right: 24px;
|
|
bottom: 24px;
|
|
z-index: 2147483647;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: flex-end;
|
|
gap: 10px;
|
|
pointer-events: none;
|
|
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 14px;
|
|
box-sizing: border-box;
|
|
width: 600px;
|
|
max-width: calc(100vw - 48px);
|
|
height: 84px;
|
|
padding: 0 18px;
|
|
border-radius: 14px;
|
|
background: #2f7b24;
|
|
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
|
|
color: #fff;
|
|
overflow: hidden;
|
|
pointer-events: auto;
|
|
line-height: normal;
|
|
animation: wad-toast-in 180ms ease-out;
|
|
transition: opacity 160ms ease, transform 160ms ease;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast--leaving {
|
|
opacity: 0;
|
|
transform: translateY(8px);
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__badge {
|
|
display: grid;
|
|
flex: none;
|
|
place-items: center;
|
|
width: 44px;
|
|
height: 44px;
|
|
border-radius: 50%;
|
|
background: #fff;
|
|
color: #2f7b24;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__divider {
|
|
flex: none;
|
|
width: 1px;
|
|
height: 44px;
|
|
background: rgba(255, 255, 255, 0.4);
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__text {
|
|
display: flex;
|
|
flex: 1 1 auto;
|
|
flex-direction: column;
|
|
gap: 4px;
|
|
min-width: 0;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__title {
|
|
font-size: 14.5px;
|
|
font-weight: 700;
|
|
line-height: 1.15;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__file {
|
|
overflow: hidden;
|
|
color: rgba(255, 255, 255, 0.82);
|
|
font-size: 12.5px;
|
|
line-height: 1.15;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__actions {
|
|
display: flex;
|
|
flex: none;
|
|
gap: 10px;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__pill {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 7px;
|
|
height: 36px;
|
|
padding: 0 14px;
|
|
border: 1.5px solid rgba(255, 255, 255, 0.9);
|
|
border-radius: 999px;
|
|
background: transparent;
|
|
color: #fff;
|
|
font: inherit;
|
|
font-size: 12.5px;
|
|
font-weight: 500;
|
|
line-height: 1;
|
|
white-space: nowrap;
|
|
appearance: none;
|
|
cursor: pointer;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__pill:hover {
|
|
background: rgba(255, 255, 255, 0.12);
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__pill:active {
|
|
background: rgba(255, 255, 255, 0.2);
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__close {
|
|
display: grid;
|
|
flex: none;
|
|
place-items: center;
|
|
width: 26px;
|
|
height: 26px;
|
|
padding: 0;
|
|
border: 0;
|
|
border-radius: 50%;
|
|
background: transparent;
|
|
color: rgba(255, 255, 255, 0.85);
|
|
appearance: none;
|
|
cursor: pointer;
|
|
}
|
|
|
|
#wad-toast-container .wad-toast__close:hover {
|
|
background: rgba(255, 255, 255, 0.14);
|
|
color: #fff;
|
|
}
|
|
|
|
#wad-toast-container button:focus-visible {
|
|
outline: 2px solid #fff;
|
|
outline-offset: 2px;
|
|
}
|
|
|
|
@keyframes wad-toast-in {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(12px);
|
|
}
|
|
|
|
to {
|
|
opacity: 1;
|
|
transform: none;
|
|
}
|
|
}
|
|
|
|
@media (prefers-reduced-motion: reduce) {
|
|
#wad-toast-container .wad-toast {
|
|
animation: none;
|
|
transition: none;
|
|
}
|
|
}
|
|
`;
|
|
|
|
const downloads = new Map(); // id -> saved file path
|
|
|
|
let mainWindow = null;
|
|
let nextId = 1;
|
|
let ipcRegistered = false;
|
|
|
|
function setWindow(win) {
|
|
mainWindow = win;
|
|
}
|
|
|
|
function isMainWindow(sender) {
|
|
return Boolean(
|
|
mainWindow &&
|
|
!mainWindow.isDestroyed() &&
|
|
sender === mainWindow.webContents
|
|
);
|
|
}
|
|
|
|
function injectStyles(webContents) {
|
|
return webContents.insertCSS(TOAST_CSS);
|
|
}
|
|
|
|
function show(filePath) {
|
|
if (!filePath || !mainWindow || mainWindow.isDestroyed()) {
|
|
return;
|
|
}
|
|
|
|
const id = nextId;
|
|
nextId += 1;
|
|
|
|
downloads.set(id, filePath);
|
|
|
|
const home = app.getPath('home');
|
|
const displayPath = filePath.startsWith(home)
|
|
? `~${filePath.slice(home.length)}`
|
|
: filePath;
|
|
|
|
mainWindow.webContents.send('download-toast:show', {
|
|
id,
|
|
name: path.basename(filePath),
|
|
location: displayPath,
|
|
timeout: TOAST_TIMEOUT_MS
|
|
});
|
|
}
|
|
|
|
function closeAll() {
|
|
downloads.clear();
|
|
|
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
|
mainWindow.webContents.send('download-toast:clear');
|
|
}
|
|
}
|
|
|
|
function registerIpc() {
|
|
if (ipcRegistered) {
|
|
return;
|
|
}
|
|
|
|
ipcRegistered = true;
|
|
|
|
ipcMain.on('download-toast:action', (event, message) => {
|
|
if (!isMainWindow(event.sender)) {
|
|
return;
|
|
}
|
|
|
|
const id = message ? message.id : undefined;
|
|
const action = message ? message.action : undefined;
|
|
const filePath = downloads.get(id);
|
|
|
|
if (!filePath) {
|
|
return;
|
|
}
|
|
|
|
if (action === 'folder') {
|
|
downloads.delete(id);
|
|
shell.showItemInFolder(filePath);
|
|
return;
|
|
}
|
|
|
|
if (action === 'open') {
|
|
downloads.delete(id);
|
|
|
|
shell.openPath(filePath).then((error) => {
|
|
if (error) {
|
|
dialog.showErrorBox('Kan bestand niet openen', error);
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (action === 'dismiss') {
|
|
downloads.delete(id);
|
|
}
|
|
});
|
|
|
|
ipcMain.on('download-toast:closed', (event, id) => {
|
|
if (!isMainWindow(event.sender)) {
|
|
return;
|
|
}
|
|
|
|
downloads.delete(id);
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
show,
|
|
closeAll,
|
|
registerIpc,
|
|
setWindow,
|
|
injectStyles
|
|
};
|