Add --start-in-tray CLI flag to let app start in background (PR #564, Fixes #522)

This commit is contained in:
Felix Eckhofer 2018-12-01 20:04:55 +01:00 committed by Ronan Jouchet
parent 1f6e74b1e3
commit 8600c78d1a
3 changed files with 37 additions and 3 deletions

View File

@ -113,6 +113,7 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
// Whether the window should always stay on top of other windows. Default is false.
alwaysOnTop: options.alwaysOnTop,
titleBarStyle: options.titleBarStyle,
show: options.tray !== 'start-in-tray',
},
DEFAULT_WINDOW_OPTIONS,
),

View File

@ -519,11 +519,13 @@ Prevents application from being run multiple times. If such an attempt occurs th
#### [tray]
```
--tray
--tray [start-in-tray]
```
Application will stay as an icon in the system tray. Prevents application from being closed from clicking the window close button.
When the optional argument `start-in-tray` is provided, i.e. the application is started using `--tray start-in-tray`, the main window will not be shown on first start.
#### [basic-auth-username]
```

View File

@ -13,6 +13,17 @@ function collect(val, memo) {
return memo;
}
function parseMaybeBoolString(val) {
switch (val) {
case 'true':
return true;
case 'false':
return false;
default:
return val;
}
}
function parseJson(val) {
if (!val) return {};
return JSON.parse(val);
@ -36,6 +47,22 @@ function checkInternet() {
}
if (require.main === module) {
const sanitizedArgs = [];
process.argv.forEach((arg) => {
if (sanitizedArgs.length > 0) {
const previousArg = sanitizedArgs[sanitizedArgs.length - 1];
// Work around commander.js not supporting default argument for options
if (
previousArg === '--tray' &&
!['true', 'false', 'start-in-tray'].includes(arg)
) {
sanitizedArgs.push('true');
}
}
sanitizedArgs.push(arg);
});
program
.version(packageJson.version, '-v, --version')
.arguments('<targetUrl> [dest]')
@ -191,7 +218,11 @@ if (require.main === module) {
'a JSON string of key/value pairs to be set as file download options. See https://github.com/sindresorhus/electron-dl for available options.',
parseJson,
)
.option('--tray', 'allow app to stay in system tray')
.option(
'--tray [start-in-tray]',
"Allow app to stay in system tray. If 'start-in-tray' is given as argument, don't show main window on first start",
parseMaybeBoolString,
)
.option('--basic-auth-username <value>', 'basic http(s) auth username')
.option('--basic-auth-password <value>', 'basic http(s) auth password')
.option('--always-on-top', 'enable always on top window')
@ -203,7 +234,7 @@ if (require.main === module) {
'--global-shortcuts <value>',
'JSON file with global shortcut configuration. See https://github.com/jiahaog/nativefier/blob/master/docs/api.md#global-shortcuts',
)
.parse(process.argv);
.parse(sanitizedArgs);
if (!process.argv.slice(2).length) {
program.help();