Skip to content
Snippets Groups Projects
Unverified Commit be810916 authored by Shazib Hussain's avatar Shazib Hussain Committed by GitHub
Browse files

More Electron fixes (#1099)

- Fix socket connection issues when reloading
- Fix external url clicking & middle clicking internal links
- Remove broken menu option. Easier for now than refactoring the
settings panel it now lives in. We can add it back later if needed?
parent 3cba8384
No related branches found
No related tags found
No related merge requests found
......@@ -165,6 +165,25 @@ async function createWindow() {
win.webContents.send('set-socket', { name: serverSocket });
});
// hit when middle-clicking buttons or <a href/> with a target set to _blank
// always deny, optionally redirect to browser
win.webContents.setWindowOpenHandler(({ url }) => {
if (isExternalUrl(url)) {
shell.openExternal(url);
}
return { action: 'deny' };
});
// hit when clicking <a href/> with no target
// optionally redirect to browser
win.webContents.on('will-navigate', (event, url) => {
if (isExternalUrl(url)) {
shell.openExternal(url);
event.preventDefault();
}
});
if (process.platform === 'win32') {
Menu.setApplicationMenu(null);
win.setMenu(getMenu(isDev, createWindow));
......@@ -175,6 +194,10 @@ async function createWindow() {
clientWin = win;
}
function isExternalUrl(url) {
return !url.includes('localhost:') && !url.includes('app://');
}
function updateMenu(isBudgetOpen) {
const menu = getMenu(isDev, createWindow);
const file = menu.items.filter(item => item.label === 'File')[0];
......
......@@ -149,15 +149,6 @@ function getMenu(isDev, createWindow) {
);
},
},
{
label: 'Repair split transactions',
enabled: false,
click: function (menuItem, focusedWin) {
focusedWin.webContents.executeJavaScript(
'__history && __history.push("/tools/fix-splits", { locationPtr: __history.location })',
);
},
},
],
},
{
......
......@@ -22,7 +22,7 @@ export const initServer: T.InitServer = handlers => {
};
};
export const clearServer: T.ClearServer = () => {
export const clearServer: T.ClearServer = async () => {
serverHandler = null;
listeners = new Map();
};
......
......@@ -188,3 +188,7 @@ export const listen: T.Listen = function (name, cb) {
export const unlisten: T.Unlisten = function (name) {
listeners.set(name, []);
};
export const clearServer: T.ClearServer = async function () {
//
};
......@@ -33,5 +33,5 @@ export type InitServer = typeof initServer;
export function serverPush(name: string, args: unknown): void;
export type ServerPush = typeof serverPush;
export function clearServer(): void;
export async function clearServer(): void;
export type ClearServer = typeof clearServer;
......@@ -72,13 +72,10 @@ function connectSocket(port, onOpen) {
onOpen();
};
client.onclose = event => {
socketClient = null;
};
}
export const init: T.Init = async function (socketName) {
await clearServer();
return new Promise(resolve => connectSocket(socketName, resolve));
};
......@@ -141,3 +138,18 @@ export const listen: T.Listen = function (name, cb) {
export const unlisten: T.Unlisten = function (name) {
listeners.set(name, []);
};
async function closeSocket(onClose) {
socketClient.onclose = event => {
socketClient = null;
onClose();
};
await socketClient.close();
}
export const clearServer: T.ClearServer = async function () {
if (socketClient != null) {
return new Promise(closeSocket);
}
};
......@@ -22,13 +22,23 @@ export const init: T.Init = function (socketName, handlers) {
// websockets doesn't support sending objects so parse/stringify needed
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
ws.on('message', data => {
let msg = JSON.parse(data);
if (ws.readyState !== 1) {
return;
}
let { id, name, args, undoTag, catchErrors } = msg;
if (handlers[name]) {
runHandler(handlers[name], args, { undoTag, name }).then(
result => {
if (ws.readyState !== 1) {
return;
}
if (catchErrors) {
result = { data: result, error: null };
}
......@@ -47,6 +57,9 @@ export const init: T.Init = function (socketName, handlers) {
);
},
nativeError => {
if (ws.readyState !== 1) {
return;
}
let error = coerceError(nativeError);
if (name.startsWith('api/')) {
......
---
category: Bugfix
authors: [Shazib]
---
Fix reloading issues, external url handling, and tidy up menus in the electron app.
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment