Skip to content

Commit

Permalink
feat: optimization menu, add store temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
UrijHoruzij committed Feb 27, 2023
1 parent 4fcc337 commit 6b64095
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 92 deletions.
Binary file modified build/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 13 additions & 53 deletions main/background.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { app, ipcMain, dialog, remote, Menu, MenuItem, BrowserWindow } = require('electron');
const { app, ipcMain, BrowserWindow } = require('electron');
const serve = require('electron-serve');
const path = require('path');
const isDev = require('electron-is-dev');
const { autoUpdate, allWindows, store, utils, ipc } = require('./helpers/');
const { autoUpdate, allWindows, store, ipc, addMenu } = require('./helpers/');

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

let mainWindow;
let temp = null;
app.on('ready', async () => {
const userDataPath = (app || remote.app).getPath('userData');
const settings = store({
configName: 'settings',
defaults: {
Expand All @@ -29,6 +26,15 @@ app.on('ready', async () => {
paths: [],
},
});
const temporary = store({
configName: 'temporary',
defaults: {
temp: null,
file: [],
filePathPosix: [],
},
});

ipcMain.handle('get-lang', async (e) => {
const lang = await settings.get('locale');
return lang;
Expand All @@ -43,55 +49,9 @@ app.on('ready', async () => {
return oldPreviews;
});
mainWindow = await allWindows();
const menu = new Menu();
menu.append(
new MenuItem({
label: 'Super-resolution',
submenu: [
{
role: 'Open',
accelerator: process.platform === 'darwin' ? 'Cmd+N' : 'Ctrl+N',
click: async () => {
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)];
mainWindow.webContents.send('open-file', true);
},
},
{
role: 'Save',
accelerator: process.platform === 'darwin' ? 'Cmd+S' : 'Ctrl+S',
click: async () => {
if (temp) {
const { filePath } = await dialog.showSaveDialog({
filters: [
{ name: 'PNG', extensions: ['png'] },
{ name: 'JPEG', extensions: ['jpg', 'jpeg'] },
{ name: 'GIF', extensions: ['gif'] },
],
});
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);
addMenu(mainWindow, temporary, previews);
autoUpdate(mainWindow);
ipc(mainWindow, temp);
ipc(mainWindow, temporary, previews);
});

app.on('window-all-closed', () => {
Expand Down
3 changes: 2 additions & 1 deletion main/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ const store = require('./store');
const utils = require('./utils');
const allWindows = require('./allWindows');
const ipc = require('./ipc');
const addMenu = require('./menu');

module.exports = { autoUpdate, binaries, createWindow, upscayl, store, utils, allWindows, ipc };
module.exports = { autoUpdate, binaries, createWindow, upscayl, store, utils, allWindows, ipc, addMenu };
34 changes: 12 additions & 22 deletions main/helpers/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ const path = require('path');
const upscayl = require('./upscayl');
const utils = require('./utils');

const ipc = (mainWindow, temp) => {
const ipc = (mainWindow, temporary, previews) => {
const userDataPath = (app || remote.app).getPath('userData');
let file = [];
let filePathPosix = [];
let flag = false;
ipcMain.on('upscayl', async (e, scale) => {
const file = await temporary.get('file');
if (!flag && file.length > 0) {
flag = true;
const percentFunc = (percent) => {
Expand All @@ -19,7 +18,8 @@ const ipc = (mainWindow, temp) => {
mainWindow.setProgressBar(0.01);
mainWindow.webContents.send('upscayl-progress', 1);
const upscale = await upscayl(file[0], userDataPath, percentFunc);
temp = upscale;
await temporary.set('temp', upscale);
const temp = await temporary.get('temp');
if (scale === 8) {
await upscayl(temp, userDataPath, percentFunc);
}
Expand All @@ -33,33 +33,23 @@ const ipc = (mainWindow, temp) => {
}
});
ipcMain.on('drag-file', async (e, filePaths) => {
file = [...filePaths];
filePathPosix = [filePaths[0].split(path.sep).join(path.posix.sep)];
await temporary.set('file', [...filePaths]);
await temporary.set('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)];
await utils.openFile(temporary);
e.reply('open-file', true);
});
ipcMain.on('opened-file', async (e) => {
const filePathPosix = await temporary.get('filePathPosix');
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;
utils.saveFile(temporary, previews);
});
ipcMain.on('close-file', async () => {
utils.closeFile(temporary);
});
// ipcMain.on('open-directory', async () => {
// const { filePaths } = await dialog.showOpenDialog({
Expand Down
39 changes: 37 additions & 2 deletions main/helpers/menu.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
const menu = () => {};
module.exports = menu;
const { Menu, MenuItem } = require('electron');
const { utils } = require('.');

const addMenu = (mainWindow, temporary, previews) => {
const menu = new Menu();
menu.append(
new MenuItem({
label: 'Super-resolution',
submenu: [
{
role: 'Open',
accelerator: process.platform === 'darwin' ? 'Cmd+N' : 'Ctrl+N',
click: async () => {
await utils.openFile(temporary);
mainWindow.webContents.send('open-file', true);
},
},
{
role: 'Save',
accelerator: process.platform === 'darwin' ? 'Cmd+S' : 'Ctrl+S',
click: async () => {
utils.saveFile(temporary, previews);
},
},
{
role: 'Exit',
accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
click: async () => {
mainWindow.close();
},
},
],
}),
);
Menu.setApplicationMenu(menu);
};
module.exports = addMenu;
38 changes: 29 additions & 9 deletions main/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const { dialog } = require('electron');
const { app, remote, dialog } = require('electron');
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const store = require('./store');

const userDataPath = (app || remote.app).getPath('userData');

const copy = (oldPath, newPath) => {
let readStream = fs.createReadStream(oldPath);
let writeStream = fs.createWriteStream(newPath);
readStream.on('error', () => {});
writeStream.on('error', () => {});
readStream.on('close', () => {
// fs.unlink(oldPath, () => {});
fs.unlink(oldPath, () => {});
});
readStream.pipe(writeStream);
};
const previews = async (previews, userDataPath, filePath) => {
const previews = async (previews, filePath) => {
if (!fs.existsSync(path.join(userDataPath, 'previews'))) {
fs.mkdirSync(path.join(userDataPath, 'previews'));
}
Expand All @@ -35,14 +36,33 @@ const previews = async (previews, userDataPath, filePath) => {
await previews.set('paths', [...oldPreviews, `${dest.split(path.sep).join(path.posix.sep)}`]);
};

const openFile = async () => {
const openFile = async (temporary) => {
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)];
return { file, filePathPosix };
await temporary.set('file', [...filePaths]);
await temporary.set('filePathPosix', [filePaths[0].split(path.sep).join(path.posix.sep)]);
};
const saveFile = async (temporary, previewsStore) => {
const temp = await temporary.get('temp');
if (temp) {
const { filePath } = await dialog.showSaveDialog({
filters: [
{ name: 'JPEG', extensions: ['jpg', 'jpeg'] },
{ name: 'PNG', extensions: ['png'] },
{ name: 'GIF', extensions: ['gif'] },
],
});
await previews(previewsStore, temp);
await copy(temp, filePath);
await temporary.set('temp', null);
}
};
const closeFile = async (temporary) => {
await temporary.set('temp', null);
await temporary.set('file', []);
await temporary.set('filePathPosix', []);
};

module.exports = { copy, previews, openFile };
module.exports = { copy, previews, openFile, saveFile, closeFile };
25 changes: 21 additions & 4 deletions src/pages/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const editorPage = () => {
useEffect(() => {
window.electron.send('opened-file');
window.electron.on('opened-file', (event, file) => {
console.log(file);
setImage({
url: file,
});
Expand Down Expand Up @@ -62,7 +63,10 @@ const editorPage = () => {
id: 'file-item-2',
name: 'Главная',
shortcut: 'CTRL+M',
onClick: () => router.push('/'),
onClick: () => {
window.electron.send('close-file');
router.push('/');
},
// subMenu: [
// { id: 'file-item-4-sub-1', name: 'Сохранить', shortcut: 'CTRL+S' },
// { id: 'file-item-4-sub-2', name: 'Сохранить', shortcut: 'CTRL+S' },
Expand All @@ -74,17 +78,30 @@ const editorPage = () => {
id: 'file-item-5',
name: 'Выход',
shortcut: 'CTRL+Q',
onClick: () => window.electron.send('window-main-close'),
onClick: () => {
window.electron.send('close-file');
window.electron.send('window-main-close');
},
},
],
],
},
{
id: 'settings',
name: messages.settings.title,
onClick: () => window.electron.send('settings'),
onClick: () => {
window.electron.send('close-file');
router.push('/settings');
},
},
{
id: 'help',
name: 'Справка',
onClick: () => {
window.electron.send('close-file');
router.push('/help');
},
},
{ id: 'help', name: 'Справка', onClick: () => router.push('/help') },
];

return (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Help = () => {
{
id: 'file-item-1',
name: 'Главная',
shortcut: 'CTRL+S',
shortcut: 'CTRL+M',
onClick: () => router.push('/'),
},
],
Expand Down

0 comments on commit 6b64095

Please sign in to comment.