Skip to content

Commit f5b2db2

Browse files
committed
Reformat and add example IPC
1 parent 0255502 commit f5b2db2

File tree

12 files changed

+124
-102
lines changed

12 files changed

+124
-102
lines changed

.vscode/extensions.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
{
2-
"recommendations": [
3-
"dbaeumer.vscode-eslint",
4-
"esbenp.prettier-vscode"
5-
]
6-
}
2+
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
3+
}

electron-builder.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ productName: slate
33
directories:
44
buildResources: build
55
files:
6-
- '!**/.vscode/*'
7-
- '!src/*'
8-
- '!electron.vite.config.{js,ts,mjs,cjs}'
9-
- '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
10-
- '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'
11-
- '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}'
6+
- "!**/.vscode/*"
7+
- "!src/*"
8+
- "!electron.vite.config.{js,ts,mjs,cjs}"
9+
- "!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}"
10+
- "!{.env,.env.*,.npmrc,pnpm-lock.yaml}"
11+
- "!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}"
1212
asarUnpack:
1313
- resources/**
1414
win:

electron.vite.config.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { resolve } from 'path'
2-
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
3-
import react from '@vitejs/plugin-react'
1+
import { resolve } from "path";
2+
import { defineConfig, externalizeDepsPlugin } from "electron-vite";
3+
import react from "@vitejs/plugin-react";
44

55
export default defineConfig({
66
main: {
7-
plugins: [externalizeDepsPlugin()]
7+
plugins: [externalizeDepsPlugin()],
88
},
99
preload: {
10-
plugins: [externalizeDepsPlugin()]
10+
plugins: [externalizeDepsPlugin()],
1111
},
1212
renderer: {
1313
resolve: {
1414
alias: {
15-
'@renderer': resolve('src/renderer/src')
16-
}
15+
"@renderer": resolve("src/renderer/src"),
16+
},
1717
},
18-
plugins: [react()]
19-
}
20-
})
18+
plugins: [react()],
19+
},
20+
});

src/main/index.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { app, shell, BrowserWindow } from 'electron'
2-
import { join } from 'path'
3-
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
4-
import icon from '../../resources/icon.png?asset'
1+
import { app, shell, BrowserWindow, ipcMain } from "electron";
2+
import { join } from "path";
3+
import { electronApp, optimizer, is } from "@electron-toolkit/utils";
4+
import icon from "../../resources/icon.png?asset";
55

66
function createWindow(): void {
77
// Create the browser window.
@@ -10,28 +10,28 @@ function createWindow(): void {
1010
height: 670,
1111
show: false,
1212
autoHideMenuBar: true,
13-
...(process.platform === 'linux' ? { icon } : {}),
13+
...(process.platform === "linux" ? { icon } : {}),
1414
webPreferences: {
15-
preload: join(__dirname, '../preload/index.js'),
16-
sandbox: false
17-
}
18-
})
15+
preload: join(__dirname, "../preload/index.js"),
16+
sandbox: false,
17+
},
18+
});
1919

20-
mainWindow.on('ready-to-show', () => {
21-
mainWindow.show()
22-
})
20+
mainWindow.on("ready-to-show", () => {
21+
mainWindow.show();
22+
});
2323

2424
mainWindow.webContents.setWindowOpenHandler((details) => {
25-
shell.openExternal(details.url)
26-
return { action: 'deny' }
27-
})
25+
shell.openExternal(details.url);
26+
return { action: "deny" };
27+
});
2828

2929
// HMR for renderer base on electron-vite cli.
3030
// Load the remote URL for development or the local html file for production.
31-
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
32-
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
31+
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
32+
mainWindow.loadURL(process.env["ELECTRON_RENDERER_URL"]);
3333
} else {
34-
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
34+
mainWindow.loadFile(join(__dirname, "../renderer/index.html"));
3535
}
3636
}
3737

@@ -40,32 +40,37 @@ function createWindow(): void {
4040
// Some APIs can only be used after this event occurs.
4141
app.whenReady().then(() => {
4242
// Set app user model id for windows
43-
electronApp.setAppUserModelId('com.electron')
43+
electronApp.setAppUserModelId("com.electron");
4444

4545
// Default open or close DevTools by F12 in development
4646
// and ignore CommandOrControl + R in production.
4747
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
48-
app.on('browser-window-created', (_, window) => {
49-
optimizer.watchWindowShortcuts(window)
50-
})
48+
app.on("browser-window-created", (_, window) => {
49+
optimizer.watchWindowShortcuts(window);
50+
});
5151

52-
createWindow()
52+
createWindow();
5353

54-
app.on('activate', function () {
54+
app.on("activate", function () {
5555
// On macOS it's common to re-create a window in the app when the
5656
// dock icon is clicked and there are no other windows open.
57-
if (BrowserWindow.getAllWindows().length === 0) createWindow()
58-
})
59-
})
57+
if (BrowserWindow.getAllWindows().length === 0) createWindow();
58+
});
59+
});
6060

6161
// Quit when all windows are closed, except on macOS. There, it's common
6262
// for applications and their menu bar to stay active until the user quits
6363
// explicitly with Cmd + Q.
64-
app.on('window-all-closed', () => {
65-
if (process.platform !== 'darwin') {
66-
app.quit()
64+
app.on("window-all-closed", () => {
65+
if (process.platform !== "darwin") {
66+
app.quit();
6767
}
68-
})
68+
});
6969

7070
// In this file you can include the rest of your app"s specific main process
7171
// code. You can also put them in separate files and require them here.
72+
73+
// "server-side API"
74+
ipcMain.handle("ping", () => {
75+
return "pong";
76+
});

src/preload/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ElectronAPI } from '@electron-toolkit/preload'
1+
import { ElectronAPI } from "@electron-toolkit/preload";
22

33
declare global {
44
interface Window {
5-
electron: ElectronAPI
6-
api: unknown
5+
electron: ElectronAPI;
6+
api: unknown;
77
}
88
}

src/preload/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { contextBridge } from 'electron'
2-
import { electronAPI } from '@electron-toolkit/preload'
1+
import { contextBridge } from "electron";
2+
import { electronAPI } from "@electron-toolkit/preload";
33

44
// Custom APIs for renderer
5-
const api = {}
5+
const api = {};
66

77
// Use `contextBridge` APIs to expose Electron APIs to
88
// renderer only if context isolation is enabled, otherwise
99
// just add to the DOM global.
1010
if (process.contextIsolated) {
1111
try {
12-
contextBridge.exposeInMainWorld('electron', electronAPI)
13-
contextBridge.exposeInMainWorld('api', api)
12+
contextBridge.exposeInMainWorld("electron", electronAPI);
13+
contextBridge.exposeInMainWorld("api", api);
1414
} catch (error) {
15-
console.error(error)
15+
console.error(error);
1616
}
1717
} else {
1818
// @ts-ignore (define in dts)
19-
window.electron = electronAPI
19+
window.electron = electronAPI;
2020
// @ts-ignore (define in dts)
21-
window.api = api
21+
window.api = api;
2222
}

src/renderer/src/App.tsx

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import Versions from './components/Versions'
2-
import icons from './assets/icons.svg'
1+
import Versions from "./components/Versions";
2+
import icons from "./assets/icons.svg";
3+
import { useEffect } from "react";
34

45
function App(): JSX.Element {
6+
useEffect(() => {
7+
// call a "server API" (defined at bottom of main.js right now)
8+
window.electron.ipcRenderer.invoke("ping", "hello").then(console.log);
9+
}, []);
10+
511
return (
612
<div className="container">
713
<Versions></Versions>
@@ -10,15 +16,20 @@ function App(): JSX.Element {
1016
<use xlinkHref={`${icons}#electron`} />
1117
</svg>
1218
<h2 className="hero-text">
13-
You{"'"}ve successfully created an Electron project with React and TypeScript
19+
You{"'"}ve successfully created an Electron project with React and
20+
TypeScript
1421
</h2>
1522
<p className="hero-tagline">
1623
Please try pressing <code>F12</code> to open the devTool
1724
</p>
1825

1926
<div className="links">
2027
<div className="link-item">
21-
<a target="_blank" href="https://electron-vite.org" rel="noopener noreferrer">
28+
<a
29+
target="_blank"
30+
href="https://electron-vite.org"
31+
rel="noopener noreferrer"
32+
>
2233
Documentation
2334
</a>
2435
</div>
@@ -49,8 +60,12 @@ function App(): JSX.Element {
4960
<article>
5061
<h2 className="title">Configuring</h2>
5162
<p className="detail">
52-
Config with <span>electron.vite.config.ts</span> and refer to the{' '}
53-
<a target="_blank" href="https://electron-vite.org/config" rel="noopener noreferrer">
63+
Config with <span>electron.vite.config.ts</span> and refer to the{" "}
64+
<a
65+
target="_blank"
66+
href="https://electron-vite.org/config"
67+
rel="noopener noreferrer"
68+
>
5469
config guide
5570
</a>
5671
.
@@ -61,7 +76,7 @@ function App(): JSX.Element {
6176
<article>
6277
<h2 className="title">HMR</h2>
6378
<p className="detail">
64-
Edit <span>src/renderer</span> files to test HMR. See{' '}
79+
Edit <span>src/renderer</span> files to test HMR. See{" "}
6580
<a
6681
target="_blank"
6782
href="https://electron-vite.org/guide/hmr.html"
@@ -77,11 +92,11 @@ function App(): JSX.Element {
7792
<article>
7893
<h2 className="title">Hot Reloading</h2>
7994
<p className="detail">
80-
Run{' '}
95+
Run{" "}
8196
<span>
8297
{"'"}electron-vite dev --watch{"'"}
83-
</span>{' '}
84-
to enable. See{' '}
98+
</span>{" "}
99+
to enable. See{" "}
85100
<a
86101
target="_blank"
87102
href="https://electron-vite.org/guide/hot-reloading.html"
@@ -97,7 +112,7 @@ function App(): JSX.Element {
97112
<article>
98113
<h2 className="title">Debugging</h2>
99114
<p className="detail">
100-
Check out <span>.vscode/launch.json</span>. See{' '}
115+
Check out <span>.vscode/launch.json</span>. See{" "}
101116
<a
102117
target="_blank"
103118
href="https://electron-vite.org/guide/debugging.html"
@@ -113,7 +128,7 @@ function App(): JSX.Element {
113128
<article>
114129
<h2 className="title">Source Code Protection</h2>
115130
<p className="detail">
116-
Supported via built-in plugin <span>bytecodePlugin</span>. See{' '}
131+
Supported via built-in plugin <span>bytecodePlugin</span>. See{" "}
117132
<a
118133
target="_blank"
119134
href="https://electron-vite.org/guide/source-code-protection.html"
@@ -129,17 +144,21 @@ function App(): JSX.Element {
129144
<article>
130145
<h2 className="title">Packaging</h2>
131146
<p className="detail">
132-
Use{' '}
133-
<a target="_blank" href="https://www.electron.build" rel="noopener noreferrer">
147+
Use{" "}
148+
<a
149+
target="_blank"
150+
href="https://www.electron.build"
151+
rel="noopener noreferrer"
152+
>
134153
electron-builder
135-
</a>{' '}
154+
</a>{" "}
136155
and pre-configured to pack your app.
137156
</p>
138157
</article>
139158
</div>
140159
</div>
141160
</div>
142-
)
161+
);
143162
}
144163

145-
export default App
164+
export default App;

src/renderer/src/assets/index.css

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ body {
55
Roboto,
66
-apple-system,
77
BlinkMacSystemFont,
8-
'Helvetica Neue',
9-
'Segoe UI',
10-
'Oxygen',
11-
'Ubuntu',
12-
'Cantarell',
13-
'Open Sans',
8+
"Helvetica Neue",
9+
"Segoe UI",
10+
"Oxygen",
11+
"Ubuntu",
12+
"Cantarell",
13+
"Open Sans",
1414
sans-serif;
1515
color: #86a5b1;
1616
background-color: #2f3241;
@@ -67,7 +67,7 @@ a:hover {
6767
float: none;
6868
clear: both;
6969
overflow: hidden;
70-
font-family: 'Menlo', 'Lucida Console', monospace;
70+
font-family: "Menlo", "Lucida Console", monospace;
7171
color: #c2f5ff;
7272
line-height: 1;
7373
transition: all 0.3s;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useState } from 'react'
1+
import { useState } from "react";
22

33
function Versions(): JSX.Element {
4-
const [versions] = useState(window.electron.process.versions)
4+
const [versions] = useState(window.electron.process.versions);
55

66
return (
77
<ul className="versions">
@@ -10,7 +10,7 @@ function Versions(): JSX.Element {
1010
<li className="node-version">Node v{versions.node}</li>
1111
<li className="v8-version">V8 v{versions.v8}</li>
1212
</ul>
13-
)
13+
);
1414
}
1515

16-
export default Versions
16+
export default Versions;

src/renderer/src/main.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom/client'
3-
import './assets/index.css'
4-
import App from './App'
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import "./assets/index.css";
4+
import App from "./App";
55

6-
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
6+
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
77
<React.StrictMode>
88
<App />
9-
</React.StrictMode>
10-
)
9+
</React.StrictMode>,
10+
);

0 commit comments

Comments
 (0)