Skip to content

Commit

Permalink
feat: refactor upscayl script
Browse files Browse the repository at this point in the history
  • Loading branch information
UrijHoruzij committed Feb 22, 2023
1 parent c0e484c commit 7619702
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 172 deletions.
28 changes: 27 additions & 1 deletion electron-builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,33 @@
"filter": ["**/*"]
}
],

"mac": {
"type": "distribution",
"hardenedRuntime": true,
"entitlements": "main/resources/entitlements.mac.plist",
"entitlementsInherit": "main/resources/entitlements.mac.plist",
"gatekeeperAssess": false,
"target": [
{
"target": "dmg",
"arch": ["x64"]
}
]
},
"dmg": {
"contents": [
{
"x": 130,
"y": 220
},
{
"x": 410,
"y": 220,
"type": "link",
"path": "/Applications"
}
]
},
"linux": {
"target": ["deb", "rpm"],
"maintainer": "Urij Horuzij <[email protected]>",
Expand Down
130 changes: 20 additions & 110 deletions main/background.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { app, ipcMain, dialog, remote, Menu, MenuItem } = require('electron');
const { app, ipcMain, dialog, remote, Menu, MenuItem, BrowserWindow } = require('electron');
const serve = require('electron-serve');
const path = require('path');
const isDev = require('electron-is-dev');
const { upscayl, autoUpdate, createWindow, store, utils } = require('./helpers/');
const { autoUpdate, allWindows, store, utils, ipc } = require('./helpers/');

if (isDev) {
require('electron-reload')(__dirname, {
Expand All @@ -14,11 +14,7 @@ if (!isDev) {
}

let mainWindow;
let splashWindow;
let file = [];
let filePathPosix = [];
let temp = [];
let flag = false;
let temp = null;
app.on('ready', async () => {
const userDataPath = (app || remote.app).getPath('userData');
const settings = store({
Expand Down Expand Up @@ -46,56 +42,7 @@ app.on('ready', async () => {
const oldPreviews = await previews.get('paths');
return oldPreviews;
});
splashWindow = createWindow(
'splash',
{
icon: path.join(__dirname, '../resources/icon.png'),
width: 750,
height: 500,
show: false,
center: true,
frame: false,
resizable: false,
backgroundColor: '#171918',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
},
true,
);
splashWindow.once('ready-to-show', () => {
splashWindow.show();
});
if (isDev) {
await splashWindow.loadURL(`http:https://localhost:3000/splash`);
} else {
await splashWindow.loadURL('app:https://./splash.html');
}
mainWindow = createWindow('main', {
icon: path.join(__dirname, '../resources/icon.png'),
width: 1000,
height: 600,
show: false,
frame: false,
backgroundColor: '#171918',
webPreferences: {
nodeIntegration: true,
webSecurity: false,
preload: path.join(__dirname, 'preload.js'),
},
});
if (isDev) {
mainWindow.webContents.openDevTools();
}
mainWindow.once('ready-to-show', () => {
splashWindow.close();
mainWindow.show();
});
if (isDev) {
await mainWindow.loadURL(`http:https://localhost:3000/`);
} else {
await mainWindow.loadURL('app:https://./index.html');
}
mainWindow = await allWindows();
const menu = new Menu();
menu.append(
new MenuItem({
Expand All @@ -118,77 +65,40 @@ app.on('ready', async () => {
role: 'Save',
accelerator: process.platform === 'darwin' ? 'Cmd+S' : 'Ctrl+S',
click: async () => {
if (temp[0]) {
if (temp) {
const { filePath } = await dialog.showSaveDialog({
filters: [
{ name: 'JPEG', extensions: ['jpg', 'jpeg'] },
{ name: 'PNG', extensions: ['png'] },
{ name: 'JPEG', extensions: ['jpg', 'jpeg'] },
{ name: 'GIF', extensions: ['gif'] },
],
});
await utils.previews(previews, userDataPath, temp[0]);
await utils.copy(temp[0], filePath);
temp[0] = null;
await utils.previews(previews, userDataPath, temp);
await utils.copy(temp, filePath);
temp = null;
}
},
},
{
role: 'Exit',
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
click: async () => {
mainWindow.close();
},
},
],
}),
);
Menu.setApplicationMenu(menu);
autoUpdate(mainWindow);

ipcMain.on('upscayl', async () => {
if (!flag && file.length > 0) {
flag = true;
await upscayl(file[0], userDataPath, mainWindow, temp);
flag = false;
}
});
ipcMain.on('drag-file', async (e, filePaths) => {
file = [...filePaths];
filePathPosix = [filePaths[0].split(path.sep).join(path.posix.sep)];
e.reply('open-file', true);
});
ipcMain.on('open-file', async (e) => {
const { filePaths } = await dialog.showOpenDialog({
filters: [{ name: 'Изображения', extensions: ['jpg', 'png', 'gif'] }],
properties: ['openFile'],
});
file = [...filePaths];
filePathPosix = [filePaths[0].split(path.sep).join(path.posix.sep)];
e.reply('open-file', true);
});
ipcMain.on('opened-file', async (e) => {
e.reply('opened-file', filePathPosix[0]);
});
ipcMain.on('save-file', async () => {
const { filePath } = await dialog.showSaveDialog({
filters: [
{ name: 'JPEG', extensions: ['jpg', 'jpeg'] },
{ name: 'PNG', extensions: ['png'] },
{ name: 'GIF', extensions: ['gif'] },
],
});
await utils.previews(previews, userDataPath, temp[0]);
await utils.copy(temp[0], filePath);
temp[0] = null;
});
// ipcMain.on('open-directory', async () => {
// const { filePaths } = await dialog.showOpenDialog({
// properties: ['openDirectory'],
// });
// saveDirectory = filePaths;
// });
ipc(mainWindow, temp);
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// app.on('activate', function () {
// // В macOS обычно заново создают окно в приложении, когда
// // нажата иконка док-станции, и другие окна не открыты.
// if (BrowserWindow.getAllWindows().length === 0) createWindow();
// });
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) mainWindow = allWindows();
});
59 changes: 59 additions & 0 deletions main/helpers/allWindows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require('path');
const isDev = require('electron-is-dev');
const createWindow = require('./createWindow');

const allWindows = async () => {
let splashWindow = createWindow(
'splash',
{
icon: path.join(__dirname, '../resources/icon.png'),
width: 750,
height: 500,
show: false,
center: true,
frame: false,
resizable: false,
backgroundColor: '#171918',
webPreferences: {
preload: path.join(__dirname, '../preload.js'),
},
},
true,
);
splashWindow.once('ready-to-show', () => {
splashWindow.show();
});
if (isDev) {
await splashWindow.loadURL(`http:https://localhost:3000/splash`);
} else {
await splashWindow.loadURL('app:https://./splash.html');
}
let mainWindow = createWindow('main', {
icon: path.join(__dirname, '../resources/icon.png'),
width: 1000,
height: 600,
show: false,
frame: false,
backgroundColor: '#171918',
webPreferences: {
nodeIntegration: true,
webSecurity: false,
preload: path.join(__dirname, '../preload.js'),
},
});
if (isDev) {
mainWindow.webContents.openDevTools();
}
mainWindow.once('ready-to-show', () => {
splashWindow.close();
mainWindow.show();
});
if (isDev) {
await mainWindow.loadURL(`http:https://localhost:3000/`);
} else {
await mainWindow.loadURL('app:https://./index.html');
}
return mainWindow;
};

module.exports = allWindows;
4 changes: 3 additions & 1 deletion main/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ const createWindow = require('./createWindow');
const upscayl = require('./upscayl');
const store = require('./store');
const utils = require('./utils');
const allWindows = require('./allWindows');
const ipc = require('./ipc');

module.exports = { autoUpdate, binaries, createWindow, upscayl, store, utils };
module.exports = { autoUpdate, binaries, createWindow, upscayl, store, utils, allWindows, ipc };
71 changes: 71 additions & 0 deletions main/helpers/ipc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { app, ipcMain, dialog, remote } = require('electron');
const path = require('path');
const upscayl = require('./upscayl');
const utils = require('./utils');

const ipc = (mainWindow, temp) => {
const userDataPath = (app || remote.app).getPath('userData');
let file = [];
let filePathPosix = [];
let flag = false;
ipcMain.on('upscayl', async (e, scale) => {
if (!flag && file.length > 0) {
flag = true;
const percentFunc = (percent) => {
mainWindow.setProgressBar(percent / 100);
mainWindow.webContents.send('upscayl-progress', percent);
};
try {
mainWindow.setProgressBar(0.01);
mainWindow.webContents.send('upscayl-progress', 1);
const upscale = await upscayl(file[0], userDataPath, percentFunc);
temp = upscale;
if (scale === 8) {
await upscayl(temp, userDataPath, percentFunc);
}
mainWindow.webContents.send('upscayl-done', temp);
} catch (err) {
mainWindow.webContents.send('upscayl-error');
} finally {
mainWindow.setProgressBar(-1);
}
flag = false;
}
});
ipcMain.on('drag-file', async (e, filePaths) => {
file = [...filePaths];
filePathPosix = [filePaths[0].split(path.sep).join(path.posix.sep)];
e.reply('open-file', true);
});
ipcMain.on('open-file', async (e) => {
const { filePaths } = await dialog.showOpenDialog({
filters: [{ name: 'Изображения', extensions: ['jpg', 'png', 'gif'] }],
properties: ['openFile'],
});
file = [...filePaths];
filePathPosix = [filePaths[0].split(path.sep).join(path.posix.sep)];
e.reply('open-file', true);
});
ipcMain.on('opened-file', async (e) => {
e.reply('opened-file', filePathPosix[0]);
});
ipcMain.on('save-file', async () => {
const { filePath } = await dialog.showSaveDialog({
filters: [
{ name: 'JPEG', extensions: ['jpg', 'jpeg'] },
{ name: 'PNG', extensions: ['png'] },
{ name: 'GIF', extensions: ['gif'] },
],
});
await utils.previews(previews, userDataPath, temp);
await utils.copy(temp, filePath);
temp = null;
});
// ipcMain.on('open-directory', async () => {
// const { filePaths } = await dialog.showOpenDialog({
// properties: ['openDirectory'],
// });
// saveDirectory = filePaths;
// });
};
module.exports = ipc;
2 changes: 2 additions & 0 deletions main/helpers/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const menu = () => {};
module.exports = menu;
Loading

0 comments on commit 7619702

Please sign in to comment.