Skip to content

Commit c23f37e

Browse files
committed
Improve autostart
1 parent e684682 commit c23f37e

File tree

6 files changed

+340
-292
lines changed

6 files changed

+340
-292
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
- Estonia and Belarus translations
1010
- Updated flag of Belarus to White-Red-White
1111
- advanced option to show break options (Skip, Pause, Reset) in Strict mode
12+
- add autostart option for Linux
1213

1314
### Changed
1415
- updated many translations
@@ -20,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2021
- multiple RTL UI issues
2122
- RPM installer conflicts with other Electron apps
2223
- improve break window loading to fix blank window
24+
- autostart option for Windows Store
2325

2426
## [1.15.1] - 2023-11-19
2527
### Fixed

app/main.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ const IdeasLoader = require('./utils/ideasLoader')
3737
const BreaksPlanner = require('./breaksPlanner')
3838
const AppIcon = require('./utils/appIcon')
3939
const { UntilMorning } = require('./utils/untilMorning')
40+
const AutostartManager = require('./utils/autostartManager')
4041
const Command = require('./utils/commands')
4142

4243
let microbreakIdeas
4344
let breakIdeas
4445
let breakPlanner
4546
let appIcon = null
47+
let autostartManager = null
4648
let processWin = null
4749
let microbreakWins = null
4850
let breakWins = null
@@ -223,6 +225,12 @@ function initialize (isAppStart = true) {
223225
breakPlanner.nextBreak()
224226
}
225227

228+
autostartManager = new AutostartManager({
229+
platform: process.platform,
230+
windowsStore: process.windowsStore,
231+
app
232+
})
233+
226234
startI18next()
227235
startProcessWin()
228236
createWelcomeWindow()
@@ -1458,14 +1466,7 @@ ipcMain.on('save-setting', function (event, key, value) {
14581466
}
14591467

14601468
if (key === 'openAtLogin') {
1461-
if (process.platform === 'win32' && process.windowsStore) {
1462-
app.setLoginItemSettings({
1463-
openAtLogin: value,
1464-
path: 'start shell:appsFolder\\33881JanHovancik.stretchly_24fg4m0zq65je!Stretchly'
1465-
})
1466-
} else {
1467-
app.setLoginItemSettings({ openAtLogin: value })
1468-
}
1469+
autostartManager.autostartEnabled = value
14691470
} else {
14701471
settings.set(key, value)
14711472
}
@@ -1494,14 +1495,12 @@ ipcMain.on('restore-defaults', (event) => {
14941495
})
14951496
})
14961497

1497-
ipcMain.on('send-settings', function (event) {
1498-
event.sender.send('renderSettings', settingsToSend())
1498+
ipcMain.on('send-settings', async function (event) {
1499+
event.sender.send('renderSettings', await settingsToSend())
14991500
})
15001501

1501-
function settingsToSend () {
1502-
const loginItemSettings = app.getLoginItemSettings()
1503-
const openAtLogin = loginItemSettings.openAtLogin
1504-
return Object.assign({}, settings.store, { openAtLogin })
1502+
async function settingsToSend () {
1503+
return Object.assign({}, settings.store, { openAtLogin: await autostartManager.autoLaunchStatus() })
15051504
}
15061505

15071506
ipcMain.on('play-sound', function (event, sound) {

app/preferences.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</a>
4545
</nav>
4646
<div class="settings">
47-
<div class="linux-hidden store-hidden">
47+
<div>
4848
<input type="checkbox" value="openAtLogin" id="openAtLogin">
4949
<label data-i18next="preferences.settings.openAtLogin" for="openAtLogin"></label>
5050
</div>

app/utils/autostartManager.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const log = require('electron-log/main')
2+
3+
class AutostartManager {
4+
constructor ({
5+
platform,
6+
windowsStore,
7+
app
8+
}) {
9+
this.platform = platform
10+
this.windowsStore = windowsStore
11+
this.app = app
12+
}
13+
14+
get autostartEnabled () {
15+
if (this.platform === 'win32') {
16+
if (this.windowsStore) {
17+
return false
18+
} else {
19+
return true
20+
}
21+
} else if (this.platform === 'darwin') {
22+
return true
23+
} else if (this.platform === 'linux') {
24+
return this.autoLaunchStatus()
25+
} else {
26+
return false
27+
}
28+
}
29+
30+
set autostartEnabled (value) {
31+
if (this.platform === 'linux') {
32+
value ? this._linuxAutoLaunch.enable() : this._linuxAutoLaunch.disable()
33+
} else if (process.platform === 'win32' && process.windowsStore) {
34+
value ? this._windowsStoreAutoLaunch.enable() : this._windowsStoreAutoLaunch.disable()
35+
} else {
36+
this.app.setLoginItemSettings({ openAtLogin: value })
37+
}
38+
log.info(`Stretchly: setting autostart to ${value} on ${this.platform}${this.platform === 'win32' && this.windowsStore ? ' (Windows Store)' : ''}`)
39+
}
40+
41+
async autoLaunchStatus () {
42+
if (this.platform === 'linux') {
43+
return await this._linuxAutoLaunch.isEnabled()
44+
} else if (this.platform === 'win32' && this.windowsStore) {
45+
return await this._windowsStoreAutoLaunch.isEnabled()
46+
} else {
47+
return Promise.resolve(this.app.getLoginItemSettings().openAtLogin)
48+
}
49+
}
50+
51+
get _linuxAutoLaunch () {
52+
const AutoLaunch = require('auto-launch')
53+
const stretchlyAutoLaunch = new AutoLaunch({
54+
name: 'stretchly'
55+
})
56+
return stretchlyAutoLaunch
57+
}
58+
59+
get _windowsStoreAutoLaunch () {
60+
const AutoLaunch = require('auto-launch')
61+
const stretchlyAutoLaunch = new AutoLaunch({
62+
name: 'Stretchly',
63+
path: '33881JanHovancik.stretchly_24fg4m0zq65je!Stretchly',
64+
isHidden: true
65+
})
66+
return stretchlyAutoLaunch
67+
}
68+
}
69+
70+
module.exports = AutostartManager

0 commit comments

Comments
 (0)