Skip to content

Commit d0f7004

Browse files
authored
Merge pull request #2 from andrewbrey/v2
2 parents 78e7247 + 2d013e4 commit d0f7004

28 files changed

+2435
-3239
lines changed

.github/workflows/app-build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55

66
defaults:
77
run:
8-
shell: 'bash'
8+
shell: "bash"
99

1010
jobs:
1111
build:
@@ -15,11 +15,11 @@ jobs:
1515
matrix:
1616
include:
1717
- platform: linux
18-
os: 'ubuntu-latest'
18+
os: "ubuntu-latest"
1919
- platform: windows
20-
os: 'windows-latest'
20+
os: "windows-latest"
2121
- platform: mac
22-
os: 'macos-latest'
22+
os: "macos-latest"
2323
runs-on: ${{ matrix.os }}
2424
steps:
2525
- uses: actions/checkout@v2

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "Nextron: Main",
99
"type": "node",
1010
"request": "attach",
11-
"protocol": "inspector",
11+
// "protocol": "inspector",
1212
"port": 9292,
1313
"skipFiles": ["<node_internals>/**"],
1414
"sourceMapPathOverrides": {
@@ -19,7 +19,7 @@
1919
},
2020
{
2121
"name": "Nextron: Renderer",
22-
"type": "pwa-chrome",
22+
"type": "chrome",
2323
"request": "attach",
2424
"port": 5858,
2525
"timeout": 10000,

main/background.ts

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
1-
import { app, BrowserWindow, globalShortcut, webFrameMain } from 'electron';
2-
import { ipcMain as ipc } from 'electron-better-ipc';
3-
import serve from 'electron-serve';
4-
import { readFile } from 'fs/promises';
5-
import fetch from 'node-fetch';
6-
import { join } from 'path';
7-
import { createWindow } from './helpers';
8-
import { omitSecurityHeadersAndBlockAds } from './helpers/web-requests';
9-
10-
const isProd: boolean = process.env.NODE_ENV === 'production';
11-
const isLinux = process.platform === 'linux';
1+
import { app, BrowserWindow, globalShortcut, webFrameMain } from "electron";
2+
import { ipcMain as ipc } from "electron-better-ipc";
3+
import serve from "electron-serve";
4+
import { readFile } from "fs/promises";
5+
import fetch from "cross-fetch";
6+
import { join } from "path";
7+
import { createWindow } from "./helpers";
8+
import { omitSecurityHeadersAndBlockAds } from "./helpers/web-requests";
9+
10+
const isProd: boolean = process.env.NODE_ENV === "production";
11+
const isLinux = process.platform === "linux";
1212

1313
const port = process.argv[2];
1414
const devURL = `http://localhost:${port}`;
1515

1616
let mainWindow: BrowserWindow | undefined;
1717
let experimentalVideoCSS = false;
1818
let forceDetachKeybindActive = false;
19-
const videoCSSOnScript = 'document.documentElement.classList.add("flot-video");';
20-
const videoCSSOffScript = 'document.documentElement.classList.remove("flot-video");';
19+
const videoCSSOnScript =
20+
'document.documentElement.classList.add("flot-video");';
21+
const videoCSSOffScript =
22+
'document.documentElement.classList.remove("flot-video");';
2123

2224
if (isLinux) {
2325
// https://github.com/electron/electron/issues/25153#issuecomment-843688494
24-
app.commandLine.appendSwitch('use-gl', 'desktop');
26+
app.commandLine.appendSwitch("use-gl", "desktop");
2527
}
2628

2729
if (isProd) {
28-
serve({ directory: 'app' });
30+
serve({ directory: "app" });
2931
} else {
30-
app.setPath('userData', `${app.getPath('userData')} (development)`);
32+
app.setPath("userData", `${app.getPath("userData")} (development)`);
3133
}
3234

3335
(async () => {
3436
await app.whenReady();
3537

36-
globalShortcut.register('CmdOrCtrl+Alt+I', () => {
38+
globalShortcut.register("CmdOrCtrl+Alt+I", () => {
3739
mainWindow?.setIgnoreMouseEvents(!forceDetachKeybindActive);
3840
forceDetachKeybindActive = !forceDetachKeybindActive;
3941
});
4042

4143
const embedCssFilePromise = isProd
42-
? readFile(join(__dirname, 'css', 'embed.css'), { encoding: 'utf8' })
44+
? readFile(join(__dirname, "css", "embed.css"), { encoding: "utf8" })
4345
: fetch(`${devURL}/css/embed.css`).then((r) => r.text());
4446

4547
const embedCss = embedCssFilePromise.catch((e) => {
4648
console.error(e);
47-
return '';
49+
return "";
4850
});
4951

5052
await new Promise((r) => setTimeout(r, isLinux ? 750 : 350)); // required otherwise transparency won't work consistently cross-platform...
5153

52-
mainWindow = createWindow('main', {
54+
mainWindow = createWindow("main", {
5355
transparent: true,
54-
title: 'Flōt',
56+
title: "Flōt",
5557
titleBarStyle: undefined,
5658
trafficLightPosition: undefined,
5759
skipTaskbar: false,
@@ -66,7 +68,7 @@ if (isProd) {
6668
minWidth: 300,
6769
maxHeight: 1000,
6870
maxWidth: 1000,
69-
icon: join(__dirname, 'images', 'logo', '256x256.png'),
71+
icon: join(__dirname, "images", "logo", "256x256.png"),
7072
alwaysOnTop: true,
7173
hasShadow: false,
7274
webPreferences: {
@@ -79,17 +81,34 @@ if (isProd) {
7981
mainWindow.setVisibleOnAllWorkspaces(true);
8082

8183
mainWindow.webContents.on(
82-
'did-fail-provisional-load',
83-
(event, code, desc, validatedUrl, isMainFrame, frameProcessId, frameRoutingId) => {
84-
if (mainWindow) ipc.callRenderer(mainWindow, 'iframe-load-failure', validatedUrl);
84+
"did-fail-provisional-load",
85+
(
86+
event,
87+
code,
88+
desc,
89+
validatedUrl,
90+
isMainFrame,
91+
frameProcessId,
92+
frameRoutingId
93+
) => {
94+
if (mainWindow)
95+
ipc.callRenderer(mainWindow, "iframe-load-failure", validatedUrl);
8596
}
8697
);
8798

8899
mainWindow.webContents.on(
89-
'did-frame-navigate',
90-
async (event, url, status, statusText, isMainFrame, frameProcessId, frameRoutingId) => {
100+
"did-frame-navigate",
101+
async (
102+
event,
103+
url,
104+
status,
105+
statusText,
106+
isMainFrame,
107+
frameProcessId,
108+
frameRoutingId
109+
) => {
91110
if (isMainFrame) return;
92-
if (url === 'about:blank') return;
111+
if (url === "about:blank") return;
93112

94113
const frame = webFrameMain.fromId(frameProcessId, frameRoutingId);
95114

@@ -121,55 +140,62 @@ if (isProd) {
121140
}
122141
);
123142

124-
mainWindow.webContents.on('did-navigate-in-page', async (event, url, isMainFrame, frameProcessId, frameRoutingId) => {
125-
if (isMainFrame) return;
126-
if (url === 'about:blank') return;
143+
mainWindow.webContents.on(
144+
"did-navigate-in-page",
145+
async (event, url, isMainFrame, frameProcessId, frameRoutingId) => {
146+
if (isMainFrame) return;
147+
if (url === "about:blank") return;
127148

128-
const frame = webFrameMain.fromId(frameProcessId, frameRoutingId);
149+
const frame = webFrameMain.fromId(frameProcessId, frameRoutingId);
129150

130-
if (frame) {
131-
frame.executeJavaScript(`parent.postMessage({key: 'location', msg: location.href}, "*")`);
151+
if (frame) {
152+
frame.executeJavaScript(
153+
`parent.postMessage({key: 'location', msg: location.href}, "*")`
154+
);
155+
}
132156
}
133-
});
157+
);
134158

135-
mainWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
159+
mainWindow.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
136160

137161
if (isProd) {
138-
await mainWindow.loadURL('app://./index.html');
162+
await mainWindow.loadURL("app://./index.html");
139163

140164
app
141165
.whenReady()
142-
.then(() => import('electron-updater'))
166+
.then(() => import("electron-updater"))
143167
.then(({ autoUpdater }) => autoUpdater.checkForUpdatesAndNotify())
144-
.catch((e) => console.error('Failed check updates:', e));
168+
.catch((e) => console.error("Failed check updates:", e));
145169
} else {
146170
await mainWindow.loadURL(`${devURL}/`);
147171
// mainWindow.webContents.openDevTools({ mode: 'detach', activate: false });
148172
}
149173
})();
150174

151-
ipc.answerRenderer('please-enable-video-css', () => {
175+
ipc.answerRenderer("please-enable-video-css", () => {
152176
getFlotEmbed()?.executeJavaScript(videoCSSOnScript);
153177
experimentalVideoCSS = true;
154178
});
155-
ipc.answerRenderer('please-disable-video-css', () => {
179+
ipc.answerRenderer("please-disable-video-css", () => {
156180
getFlotEmbed()?.executeJavaScript(videoCSSOffScript);
157181
experimentalVideoCSS = false;
158182
});
159-
ipc.answerRenderer('please-detach', () => mainWindow?.setIgnoreMouseEvents(true));
160-
ipc.answerRenderer('please-attach', () => {
183+
ipc.answerRenderer("please-detach", () =>
184+
mainWindow?.setIgnoreMouseEvents(true)
185+
);
186+
ipc.answerRenderer("please-attach", () => {
161187
if (!forceDetachKeybindActive) mainWindow?.setIgnoreMouseEvents(false);
162188
});
163-
ipc.answerRenderer('please-quit', () => app.quit());
189+
ipc.answerRenderer("please-quit", () => app.quit());
164190

165-
app.on('window-all-closed', () => app.quit());
166-
app.on('will-quit', () => globalShortcut.unregisterAll());
191+
app.on("window-all-closed", () => app.quit());
192+
app.on("will-quit", () => globalShortcut.unregisterAll());
167193

168194
function getFlotEmbed() {
169195
const frames = mainWindow?.webContents.mainFrame.frames ?? [];
170196

171197
// Some websites seem to cause the embed frame to be renamed...not sure how, it's
172198
// supposed to be a readonly property, but ah well, if this happens, just try the
173199
// using the first frame
174-
return frames.find((f) => f.name === 'flot-embed') ?? frames[0];
200+
return frames.find((f) => f.name === "flot-embed") ?? frames[0];
175201
}

main/helpers/create-window.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { BrowserWindow, BrowserWindowConstructorOptions, Rectangle, screen } from 'electron';
2-
import Store from 'electron-store';
1+
import {
2+
BrowserWindow,
3+
BrowserWindowConstructorOptions,
4+
Rectangle,
5+
screen,
6+
} from "electron";
7+
import Store from "electron-store";
38

49
interface WindowState {
510
x: number;
@@ -8,10 +13,18 @@ interface WindowState {
813
height: any;
914
}
1015

11-
export default (windowName: string, options: BrowserWindowConstructorOptions): BrowserWindow => {
12-
const key = 'window-state';
16+
export default (
17+
windowName: string,
18+
options: BrowserWindowConstructorOptions
19+
): BrowserWindow => {
20+
const key = "window-state";
1321
const name = `window-state-${windowName}`;
14-
const defaultState: WindowState = { x: 0, y: 0, width: options.width ?? 600, height: options.height ?? 400 };
22+
const defaultState: WindowState = {
23+
x: 0,
24+
y: 0,
25+
width: options.width ?? 600,
26+
height: options.height ?? 400,
27+
};
1528
const store = new Store<WindowState>({
1629
name,
1730
defaults: { ...defaultState },
@@ -76,14 +89,13 @@ export default (windowName: string, options: BrowserWindowConstructorOptions): B
7689
...state,
7790
webPreferences: {
7891
nodeIntegration: true,
79-
enableRemoteModule: true,
8092
contextIsolation: false,
8193
...options.webPreferences,
8294
},
8395
};
8496
win = new BrowserWindow(browserOptions);
8597

86-
win.on('close', saveState);
98+
win.on("close", saveState);
8799

88100
return win;
89101
};

0 commit comments

Comments
 (0)