Skip to content

Commit

Permalink
feat: add launch at startup
Browse files Browse the repository at this point in the history
Copied from beakerbrowser#1753
  • Loading branch information
knownasilya committed Mar 12, 2022
1 parent b1d705c commit 37d815c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
46 changes: 46 additions & 0 deletions app/bg/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import toIco from 'to-ico';
import emitStream from 'emit-stream';
import EventEmitter from 'events';
import LRU from 'lru';
import AutoLaunch from 'auto-launch';
const exec = require('util').promisify(require('child_process').exec);
import * as logLib from './logger';
import * as adblocker from './adblocker';
Expand Down Expand Up @@ -202,6 +203,10 @@ export async function setup() {
});
cb(request.errorCode);
});

// Ensure run on startup is set correctly
const runOnStartup = getSetting('launch_on_startup');
await setRunOnStartup(runOnStartup);
}

export const WEBAPI = {
Expand All @@ -211,6 +216,7 @@ export const WEBAPI = {
getDaemonNetworkStatus,
checkForUpdates,
restartBrowser,
setRunOnStartup,

getSetting,
getSettings,
Expand Down Expand Up @@ -943,3 +949,43 @@ function onCompleted(details) {
set(details.responseHeaders['content-type']);
}
}

async function setRunOnStartup(desiredState) {
if (IS_LINUX) {
const autoLauncher = new AutoLaunch({
name: 'Beaker',
path: process.execPath,
});
const currentState = await autoLauncher.isEnabled();
if (currentState !== desiredState) {
if (desiredState === true) {
await autoLauncher.enable();
} else {
await autoLauncher.disable();
}
}
return;
}
//using setLoginItems
const opts = {};
if (process.platform !== 'darwin') {
// mac
opts.path = path.resolve(
path.dirname(process.execPath),
'..',
'Update.exe'
);
opts.args = [
'--processStart',
`"${path.basename(process.execPath)}"`,
'--process-start-args',
`"--hidden"`,
];
}
const currentState = app.getLoginItemSettings(opts);

if (currentState !== desiredState) {
opts.openAtLogin = desiredState;
app.setLoginItemSettings(opts);
}
}
1 change: 1 addition & 0 deletions app/bg/dbs/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const setup = async function (opts) {
new_tabs_in_foreground: 0,
run_background: 1,
default_zoom: 0,
launch_on_startup: 0,
browser_theme: 'system',
analytics_enabled: 1,
extended_network_index: 'default',
Expand Down
1 change: 1 addition & 0 deletions app/bg/web-apis/manifests/internal/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default {
getDaemonNetworkStatus: 'promise',
checkForUpdates: 'promise',
restartBrowser: 'sync',
setRunOnStartup: 'promise',

getSettings: 'promise',
getSetting: 'promise',
Expand Down
27 changes: 27 additions & 0 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"abort-controller": "^3.0.0",
"ajv": "^6.10.2",
"anymatch": "^2.0.0",
"auto-launch": "^5.0.5",
"await-lock": "^1.2.1",
"base32.js": "^0.1.0",
"beaker-error-constants": "^1.5.0",
Expand Down
42 changes: 34 additions & 8 deletions app/userland/settings/js/views/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ class GeneralSettingsView extends LitElement {
</div>
<div class="form-group">
<h2>Browser Settings</h2>
${this.renderRunBackgroundSettings()} ${this.renderProtocolSettings()}
${this.renderThemeSettings()}
${this.renderRunBackgroundSettings()} ${this.renderStartupSettings()}
${this.renderProtocolSettings()} ${this.renderThemeSettings()}
</div>
<div class="form-group">
<h2>Search Settings</h2>
${this.renderSearchSettings()}
</div>
<div class="form-group">
<h2>Beaker Analytics</h2>
<h2>Nomad Analytics</h2>
${this.renderAnalyticsSettings()}
</div>
`;
Expand Down Expand Up @@ -277,7 +277,7 @@ class GeneralSettingsView extends LitElement {
?checked=${this.settings.custom_start_page === 'blank'}
@change=${this.onCustomStartPageChange}
/>
<label for="customStartPage1"> Show a new tab </label>
<label for="customStartPage1">Show a new tab</label>
</div>
<div class="radio-item">
<input
Expand All @@ -298,7 +298,7 @@ class GeneralSettingsView extends LitElement {

renderRunBackgroundSettings() {
return html`
<div class="section on-startup">
<div class="section run-background">
<p>
Running in the background helps keep your data online even if you're
not using Beaker.
Expand All @@ -311,12 +311,31 @@ class GeneralSettingsView extends LitElement {
?checked=${this.settings.run_background == 1}
@change=${this.onRunBackgroundToggle}
/>
<label for="runBackground"> Let Beaker run in the background </label>
<label for="runBackground">Let Nomad run in the background</label>
</div>
</div>
`;
}

renderStartupSettings() {
return html`<div class="section on-startup">
<p>
When running in the background it's helpful to also have the browser
open on startup of your computer.
</p>
<div class="radio-item">
<input
type="checkbox"
id="onStart"
?checked=${this.settings.launch_on_startup == 1}
@change=${this.onLaunchOnStartupToggle}
/>
<label for="onStart">Launch Nomad on computer startup</label>
</div>
</div>`;
}

renderNewTabSettings() {
return html`
<div class="section">
Expand Down Expand Up @@ -425,7 +444,7 @@ class GeneralSettingsView extends LitElement {
};

return html` <div class="section">
<p>Set Beaker as the default browser for:</p>
<p>Set Nomad as the default browser for:</p>
${Object.keys(this.defaultProtocolSettings).map(
(proto) => html`
Expand Down Expand Up @@ -521,7 +540,7 @@ class GeneralSettingsView extends LitElement {
<ul>
<li>An anonymous ID</li>
<li>Your Beaker version, e.g. ${this.browserInfo.version}</li>
<li>Your Nomad version, e.g. ${this.browserInfo.version}</li>
<li>Your operating system, e.g. Windows 10</li>
</ul>
</div>
Expand Down Expand Up @@ -605,6 +624,13 @@ class GeneralSettingsView extends LitElement {
toast.create('Setting updated');
}

async onLaunchOnStartupToggle(e) {
const desiredState = e.target.checked;
beaker.browser.setSetting('launch_on_startup', desiredState ? 1 : 0);
await beaker.browser.setRunOnStartup(desiredState);
toast.create('Setting updated');
}

onNewTabChange(e) {
this.settings.new_tab = e.target.value;
beaker.browser.setSetting('new_tab', this.settings.new_tab);
Expand Down

0 comments on commit 37d815c

Please sign in to comment.