Skip to content

Commit

Permalink
Improve autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
hovancik committed May 9, 2024
1 parent 1367a36 commit a3c0fdc
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 298 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
39 changes: 19 additions & 20 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 @@ -163,7 +165,7 @@ app.on('before-quit', () => {
globalShortcut.unregisterAll()
})

function initialize (isAppStart = true) {
async function initialize (isAppStart = true) {
if (!gotTheLock) {
return
}
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 All @@ -244,13 +252,13 @@ function initialize (isAppStart = true) {
})
startPowerMonitoring()
if (preferencesWin) {
preferencesWin.send('renderSettings', settingsToSend())
preferencesWin.send('renderSettings', await settingsToSend())
}
if (welcomeWin) {
welcomeWin.send('renderSettings', settingsToSend())
welcomeWin.send('renderSettings', await settingsToSend())
}
if (contributorPreferencesWindow) {
contributorPreferencesWindow.send('renderSettings', settingsToSend())
contributorPreferencesWindow.send('renderSettings', await settingsToSend())
}
globalShortcut.unregisterAll()
if (settings.get('pauseBreaksToggleShortcut') !== '') {
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.setAutostartEnabled(value)
} else {
settings.set(key, value)
}
Expand All @@ -1484,24 +1485,22 @@ ipcMain.on('restore-defaults', (event) => {
message: i18next.t('main.warning'),
buttons: [i18next.t('main.continue'), i18next.t('main.cancel')]
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
dialog.showMessageBox(dialogOpts).then(async (returnValue) => {
if (returnValue.response === 0) {
log.info('Stretchly: restoring default settings')
settings.store = Object.assign(require('./utils/defaultSettings'), { isFirstRun: false })
initialize(false)
event.sender.send('renderSettings', settingsToSend())
event.sender.send('renderSettings', await settingsToSend())
}
})
})

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
54 changes: 54 additions & 0 deletions app/utils/autostartManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const log = require('electron-log/main')

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

setAutostartEnabled (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
Loading

0 comments on commit a3c0fdc

Please sign in to comment.