Skip to content

Commit

Permalink
Support adding desktop and start menu shortcuts on windows (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
arielj committed Nov 5, 2021
1 parent bba8226 commit e7ec3cb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 32 deletions.
97 changes: 67 additions & 30 deletions electron/legendary/games.ts
Expand Up @@ -6,7 +6,7 @@ import {
} from 'graceful-fs'
import axios from 'axios';

import { BrowserWindow, app } from 'electron';
import { BrowserWindow, app, shell } from 'electron';
import { DXVK } from '../dxvk'
import { ExtraInfo, GameStatus, InstallArgs } from '../types';
import { Game } from '../games';
Expand Down Expand Up @@ -243,6 +243,28 @@ class LegendaryGame extends Game {
return icon
}

private shortcutFiles(gameTitle: string) {
let desktopFile
let menuFile

switch (process.platform) {
case 'linux': {
desktopFile = `${app.getPath('desktop')}/${gameTitle}.desktop`
menuFile = `${home}/.local/share/applications/${gameTitle}.desktop`
break
}
case 'win32': {
desktopFile = `${app.getPath('desktop')}\\${gameTitle}.lnk`
menuFile = `${app.getPath('appData')}\\Microsoft\\Windows\\Start Menu\\Programs\\${gameTitle}.lnk`
break
}
default:
logError("Shortcuts haven't been implemented in the current platform.")
}

return [desktopFile, menuFile]
}

/**
* Adds a desktop shortcut to $HOME/Desktop and to /usr/share/applications
* so that the game can be opened from the start menu and the desktop folder.
Expand All @@ -251,45 +273,59 @@ class LegendaryGame extends Game {
* @public
*/
public async addDesktopShortcut(fromMenu?: boolean) {
if (process.platform !== 'linux') {
if (process.platform === 'darwin') {
return
}

const gameInfo = await this.getGameInfo()
const desktopFile = `${app.getPath('desktop')}/${gameInfo.title}.desktop`
const applicationsFolder = `${home}/.local/share/applications/${gameInfo.title}.desktop`
let shortcut;
const icon = await this.getIcon(gameInfo.app_name)
const launchWithProtocol = `heroic://launch/${gameInfo.app_name}`
const [ desktopFile, menuFile ] = this.shortcutFiles(gameInfo.title)
const { addDesktopShortcuts, addStartMenuShortcuts } = await GlobalConfig.get().getSettings()

switch (process.platform) {
case 'linux': {
shortcut = `[Desktop Entry]
const icon = await this.getIcon(gameInfo.app_name)
const shortcut = `[Desktop Entry]
Name=${gameInfo.title}
Exec=xdg-open heroic://launch/${gameInfo.app_name}
Exec=xdg-open ${launchWithProtocol}
Terminal=false
Type=Application
MimeType=x-scheme-handler/heroic;
Icon=${icon}
Categories=Game;
`
break;
}
default:
logError("Shortcuts haven't been implemented in the current platform.")
return

if (addDesktopShortcuts || fromMenu) {
writeFile(desktopFile, shortcut, () => {
logInfo('Shortcut saved on ' + desktopFile)
})
}
if (addStartMenuShortcuts || fromMenu) {
writeFile(menuFile, shortcut, () => {
logInfo('Shortcut saved on ' + menuFile)
})
}
break
}
const { addDesktopShortcuts, addStartMenuShortcuts } = await GlobalConfig.get().getSettings()
case 'win32': {
const shortcutOptions = {
target: launchWithProtocol,
icon: `${gameInfo.install.install_path}\\${gameInfo.install.executable}`,
iconIndex: 0
}

if (addDesktopShortcuts || fromMenu) {
writeFile(desktopFile, shortcut, () => {
logInfo('Shortcut saved on ' + desktopFile)
})
if (addDesktopShortcuts || fromMenu) {
shell.writeShortcutLink(desktopFile, shortcutOptions)
}

if (addStartMenuShortcuts || fromMenu) {
shell.writeShortcutLink(menuFile, shortcutOptions)
}
break
}
if (addStartMenuShortcuts || fromMenu) {
writeFile(applicationsFolder, shortcut, () => {
logInfo('Shortcut saved on ' + applicationsFolder)
})
default:
logError("Shortcuts haven't been implemented in the current platform.")
}
return
}

/**
Expand All @@ -298,14 +334,15 @@ Categories=Game;
* @public
*/
public async removeDesktopShortcut() {
if (process.platform !== 'linux') {
return
}
const gameInfo = await this.getGameInfo()
const desktopFile = `${app.getPath('desktop')}/${gameInfo.title}.desktop`
const applicationsFile = `${home}/.local/share/applications/${gameInfo.title}.desktop`
unlink(desktopFile, () => logInfo('Desktop shortcut removed'))
unlink(applicationsFile, () => logInfo('Applications shortcut removed'))
const [ desktopFile, menuFile ] = this.shortcutFiles(gameInfo.title)

if (desktopFile) {
unlink(desktopFile, () => logInfo('Desktop shortcut removed'))
}
if (menuFile) {
unlink(menuFile, () => logInfo('Applications shortcut removed'))
}
}

private getSdlList(sdlList: Array<string>){
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Game/GameSubMenu/index.tsx
Expand Up @@ -38,7 +38,7 @@ export default function GamesSubmenu({
ContextProvider
)
const isWin = platform === 'win32'
const isLinux = platform === 'linux'
const isMac = platform === 'darwin'
const [info, setInfo] = useState({prefix: '', wine: ''} as otherInfo)

const { t, i18n } = useTranslation('gamepage')
Expand Down Expand Up @@ -163,7 +163,7 @@ export default function GamesSubmenu({
>
{t('submenu.log')}
</span>
{isLinux && <span
{!isMac && <span
onClick={() => handleShortcuts()}
className="link"
>
Expand Down

0 comments on commit e7ec3cb

Please sign in to comment.