Skip to content

Commit 37d815c

Browse files
committed
feat: add launch at startup
Copied from beakerbrowser#1753
1 parent b1d705c commit 37d815c

6 files changed

Lines changed: 110 additions & 8 deletions

File tree

app/bg/browser.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import toIco from 'to-ico';
2020
import emitStream from 'emit-stream';
2121
import EventEmitter from 'events';
2222
import LRU from 'lru';
23+
import AutoLaunch from 'auto-launch';
2324
const exec = require('util').promisify(require('child_process').exec);
2425
import * as logLib from './logger';
2526
import * as adblocker from './adblocker';
@@ -202,6 +203,10 @@ export async function setup() {
202203
});
203204
cb(request.errorCode);
204205
});
206+
207+
// Ensure run on startup is set correctly
208+
const runOnStartup = getSetting('launch_on_startup');
209+
await setRunOnStartup(runOnStartup);
205210
}
206211

207212
export const WEBAPI = {
@@ -211,6 +216,7 @@ export const WEBAPI = {
211216
getDaemonNetworkStatus,
212217
checkForUpdates,
213218
restartBrowser,
219+
setRunOnStartup,
214220

215221
getSetting,
216222
getSettings,
@@ -943,3 +949,43 @@ function onCompleted(details) {
943949
set(details.responseHeaders['content-type']);
944950
}
945951
}
952+
953+
async function setRunOnStartup(desiredState) {
954+
if (IS_LINUX) {
955+
const autoLauncher = new AutoLaunch({
956+
name: 'Beaker',
957+
path: process.execPath,
958+
});
959+
const currentState = await autoLauncher.isEnabled();
960+
if (currentState !== desiredState) {
961+
if (desiredState === true) {
962+
await autoLauncher.enable();
963+
} else {
964+
await autoLauncher.disable();
965+
}
966+
}
967+
return;
968+
}
969+
//using setLoginItems
970+
const opts = {};
971+
if (process.platform !== 'darwin') {
972+
// mac
973+
opts.path = path.resolve(
974+
path.dirname(process.execPath),
975+
'..',
976+
'Update.exe'
977+
);
978+
opts.args = [
979+
'--processStart',
980+
`"${path.basename(process.execPath)}"`,
981+
'--process-start-args',
982+
`"--hidden"`,
983+
];
984+
}
985+
const currentState = app.getLoginItemSettings(opts);
986+
987+
if (currentState !== desiredState) {
988+
opts.openAtLogin = desiredState;
989+
app.setLoginItemSettings(opts);
990+
}
991+
}

app/bg/dbs/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const setup = async function (opts) {
4040
new_tabs_in_foreground: 0,
4141
run_background: 1,
4242
default_zoom: 0,
43+
launch_on_startup: 0,
4344
browser_theme: 'system',
4445
analytics_enabled: 1,
4546
extended_network_index: 'default',

app/bg/web-apis/manifests/internal/browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default {
55
getDaemonNetworkStatus: 'promise',
66
checkForUpdates: 'promise',
77
restartBrowser: 'sync',
8+
setRunOnStartup: 'promise',
89

910
getSettings: 'promise',
1011
getSetting: 'promise',

app/package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"abort-controller": "^3.0.0",
1414
"ajv": "^6.10.2",
1515
"anymatch": "^2.0.0",
16+
"auto-launch": "^5.0.5",
1617
"await-lock": "^1.2.1",
1718
"base32.js": "^0.1.0",
1819
"beaker-error-constants": "^1.5.0",

app/userland/settings/js/views/general.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ class GeneralSettingsView extends LitElement {
7777
</div>
7878
<div class="form-group">
7979
<h2>Browser Settings</h2>
80-
${this.renderRunBackgroundSettings()} ${this.renderProtocolSettings()}
81-
${this.renderThemeSettings()}
80+
${this.renderRunBackgroundSettings()} ${this.renderStartupSettings()}
81+
${this.renderProtocolSettings()} ${this.renderThemeSettings()}
8282
</div>
8383
<div class="form-group">
8484
<h2>Search Settings</h2>
8585
${this.renderSearchSettings()}
8686
</div>
8787
<div class="form-group">
88-
<h2>Beaker Analytics</h2>
88+
<h2>Nomad Analytics</h2>
8989
${this.renderAnalyticsSettings()}
9090
</div>
9191
`;
@@ -277,7 +277,7 @@ class GeneralSettingsView extends LitElement {
277277
?checked=${this.settings.custom_start_page === 'blank'}
278278
@change=${this.onCustomStartPageChange}
279279
/>
280-
<label for="customStartPage1"> Show a new tab </label>
280+
<label for="customStartPage1">Show a new tab</label>
281281
</div>
282282
<div class="radio-item">
283283
<input
@@ -298,7 +298,7 @@ class GeneralSettingsView extends LitElement {
298298

299299
renderRunBackgroundSettings() {
300300
return html`
301-
<div class="section on-startup">
301+
<div class="section run-background">
302302
<p>
303303
Running in the background helps keep your data online even if you're
304304
not using Beaker.
@@ -311,12 +311,31 @@ class GeneralSettingsView extends LitElement {
311311
?checked=${this.settings.run_background == 1}
312312
@change=${this.onRunBackgroundToggle}
313313
/>
314-
<label for="runBackground"> Let Beaker run in the background </label>
314+
<label for="runBackground">Let Nomad run in the background</label>
315315
</div>
316316
</div>
317317
`;
318318
}
319319

320+
renderStartupSettings() {
321+
return html`<div class="section on-startup">
322+
<p>
323+
When running in the background it's helpful to also have the browser
324+
open on startup of your computer.
325+
</p>
326+
327+
<div class="radio-item">
328+
<input
329+
type="checkbox"
330+
id="onStart"
331+
?checked=${this.settings.launch_on_startup == 1}
332+
@change=${this.onLaunchOnStartupToggle}
333+
/>
334+
<label for="onStart">Launch Nomad on computer startup</label>
335+
</div>
336+
</div>`;
337+
}
338+
320339
renderNewTabSettings() {
321340
return html`
322341
<div class="section">
@@ -425,7 +444,7 @@ class GeneralSettingsView extends LitElement {
425444
};
426445

427446
return html` <div class="section">
428-
<p>Set Beaker as the default browser for:</p>
447+
<p>Set Nomad as the default browser for:</p>
429448
430449
${Object.keys(this.defaultProtocolSettings).map(
431450
(proto) => html`
@@ -521,7 +540,7 @@ class GeneralSettingsView extends LitElement {
521540
522541
<ul>
523542
<li>An anonymous ID</li>
524-
<li>Your Beaker version, e.g. ${this.browserInfo.version}</li>
543+
<li>Your Nomad version, e.g. ${this.browserInfo.version}</li>
525544
<li>Your operating system, e.g. Windows 10</li>
526545
</ul>
527546
</div>
@@ -605,6 +624,13 @@ class GeneralSettingsView extends LitElement {
605624
toast.create('Setting updated');
606625
}
607626

627+
async onLaunchOnStartupToggle(e) {
628+
const desiredState = e.target.checked;
629+
beaker.browser.setSetting('launch_on_startup', desiredState ? 1 : 0);
630+
await beaker.browser.setRunOnStartup(desiredState);
631+
toast.create('Setting updated');
632+
}
633+
608634
onNewTabChange(e) {
609635
this.settings.new_tab = e.target.value;
610636
beaker.browser.setSetting('new_tab', this.settings.new_tab);

0 commit comments

Comments
 (0)