From 847757ef7049a5235731b50cae443c23a56b4d5e Mon Sep 17 00:00:00 2001 From: Nayam Amarshe <25067102+NayamAmarshe@users.noreply.github.com> Date: Sun, 10 Sep 2023 23:24:08 +0530 Subject: [PATCH] Try fixing overwrite --- electron/commands/custom-models-select.ts | 4 +- electron/commands/get-models-list.ts | 4 +- electron/commands/image-upscayl.ts | 5 +- electron/commands/select-file.ts | 4 +- electron/commands/stop.ts | 4 +- electron/index.ts | 95 +++++++++++++++++++++++ electron/main-window.ts | 83 -------------------- electron/utils/config-variables.ts | 12 +++ 8 files changed, 117 insertions(+), 94 deletions(-) diff --git a/electron/commands/custom-models-select.ts b/electron/commands/custom-models-select.ts index 867edb5..56e05cb 100644 --- a/electron/commands/custom-models-select.ts +++ b/electron/commands/custom-models-select.ts @@ -9,9 +9,9 @@ import COMMAND from "../constants/commands"; import getModels from "../utils/get-models"; import { getMainWindow } from "../main-window"; -const mainWindow = getMainWindow(); - const customModelsSelect = async (event, message) => { + const mainWindow = getMainWindow(); + if (!mainWindow) return; const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({ properties: ["openDirectory"], diff --git a/electron/commands/get-models-list.ts b/electron/commands/get-models-list.ts index e754ea9..420e855 100644 --- a/electron/commands/get-models-list.ts +++ b/electron/commands/get-models-list.ts @@ -7,9 +7,9 @@ import { import getModels from "../utils/get-models"; import logit from "../utils/logit"; -const mainWindow = getMainWindow(); - const getModelsList = async (event, payload) => { + const mainWindow = getMainWindow(); + if (!mainWindow) return; if (payload) { setCustomModelsFolderPath(payload); diff --git a/electron/commands/image-upscayl.ts b/electron/commands/image-upscayl.ts index 1b8b85d..763384e 100644 --- a/electron/commands/image-upscayl.ts +++ b/electron/commands/image-upscayl.ts @@ -20,7 +20,6 @@ import { spawnUpscayl } from "../utils/spawn-upscayl"; import { parse } from "path"; import DEFAULT_MODELS from "../constants/models"; import { getMainWindow } from "../main-window"; -import stop from "./stop"; const imageUpscayl = async (event, payload) => { const mainWindow = getMainWindow(); @@ -73,18 +72,18 @@ const imageUpscayl = async (event, payload) => { saveImageAs; // GET OVERWRITE SETTINGS FROM LOCAL STORAGE - mainWindow.webContents .executeJavaScript('localStorage.getItem("overwrite");', true) .then((lastSavedOverwrite: boolean | null) => { if (lastSavedOverwrite !== null) { console.log("Overwrite: ", lastSavedOverwrite); setOverwrite(lastSavedOverwrite); + console.log("NEW OVERWRITE: ", overwrite); } }); // UPSCALE - if (fs.existsSync(outFile) && overwrite === false) { + if (fs.existsSync(outFile) && !overwrite) { // If already upscayled, just output that file logit("✅ Already upscayled at: ", outFile); mainWindow.webContents.send( diff --git a/electron/commands/select-file.ts b/electron/commands/select-file.ts index 7ee9679..fbcdf21 100644 --- a/electron/commands/select-file.ts +++ b/electron/commands/select-file.ts @@ -3,9 +3,9 @@ import { getMainWindow } from "../main-window"; import { imagePath, setImagePath } from "../utils/config-variables"; import logit from "../utils/logit"; -const mainWindow = getMainWindow(); - const selectFile = async () => { + const mainWindow = getMainWindow(); + const { canceled, filePaths } = await dialog.showOpenDialog({ properties: ["openFile", "multiSelections"], title: "Select Image", diff --git a/electron/commands/stop.ts b/electron/commands/stop.ts index 164fd6f..048c258 100644 --- a/electron/commands/stop.ts +++ b/electron/commands/stop.ts @@ -2,9 +2,9 @@ import { getMainWindow } from "../main-window"; import { childProcesses, setStopped } from "../utils/config-variables"; import logit from "../utils/logit"; -const mainWindow = getMainWindow(); - const stop = async (event, payload) => { + const mainWindow = getMainWindow(); + setStopped(true); mainWindow && mainWindow.setProgressBar(-1); childProcesses.forEach((child) => { diff --git a/electron/index.ts b/electron/index.ts index 7b45a4f..7574fcb 100644 --- a/electron/index.ts +++ b/electron/index.ts @@ -20,10 +20,12 @@ import getModelsList from "./commands/get-models-list"; import customModelsSelect from "./commands/custom-models-select"; import imageUpscayl from "./commands/image-upscayl"; import { + overwrite, setCustomModelsFolderPath, setFolderPath, setImagePath, setOutputFolderPath, + setOverwrite, setQuality, setSaveOutputFolder, setStopped, @@ -50,6 +52,99 @@ app.whenReady().then(async () => { if (!electronIsDev) { autoUpdater.checkForUpdates(); } + + const mainWindow = getMainWindow(); + + if (!mainWindow) { + console.log("No main window"); + return; + } + + const url = electronIsDev + ? "http://localhost:8000" + : (new URL("file:///").pathname = join( + __dirname, + "../renderer/out/index.html" + )).toString(); + mainWindow.loadURL(url); + + mainWindow.webContents.setWindowOpenHandler(({ url }) => { + shell.openExternal(url); + return { action: "deny" }; + }); + + mainWindow.once("ready-to-show", () => { + if (!mainWindow) return; + mainWindow.show(); + }); + + // GET LAST IMAGE PATH TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript('localStorage.getItem("lastImagePath");', true) + .then((lastImagePath: string | null) => { + if (lastImagePath && lastImagePath.length > 0) { + setImagePath(lastImagePath); + } + }); + // GET LAST FOLDER PATH TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript('localStorage.getItem("lastFolderPath");', true) + .then((lastFolderPath: string | null) => { + if (lastFolderPath && lastFolderPath.length > 0) { + setFolderPath(lastFolderPath); + } + }); + // GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript( + 'localStorage.getItem("lastCustomModelsFolderPath");', + true + ) + .then((lastCustomModelsFolderPath: string | null) => { + if (lastCustomModelsFolderPath && lastCustomModelsFolderPath.length > 0) { + setCustomModelsFolderPath(lastCustomModelsFolderPath); + } + }); + // GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript('localStorage.getItem("lastOutputFolderPath");', true) + .then((lastOutputFolderPath: string | null) => { + if (lastOutputFolderPath && lastOutputFolderPath.length > 0) { + setOutputFolderPath(lastOutputFolderPath); + } + }); + // GET LAST SAVE OUTPUT FOLDER (BOOLEAN) TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript('localStorage.getItem("rememberOutputFolder");', true) + .then((lastSaveOutputFolder: boolean | null) => { + if (lastSaveOutputFolder !== null) { + setSaveOutputFolder(lastSaveOutputFolder); + } + }); + // GET IMAGE QUALITY (NUMBER) TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript('localStorage.getItem("quality");', true) + .then((lastSavedQuality: string | null) => { + if (lastSavedQuality !== null) { + if (parseInt(lastSavedQuality) === 100) { + setQuality(99); + } else { + setQuality(parseInt(lastSavedQuality)); + } + } + }); + // GET IMAGE QUALITY (NUMBER) TO LOCAL STORAGE + mainWindow.webContents + .executeJavaScript('localStorage.getItem("overwrite");', true) + .then((lastSavedOverwrite: string | null) => { + if (lastSavedOverwrite !== null) { + console.log("Setting Overwrite: ", lastSavedOverwrite); + setOverwrite(lastSavedOverwrite === "true"); + console.log("Set Overwrite: ", overwrite); + } + }); + + mainWindow.webContents.send(COMMAND.OS, getPlatform()); }); // Quit the app once all windows are closed diff --git a/electron/main-window.ts b/electron/main-window.ts index ca7d9f8..ed3d1b6 100644 --- a/electron/main-window.ts +++ b/electron/main-window.ts @@ -34,89 +34,6 @@ const createMainWindow = () => { }); mainWindow.setMenuBarVisibility(false); - - const url = electronIsDev - ? "http://localhost:8000" - : (new URL("file:///").pathname = join( - __dirname, - "../renderer/out/index.html" - )).toString(); - mainWindow.loadURL(url); - - mainWindow.webContents.setWindowOpenHandler(({ url }) => { - shell.openExternal(url); - return { action: "deny" }; - }); - - mainWindow.once("ready-to-show", () => { - if (!mainWindow) return; - mainWindow.show(); - }); - - // GET LAST IMAGE PATH TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript('localStorage.getItem("lastImagePath");', true) - .then((lastImagePath: string | null) => { - if (lastImagePath && lastImagePath.length > 0) { - setImagePath(lastImagePath); - } - }); - // GET LAST FOLDER PATH TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript('localStorage.getItem("lastFolderPath");', true) - .then((lastFolderPath: string | null) => { - if (lastFolderPath && lastFolderPath.length > 0) { - setFolderPath(lastFolderPath); - } - }); - // GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript( - 'localStorage.getItem("lastCustomModelsFolderPath");', - true - ) - .then((lastCustomModelsFolderPath: string | null) => { - if (lastCustomModelsFolderPath && lastCustomModelsFolderPath.length > 0) { - setCustomModelsFolderPath(lastCustomModelsFolderPath); - } - }); - // GET LAST CUSTOM MODELS FOLDER PATH TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript('localStorage.getItem("lastOutputFolderPath");', true) - .then((lastOutputFolderPath: string | null) => { - if (lastOutputFolderPath && lastOutputFolderPath.length > 0) { - setOutputFolderPath(lastOutputFolderPath); - } - }); - // GET LAST SAVE OUTPUT FOLDER (BOOLEAN) TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript('localStorage.getItem("rememberOutputFolder");', true) - .then((lastSaveOutputFolder: boolean | null) => { - if (lastSaveOutputFolder !== null) { - setSaveOutputFolder(lastSaveOutputFolder); - } - }); - // GET IMAGE QUALITY (NUMBER) TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript('localStorage.getItem("quality");', true) - .then((lastSavedQuality: string | null) => { - if (lastSavedQuality !== null) { - if (parseInt(lastSavedQuality) === 100) { - setQuality(99); - } else { - setQuality(parseInt(lastSavedQuality)); - } - } - }); - // GET IMAGE QUALITY (NUMBER) TO LOCAL STORAGE - mainWindow.webContents - .executeJavaScript('localStorage.getItem("overwrite");', true) - .then((lastSavedOverwrite: string | null) => { - if (lastSavedOverwrite !== null) { - setOverwrite(lastSavedOverwrite === "true"); - } - }); - mainWindow.webContents.send(COMMAND.OS, getPlatform()); }; const getMainWindow = () => { diff --git a/electron/utils/config-variables.ts b/electron/utils/config-variables.ts index 84e4722..f57bc61 100644 --- a/electron/utils/config-variables.ts +++ b/electron/utils/config-variables.ts @@ -13,6 +13,18 @@ export let childProcesses: { kill: () => boolean; }[] = []; +console.log({ + imagePath, + folderPath, + customModelsFolderPath, + outputFolderPath, + saveOutputFolder, + quality, + overwrite, + stopped, + childProcesses, +}); + export function setImagePath(value: string | undefined): void { imagePath = value; }