2024-07-22 16:30:35 +08:00
|
|
|
/**
|
|
|
|
* @description: electron 封装的工具函数
|
2024-07-25 13:24:31 +08:00
|
|
|
* 消息整理
|
|
|
|
* tool-sphere:create 创建-悬浮球-窗口
|
2024-07-22 16:30:35 +08:00
|
|
|
*/
|
2024-07-23 17:20:36 +08:00
|
|
|
import { app, shell, BrowserWindow, ipcMain } from 'electron'
|
|
|
|
import { is } from '@electron-toolkit/utils'
|
2024-07-22 16:30:35 +08:00
|
|
|
|
2024-07-23 17:20:36 +08:00
|
|
|
// const baseUrl = 'http://localhost:5173/#' // 开发环境使用
|
|
|
|
const baseUrl = process.env['ELECTRON_RENDERER_URL']+'/#' // 开发环境使用
|
2024-07-25 13:24:31 +08:00
|
|
|
// 所有窗口
|
|
|
|
let allWindow = {}
|
|
|
|
// 其他已有窗口 wins
|
|
|
|
export function init() {
|
2024-07-22 16:30:35 +08:00
|
|
|
// 创建工具-悬浮球
|
2024-07-25 13:24:31 +08:00
|
|
|
ipcMain.on('tool-sphere:create', async(e, data) => {
|
|
|
|
// console.log('测试xxxx', data)
|
2024-07-23 17:20:36 +08:00
|
|
|
await createTools(data) // 执行逻辑
|
2024-07-25 13:24:31 +08:00
|
|
|
e.reply('tool-sphere:create-reply', {code: 200, msg: 'success'}) // 返回结果
|
2024-07-22 16:30:35 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @description: 创建工具
|
|
|
|
* @param {*} url 路由地址
|
|
|
|
* @param {number} [width=800] 窗口宽度
|
|
|
|
* @param {number} [height=600] 窗口高度
|
|
|
|
* @param {{}} [option={}] 自定义选项
|
|
|
|
* @author: zdg
|
|
|
|
* @date 2021-07-05 14:07:01
|
|
|
|
*/
|
2024-07-25 13:24:31 +08:00
|
|
|
export function createTools({url, width = 800, height = 600, option={}}) {
|
|
|
|
const { mainWindow } = allWindow||{} // 获取主窗口
|
2024-07-23 17:20:36 +08:00
|
|
|
const devUrl = `${baseUrl}${url}`
|
|
|
|
const buildUrl = `file://${__dirname}/index.html${url}`
|
|
|
|
const urlAll = is.dev ? devUrl : buildUrl
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let win = new BrowserWindow({
|
|
|
|
width, height,
|
2024-07-25 13:24:31 +08:00
|
|
|
type: 'toolbar', // 创建的窗口类型为工具栏窗口
|
|
|
|
frame: false, // 要创建无边框窗口
|
|
|
|
resizable: false, // 禁止窗口大小缩放
|
|
|
|
transparent: true, // 设置透明
|
|
|
|
alwaysOnTop: true, // 窗口是否总是显示在其他窗口之前
|
|
|
|
|
|
|
|
parent: mainWindow, // 父窗口
|
|
|
|
autoClose: true, // 关闭窗口后自动关闭
|
2024-07-23 17:20:36 +08:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true, // nodeApi调用
|
|
|
|
contextIsolation: false, // 沙箱取消
|
|
|
|
webSecurity: false // 跨域关闭
|
|
|
|
},
|
|
|
|
...option
|
|
|
|
})
|
|
|
|
// console.log(urlAll)
|
|
|
|
// url = 'https://www.baidu.com'
|
|
|
|
console.log(urlAll)
|
|
|
|
win.loadURL(urlAll)
|
2024-07-26 12:48:03 +08:00
|
|
|
win.setFullScreen(true) // 设置窗口为全屏
|
2024-07-25 13:24:31 +08:00
|
|
|
win.setIgnoreMouseEvents(true) // 忽略鼠标事件|使窗口不可选中
|
2024-07-23 17:20:36 +08:00
|
|
|
win.once('ready-to-show', () => {
|
|
|
|
win.show()
|
|
|
|
resolve(win)
|
|
|
|
})
|
|
|
|
win.on('closed', () => {
|
|
|
|
win = null
|
|
|
|
})
|
2024-07-22 16:30:35 +08:00
|
|
|
})
|
2024-07-25 13:24:31 +08:00
|
|
|
}
|
|
|
|
// 保存窗口
|
|
|
|
export function setWin(win = {}) {
|
|
|
|
if (win && Object.keys(win).length){
|
|
|
|
Object.keys(win).forEach(key => {
|
|
|
|
if (!allWindow[key]) { // 不存在就保存
|
|
|
|
allWindow[key] = win[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|