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 c23f37e
Show file tree
Hide file tree
Showing 6 changed files with 340 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
27 changes: 13 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,12 @@ function initialize (isAppStart = true) {
breakPlanner.nextBreak()
}

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

startI18next()
startProcessWin()
createWelcomeWindow()
Expand Down Expand Up @@ -1458,14 +1466,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 +1495,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
70 changes: 70 additions & 0 deletions app/utils/autostartManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const log = require('electron-log/main')

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

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 {
this.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 Promise.resolve(this.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

0 comments on commit c23f37e

Please sign in to comment.