ytDownloader/main.js

182 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-09-18 22:56:36 +06:00
const { app, BrowserWindow, dialog, ipcMain, shell } = require("electron");
2022-07-31 12:45:09 +06:00
const { autoUpdater } = require("electron-updater");
2022-10-27 12:49:15 +06:00
const fs = require("fs");
const path = require("path");
2022-09-02 00:25:25 +06:00
autoUpdater.autoDownload = false;
2022-08-27 12:02:54 +06:00
let win, secondaryWindow;
2022-07-31 12:45:09 +06:00
2022-09-18 22:56:36 +06:00
app.commandLine.appendSwitch("--enable-features", "Metal");
2022-09-10 13:37:48 +06:00
2022-07-31 12:45:09 +06:00
function createWindow() {
2022-08-02 23:27:08 +06:00
let isTransparent = false;
if (process.platform == "linux") {
isTransparent = true;
}
2022-08-05 17:37:17 +06:00
win = new BrowserWindow({
2022-07-31 12:45:09 +06:00
show: false,
icon: __dirname + "/public/icon.png",
2022-08-02 23:27:08 +06:00
spellcheck: false,
transparent: isTransparent,
webPreferences: {
2022-08-05 17:37:17 +06:00
nodeIntegration: true,
2022-09-18 22:56:36 +06:00
contextIsolation: false,
2022-08-05 17:37:17 +06:00
},
2022-07-31 12:45:09 +06:00
});
2022-08-05 17:37:17 +06:00
win.loadFile("html/index.html");
2022-07-31 12:45:09 +06:00
win.maximize();
2022-08-27 21:19:29 +06:00
// win.setMenu(null)
2022-07-31 12:45:09 +06:00
win.show();
2022-08-05 18:16:42 +06:00
// win.webContents.openDevTools();
2022-09-18 22:56:36 +06:00
autoUpdater.checkForUpdates();
2022-07-25 22:15:38 +06:00
}
2022-10-27 12:49:15 +06:00
let loadedLanguage;
2022-07-25 22:15:38 +06:00
app.whenReady().then(() => {
2022-09-02 00:25:25 +06:00
// Logging
console.log("Locale:" + app.getLocale());
console.log("Version: " + app.getVersion());
2022-10-27 12:49:15 +06:00
let locale = app.getLocale();
if (fs.existsSync(path.join(__dirname, "translations", locale + ".json"))) {
loadedLanguage = JSON.parse(
fs.readFileSync(
path.join(__dirname, "translations", locale + ".json"),
"utf8"
)
);
} else {
loadedLanguage = JSON.parse(
fs.readFileSync(
path.join(__dirname, "translations", "en.json"),
"utf8"
)
);
}
2022-07-31 12:45:09 +06:00
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
if (process.platform === "win32") {
app.setAppUserModelId(app.name);
}
});
2022-07-25 22:15:38 +06:00
2022-09-03 23:22:49 +06:00
ipcMain.on("restart", () => {
app.relaunch();
app.exit();
2022-09-18 22:56:36 +06:00
});
2022-09-03 23:22:49 +06:00
2022-09-02 00:25:25 +06:00
ipcMain.on("get-version", () => {
const version = app.getVersion();
secondaryWindow.webContents.send("version", version);
});
2022-09-18 22:56:36 +06:00
ipcMain.on("load-win", (event, file) => {
win.loadFile(file);
});
2022-08-27 12:02:54 +06:00
ipcMain.on("load-page", (event, file) => {
secondaryWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
parent: win,
modal: true,
show: false,
});
secondaryWindow.loadFile(file);
2022-09-05 21:47:07 +06:00
secondaryWindow.setMenu(null);
2022-09-03 23:22:49 +06:00
// secondaryWindow.maximize();
2022-08-27 12:02:54 +06:00
secondaryWindow.show();
});
ipcMain.on("close-secondary", () => {
secondaryWindow.close();
secondaryWindow = null;
2022-08-05 17:37:17 +06:00
});
2022-08-05 17:37:17 +06:00
ipcMain.on("select-location", () => {
2022-08-27 13:07:12 +06:00
const location = dialog.showOpenDialogSync(secondaryWindow, {
2022-08-05 17:37:17 +06:00
properties: ["openFile", "openDirectory"],
});
2022-08-05 17:37:17 +06:00
if (location) {
2022-08-27 13:07:12 +06:00
secondaryWindow.webContents.send("downloadPath", location);
}
2022-08-05 17:37:17 +06:00
});
2022-07-31 12:45:09 +06:00
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
// Auto updater events
autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
2022-09-18 22:56:36 +06:00
// For macOS
if (process.platform === "darwin") {
const dialogOpts = {
type: "info",
2022-10-27 12:49:15 +06:00
buttons: [i18n("Download"), i18n("No")],
2022-09-18 22:56:36 +06:00
title: "Update Available",
detail: releaseName,
2022-10-27 12:49:15 +06:00
message: i18n(
"A new version is available, do you want to download it?"
),
2022-09-18 22:56:36 +06:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
shell.openExternal(
"https://github.com/aandrew-me/ytDownloader/releases/latest/download/YTDownloader_Mac.zip"
);
}
});
}
// For Windows and Linux
else {
const dialogOpts = {
type: "info",
2022-10-27 12:49:15 +06:00
buttons: [i18n("Update"), i18n("No")],
2022-09-18 22:56:36 +06:00
title: "Update Available",
detail: process.platform === "win32" ? releaseNotes : releaseName,
2022-10-27 12:49:15 +06:00
message: i18n("A new version is available, do you want to update?"),
2022-09-18 22:56:36 +06:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
autoUpdater.downloadUpdate();
}
});
}
2022-08-02 23:27:08 +06:00
});
2022-07-31 12:45:09 +06:00
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
2022-08-02 23:27:08 +06:00
type: "info",
2022-10-27 12:49:15 +06:00
buttons: [i18n("Restart"), i18n("Later")],
2022-09-02 00:25:25 +06:00
title: "Update Ready",
2022-10-27 12:49:15 +06:00
message: i18n("Install and restart now?"),
2022-07-31 12:45:09 +06:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
2022-09-02 00:25:25 +06:00
if (returnValue.response === 0) {
autoUpdater.quitAndInstall();
} else {
autoUpdater.autoInstallOnAppQuit();
}
2022-08-02 23:27:08 +06:00
});
2022-07-31 19:14:15 +06:00
});
2022-10-27 12:49:15 +06:00
// Translation
function i18n(phrase) {
let translation = loadedLanguage[phrase];
if (translation === undefined) {
translation = phrase;
}
return translation;
}