Skip to content

Commit 6df85f3

Browse files
🚀 feat: Enhance electron main process with deep link handling
- Implemented functionality to focus the main window if it already exists, preventing multiple instances. - Added deep link handling to process URLs with custom protocols directly within the app. - Introduced a mechanism to prevent multiple instances of the app on Windows, ensuring only one instance runs at a time. - Streamlined event handling for 'open-url', focusing on robust parsing and forwarding of deep link information to the renderer process.
1 parent b0d415c commit 6df85f3

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

‎apps/desktop/electron/main.ts‎

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,11 @@ function createWindow() {
897897
autoUpdater.forceDevUpdateConfig = true;
898898
autoUpdater.checkForUpdates();
899899

900+
if (mainWindow) {
901+
mainWindow.focus();
902+
return;
903+
}
904+
900905
const applicationIconPath = path.join(process.env.VITE_PUBLIC, 'apple.icns');
901906
let applicationIcon = nativeImage.createFromPath(applicationIconPath);
902907
applicationIcon = applicationIcon.resize({
@@ -1030,39 +1035,81 @@ app.on('activate', () => {
10301035
// On OS X it's common to re-create a window in the app when the
10311036
// dock icon is clicked and there are no other windows open.
10321037
if (BrowserWindow.getAllWindows().length === 0) {
1033-
createWindow()
1038+
createWindow();
1039+
} else if (mainWindow) {
1040+
mainWindow.focus();
10341041
}
10351042
})
10361043

10371044
// This event will be emitted when your app is opened with a URL that uses your protocol
10381045
app.on('open-url', (event, url) => {
1039-
try {
1040-
// Prevent the default behavior
1041-
event.preventDefault();
1042-
1043-
// Parse the URL
1044-
const parsedUrl = new URL(url);
1045-
const deviceCode = parsedUrl.host.split('=')[1];
1046-
// Log the device code
1047-
console.log(`Device code: ${deviceCode}`);
1048-
Sentry.captureMessage(`Device code added: ${deviceCode}, parsedUrl: ${JSON.stringify(parsedUrl)}`);
1049-
1050-
// Write the device code to a file in the sessionData directory
1051-
writeFilePromise(deviceCodeFilePath, deviceCode).then(() => {
1052-
console.log('Device code saved successfully!');
1053-
});
1046+
event.preventDefault();
1047+
handleDeepLink(url);
1048+
});
10541049

1055-
// Send the device code to the renderer process
1050+
// Windows specific handling for single instance
1051+
const gotTheLock = app.requestSingleInstanceLock();
1052+
if (!gotTheLock) {
1053+
app.quit();
1054+
} else {
1055+
app.on('second-instance', (event, commandLine, workingDirectory) => {
1056+
// Someone tried to run a second instance, we should focus our window.
10561057
if (mainWindow) {
1057-
mainWindow.webContents.send('device-code', deviceCode);
1058+
if (mainWindow.isMinimized()) mainWindow.restore();
1059+
mainWindow.focus();
10581060
}
1059-
} catch (error: any) {
1060-
console.log(error)
1061-
Sentry.captureException(new Error(`Failed to open url: ${error?.message}`), {
1062-
tags: { module: "openUrl" },
1063-
extra: { error }
1064-
});
1061+
1062+
// Handle deep link for Windows
1063+
const url = commandLine.find(arg => arg.startsWith('screenlinkDesktop://'));
1064+
if (url) handleDeepLink(url);
1065+
});
1066+
1067+
// Create mainWindow, load the rest of the app, etc...
1068+
app.whenReady().then(createWindow);
1069+
}
1070+
1071+
function handleDeepLink(url: string) {
1072+
console.log(`Deep link URL: ${url}`);
1073+
// Parse and handle the deep link URL
1074+
// For example, extract the deviceCode and do something with it
1075+
const parsedUrl = new URL(url);
1076+
const deviceCode = parsedUrl.searchParams.get('deviceCode');
1077+
console.log(`Device code: ${deviceCode}`);
1078+
1079+
// Ensure mainWindow is available and send the device code to it
1080+
if (mainWindow) {
1081+
mainWindow.webContents.send('device-code', deviceCode);
10651082
}
1066-
});
1083+
}
1084+
1085+
// app.on('open-url', (event, url) => {
1086+
// try {
1087+
// // Prevent the default behavior
1088+
// event.preventDefault();
1089+
1090+
// // Parse the URL
1091+
// const parsedUrl = new URL(url);
1092+
// const deviceCode = parsedUrl.host.split('=')[1];
1093+
// // Log the device code
1094+
// console.log(`Device code: ${deviceCode}`);
1095+
// Sentry.captureMessage(`Device code added: ${deviceCode}, parsedUrl: ${JSON.stringify(parsedUrl)}`);
1096+
1097+
// // Write the device code to a file in the sessionData directory
1098+
// writeFilePromise(deviceCodeFilePath, deviceCode).then(() => {
1099+
// console.log('Device code saved successfully!');
1100+
// });
1101+
1102+
// // Send the device code to the renderer process
1103+
// if (mainWindow) {
1104+
// mainWindow.webContents.send('device-code', deviceCode);
1105+
// }
1106+
// } catch (error: any) {
1107+
// console.log(error)
1108+
// Sentry.captureException(new Error(`Failed to open url: ${error?.message}`), {
1109+
// tags: { module: "openUrl" },
1110+
// extra: { error }
1111+
// });
1112+
// }
1113+
// });
10671114

10681115
app.whenReady().then(createWindow)

0 commit comments

Comments
 (0)