Skip to content

Commit

Permalink
feat: Add option for Autostart in TSR Bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Sep 18, 2023
1 parent 0e8be1e commit 65962da
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/tsr-bridge/src/electron/storageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class StorageHandler extends EventEmitter {

superConductorHost: 'ws://127.0.0.1:5400',
bridgeId: protectString<BridgeId>(`${os.hostname()}_${shortID()}`),
autostart: false,
},
},
}
Expand Down
26 changes: 23 additions & 3 deletions apps/tsr-bridge/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { CURRENT_VERSION, TSRBridgeServer } from './electron/server'
import { IPCClient } from './electron/IPCClient'
import { StorageHandler } from './electron/storageHandler'
import { IPCServer } from './electron/IPCServer'
import { AppSystem } from './models/AppData'
import { AppData, AppSettings, AppSystem } from './models/AppData'
import os from 'os'

let isQuitting = false

const APP_NAME = 'SuperConductor TSR-Bridge'

// Keep a global reference to prevent garbage collection.
let tray: Tray

Expand Down Expand Up @@ -73,9 +75,10 @@ const createWindow = async (): Promise<void> => {

await win.loadURL(isDev ? 'http://localhost:9125' : `file://${app.getAppPath()}/dist/index.html`)

storage.on('appData', (appData) => {
storage.on('appData', (appData: AppData) => {
ipcClient.settings(appData.settings)
updateSystem()
onUpdatedSettings(appData.settings)
})

const assetsPath = app.isPackaged
Expand Down Expand Up @@ -104,7 +107,7 @@ const createWindow = async (): Promise<void> => {
},
])
tray.setContextMenu(trayContextMenu)
tray.setToolTip('TSR-Bridge')
tray.setToolTip(APP_NAME)

// Listen to and update the size and position of the app, so that it starts in the same place next time:
const updateSizeAndPosition = () => {
Expand Down Expand Up @@ -174,6 +177,23 @@ const createWindow = async (): Promise<void> => {
updateSystem()
}, 10000)

const prevSettings: Partial<AppSettings> = {}
function onUpdatedSettings(settings: AppSettings) {
const autostart = Boolean(settings.autostart)
if (prevSettings.autostart !== autostart) {
prevSettings.autostart = autostart

const loginItemSettings = app.getLoginItemSettings()

if (loginItemSettings.openAtLogin !== autostart) {
log.info(`Setting open at login to ${autostart}`)
app.setLoginItemSettings({
openAtLogin: autostart,
})
}
}
}

log.info('TSR-Bridge current version:', CURRENT_VERSION)

if (!server) {
Expand Down
5 changes: 5 additions & 0 deletions apps/tsr-bridge/src/models/AppData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface AppSettings {

superConductorHost: string
bridgeId: BridgeId

/**
* True if the app should start automatically upon system startup
*/
autostart: boolean
}
export type WindowPosition =
| {
Expand Down
15 changes: 15 additions & 0 deletions apps/tsr-bridge/src/react/components/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ export const Settings: React.FC<{
{settings.guiSettingsOpen ? <MdKeyboardArrowUp /> : <MdKeyboardArrowDown />}
</a>
<div className="content">
<div className="setting">
<div className="label">Autostart on system startup</div>
<div className="value">
<div className="sc-switch">
<Toggle
defaultChecked={settings.autostart}
onChange={() =>
updateSettings({
autostart: !settings.autostart,
})
}
/>
</div>
</div>
</div>
<div className="setting">
<div className="label">Mode select: Client / Server</div>
<div className="value">
Expand Down

0 comments on commit 65962da

Please sign in to comment.