Skip to content

Commit efbc347

Browse files
committed
move flatpak portal management code entirely to it, other small fixes
Signed-off-by: Andrew Bernal <[email protected]>
1 parent 75fbb64 commit efbc347

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

app/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ async function initialize (isAppStart = true) {
348348
} catch (error) {
349349
log.error('Stretchly: error creating images directory', error)
350350
}
351+
}
351352
// Initialize portal early for Flatpak so it's ready when user opens preferences
352353
if (process.platform === 'linux' && insideFlatpak()) {
353354
autostartManager.flatpakPortalManager.initialize().catch(err => {

app/utils/autostartManager.js

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,23 @@ class AutostartManager {
1313
this.platform = platform
1414
this.windowsStore = windowsStore
1515
this.app = app
16-
this.settings = settings
17-
this.flatpakPortalManager = new FlatpakPortalManager()
1816

19-
// Decide the Linux autostart strategy once during construction
20-
if (this.platform === 'linux') {
21-
if (insideFlatpak()) {
22-
this._linuxAutoLaunch = this._createFlatpakAutostarter()
23-
} else {
24-
this._linuxAutoLaunch = new AutoLaunch({ name: 'stretchly' })
25-
}
26-
}
27-
}
17+
this.isFlatpak = this.platform === 'linux' && insideFlatpak()
2818

29-
_createFlatpakAutostarter () {
30-
return {
31-
enable: async () => {
32-
try {
33-
await this.flatpakPortalManager.initialize()
34-
await this.flatpakPortalManager.setAutostart(true)
35-
this.settings.set('flatpakAutostart', true)
36-
} catch (error) {
37-
log.error('Stretchly: Failed to set autostart (enable) via XDG Portal', error)
38-
}
39-
},
40-
disable: async () => {
41-
try {
42-
await this.flatpakPortalManager.initialize()
43-
await this.flatpakPortalManager.setAutostart(false)
44-
this.settings.set('flatpakAutostart', false)
45-
} catch (error) {
46-
log.error('Stretchly: Failed to set autostart (disable) via XDG Portal', error)
47-
}
48-
},
49-
isEnabled: async () => {
50-
// XDG portals don't provide a reliable query method, so we read from our cache
51-
return Promise.resolve(this.settings.get('flatpakAutostart', false))
52-
}
19+
if (this.isFlatpak) {
20+
this.flatpakPortalManager = new FlatpakPortalManager(settings)
21+
} else if (this.platform === 'linux') {
22+
this.nativeAutoLauncher = new AutoLaunch({ name: 'stretchly' })
5323
}
5424
}
5525

5626
async setAutostartEnabled (value) {
57-
log.info(`Stretchly: setting autostart to ${value} on ${this.platform}${this.platform === 'win32' && this.windowsStore ? ' (Windows Store)' : ''}${insideFlatpak() && this.platform === 'linux' ? ' (Flatpak)' : ''}`)
58-
if (this.platform === 'linux') {
59-
await (value ? this._linuxAutoLaunch.enable() : this._linuxAutoLaunch.disable())
27+
log.info(`Stretchly: setting autostart to ${value} on ${this.platform}${this.platform === 'win32' && this.windowsStore ? ' (Windows Store)' : ''}${this.isFlatpak ? ' (Flatpak)' : ''}`)
28+
29+
if (this.isFlatpak) {
30+
await (value ? this.flatpakPortalManager.enableAutostart() : this.flatpakPortalManager.disableAutostart())
31+
} else if (this.platform === 'linux') {
32+
await (value ? this.nativeAutoLauncher.enable() : this.nativeAutoLauncher.disable())
6033
} else if (this.platform === 'win32' && this.windowsStore) {
6134
await (value ? this._windowsStoreAutoLaunch.enable() : this._windowsStoreAutoLaunch.disable())
6235
} else {
@@ -65,8 +38,10 @@ class AutostartManager {
6538
}
6639

6740
async autoLaunchStatus () {
68-
if (this.platform === 'linux') {
69-
return await this._linuxAutoLaunch.isEnabled()
41+
if (this.isFlatpak) {
42+
return await this.flatpakPortalManager.isAutostartEnabled()
43+
} else if (this.platform === 'linux') {
44+
return await this.nativeAutoLauncher.isEnabled()
7045
} else if (this.platform === 'win32' && this.windowsStore) {
7146
return await this._windowsStoreAutoLaunch.isEnabled()
7247
} else {

app/utils/defaultSettings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,6 @@ export default {
8080
hidePreferencesFileLocation: false,
8181
hideStrictModePreferences: false,
8282
miniBreakManualFinish: false,
83-
longBreakManualFinish: false
83+
longBreakManualFinish: false,
84+
flatpakAutostart: false
8485
}

app/utils/flatpakPortalManager.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import log from 'electron-log/main.js'
44
const { Variant } = dbus
55

66
class FlatpakPortalManager {
7-
constructor () {
7+
constructor (settings) {
8+
this.settings = settings
89
this.bus = null
910
this.portal = null
1011
this.initialized = false
@@ -34,7 +35,6 @@ class FlatpakPortalManager {
3435
} catch (error) {
3536
log.error('Stretchly: Failed to initialize XDG Background Portal:', error)
3637
this.initialized = false
37-
throw error
3838
}
3939
}
4040

@@ -46,6 +46,11 @@ class FlatpakPortalManager {
4646
async setAutostart (enabled) {
4747
await this.initialize()
4848

49+
if (!this.initialized) {
50+
log.error('Stretchly: Cannot set autostart - portal not initialized')
51+
return false
52+
}
53+
4954
try {
5055
const background = this.portal.getInterface('org.freedesktop.portal.Background')
5156
const handleToken = `stretchly_autostart_${Date.now()}_${Math.random().toString(36).substring(7)}`
@@ -131,10 +136,33 @@ class FlatpakPortalManager {
131136
})
132137
} catch (error) {
133138
log.error(`Stretchly: Failed to set autostart=${enabled} via XDG Portal:`, error)
134-
throw error
139+
return false
140+
}
141+
}
142+
143+
async enableAutostart () {
144+
try {
145+
await this.setAutostart(true)
146+
this.settings.set('flatpakAutostart', true)
147+
} catch (error) {
148+
log.error('Stretchly: Failed to set autostart (enable) via XDG Portal', error)
135149
}
136150
}
137151

152+
async disableAutostart () {
153+
try {
154+
await this.setAutostart(false)
155+
this.settings.set('flatpakAutostart', false)
156+
} catch (error) {
157+
log.error('Stretchly: Failed to set autostart (disable) via XDG Portal', error)
158+
}
159+
}
160+
161+
async isAutostartEnabled () {
162+
// XDG portals don't provide a reliable query method, so we read from our cache
163+
return this.settings.get('flatpakAutostart')
164+
}
165+
138166
disconnect () {
139167
if (this.bus) {
140168
this.bus.disconnect()

0 commit comments

Comments
 (0)