初步实现功能
This commit is contained in:
48
main/main.js
48
main/main.js
@ -1,7 +1,7 @@
|
||||
const { app, BrowserWindow, ipcMain } = require('electron')
|
||||
const path = require('path')
|
||||
const { app, BrowserWindow, ipcMain } = require("electron");
|
||||
const path = require("path");
|
||||
|
||||
let mainWindow
|
||||
let mainWindow;
|
||||
|
||||
function createWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
@ -9,22 +9,42 @@ function createWindow() {
|
||||
height: 300,
|
||||
transparent: true,
|
||||
frame: false,
|
||||
resizable: false, // 固定大小
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
contextIsolation: true
|
||||
}
|
||||
})
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
contextIsolation: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 开发模式加载Vite服务器
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
mainWindow.loadURL('http://localhost:5173')
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
mainWindow.loadURL("http://localhost:5173");
|
||||
} else {
|
||||
mainWindow.loadFile(path.join(__dirname, '../renderer/dist/index.html'))
|
||||
mainWindow.loadFile(path.join(__dirname, "../renderer/dist/index.html"));
|
||||
}
|
||||
|
||||
mainWindow.setIgnoreMouseEvents(true, {
|
||||
forward: true
|
||||
})
|
||||
// 窗口拖拽功能
|
||||
let isDragging = false;
|
||||
mainWindow.webContents.on("before-input-event", (_, input) => {
|
||||
if (input.type === "mouseDown") {
|
||||
isDragging = true;
|
||||
mainWindow.webContents.executeJavaScript(`
|
||||
window.dragOffset = { x: ${input.x}, y: ${input.y} }
|
||||
`);
|
||||
} else if (input.type === "mouseUp") {
|
||||
isDragging = false;
|
||||
}
|
||||
});
|
||||
|
||||
mainWindow.on("moved", () => {
|
||||
if (isDragging) {
|
||||
mainWindow.webContents.executeJavaScript(`
|
||||
window.electronAPI.updatePosition()
|
||||
`);
|
||||
}
|
||||
const [x, y] = mainWindow.getPosition();
|
||||
mainWindow.webContents.send("update-position", { x, y });
|
||||
});
|
||||
}
|
||||
|
||||
app.whenReady().then(createWindow)
|
||||
app.whenReady().then(createWindow);
|
||||
|
@ -1,6 +1,9 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
playSound: (file) => ipcRenderer.send('play-sound', file),
|
||||
showTooltip: (text) => ipcRenderer.send('show-tooltip', text)
|
||||
playSound: (soundFile) => ipcRenderer.send('play-sound', soundFile),
|
||||
showTooltip: (text) => ipcRenderer.send('show-tooltip', text),
|
||||
onUpdatePosition: (callback) => {
|
||||
ipcRenderer.on('update-position', (_, position) => callback(position))
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user