Skip to content

Commit 762952d

Browse files
committed
Massive code refactor to Typescript; resolving #3
1 parent d97d96d commit 762952d

29 files changed

+4217
-23
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
srcdist/** linguist-generated=true
2+
package-lock.json linguist-generated=true

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules/
22
dist/
33
*.tgz
44
*.log
5-
package-lock.json
5+
.vscode
File renamed without changes.

package-lock.json

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

package.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
{
22
"name": "transformice-electron",
3-
"version": "1.3.1",
3+
"version": "2.0.0",
44
"description": "A cross-platform Transformice standalone app running on Electron.",
5-
"main": "main.js",
5+
"main": "srcdist/main.js",
66
"scripts": {
7-
"start": "electron .",
8-
"clean": "node resources/clean.js",
9-
"build": "electron-builder --x64",
10-
"buildwin": "electron-builder -w --x64",
11-
"buildmac": "electron-builder -m --x64",
12-
"buildlnx": "electron-builder -l --x64",
13-
"buildall": "electron-builder -wml --x64"
7+
"start": "npm run tsc && electron .",
8+
"clean": "node clean.js",
9+
"tsc": "tsc",
10+
"lint": "eslint -c .eslintrc --ext .ts ./src",
11+
"build": "npm run tsc && electron-builder --x64",
12+
"buildwin": "npm run tsc && electron-builder -w --x64",
13+
"buildmac": "npm run tsc && electron-builder -m --x64",
14+
"buildlnx": "npm run tsc && electron-builder -l --x64",
15+
"buildall": "npm run tsc && electron-builder -wml --x64"
1416
},
1517
"build": {
18+
"asar": false,
1619
"productName": "Transformice",
1720
"appId": "io.bagueatt.transformice.electron",
1821
"icon": "resources/icon.png",
1922
"files": [
2023
"resources",
2124
"node_modules",
22-
"main.js",
23-
"preload.js"
25+
"srcdist"
2426
],
2527
"win": {
2628
"target": "nsis",
@@ -65,8 +67,12 @@
6567
},
6668
"homepage": "https://github.com/Cassolette/Transformice-Electron#readme",
6769
"devDependencies": {
70+
"@typescript-eslint/eslint-plugin": "^4.22.0",
71+
"@typescript-eslint/parser": "^4.22.0",
6872
"electron": "^11.1.1",
69-
"electron-builder": "^22.9.1"
73+
"electron-builder": "^22.9.1",
74+
"eslint": "^7.25.0",
75+
"typescript": "^4.2.4"
7076
},
7177
"dependencies": {
7278
"electron-settings": "^4.0.2",

preload.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

resources/prefs/general.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ <h1>Alignment</h1>
6666
<script>
6767
let active_btn = null;
6868

69-
let electronSets = window.parent.window.exports.electronSets;
69+
let electronSets = window.parent.window.electron.electronSets;
7070

7171
function setActiveBtn(x, y) {
7272
if (active_btn)

resources/prefs/prefs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ var nav = {};
4343
document.getElementById(el_id).classList.add("active");
4444

4545
// load content
46-
if (!window.exports) { console.log("not loaded in electron"); return; }
46+
if (!window.electron) { console.log("not loaded in electron"); return; }
4747

48-
window.exports.readFile(submenus[id].file, (err, contents) => {
48+
window.electron.readFile(submenus[id].file, (err, contents) => {
4949
let frame = document.createElement("iframe");
5050
frame.setAttribute("srcdoc", contents || "");
5151

resources/prefs/preload_prefs.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
var electronSets = require("electron-settings");
2+
var fs = require("fs");
3+
var path = require("path");
34

4-
window.exports = {
5-
readFile: function (filename, cb) {
6-
fs.readFile(path.join(__dirname, filename), cb);
5+
window.electron = {
6+
readFile: (filename, cb) => {
7+
return fs.readFile(path.join(__dirname, filename), cb);
78
},
8-
electronSets: require("electron-settings"),
9+
electronSets: electronSets
910
};

src/TeWindow.ts

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import { TeGames } from "./te-enums";
2+
import * as path from "path";
3+
import {
4+
app,
5+
BrowserWindow,
6+
dialog,
7+
Menu,
8+
shell as electronShell
9+
} from "electron";
10+
import { APP_NAME } from "./te-consts";
11+
import { newTEProcess } from "./te-process";
12+
13+
const BASE_DIR = path.join(__dirname, "..");
14+
15+
const FILE_BASE = "file://" + BASE_DIR;
16+
const FILE_URL_FAILURE = FILE_BASE + "/resources/failure.html"
17+
const FILE_URL_PREFS = FILE_BASE + "/resources/prefs/prefs.html";
18+
19+
export abstract class TeWindow {
20+
protected httpUrl: string;
21+
/** The underlying electron browser window */
22+
public browserWindow: BrowserWindow;
23+
protected prefsWin: BrowserWindow;
24+
protected windowTitle: string = APP_NAME;
25+
protected windowBgColor: string = "#000000";
26+
27+
/* TODO: Find out if class properties can be overriden before constructor() is called.. */
28+
_constructor(httpUrl: string) {
29+
this.httpUrl = httpUrl;
30+
31+
let bwin = new BrowserWindow({
32+
width: 800,
33+
height: 600,
34+
frame: true, // show default OS window frame
35+
useContentSize: true, // make width & height relative to the content, not the whole window
36+
show: true, // show app background instantly until content is loaded
37+
backgroundColor: this.windowBgColor,
38+
title: this.windowTitle,
39+
icon: path.join(BASE_DIR, "resources", "icon.png"),
40+
webPreferences: {
41+
plugins: true,
42+
sandbox: true,
43+
contextIsolation: false,
44+
preload: path.join(__dirname, "preload.js")
45+
}
46+
});
47+
48+
bwin.setMenu(Menu.buildFromTemplate([
49+
{
50+
label: 'Zoom In',
51+
click: () => {
52+
var webContents = bwin.webContents;
53+
/* JS messes up when doing arithmetics against floats */
54+
var zoomFactor = Math.round(webContents.getZoomFactor() * 100 + 10) / 100;
55+
webContents.setZoomFactor(zoomFactor);
56+
}
57+
},
58+
{
59+
label: 'Zoom Out',
60+
click: () => {
61+
var webContents = bwin.webContents;
62+
/* JS messes up when doing arithmetics against floats */
63+
var zoomFactor = Math.round(webContents.getZoomFactor() * 100 - 10) / 100;
64+
if (zoomFactor > 0) webContents.setZoomFactor(zoomFactor);
65+
}
66+
},
67+
{
68+
label: 'Reset Zoom',
69+
click: () => {
70+
var webContents = bwin.webContents
71+
var currentZoomFactor = webContents.getZoomFactor();
72+
webContents.setZoomFactor(1);
73+
}
74+
},
75+
{
76+
label: 'More',
77+
submenu: [
78+
{
79+
label: 'Reload',
80+
click: () => {
81+
this.load();
82+
}
83+
},
84+
{
85+
label: 'Fullscreen',
86+
click: () => {
87+
bwin.setFullScreen(!bwin.isFullScreen());
88+
}
89+
},
90+
{
91+
label: 'Fit Window',
92+
click: () => {
93+
bwin.unmaximize();
94+
bwin.setFullScreen(false);
95+
bwin.setContentSize(800, 600);
96+
}
97+
},
98+
{
99+
label: 'Clear Cache',
100+
click: () => {
101+
dialog.showMessageBox(bwin, {
102+
type: "question",
103+
title: "Clear Cache",
104+
message: "Are you sure you want to clear the cache?",
105+
detail: "This will delete cached images such as profile pictures. This is useful to reload profile pictures that have since changed. Flash player cache is NOT cleared. Please also reload the app for changes to apply.",
106+
buttons: ["Cancel", "Yes"],
107+
cancelId: 0,
108+
defaultId: 1
109+
}).then((res) => {
110+
if (res.response == 1) {
111+
bwin.webContents.session.clearCache().then(() => {
112+
dialog.showMessageBox(bwin, {
113+
type: "info",
114+
title: "Clear Cache",
115+
message: "Successfully cleared cache."
116+
});
117+
});
118+
}
119+
});
120+
}
121+
},
122+
{
123+
label: 'Preferences',
124+
click: () => {
125+
this.showPreferences();
126+
}
127+
},
128+
{
129+
label: 'DevTools',
130+
accelerator: 'CmdOrCtrl+Shift+I',
131+
click: () => {
132+
bwin.webContents.openDevTools();
133+
}
134+
},
135+
{
136+
label: 'About',
137+
click: () => {
138+
dialog.showMessageBox(bwin, {
139+
type: "info",
140+
title: "About " + APP_NAME,
141+
message: "Version: " + app.getVersion()
142+
});
143+
}
144+
},
145+
]
146+
},
147+
{
148+
label: 'Other Games',
149+
submenu: [
150+
{
151+
label: 'Transformice',
152+
click: () => {
153+
newTEProcess(TeGames.TRANSFORMICE);
154+
}
155+
},
156+
{
157+
label: 'DeadMaze',
158+
click: () => {
159+
newTEProcess(TeGames.DEADMAZE);
160+
}
161+
},
162+
]
163+
}])
164+
);
165+
166+
bwin.webContents.on('did-finish-load', () => {
167+
//this.onReady();
168+
});
169+
170+
bwin.webContents.on('did-fail-load', (event, errCode, errDesc) => {
171+
this.onFail(errDesc);
172+
});
173+
174+
/* Open external links in user's preferred browser rather than in Electron */
175+
bwin.webContents.on('new-window', (event, url) => {
176+
event.preventDefault();
177+
electronShell.openExternal(url);
178+
});
179+
180+
/* Don't change the window title */
181+
bwin.on('page-title-updated', (event) => {
182+
event.preventDefault();
183+
});
184+
185+
this.browserWindow = bwin;
186+
}
187+
188+
onFail(errDesc: string) {
189+
let bwin = this.browserWindow;
190+
if (!bwin.isDestroyed()) {
191+
bwin.loadURL(FILE_URL_FAILURE);
192+
//win.show();
193+
}
194+
//this.errorDesc = errDesc;
195+
}
196+
197+
showPreferences() {
198+
if (this.prefsWin) {
199+
/* already open - focus and bail out */
200+
this.prefsWin.focus();
201+
return;
202+
}
203+
204+
this.prefsWin = new BrowserWindow({
205+
width: 680,
206+
height: 400,
207+
frame: true, /* show the default window frame (exit buttons, etc.) */
208+
useContentSize: true, /* make width & height relative to the content, not the whole window */
209+
autoHideMenuBar: true,
210+
title: APP_NAME,
211+
icon: path.join(BASE_DIR, "resources", "icon.png"),
212+
parent: this.browserWindow,
213+
webPreferences: {
214+
plugins: false,
215+
contextIsolation: false,
216+
enableRemoteModule: true,
217+
preload: path.join(BASE_DIR, "resources", "prefs", "preload_prefs.js")
218+
}
219+
});
220+
221+
this.prefsWin.on("closed", () => {
222+
this.prefsWin = null;
223+
this.browserWindow.focus();
224+
});
225+
226+
this.prefsWin.loadURL(FILE_URL_PREFS);
227+
}
228+
229+
/** This is called when the window needs to display content. */
230+
abstract load(): void;
231+
}

0 commit comments

Comments
 (0)