Skip to content

Commit

Permalink
feat: ipc
Browse files Browse the repository at this point in the history
  • Loading branch information
QC2168 committed Feb 24, 2023
1 parent 14f2df0 commit 1fa265e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
28 changes: 16 additions & 12 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* eslint-disable import/prefer-default-export */
import {
app, BrowserWindow, ipcMain,
} from 'electron';
import Mib from '@qc2168/mib';
import { app, BrowserWindow, ipcMain } from 'electron';
import { release } from 'os';
// import installExtension, {
// REACT_DEVELOPER_TOOLS,
// } from 'electron-devtools-installer';

const { join } = require('path');

const preload = join(__dirname, './preload.js');

const mibInstance = new Mib();

// Disable GPU Acceleration for Windows 7
if (release().startsWith('6.1')) app.disableHardwareAcceleration();

Expand Down Expand Up @@ -41,15 +45,16 @@ async function createWindow() {
win = new BrowserWindow({
title: 'Main window',
icon: join(ROOT_PATH.public, 'favicon.svg'),
frame: false,
frame: true,
minWidth: 970,
minHeight: 580,
width: 970,
height: 580,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: false,
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
},
});
if (app.isPackaged) {
Expand All @@ -71,9 +76,7 @@ async function createWindow() {
// });
}

app
.whenReady()
.then(createWindow);
app.whenReady().then(createWindow);
// .then(() => {
// installExtension(REACT_DEVELOPER_TOOLS.id)
// .then((name) => console.log(`Added Extension: ${name}`))
Expand Down Expand Up @@ -105,8 +108,7 @@ app.on('activate', () => {
// new window example arg: new windows url
ipcMain.handle('open-win', (event, arg) => {
const childWindow = new BrowserWindow({
webPreferences: {
},
webPreferences: {},
});

if (app.isPackaged) {
Expand All @@ -128,3 +130,5 @@ ipcMain.handle('minimize-win', () => {
ipcMain.handle('maximize-win', () => {
win.maximize();
});

ipcMain.handle('mibInstance', () => mibInstance);
22 changes: 21 additions & 1 deletion electron/preload/index.ts → electron/preload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-undef */
/* eslint-disable no-unused-expressions */
/* eslint-disable react-hooks/rules-of-hooks */
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
function domReady(
condition: DocumentReadyState[] = ['complete', 'interactive'],
) {
return new Promise((resolve) => {
if (condition.includes(document.readyState)) {
resolve(true);
Expand Down Expand Up @@ -94,3 +96,21 @@ window.onmessage = (ev) => {
};

setTimeout(removeLoading, 4999);

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('versions', {
node: () => process.versions.node,
chrome: () => process.versions.chrome,
electron: () => process.versions.electron,
});

contextBridge.exposeInMainWorld('win', {
close: () => ipcRenderer.invoke('close-win'),
minimize: () => ipcRenderer.invoke('minimize-win'),
maximize: () => ipcRenderer.invoke('maximize-win'),
});

contextBridge.exposeInMainWorld('core', {
instance: () => ipcRenderer.invoke('mibInstance'),
});
17 changes: 17 additions & 0 deletions src/renderer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type Mib from '@qc2168/mib';

export interface WinApi {
close: () => Promise<void>,
minimize: () => Promise<void>,
maximize: () => Promise<void>,
}
export interface MibApi {
instance: () => Promise<Mib>,
}

declare global {
interface Window {
win: WinApi;
core: MibApi;
}
}
35 changes: 9 additions & 26 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,21 @@ export default defineConfig({
},
plugins: [
react(),
electron({
electron([{
entry: 'electron/main.ts',
vite: {
build: {
sourcemap: true,
},
},
// main: {
// entry: 'electron/main/main.ts',
// vite: {
// build: {
// sourcemap: false,
// outDir: 'dist/electron/main',
// },
// },
// },
// preload: {
// input: {
// // You can configure multiple preload scripts here
// index: join(__dirname, 'electron/preload/main.ts'),
// },
// vite: {
// build: {
// // For debug
// sourcemap: 'inline',
// outDir: 'dist/electron/preload',
// },
// },
// },
// Enables use of Node.js API in the Electron-Renderer
// renderer: {},
}),
},
{
entry: 'electron/preload.ts',
onstart(options) {
options.reload();
},
},
]),

Unocss({
presets: [
Expand Down

0 comments on commit 1fa265e

Please sign in to comment.