Download progress in taskbar

pull/191/head
Andrew 2 years ago
parent 14f45830f2
commit 25fb910c56

@ -12,27 +12,31 @@ const {autoUpdater} = require("electron-updater");
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true"; process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
/**@type {BrowserWindow} */
let win = null; let win = null;
let secondaryWindow = null; let secondaryWindow = null;
let tray = null; let tray = null;
let isQuiting = false; let isQuiting = false;
let indexIsOpen = true; let indexIsOpen = true;
let trayEnabled = false; let trayEnabled = false;
const configFile = path.join(app.getPath("userData"), "config.json");
function createWindow() { function createWindow() {
const bounds = JSON.parse((getItem("bounds", configFile) || "{}"));
console.log("bounds:", bounds)
win = new BrowserWindow({ win = new BrowserWindow({
autoHideMenuBar: true, autoHideMenuBar: true,
show: false, show: false,
icon: __dirname + "/assets/images/icon.png", icon: __dirname + "/assets/images/icon.png",
// @ts-ignore
spellcheck: false,
webPreferences: { webPreferences: {
nodeIntegration: true, nodeIntegration: true,
contextIsolation: false, contextIsolation: false,
spellcheck: false,
}, },
}); });
win.setBounds(bounds)
win.on("close", (event) => { win.on("close", (event) => {
if (!isQuiting && trayEnabled) { if (!isQuiting && trayEnabled) {
event.preventDefault(); event.preventDefault();
@ -41,11 +45,14 @@ function createWindow() {
} }
return false; return false;
}); });
win.on("resize", (event) => {
setItem("bounds", JSON.stringify(win.getBounds()), configFile);
});
win.loadFile("html/index.html"); win.loadFile("html/index.html");
win.maximize();
// win.setMenu(null) // win.setMenu(null)
win.show(); win.show();
// win.webContents.openDevTools();
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} }
let loadedLanguage; let loadedLanguage;
@ -56,7 +63,6 @@ if (!gotTheLock) {
app.quit(); app.quit();
} else { } else {
app.on("second-instance", (event, commandLine, workingDirectory) => { app.on("second-instance", (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (win) { if (win) {
win.show(); win.show();
} }
@ -311,6 +317,12 @@ ipcMain.on("autoUpdate", (event, status) => {
} }
}); });
ipcMain.on("progress", (_event, percentage) => {
if (win) {
win.setProgressBar(percentage)
}
})
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => { autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
/** /**
* @type {Electron.MessageBoxOptions} * @type {Electron.MessageBoxOptions}
@ -338,3 +350,39 @@ function i18n(phrase) {
} }
return translation; return translation;
} }
/**
* @param {string} itemName
* @param {string} itemContent
* @param {string} configPath
*/
function setItem(itemName, itemContent, configPath) {
let config = {};
if (fs.existsSync(configPath)) {
const fileContent = fs.readFileSync(configPath).toString();
config = JSON.parse(fileContent);
config[itemName] = itemContent;
} else {
config[itemName] = itemContent;
}
fs.writeFileSync(configPath, JSON.stringify(config));
}
/**
* @param {string} item
* @param {string} configPath
* @returns {string}
*/
function getItem(item, configPath) {
if (fs.existsSync(configPath)) {
try {
const configData = JSON.parse(fs.readFileSync(configPath, "utf8"));
return configData[item] || "";
} catch (err) {
return "";
}
} else {
return "";
}
}

@ -4,7 +4,7 @@
"yt-dlp-wrap-plus": "^2.3.18" "yt-dlp-wrap-plus": "^2.3.18"
}, },
"name": "ytdownloader", "name": "ytdownloader",
"version": "3.16.1", "version": "3.16.2",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"start": "electron .", "start": "electron .",

@ -466,14 +466,15 @@ async function getInfo(url) {
1000000 1000000
).toFixed(2); ).toFixed(2);
} else { } else {
if (format.tbr) { // if (format.tbr) {
size = ( // size = (
(format.tbr * 128 * duration) / // (format.tbr * 50 * duration) /
1000000 // 1000000
).toFixed(2); // ).toFixed(2);
} else { // } else {
size = i18n.__("Unknown size");
} // }
size = i18n.__("Unknown size");
} }
// For videos // For videos
@ -526,7 +527,9 @@ async function getInfo(url) {
format.format_id || format.format_id ||
"Unknown quality"; "Unknown quality";
const spaceAfterQuality = "&#160".repeat( const spaceAfterQuality = "&#160".repeat(
8 - quality.length quality.length > 8 && 8 - quality.length > 0
? 8 - quality.length
: quality.length + 2
); );
// Extension // Extension
@ -1121,16 +1124,21 @@ function download(
if (progress.percent == 100) { if (progress.percent == 100) {
getId(randomId + "prog").textContent = getId(randomId + "prog").textContent =
i18n.__("Processing") + "..."; i18n.__("Processing") + "...";
ipcRenderer.send("progress", 0)
} else { } else {
getId(randomId + "speed").textContent = `${i18n.__("Speed")}: ${ getId(randomId + "speed").textContent = `${i18n.__("Speed")}: ${
progress.currentSpeed || 0 progress.currentSpeed || 0
}`; }`; ipcRenderer.send("progress", progress.percent)
getId( getId(
randomId + "prog" randomId + "prog"
).innerHTML = `<progress class="progressBar" min=0 max=100 value=${progress.percent}>`; ).innerHTML = `<progress class="progressBar" min=0 max=100 value=${progress.percent}>`;
ipcRenderer.send("progress", progress.percent / 100)
} }
}) })
.once("ytDlpEvent", (eventType, eventData) => { .once("ytDlpEvent", (_eventType, _eventData) => {
getId(randomId + "prog").textContent = i18n.__("Downloading..."); getId(randomId + "prog").textContent = i18n.__("Downloading...");
}) })
.once("close", (code) => { .once("close", (code) => {

Loading…
Cancel
Save