Skip to content

Commit 106962d

Browse files
LevevTibixDev
andauthored
feat: Remember window properties upon closing WinBoat, feat: Migrate electron files to ESM (#196)
* feat: Remember window properties upon closing WinBoat, feat: Migrate electron files to ESM This issue fixes #128 Co-authored-by: Tibix <fuloptibi03@gmail.com> * chore: Migrate build script to ESM Co-authored-by: Tibix <fuloptibi03@gmail.com> --------- Co-authored-by: Tibix <fuloptibi03@gmail.com>
1 parent 62c10fa commit 106962d

10 files changed

Lines changed: 371 additions & 60 deletions

File tree

package-lock.json

Lines changed: 267 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "winboat",
33
"version": "0.8.5",
44
"description": "Windows for Penguins",
5+
"type": "module",
56
"main": "main/main.js",
67
"scripts": {
78
"dev": "node scripts/dev-server.js",
@@ -38,6 +39,7 @@
3839
"@vueuse/motion": "^2.2.6",
3940
"apexcharts": "^4.5.0",
4041
"consola": "^3.4.2",
42+
"electron-store": "^11.0.0",
4143
"form-data": "^4.0.4",
4244
"jimp": "^1.6.0",
4345
"json-to-pretty-yaml": "^1.2.2",

postcss.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
plugins: {
33
tailwindcss: {},
44
autoprefixer: {},

scripts/build.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
const Path = require('path');
2-
const Chalk = require('chalk');
3-
const FileSystem = require('fs');
4-
const Vite = require('vite');
5-
const compileTs = require('./private/tsc');
1+
import Path from 'path';
2+
import Chalk from 'chalk';
3+
import FileSystem from 'fs';
4+
import * as Vite from 'vite';
5+
import compileTs from './private/tsc.js';
6+
import { EOL } from 'os';
7+
import { fileURLToPath } from 'url';
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = Path.dirname(__filename);
611

712
function buildRenderer() {
813
return Vite.build({

scripts/dev-server.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
process.env.NODE_ENV = 'development';
22

3-
const Vite = require('vite');
4-
const ChildProcess = require('child_process');
5-
const Path = require('path');
6-
const Chalk = require('chalk');
7-
const Chokidar = require('chokidar');
8-
const Electron = require('electron');
9-
const compileTs = require('./private/tsc');
10-
const FileSystem = require('fs');
11-
const { EOL } = require('os');
3+
import * as Vite from 'vite';
4+
import ChildProcess from 'child_process';
5+
import Path from 'path';
6+
import Chalk from 'chalk';
7+
import Chokidar from 'chokidar';
8+
import Electron from 'electron';
9+
import compileTs from './private/tsc.js';
10+
import FileSystem from 'fs';
11+
import { EOL } from 'os';
12+
import { fileURLToPath } from 'url';
13+
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = Path.dirname(__filename);
1216

1317
let viteServer = null;
1418
let electronProcess = null;
1519
let electronProcessLocker = false;
1620
let rendererPort = 0;
1721

22+
1823
async function startRenderer() {
1924
viteServer = await Vite.createServer({
2025
configFile: Path.join(__dirname, '..', 'vite.config.ts'),

scripts/private/tsc.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const ChildProcess = require('child_process');
2-
const Chalk = require('chalk');
1+
import ChildProcess from 'child_process';
2+
import Chalk from 'chalk';
33

4-
function compile(directory) {
4+
export default function compile(directory) {
55
return new Promise((resolve, reject) => {
66
const tscProcess = ChildProcess.exec('tsc', {
77
cwd: directory,
@@ -21,4 +21,3 @@ function compile(directory) {
2121
});
2222
}
2323

24-
module.exports = compile;

src/main/main.ts

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
11
import { app, BrowserWindow, ipcMain, session, dialog } from 'electron';
22
import { join } from 'path';
3-
import { initialize, enable } from '@electron/remote/main';
3+
import { initialize, enable } from '@electron/remote/main/index.js';
4+
import Store from 'electron-store';
45

56
initialize();
67

8+
// Window Constants
9+
const WINDOW_MIN_WIDTH = 1280;
10+
const WINDOW_MIN_HEIGHT = 800;
11+
12+
// For electron-store Type-Safety
13+
type SchemaType = {
14+
dimensions: {
15+
width: number,
16+
height: number
17+
},
18+
position: {
19+
x: number,
20+
y: number
21+
}
22+
};
23+
24+
const windowStore = new Store<SchemaType>({ schema: {
25+
dimensions: {
26+
type: 'object',
27+
properties: {
28+
width: {
29+
type: 'number',
30+
minimum: WINDOW_MIN_WIDTH,
31+
default: WINDOW_MIN_WIDTH
32+
},
33+
height: {
34+
type: 'number',
35+
minimum: WINDOW_MIN_HEIGHT,
36+
default: WINDOW_MIN_HEIGHT
37+
},
38+
},
39+
required: ['width', 'height']
40+
},
41+
position: {
42+
type: 'object',
43+
properties: {
44+
x: {
45+
type: 'number'
46+
},
47+
y: {
48+
type: 'number'
49+
}
50+
},
51+
required: ['x', 'y']
52+
}
53+
}});
54+
755
let mainWindow: BrowserWindow | null = null;
856

957
function createWindow() {
@@ -19,12 +67,12 @@ function createWindow() {
1967
}
2068

2169
mainWindow = new BrowserWindow({
22-
minWidth: 1280,
23-
minHeight: 800,
24-
width: 1280,
25-
height: 800,
26-
y: 0,
27-
x: 500,
70+
minWidth: WINDOW_MIN_WIDTH,
71+
minHeight: WINDOW_MIN_HEIGHT,
72+
width: windowStore.get('dimensions.width'),
73+
height: windowStore.get('dimensions.height'),
74+
x: windowStore.get('position.x'),
75+
y: windowStore.get('position.y'),
2876
transparent: false,
2977
frame: false,
3078
webPreferences: {
@@ -34,6 +82,20 @@ function createWindow() {
3482
}
3583
});
3684

85+
mainWindow.on('close', () => {
86+
const bounds = mainWindow?.getBounds();
87+
88+
windowStore.set('dimensions', {
89+
width: bounds?.width,
90+
height: bounds?.height
91+
});
92+
93+
windowStore.set('position', {
94+
x: bounds?.x,
95+
y: bounds?.y
96+
});
97+
});
98+
3799
enable(mainWindow.webContents);
38100

39101
if (process.env.NODE_ENV === 'development') {

src/main/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2015",
4-
"module": "commonjs",
3+
"target": "esnext",
4+
"module": "nodenext",
55
"strict": true,
66
"esModuleInterop": true,
77
"skipLibCheck": true,

src/renderer/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"compilerOptions": {
33
"target": "esnext",
44
"useDefineForClassFields": true,
5-
"module": "esnext",
6-
"moduleResolution": "node",
5+
"module": "nodenext",
6+
"moduleResolution": "nodenext",
77
"strict": true,
88
"jsx": "preserve",
99
"sourceMap": true,

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ const config = defineConfig({
3737
},
3838
});
3939

40-
module.exports = config;
40+
export default config;

0 commit comments

Comments
 (0)