From 086ee8ac045110f0efadebe5aad030e63f872c39 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:42:45 -0300 Subject: [PATCH] feat: create game shortcut --- package.json | 1 + src/main/events/index.ts | 1 + .../events/library/create-game-shortcut.ts | 26 +++++++++++++++++++ src/preload/index.ts | 2 ++ src/renderer/src/declaration.d.ts | 1 + .../modals/game-options-modal.tsx | 13 ++++++++++ yarn.lock | 9 ++++++- 7 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/main/events/library/create-game-shortcut.ts diff --git a/package.json b/package.json index d7b6fb9e..31bac590 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "classnames": "^2.5.1", "color": "^4.2.3", "color.js": "^1.2.0", + "create-desktop-shortcuts": "^1.11.0", "date-fns": "^3.6.0", "electron-log": "^5.1.4", "electron-updater": "^6.1.8", diff --git a/src/main/events/index.ts b/src/main/events/index.ts index 3d406b64..86e74fcb 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -10,6 +10,7 @@ import "./catalogue/search-games"; import "./catalogue/search-game-repacks"; import "./hardware/get-disk-free-space"; import "./library/add-game-to-library"; +import "./library/create-game-shortcut"; import "./library/close-game"; import "./library/delete-game-folder"; import "./library/get-game-by-object-id"; diff --git a/src/main/events/library/create-game-shortcut.ts b/src/main/events/library/create-game-shortcut.ts new file mode 100644 index 00000000..7ce37bee --- /dev/null +++ b/src/main/events/library/create-game-shortcut.ts @@ -0,0 +1,26 @@ +import { gameRepository } from "@main/repository"; +import { registerEvent } from "../register-event"; +import { IsNull, Not } from "typeorm"; +import createDesktopShortcut from "create-desktop-shortcuts"; + +const createGameShortcut = async ( + _event: Electron.IpcMainInvokeEvent, + id: number +): Promise => { + const game = await gameRepository.findOne({ + where: { id, executablePath: Not(IsNull()) }, + }); + + if (game) { + const filePath = game.executablePath; + return createDesktopShortcut({ + windows: { filePath }, + linux: { filePath }, + osx: { filePath }, + }); + } + + return false; +}; + +registerEvent("createGameShortcut", createGameShortcut); diff --git a/src/preload/index.ts b/src/preload/index.ts index 5e6d97af..3748baaa 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -74,6 +74,8 @@ contextBridge.exposeInMainWorld("electron", { shop, executablePath ), + createGameShortcut: (id: number) => + ipcRenderer.invoke("createGameShortcut", id), updateExecutablePath: (id: number, executablePath: string) => ipcRenderer.invoke("updateExecutablePath", id, executablePath), getLibrary: () => ipcRenderer.invoke("getLibrary"), diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 7f3fad3d..14d8bb80 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -59,6 +59,7 @@ declare global { shop: GameShop, executablePath: string | null ) => Promise; + createGameShortcut: (id: number) => Promise; updateExecutablePath: (id: number, executablePath: string) => Promise; getLibrary: () => Promise; openGameInstaller: (gameId: number) => Promise; diff --git a/src/renderer/src/pages/game-details/modals/game-options-modal.tsx b/src/renderer/src/pages/game-details/modals/game-options-modal.tsx index f23db9b1..106e725a 100644 --- a/src/renderer/src/pages/game-details/modals/game-options-modal.tsx +++ b/src/renderer/src/pages/game-details/modals/game-options-modal.tsx @@ -50,6 +50,10 @@ export function GameOptionsModal({ } }; + const handleCreateShortcut = async () => { + await window.electron.createGameShortcut(game.id); + }; + const handleDeleteGame = async () => { await removeGameInstaller(game.id); }; @@ -160,6 +164,15 @@ export function GameOptionsModal({ {t("open_download_options")} +
+ +
diff --git a/yarn.lock b/yarn.lock index 5ffbd1a1..f438ecf4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2403,6 +2403,13 @@ crc@^3.8.0: dependencies: buffer "^5.1.0" +create-desktop-shortcuts@^1.11.0: + version "1.11.0" + resolved "https://registry.yarnpkg.com/create-desktop-shortcuts/-/create-desktop-shortcuts-1.11.0.tgz#8eed89329e9bce70dece46d02a80573fe1f2536d" + integrity sha512-nmVtPVqNyMuAyMpDnd7l++hb2laqCWZXnHQaFhqGT1YEi2Ve3unu6QyuyIpGxAwIscNHcG1Ehnl+lFw6ygB2nQ== + dependencies: + which "2.0.2" + cross-fetch-ponyfill@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/cross-fetch-ponyfill/-/cross-fetch-ponyfill-1.0.3.tgz#5c5524e3bd3374e71d5016c2327e416369a57527" @@ -6278,7 +6285,7 @@ which-typed-array@^1.1.14, which-typed-array@^1.1.15, which-typed-array@^1.1.9: gopd "^1.0.1" has-tostringtag "^1.0.2" -which@^2.0.1: +which@2.0.2, which@^2.0.1: version "2.0.2" resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1" integrity sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==