Skip to content

Commit

Permalink
fix: linux desktop file name and icon
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Apr 15, 2022
1 parent 51d9e75 commit 96f246a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion xmcl-electron-app/main/ElectronLauncherApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export default class ElectronLauncherApp extends LauncherApp {
})

if (this.platform.name === 'linux') {
await setLinuxProtocol(app.getPath('home'))
await setLinuxProtocol(app.getPath('home'), app.getPath('exe'))
}

await super.setup()
Expand Down
14 changes: 10 additions & 4 deletions xmcl-electron-app/main/utils/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path'

async function writeMimeList(mimesAppsList: string) {
if (!existsSync(mimesAppsList)) {
await writeFile(mimesAppsList, ['[Default Applications]', 'x-scheme-handler/xmcl=xmcl.desktop'].join('\n'))
await writeFile(mimesAppsList, ['[Default Applications]', 'x-scheme-handler/xmcl=xmcl.desktop;\n'].join('\n'))
return
}
const content = await readFile(mimesAppsList, 'utf-8')
Expand All @@ -13,15 +13,21 @@ async function writeMimeList(mimesAppsList: string) {
const defaultAppsHeaderIndex = lines.indexOf('[Default Applications]')
if (defaultAppsHeaderIndex === -1) {
lines.push('[Default Applications]')
lines.push('x-scheme-handler/xmcl=xmcl.desktop')
lines.push('x-scheme-handler/xmcl=xmcl.desktop;')
} else {
lines = [...lines.slice(0, defaultAppsHeaderIndex + 1), 'x-scheme-handler/xmcl=xmcl.desktop', ...lines.slice(defaultAppsHeaderIndex + 1)]
lines = [...lines.slice(0, defaultAppsHeaderIndex + 1), 'x-scheme-handler/xmcl=xmcl.desktop;', ...lines.slice(defaultAppsHeaderIndex + 1)]
}
await writeFile(mimesAppsList, lines.join('\n'))
}
}

export async function setLinuxProtocol(homePath: string) {
async function ensureDesktopFile(homePath: string, exePath: string) {
const desktopFile = join(homePath, '.local', 'share', 'applications', 'xmcl.desktop')
await writeFile(desktopFile, `[Desktop Entry]\nName=X Minecraft Launcher\nExec=${exePath} %u\nIcon=${exePath}\nType=Application\nMimeType=x-scheme-handler/xmcl;`)
}

export async function setLinuxProtocol(homePath: string, exePath: string) {
await ensureDesktopFile(homePath, exePath)
const mimesAppsList = join(homePath, '.config', 'mimeapps.list')
await writeMimeList(mimesAppsList)
.catch(() => writeMimeList(join(homePath, '.local', 'share', 'applications', 'mimeapps.list')))
Expand Down

0 comments on commit 96f246a

Please sign in to comment.