Skip to content

Commit

Permalink
Commands Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomhuel committed Jul 9, 2024
1 parent 898f23e commit 7dcbbb8
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist
out
.DS_Store
*.log*
pnpm-lock.yaml
pnpm-lock.yaml
package-lock.json
Binary file modified build/icon.ico
Binary file not shown.
Binary file modified build/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resources/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/main/app/mods/descompress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function descompress(args: any) {
console.log(args);
}
17 changes: 17 additions & 0 deletions src/main/eventHandler/eventHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ipcMain } from "electron";
import { descompress } from "../app/mods/descompress";

/**
* Event Handler (IPC events emitted by front)
* @param app Electron's Application
*/
export default function handleEvent() {
ipcMain.handle('hello', async (_, __) => {
console.log('Hello from back');
return __dirname;
});

ipcMain.handle('descompress', async (e, args) => {
descompress(args);
});
}
8 changes: 4 additions & 4 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import handleEvent from './eventHandler/eventHandler'

function createWindow(): void {
// Create the browser window.
Expand All @@ -10,7 +11,7 @@ function createWindow(): void {
height: 670,
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
icon,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
Expand All @@ -35,6 +36,8 @@ function createWindow(): void {
}
}

handleEvent();

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
Expand All @@ -49,9 +52,6 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window)
})

// IPC test
ipcMain.on('ping', () => console.log('pong'))

createWindow()

app.on('activate', function () {
Expand Down
23 changes: 18 additions & 5 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { contextBridge } from 'electron'
import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {}
const names = [
'hello',
'descompress',
'unable_mod'
];

function invoke(names: string[]) {
const obj: object = {};
names.forEach((name: string) => {
obj[name] = (...args) => ipcRenderer.invoke(name, args);
});
return obj;
}

const api = invoke(names);

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
try {
contextBridge.exposeInMainWorld('electron', electronAPI)
contextBridge.exposeInMainWorld('api', api)
contextBridge.exposeInMainWorld('electron', api)
// contextBridge.exposeInMainWorld('api', api)
} catch (error) {
console.error(error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<Modcard
modName="Hu Tao Programa"
active={index % 2 == 0 ? true : false}
realModName={`my_real_mod_name_${index}`}
realModName={`my_real_mod_name_${mod ?? index}`}
/>
{/each}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import Play from "../../../icons/play.svelte";
</script>

<button class="bg-primary flex justify-between items-center gap-1 text-light ps-3 pe-5 py-2 rounded-button my-3 transition-all duration-500 hover:bg-primary-darker">
<button class="bg-primary flex justify-between items-center gap-1 text-light ps-3 pe-5 py-2 rounded-button my-3 transition-all duration-500 hover:bg-primary-darker" on:click={async () => {
console.log(await window.electron.hello('hello'));
}}>
<Play className="w-9" />
<span class="font-yeon text-3xl min-h-full">Play</span>
</button>

0 comments on commit 7dcbbb8

Please sign in to comment.