Skip to content

Commit

Permalink
Improve autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
hovancik committed Apr 27, 2024
1 parent e684682 commit 3508454
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 292 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Estonia and Belarus translations
- Updated flag of Belarus to White-Red-White
- advanced option to show break options (Skip, Pause, Reset) in Strict mode
- add autostart option for Linux

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

## [1.15.1] - 2023-11-19
### Fixed
Expand Down
26 changes: 12 additions & 14 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ const IdeasLoader = require('./utils/ideasLoader')
const BreaksPlanner = require('./breaksPlanner')
const AppIcon = require('./utils/appIcon')
const { UntilMorning } = require('./utils/untilMorning')
const AutostartManager = require('./utils/autostartManager')
const Command = require('./utils/commands')

let microbreakIdeas
let breakIdeas
let breakPlanner
let appIcon = null
let autostartManager = null
let processWin = null
let microbreakWins = null
let breakWins = null
Expand Down Expand Up @@ -223,6 +225,11 @@ function initialize (isAppStart = true) {
breakPlanner.nextBreak()
}

autostartManager = new AutostartManager({
platform: process.platform,
windowsStore: process.windowsStore
})

startI18next()
startProcessWin()
createWelcomeWindow()
Expand Down Expand Up @@ -1458,14 +1465,7 @@ ipcMain.on('save-setting', function (event, key, value) {
}

if (key === 'openAtLogin') {
if (process.platform === 'win32' && process.windowsStore) {
app.setLoginItemSettings({
openAtLogin: value,
path: 'start shell:appsFolder\\33881JanHovancik.stretchly_24fg4m0zq65je!Stretchly'
})
} else {
app.setLoginItemSettings({ openAtLogin: value })
}
autostartManager.autostartEnabled = value
} else {
settings.set(key, value)
}
Expand Down Expand Up @@ -1494,14 +1494,12 @@ ipcMain.on('restore-defaults', (event) => {
})
})

ipcMain.on('send-settings', function (event) {
event.sender.send('renderSettings', settingsToSend())
ipcMain.on('send-settings', async function (event) {
event.sender.send('renderSettings', await settingsToSend())
})

function settingsToSend () {
const loginItemSettings = app.getLoginItemSettings()
const openAtLogin = loginItemSettings.openAtLogin
return Object.assign({}, settings.store, { openAtLogin })
async function settingsToSend () {
return Object.assign({}, settings.store, { openAtLogin: await autostartManager.autoLaunchStatus() })
}

ipcMain.on('play-sound', function (event, sound) {
Expand Down
2 changes: 1 addition & 1 deletion app/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</a>
</nav>
<div class="settings">
<div class="linux-hidden store-hidden">
<div>
<input type="checkbox" value="openAtLogin" id="openAtLogin">
<label data-i18next="preferences.settings.openAtLogin" for="openAtLogin"></label>
</div>
Expand Down
69 changes: 69 additions & 0 deletions app/utils/autostartManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const remote = require('@electron/remote/main')
const log = require('electron-log/main')

class AutostartManager {
constructor ({
platform,
windowsStore
}) {
this.platform = platform
this.windowsStore = windowsStore
}

get autostartEnabled () {
if (this.platform === 'win32') {
if (this.windowsStore) {
return false
} else {
return true
}
} else if (this.platform === 'darwin') {
return true
} else if (this.platform === 'linux') {
return this.autoLaunchStatus()
} else {
return false
}
}

set autostartEnabled (value) {
if (this.platform === 'linux') {
value ? this._linuxAutoLaunch.enable() : this._linuxAutoLaunch.disable()
} else if (process.platform === 'win32' && process.windowsStore) {
value ? this._windowsStoreAutoLaunch.enable() : this._windowsStoreAutoLaunch.disable()
} else {
remote.app.setLoginItemSettings({ openAtLogin: value })
}
log.info(`Stretchly: setting autostart to ${value} on ${this.platform}${this.platform === 'win32' && this.windowsStore ? ' (Windows Store)' : ''}`)
}

async autoLaunchStatus () {
if (this.platform === 'linux') {
return await this._linuxAutoLaunch.isEnabled()
} else if (this.platform === 'win32' && this.windowsStore) {
return await this._windowsStoreAutoLaunch.isEnabled()
} else {
return remote.app.getLoginItemSettings().openAtLogin
}
}

get _linuxAutoLaunch () {
const AutoLaunch = require('auto-launch')
const stretchlyAutoLaunch = new AutoLaunch({
name: 'stretchly'
})
return stretchlyAutoLaunch
}

get _windowsStoreAutoLaunch () {
const AutoLaunch = require('auto-launch')
const stretchlyAutoLaunch = new AutoLaunch({
name: 'Stretchly',
path: '33881JanHovancik.stretchly_24fg4m0zq65je!Stretchly',
isHidden: true
})
return stretchlyAutoLaunch
}
}

module.exports = AutostartManager
Loading

0 comments on commit 3508454

Please sign in to comment.