1549469775 4 years ago
parent
commit
a191fedd57
  1. 22
      package-lock.json
  2. BIN
      resource/electron/static/icon.png
  3. 2
      script/run.js
  4. 2
      script/webpack/webpack.main.config.js
  5. 14
      src/main/float.ts
  6. 65
      src/main/index.ts
  7. 25
      src/main/menu.ts
  8. 12
      src/main/util/index.ts
  9. 1
      src/render/index.html
  10. 67
      test.js
  11. 2
      vite.config.ts
  12. 2
      说明.md

22
package-lock.json

@ -1734,7 +1734,7 @@
},
"@types/node": {
"version": "15.14.3",
"resolved": "https://registry.nlark.com/@types/node/download/@types/node-15.14.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-15.14.3.tgz",
"resolved": "https://registry.nlark.com/@types/node/download/@types/node-15.14.3.tgz?cache=0&sync_timestamp=1627257813519&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-15.14.3.tgz",
"integrity": "sha1-MwdjuXPQrKy3/czHWU4fsoHtGxw=",
"dev": true
},
@ -4492,7 +4492,7 @@
},
"ejs": {
"version": "3.1.6",
"resolved": "https://registry.npm.taobao.org/ejs/download/ejs-3.1.6.tgz",
"resolved": "https://registry.nlark.com/ejs/download/ejs-3.1.6.tgz",
"integrity": "sha1-W/0KBol0O7UmizVQzO7rvBcCgio=",
"dev": true,
"requires": {
@ -6365,7 +6365,7 @@
},
"jake": {
"version": "10.8.2",
"resolved": "https://registry.npm.taobao.org/jake/download/jake-10.8.2.tgz?cache=0&sync_timestamp=1591684013969&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fjake%2Fdownload%2Fjake-10.8.2.tgz",
"resolved": "https://registry.npm.taobao.org/jake/download/jake-10.8.2.tgz",
"integrity": "sha1-68nehVgWCmbYLQ6txqLlj7xQCns=",
"dev": true,
"requires": {
@ -6418,7 +6418,7 @@
},
"escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npm.taobao.org/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz?cache=0&sync_timestamp=1618677243201&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fescape-string-regexp%2Fdownload%2Fescape-string-regexp-1.0.5.tgz",
"resolved": "https://registry.nlark.com/escape-string-regexp/download/escape-string-regexp-1.0.5.tgz",
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
"dev": true
},
@ -7196,7 +7196,7 @@
},
"object.assign": {
"version": "4.1.2",
"resolved": "https://registry.npm.taobao.org/object.assign/download/object.assign-4.1.2.tgz?cache=0&sync_timestamp=1604115158081&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fobject.assign%2Fdownload%2Fobject.assign-4.1.2.tgz",
"resolved": "https://registry.nlark.com/object.assign/download/object.assign-4.1.2.tgz",
"integrity": "sha1-DtVKNC7Os3s4/3brgxoOeIy2OUA=",
"dev": true,
"requires": {
@ -9157,7 +9157,7 @@
"dependencies": {
"commander": {
"version": "2.20.3",
"resolved": "https://registry.nlark.com/commander/download/commander-2.20.3.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-2.20.3.tgz",
"resolved": "https://registry.nlark.com/commander/download/commander-2.20.3.tgz?cache=0&sync_timestamp=1627358203890&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-2.20.3.tgz",
"integrity": "sha1-/UhehMA+tIgcIHIrpIA16FMa6zM=",
"dev": true
},
@ -9174,7 +9174,7 @@
},
"terser": {
"version": "5.7.1",
"resolved": "https://registry.nlark.com/terser/download/terser-5.7.1.tgz",
"resolved": "https://registry.nlark.com/terser/download/terser-5.7.1.tgz?cache=0&sync_timestamp=1624884102109&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fterser%2Fdownload%2Fterser-5.7.1.tgz",
"integrity": "sha1-LcemEAm2a7Y4MFyyqCR2OxFr94Q=",
"dev": true,
"requires": {
@ -9185,7 +9185,7 @@
"dependencies": {
"source-map": {
"version": "0.7.3",
"resolved": "https://registry.npm.taobao.org/source-map/download/source-map-0.7.3.tgz",
"resolved": "https://registry.nlark.com/source-map/download/source-map-0.7.3.tgz",
"integrity": "sha1-UwL4FpAxc1ImVECS5kmB91F1A4M=",
"dev": true
}
@ -9749,7 +9749,7 @@
},
"vite": {
"version": "2.4.3",
"resolved": "https://registry.nlark.com/vite/download/vite-2.4.3.tgz?cache=0&sync_timestamp=1626783291368&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fvite%2Fdownload%2Fvite-2.4.3.tgz",
"resolved": "https://registry.nlark.com/vite/download/vite-2.4.3.tgz",
"integrity": "sha1-/kqnjp3X02vLEuzL1SMTsmz633c=",
"dev": true,
"requires": {
@ -9923,7 +9923,7 @@
"dependencies": {
"commander": {
"version": "7.2.0",
"resolved": "https://registry.nlark.com/commander/download/commander-7.2.0.tgz?cache=0&sync_timestamp=1624609539421&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-7.2.0.tgz",
"resolved": "https://registry.nlark.com/commander/download/commander-7.2.0.tgz?cache=0&sync_timestamp=1627358203890&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fcommander%2Fdownload%2Fcommander-7.2.0.tgz",
"integrity": "sha1-o2y1fQtQHOEI5NIFWaFQo5HZerc=",
"dev": true
}
@ -10515,7 +10515,7 @@
},
"websocket-driver": {
"version": "0.7.4",
"resolved": "https://registry.npm.taobao.org/websocket-driver/download/websocket-driver-0.7.4.tgz",
"resolved": "https://registry.nlark.com/websocket-driver/download/websocket-driver-0.7.4.tgz",
"integrity": "sha1-ia1Slbv2S0gKvLox5JU6ynBvV2A=",
"dev": true,
"requires": {

BIN
resource/electron/static/icon.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 18 KiB

2
script/run.js

@ -10,7 +10,7 @@ if (argv.build) {
"npm run build:in"
])
} else {
crossEnv([
const process = crossEnv([
`STATIC=${join(__dirname, '../resource/electron/static')}`,
"PORT=3344",
"NODE_ENV=development",

2
script/webpack/webpack.main.config.js

@ -57,7 +57,7 @@ let mainConfig = {
if (process.env.NODE_ENV !== 'production') {
mainConfig.plugins.push(
new webpack.DefinePlugin({
'__static': `"${join(__dirname, '../../resource/electron/static').replace(/\\/g, '\\\\')}"`
'__static': `"${path.join(__dirname, '../../resource/electron/static').replace(/\\/g, '\\\\')}"`
})
)
}

14
src/main/float.ts

@ -1,22 +1,19 @@
import Shared from "./share"
import { BrowserWindow, ipcMain, screen, Menu, shell, webContents } from "electron"
import { getFileUrl } from './util'
import { BrowserWindow, ipcMain, screen, Menu, shell, webContents, app } from "electron"
// webContents
console.log(webContents.getAllWebContents())
const window: any = null //BrowserWindow.fromWebContents(webContents.getFocusedWebContents())
const winURL =
process.env.NODE_ENV === "development" ? `http://localhost:${process.env.PORT}/#/float` : `file://${__dirname}/index.html#/float`
ipcMain.on("@float:setPosition", (event, x, y) => {
Shared.data.floatWindow?.setPosition(x, y)
})
ipcMain.on("showSuspensionWindow", () => {
console.log(24324)
if (Shared.data.floatWindow) {
if (Shared.data.floatWindow.isVisible()) {
createSuspensionWindow()
// createSuspensionWindow()
} else {
Shared.data.floatWindow.showInactive()
}
@ -73,7 +70,8 @@ ipcMain.on("createSuspensionMenu", e => {
{
label: "退出软件",
click: () => {
Shared.data.mainWindow.close()
Shared.data.forceClose = true
app.quit()
},
},
])
@ -101,7 +99,7 @@ function createSuspensionWindow() {
// 设置窗口的位置 注意x轴要桌面的宽度 - 窗口的宽度
Shared.data.floatWindow.setPosition(size.width - winSize[0], 100)
Shared.data.floatWindow.setPosition(size.width / 2, size.height / 2)
Shared.data.floatWindow.loadURL(winURL)
Shared.data.floatWindow.loadURL(getFileUrl("float"))
Shared.data.floatWindow.once("ready-to-show", () => {
Shared.data.floatWindow.show()

65
src/main/index.ts

@ -1,34 +1,32 @@
/**
* electron
*/
'use strict'
import Shared from './share'
import setTray from './disk'
"use strict"
import Shared from "./share"
import setTray from "./disk"
import { getFileUrl } from "./util"
// import '../renderer/store'
import './facilities'
import { app, BrowserWindow, dialog } from 'electron'
import "./facilities"
import { app, BrowserWindow, dialog } from "electron"
Shared.data = {
mainWindow: null,
floatWindow: null,
mainWindow: null, // 主窗口
floatWindow: null, // 浮动窗口
miniWindow: null,
forceClose: false,
lastChoice: -1,
lastChoice: -1, // 做过的选择
}
console.log('asdasadsads')
console.log("asdasadsads")
/**
* Set `__static` path to static files in production
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
*/
let isDev = process.env.NODE_ENV == "development" ? true : false
let isDev = process.env.NODE_ENV == "development" ? true : false
if (!isDev) {
// @ts-ignore
global.__static = require("path").join(__dirname, "/static").replace(/\\/g, "\\\\")
}
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:${process.env.PORT}/#/` : `file://${__dirname}/index.html`
if (!isDev) {
// @ts-ignore
global.__static = require("path").join(__dirname, "/static").replace(/\\/g, "\\\\")
}
function createWindow() {
/**
@ -41,17 +39,18 @@ function createWindow() {
resizable: true,
minWidth: 450,
minHeight: 400,
icon: __static + "/icon.png",
// frame: false,
// transparent: true,
alwaysOnTop: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
contextIsolation: false,
},
})
Shared.data.mainWindow.loadURL(winURL)
Shared.data.mainWindow.on('close', (event:any) => {
Shared.data.mainWindow.loadURL(getFileUrl(""))
Shared.data.mainWindow.on("close", (event: any) => {
if (Shared.data.forceClose) {
Shared.data.mainWindow = null
app.quit()
@ -65,12 +64,12 @@ function createWindow() {
event.preventDefault()
} else {
const choice = dialog.showMessageBoxSync(Shared.data.mainWindow, {
type: 'info',
title: 'Information',
type: "info",
title: "Information",
defaultId: 0,
cancelId: 0,
message: '确定要关闭吗?',
buttons: ['没事', '最小化到托盘', '直接退出']
message: "确定要关闭吗?",
buttons: ["没事", "最小化到托盘", "直接退出"],
})
if (choice === 1) {
Shared.data.lastChoice = 1
@ -98,10 +97,10 @@ const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.exit()
} else {
require('./menu')
require('./float')
require("./menu")
require("./float")
app.on('second-instance', (event, commandLine, workingDirectory) => {
app.on("second-instance", (event, commandLine, workingDirectory) => {
// 当运行第二个实例时,将会聚焦到mainWindow这个窗口
if (Shared.data.mainWindow) {
if (Shared.data.mainWindow.isMinimized()) Shared.data.mainWindow.restore()
@ -110,9 +109,9 @@ if (!gotTheLock) {
}
})
app.on('ready', createWindow)
app.on("ready", createWindow)
app.on('before-quit', event => {
app.on("before-quit", event => {
if (Shared.data.forceClose) {
app.exit()
} else {
@ -120,13 +119,13 @@ if (!gotTheLock) {
}
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.exit()
}
})
app.on('activate', () => {
app.on("activate", () => {
if (Shared.data.mainWindow === null) {
createWindow()
}

25
src/main/menu.ts

@ -1,6 +1,7 @@
// const path = require('path')
import { BrowserWindow } from "electron"
import Shared from "./share"
import { getFileUrl } from "./util"
const electron = require("electron")
const setTray = require("./disk").default
// const BrowserWindow = electron.BrowserWindow
@ -9,12 +10,12 @@ const app = electron.app
const dialog = electron.dialog
const ipcMain = electron.ipcMain
let newwin:BrowserWindow |null
let newwin: BrowserWindow | null
let template = [
{
label: "选择保存目录",
click: function (item:any, focusedWindow:BrowserWindow) {
click: function (item: any, focusedWindow: BrowserWindow) {
dialog
.showOpenDialog(focusedWindow, {
properties: ["openDirectory"],
@ -30,7 +31,7 @@ let template = [
{
label: "置顶",
key: "alwaysTop",
click: function (item: any, focusedWindow:BrowserWindow) {
click: function (item: any, focusedWindow: BrowserWindow) {
if (Shared.data.mainWindow.isAlwaysOnTop()) {
Shared.data.mainWindow.setAlwaysOnTop(false)
} else {
@ -41,7 +42,7 @@ let template = [
{
label: "重载",
accelerator: "CmdOrCtrl+R",
click: function (item: any, focusedWindow:BrowserWindow) {
click: function (item: any, focusedWindow: BrowserWindow) {
if (focusedWindow) {
// 重载之后, 刷新并关闭所有的次要窗体
if (focusedWindow.id === 1) {
@ -60,14 +61,14 @@ let template = [
submenu: [
{
label: "悬浮窗",
click: function (item: any, focusedWindow:BrowserWindow) {
click: function (item: any, focusedWindow: BrowserWindow) {
ipcMain.emit("showSuspensionWindow")
},
},
{
label: "最小化到托盘",
click: function (item: any, focusedWindow:BrowserWindow) {
Shared.data.lastChoice = 1;
click: function (item: any, focusedWindow: BrowserWindow) {
Shared.data.lastChoice = 1
if (Shared.data.miniWindow) {
Shared.data.mainWindow.hide() // 调用 最小化实例方法
} else {
@ -84,7 +85,7 @@ let template = [
return "F11"
}
})(),
click: function (item: any, focusedWindow:BrowserWindow) {
click: function (item: any, focusedWindow: BrowserWindow) {
if (focusedWindow) {
focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
}
@ -135,16 +136,16 @@ let template = [
modal: true,
show: false,
resizable: false,
webPreferences:{
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
contextIsolation: false,
},
// parent: focusedWindow // win是主窗口
})
// 隐藏菜单
newwin.setMenuBarVisibility(false)
// vue是单页面,需要改成多页面才行
newwin.loadURL(process.env.NODE_ENV === "development" ? `http://localhost:${process.env.PORT}/#/about` : `file://${__dirname}#/about`)
newwin.loadURL(getFileUrl("about"))
newwin.on("ready-to-show", () => {
newwin?.show()
})

12
src/main/util/index.ts

@ -0,0 +1,12 @@
export function getFileUrl(route: string, isSPA: boolean = true) {
if (isSPA) {
const winURL =
process.env.NODE_ENV === "development"
? `http://localhost:${process.env.PORT}/#/${route}`
: `file://${__dirname}/index.html#/${route}`
return winURL
} else {
return ""
}
}

1
src/render/index.html

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self' https: 'unsafe-inline'; style-src * 'unsafe-inline'; font-src * data:;"> -->
<!-- <meta http-equiv="X-Content-Security-Policy" content="default-src 'self' https: 'unsafe-inline'; style-src * 'unsafe-inline'; font-src * data:;"> -->
<!-- -->
<title><%- title %></title>
</head>
<body>

67
test.js

@ -1,57 +1,18 @@
// // http://localhost:3344
// get("http://baidu.com",res=>{
// console.log(res.statusCode);
// })
// import { spawn, ChildProcess } from 'child_process'
// spawn(
// electronForge,
// [
// "start",
// "--inspect-electron",
// "--app-path",
// join(__dirname, "dist/electron/entry.js"),//join(__dirname, `../${main}`),
// ] ,
// {
// stdio: 'inherit',
// env: Object.assign(process.env, { NODE_ENV: "development" }),
// })
const { say } = require("cfonts")
const chalk = require("chalk")
const electron = require("electron")
const { spawn } = require("child_process")
const { join } = require("path")
console.log(electron);
// let child = spawn(
// electron,
// [join(__dirname, `dist/electron/entry.js`)]
// )
// if(child){
// setTimeout(() => {
// child.kill()
// }, 5000);
// }
function runVite () {
return new Promise((resolve, reject) => {
let viteProcess = spawn('node', [`D:/1XYX/pro/vite-electron/node_modules/vite/bin/vite.js`])
let isReady = false;
viteProcess.stdout.on('data', (data) => {
console.log(`${data}`);
if (!isReady && data.indexOf("ready") != -1) {
resolve()
}
});
viteProcess.stderr.on('data', (data) => {
console.error(`[vite err]: ${data}`);
reject()
});
viteProcess.on('close', (code) => {
console.log(`[vite close]: exited with code ${code}`);
reject()
});
function electronLog(data, color) {
let log = ""
data = data.toString().split(/\r?\n/)
data.forEach(line => {
log += ` ${line}\n`
})
}
if (/[0-9A-z]+/.test(log)) {
console.log(
chalk[color].bold("┏ Electron -------------------") + "\n\n" + log + chalk[color].bold("┗ ----------------------------") + "\n"
)
}
}
electronLog("dfssssssssssssssssssssssssssssssssssssssssssssssss","red")

2
vite.config.ts

@ -58,7 +58,7 @@ export default defineConfig({
minifyHtml(),
injectHtml({
injectData: {
title: "vite-plugin-htmlaa-example"
title: "代码片段管理工具"
},
}),
],

2
说明.md

@ -1,5 +1,7 @@
### 渲染层
* 必须使用hash模式,方便
全局替换字符
* `__static`: 静态资源字符串,注意没有用引号包裹,不能直接赋值

Loading…
Cancel
Save