fix system context menu entry on windows

This commit is contained in:
Labhansh Agrawal 2020-07-10 03:15:12 +05:30 committed by Benjamin Staneck
parent 6f17511e7b
commit 2e4108e8b2
3 changed files with 43 additions and 35 deletions

View file

@ -29,18 +29,13 @@ if (process.platform === 'win32') {
switch (process.argv[1]) { switch (process.argv[1]) {
case '--squirrel-install': case '--squirrel-install':
case '--squirrel-updated': case '--squirrel-updated':
systemContextMenu.add(() => { systemContextMenu.add();
checkSquirrel();
});
break; break;
case '--squirrel-uninstall': case '--squirrel-uninstall':
systemContextMenu.remove(() => { systemContextMenu.remove();
checkSquirrel();
});
break; break;
default:
checkSquirrel();
} }
checkSquirrel();
} }
// Native // Native

View file

@ -9,14 +9,18 @@ if (process.platform === 'win32') {
} }
const appPath = `"${process.execPath}"`; const appPath = `"${process.execPath}"`;
const regKey = `Software\\Classes\\Directory\\background\\shell\\Hyper`; const regKeys = [
`Software\\Classes\\Directory\\Background\\shell\\Hyper`,
`Software\\Classes\\Directory\\shell\\Hyper`,
`Software\\Classes\\Drive\\shell\\Hyper`
];
const regParts = [ const regParts = [
{key: 'command', name: '', value: `${appPath} "%V"`}, {key: 'command', name: '', value: `${appPath} "%V"`},
{name: '', value: 'Open Hyper here'}, {name: '', value: 'Open Hyper here'},
{name: 'Icon', value: `${appPath}`} {name: 'Icon', value: `${appPath}`}
]; ];
function addValues(hyperKey: regTypes.HKEY, commandKey: regTypes.HKEY, callback: Function) { function addValues(hyperKey: regTypes.HKEY, commandKey: regTypes.HKEY) {
try { try {
Registry.setValueSZ(hyperKey, regParts[1].name, regParts[1].value); Registry.setValueSZ(hyperKey, regParts[1].name, regParts[1].value);
} catch (error) { } catch (error) {
@ -32,30 +36,32 @@ function addValues(hyperKey: regTypes.HKEY, commandKey: regTypes.HKEY, callback:
} catch (err_) { } catch (err_) {
console.error(err_); console.error(err_);
} }
callback();
} }
export const add = (callback: Function) => { export const add = () => {
try { regKeys.forEach((regKey) => {
const hyperKey = try {
Registry.openKey(Registry.HKCU, regKey, Registry.Access.ALL_ACCESS) || const hyperKey =
Registry.createKey(Registry.HKCU, regKey, Registry.Access.ALL_ACCESS); Registry.openKey(Registry.HKCU, regKey, Registry.Access.ALL_ACCESS) ||
const commandKey = Registry.createKey(Registry.HKCU, regKey, Registry.Access.ALL_ACCESS);
Registry.openKey(Registry.HKCU, `${regKey}\\${regParts[0].key}`, Registry.Access.ALL_ACCESS) || const commandKey =
Registry.createKey(Registry.HKCU, `${regKey}\\${regParts[0].key}`, Registry.Access.ALL_ACCESS); Registry.openKey(Registry.HKCU, `${regKey}\\${regParts[0].key}`, Registry.Access.ALL_ACCESS) ||
addValues(hyperKey, commandKey, callback); Registry.createKey(Registry.HKCU, `${regKey}\\${regParts[0].key}`, Registry.Access.ALL_ACCESS);
Registry.closeKey(hyperKey); addValues(hyperKey, commandKey);
Registry.closeKey(commandKey); Registry.closeKey(hyperKey);
} catch (error) { Registry.closeKey(commandKey);
console.error(error); } catch (error) {
} console.error(error);
}
});
}; };
export const remove = (callback: Function) => { export const remove = () => {
try { regKeys.forEach((regKey) => {
Registry.deleteTree(Registry.HKCU, regKey); try {
} catch (err) { Registry.deleteTree(Registry.HKCU, regKey);
console.error(err); } catch (err) {
} console.error(err);
callback(); }
});
}; };

View file

@ -1,5 +1,5 @@
import {app, BrowserWindow, shell, Menu, BrowserWindowConstructorOptions} from 'electron'; import {app, BrowserWindow, shell, Menu, BrowserWindowConstructorOptions} from 'electron';
import {isAbsolute} from 'path'; import {isAbsolute, normalize, sep} from 'path';
import {parse as parseUrl} from 'url'; import {parse as parseUrl} from 'url';
import {v4 as uuidv4} from 'uuid'; import {v4 as uuidv4} from 'uuid';
import fileUriToPath from 'file-uri-to-path'; import fileUriToPath from 'file-uri-to-path';
@ -60,9 +60,16 @@ export function newWindow(
}; };
// set working directory // set working directory
let argPath = process.argv[1];
if (argPath && process.platform === 'win32') {
if (/[a-zA-Z]:"/.test(argPath)) {
argPath = argPath.replace('"', sep);
}
argPath = normalize(argPath + sep);
}
let workingDirectory = homeDirectory; let workingDirectory = homeDirectory;
if (process.argv[1] && isAbsolute(process.argv[1])) { if (argPath && isAbsolute(argPath)) {
workingDirectory = process.argv[1]; workingDirectory = argPath;
} else if (cfg.workingDirectory && isAbsolute(cfg.workingDirectory)) { } else if (cfg.workingDirectory && isAbsolute(cfg.workingDirectory)) {
workingDirectory = cfg.workingDirectory; workingDirectory = cfg.workingDirectory;
} }