Skip to content

Commit

Permalink
Add new --no-gui parameter (#1362)
Browse files Browse the repository at this point in the history
* Make handleProtocol search for the protocol string by itself

* Hide UI if --no-ui parameter is passed

* Exit after game launch if --no-ui is passed

* Rename "--no-ui" -> "--no-gui"
  • Loading branch information
CommandMC authored May 24, 2022
1 parent be15ad4 commit f10051f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
34 changes: 16 additions & 18 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ const isWindows = platform() === 'win32'
let mainWindow: BrowserWindow = null

async function createWindow(): Promise<BrowserWindow> {
const { exitToTray, startInTray } = await GlobalConfig.get().getSettings()
configStore.set('userHome', userHome)

let windowProps: Electron.Rectangle = {
Expand Down Expand Up @@ -139,7 +138,7 @@ async function createWindow(): Promise<BrowserWindow> {
...windowProps,
minHeight: 345,
minWidth: 600,
show: !(exitToTray && startInTray),
show: false,
webPreferences: {
webviewTag: true,
contextIsolation: false,
Expand Down Expand Up @@ -280,17 +279,7 @@ if (!gotTheLock) {
mainWindow.show()
}

// Figure out which argv element is our protocol
let heroicProtocolString = ''
argv.forEach((value) => {
if (value.startsWith('heroic://')) {
heroicProtocolString = value
}
})

if (heroicProtocolString) {
handleProtocol(mainWindow, heroicProtocolString)
}
handleProtocol(mainWindow, argv)
})
app.whenReady().then(async () => {
const systemInfo = await getSystemInfo()
Expand Down Expand Up @@ -364,7 +353,7 @@ if (!gotTheLock) {
await createWindow()

protocol.registerStringProtocol('heroic', (request, callback) => {
handleProtocol(mainWindow, request.url)
handleProtocol(mainWindow, [request.url])
callback('Operation initiated.')
})
if (!app.isDefaultProtocolClient('heroic')) {
Expand All @@ -376,11 +365,15 @@ if (!gotTheLock) {
} else {
logWarning('Protocol already registered.', LogPrefix.Backend)
}
if (process.argv[1]) {
const url = process.argv[1]
handleProtocol(mainWindow, url)

const { startInTray } = await GlobalConfig.get().getSettings()
const headless = process.argv.includes('--no-gui') || startInTray
if (!headless) {
mainWindow.show()
}

handleProtocol(mainWindow, process.argv)

// set initial zoom level after a moment, if set in sync the value stays as 1
setTimeout(() => {
const zoomFactor =
Expand Down Expand Up @@ -512,7 +505,7 @@ app.on('window-all-closed', () => {

app.on('open-url', (event, url) => {
event.preventDefault()
handleProtocol(mainWindow, url)
handleProtocol(mainWindow, [url])
})

ipcMain.on('openFolder', async (event, folder) => openUrlOrFile(folder))
Expand Down Expand Up @@ -895,6 +888,11 @@ Game Settings: ${JSON.stringify(gameSettings, null, '\t')}
runner,
status: 'done'
})

// Exit if we've been launched without UI
if (process.argv.includes('--no-gui')) {
app.exit()
}
})
}
)
Expand Down
11 changes: 10 additions & 1 deletion electron/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ import { Game, Runner } from './games'
import { logInfo, LogPrefix } from './logger/logger'
import i18next from 'i18next'

export async function handleProtocol(window: BrowserWindow, url: string) {
export async function handleProtocol(window: BrowserWindow, args: string[]) {
const mainWindow = BrowserWindow.getAllWindows()[0]

// Figure out which argv element is our protocol
let url = ''
args.forEach((val) => {
if (val.startsWith('heroic://')) {
url = val
}
})

const [scheme, path] = url.split('://')
if (!url || scheme !== 'heroic' || !path) {
return
Expand Down

0 comments on commit f10051f

Please sign in to comment.