Skip to content

Commit

Permalink
feat: setup tipc
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 18, 2024
1 parent a60ce0d commit 591b642
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"prepare": "husky"
},
"dependencies": {
"@egoist/tipc": "0.3.1",
"@electron-toolkit/preload": "^3.0.0",
"@electron-toolkit/utils": "^3.0.0",
"@fontsource/sn-pro": "5.0.1",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { app, BrowserWindow, ipcMain, ShareMenu } from "electron"
import path from "path"
import { electronApp, optimizer } from "@electron-toolkit/utils"
import { createWindow } from "./window"
import { registerIpcMain } from "@egoist/tipc/main"
import { router } from "./tipc"

registerIpcMain(router)

if (process.defaultApp) {
if (process.argv.length >= 2) {
Expand Down
46 changes: 46 additions & 0 deletions src/main/tipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { tipc } from "@egoist/tipc/main"
import { Menu } from "electron"

const t = tipc.create()

export const router = {
sum: t.procedure
.input<{ a: number; b: number }>()
.action(async ({ input }) => {
return input.a + input.b
}),
inspectElement: t.procedure
.input<{ x: number; y: number }>()
.action(async ({ input, context }) => {
context.sender.inspectElement(input.x, input.y)
}),
showContextMenu: t.procedure
.input<{
items: Array<{ type: "text"; label: string } | { type: "separator" }>
}>()
.action(async ({ input, context }) => {
const menu = Menu.buildFromTemplate(
input.items.map((item, index) => {
if (item.type === "separator") {
return {
type: "separator" as const,
}
}
return {
label: item.label,
click() {
context.sender.send("menu-click", index)
},
}
}),
)

menu.popup({
callback: () => {
context.sender.send("menu-closed")
},
})
}),
}

export type Router = typeof router
6 changes: 6 additions & 0 deletions src/renderer/src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createClient } from "@egoist/tipc/renderer"
import type { Router } from "@main/tipc"

export const client = createClient<Router>({
ipcInvoke: window.electron.ipcRenderer.invoke,
})
8 changes: 6 additions & 2 deletions tsconfig.web.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"src/renderer/src/env.d.ts",
"src/renderer/src/**/*",
"src/renderer/src/**/*.tsx",
"src/preload/*.d.ts"
"src/preload/*.d.ts",
"src/main/*.ts",
],
"compilerOptions": {
"composite": true,
Expand All @@ -13,7 +14,10 @@
"paths": {
"@renderer/*": [
"src/renderer/src/*"
]
],
"@main/*": [
"src/main/*"
],
}
}
}

0 comments on commit 591b642

Please sign in to comment.