Skip to content

Commit 12c4c04

Browse files
fix: replace the default electron setProtocol (#38)
1 parent 5a17c4f commit 12c4c04

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

package-lock.json

Lines changed: 8 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"@electron-toolkit/tsconfig": "^1.0.1",
4242
"@electron-toolkit/utils": "^3.0.0",
4343
"@fortawesome/fontawesome-svg-core": "^6.5.1",
44-
"@fortawesome/free-solid-svg-icons": "^6.5.1",
4544
"@fortawesome/free-regular-svg-icons": "^6.5.1",
45+
"@fortawesome/free-solid-svg-icons": "^6.5.1",
4646
"@fortawesome/react-fontawesome": "^0.2.0",
4747
"@headlessui/react": "^1.7.18",
4848
"@hookform/resolvers": "^3.3.4",
@@ -79,6 +79,7 @@
7979
"path-browserify": "^1.0.1",
8080
"postcss": "^8.4.35",
8181
"prettier": "^3.2.4",
82+
"rage-edit": "^1.2.0",
8283
"react": "^18.2.0",
8384
"react-dom": "^18.2.0",
8485
"react-hook-form": "^7.50.1",

src/main/main.ts

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import path from 'path'
1919
import i18next from 'i18next'
2020
import Backend from 'i18next-fs-backend'
2121
import { uniq } from 'lodash'
22+
import { Registry } from 'rage-edit';
2223

2324

2425
//get app parameter
@@ -41,9 +42,11 @@ function startup() {
4142
} else {
4243

4344
//I set the app to open at operating system startup
44-
app.setLoginItemSettings({
45-
openAtLogin: true
46-
})
45+
if (process.env.node_env !== 'development') {
46+
app.setLoginItemSettings({
47+
openAtLogin: true
48+
})
49+
}
4750

4851
///LOGGER
4952
startLogger()
@@ -183,7 +186,7 @@ function attachOnReadyProcess() {
183186
SplashScreenController.instance.window.addOnBuildListener(() => {
184187
setTimeout(startApp, 2500)
185188
})
186-
attachProtocolListeners()
189+
await attachProtocolListeners()
187190

188191
app.on('activate', (e, isWindowOpen) => {
189192
log('ACTIVATE WINDOW', e, isWindowOpen)
@@ -283,25 +286,69 @@ function attachOnReadyProcess() {
283286
})
284287
}
285288

289+
async function registryProtocol(protocol: string) {
290+
const AppName = app.getName();
291+
292+
await Registry.set(`HKCU\\Software\\${AppName}\\Capabilities`, 'ApplicationName', AppName);
293+
await Registry.set(`HKCU\\Software\\${AppName}\\Capabilities`, 'ApplicationDescription', AppName);
294+
295+
await Registry.set(`HKCU\\Software\\${AppName}\\Capabilities\\URLAssociations`, protocol, `${AppName}.${protocol}`);
296+
297+
await Registry.set(`HKCU\\Software\\Classes\\${AppName}.${protocol}\\DefaultIcon`, '', process.execPath);
298+
299+
await Registry.set(`HKCU\\Software\\Classes\\${AppName}.${protocol}\\shell\\open\\command`, '', `"${process.execPath}" "%1"`);
300+
301+
await Registry.set(`HKCU\\Software\\RegisteredApplications`, AppName, `Software\\${AppName}\\Capabilities`);
302+
303+
}
304+
305+
async function removeRegistryProtocol(protocol: string) {
306+
const AppName = app.getName();
286307

287-
function attachProtocolListeners() {
288-
// remove so we can register each time as we run the app.
289-
app.removeAsDefaultProtocolClient('tel')
290-
app.removeAsDefaultProtocolClient('callto')
291-
app.removeAsDefaultProtocolClient('nethlink')
308+
await Registry.delete(`HKCU\\Software\\${AppName}`);
309+
310+
await Registry.delete(`HKCU\\Software\\Classes\\${AppName}.${protocol}`);
311+
312+
await Registry.delete(`HKCU\\Software\\RegisteredApplications`, AppName);
313+
314+
}
315+
316+
async function attachProtocolListeners() {
317+
if (process.env.node_env === 'development') {
318+
// remove so we can register each time as we run the app.
319+
if (process.platform === 'win32') {
320+
await removeRegistryProtocol('tel')
321+
await removeRegistryProtocol('callto')
322+
await removeRegistryProtocol('nethlink')
323+
} else {
324+
app.removeAsDefaultProtocolClient('tel')
325+
app.removeAsDefaultProtocolClient('callto')
326+
app.removeAsDefaultProtocolClient('nethlink')
327+
328+
}
329+
}
330+
if (process.platform === 'win32') {
331+
await registryProtocol('tel')
332+
await registryProtocol('callto')
333+
await registryProtocol('nethlink')
334+
}
292335

293336
// if we are running a non-packaged version of the app && on windows
294-
if (process.env.node_env === 'development' && process.platform === 'win32') {
337+
const res: { [protocol: string]: boolean } = {}
338+
if (process.env.node_env === 'development' && process.argv.length > 2) {
339+
log(process.argv.join('; '))
295340
// set the path of electron.exe and your app.
296341
// these two additional parameters are only available on windows.
297-
app.setAsDefaultProtocolClient('tel', process.execPath, [resolve(process.argv[1])])
298-
app.setAsDefaultProtocolClient('callto', process.execPath, [resolve(process.argv[1])])
299-
app.setAsDefaultProtocolClient('nethlink', process.execPath, [resolve(process.argv[1])])
342+
res['tel'] = app.setAsDefaultProtocolClient('tel', process.execPath, [resolve(process.argv[1])])
343+
res['callto'] = app.setAsDefaultProtocolClient('callto', process.execPath, [resolve(process.argv[1])])
344+
res['nethlink'] = app.setAsDefaultProtocolClient('nethlink', process.execPath, [resolve(process.argv[1])])
300345
} else {
301-
app.setAsDefaultProtocolClient('tel')
302-
app.setAsDefaultProtocolClient('callto')
303-
app.setAsDefaultProtocolClient('nethlink')
346+
res['tel'] = app.setAsDefaultProtocolClient('tel')
347+
res['callto'] = app.setAsDefaultProtocolClient('callto')
348+
res['nethlink'] = app.setAsDefaultProtocolClient('nethlink')
349+
304350
}
351+
log('associated protocols:', res)
305352

306353
app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
307354
// Print out data received from the second instance.

0 commit comments

Comments
 (0)