Lv110版本源码上传

This commit is contained in:
西瓜°
2024-11-04 15:00:40 +08:00
parent a6dbccdead
commit e029da72e4
6642 changed files with 131203 additions and 136087 deletions
+217
View File
@@ -0,0 +1,217 @@
import { join } from "node:path"
import path from "path"
import { BrowserWindow, app, ipcMain, screen, shell } from "electron"
import { startServer, stopServer } from "./server"
// Disable GPU Acceleration for Windows 7
// if (release().startsWith("6.1"))
app.disableHardwareAcceleration()
// Set application name for Windows 10+ notifications
if (process.platform === "win32") {
app.setAppUserModelId(app.getName())
}
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true"
if (!app.requestSingleInstanceLock()) {
app.quit()
process.exit(0)
}
let win: BrowserWindow | null = null
const storage:any = {}
let factor = 1.0
async function createWindow() {
factor = Math.min(screen.getPrimaryDisplay().workAreaSize.height * 0.75 / 680, screen.getPrimaryDisplay().scaleFactor);
win = new BrowserWindow({
frame: false,
width: Math.round(920 * factor),
height: Math.round(680 * factor),
resizable: false,
icon: join(__dirname, "../renderer/favicon.ico"),
webPreferences: {
preload: join(__dirname, "../preload/index.cjs"),
nodeIntegration: true,
contextIsolation: false,
webSecurity: false,
devTools: import.meta.env.DEV,
partition: String(+new Date()),
zoomFactor: factor
}
})
win.setMenuBarVisibility(false)
win.webContents.session.clearCache()
if (app.isPackaged) {
win.loadURL(`http://localhost:17173?r=${Math.random()}`)
// win.webContents.reloadIgnoringCache()
} else {
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin
const url = `http://${process.env.VITE_DEV_SERVER_HOST}:${process.env.VITE_DEV_SERVER_PORT}?r=${Math.random()}`
win.loadURL(url)
// win.webContents.openDevTools({ mode: "undocked", activate: true })
}
// Test active push message to Renderer-process
// win.webContents.on("did-finish-load", () => {
// win?.webContents.send("main-process-message", new Date().toLocaleString())
// })
// Make all links open with the browser, not with the application
win.webContents.setWindowOpenHandler(({ url }) => {
const { origin } = new URL(url)
if (origin != "localhost") {
shell.openExternal(url)
}
return { action: "deny" }
})
win.webContents.closeDevTools()
win.setMenuBarVisibility(false)
// 注册自定义协议
const agreement = 'dcalc' // 自定义协议名
let isSet = false // 是否注册成功
app.removeAsDefaultProtocolClient(agreement) // 每次运行都删除自定义协议 然后再重新注册
// 开发模式下在window运行需要做兼容
if (process.env.NODE_ENV === 'development' && process.platform === 'win32') {
// 设置electron.exe 和 app的路径
isSet = app.setAsDefaultProtocolClient(agreement, process.execPath, [
path.resolve(process.argv[1]),
])
} else {
isSet = app.setAsDefaultProtocolClient(agreement)
}
}
startServer()
setTimeout(() => {
app.whenReady().then(createWindow)
}, 5000)
app.on("window-all-closed", () => {
win = null
app.quit()
})
app.on("quit", () => {
stopServer().then(() => {
console.log("app quit.")
app.exit(0)
})
})
app.on("second-instance", (event,commandLine) => {
let uid = undefined
const command = commandLine.find(a=>a.startsWith("dcalc://"))
if(!!command){
uid = command.split('uid=')?.[1] ?? ""
}
storage["uid"] = decodeURIComponent(uid ?? "")
if (win) {
// Focus on the main window if the user tried to open another
if (win.isMinimized()) {
win.restore()
}
if (!undefined){
win.reload()
}
win.focus()
}
})
app.on("activate", () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
allWindows[0].focus()
} else {
createWindow()
}
})
ipcMain.handle("open-win", (event, arg) => {
const childWindow = new BrowserWindow({
width: Math.round(arg.width * factor),
height: Math.round(arg.height * factor),
resizable: false,
frame: false,
icon: join(__dirname, "../renderer/favicon.ico"),
webPreferences: {
preload: join(__dirname, "../preload/index.cjs"),
webSecurity: false,
nodeIntegration: true,
contextIsolation: false,
partition: String(+new Date()),
zoomFactor: factor
}
})
childWindow.webContents.session.clearCache()
if (app.isPackaged) {
const url = `http://localhost:17173${arg.url}`
childWindow.loadURL(url)
// childWindow.webContents.reloadIgnoringCache()
} else {
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin
const url = `http://${process.env.VITE_DEV_SERVER_HOST}:${process.env.VITE_DEV_SERVER_PORT}${arg.url}`
childWindow.loadURL(url)
// childWindow.webContents.openDevTools({ mode: "undocked", activate: true })
}
})
ipcMain.handle("minimize-win", event => {
const window = BrowserWindow.fromWebContents(event.sender)
if (window) {
window.minimize()
}
})
ipcMain.handle("close-win", event => {
const window = BrowserWindow.fromWebContents(event.sender)
if (window) {
window.close()
}
})
ipcMain.handle("restart", () => {
win?.webContents.session.clearCache().then(() => {
app.relaunch()
app.quit()
console.log("restart...")
})
})
ipcMain.handle("getStorage", (event, arg) => {
return storage[arg] ?? undefined
})
ipcMain.handle("getVersion", () => {
return app.getVersion()
})
ipcMain.handle("setStorage", (event, arg) => {
Object.defineProperty(storage, arg.key, {
value: arg.value,
writable: true,
enumerable: true,
configurable: true
})
})
ipcMain.handle("open-url", (event, url) => {
shell.openExternal(url)
})
process.on("exit", () => {
app.quit()
})
+23
View File
@@ -0,0 +1,23 @@
{
"name": "dcalc-main",
"version": "2022.6.6",
"license": "GNU",
"scripts": {
"build": "vite build"
},
"engines": {
"node": ">=14.17.0"
},
"dependencies": {
"axios": "^1.6.0"
},
"devDependencies": {
"electron": "22.3.12",
"vite": "^4.5.0"
},
"env": {
"//": "Used in build scripts",
"VITE_DEV_SERVER_HOST": "127.0.0.1",
"VITE_DEV_SERVER_PORT": 25252
}
}
+78
View File
@@ -0,0 +1,78 @@
import child_process from "node:child_process"
import { join } from "node:path"
import { app } from "electron"
const port: number = 17173
let instance: child_process.ChildProcess
/**
* 开启python的api服务
* @returns
*/
export function startServer() {
judgeServerOpen(port).then(async res => {
if (res) {
console.log(`${port} is in used"`)
await stopServer()
}
if (app.isPackaged) {
// exec不会报错 spawn会抛错
instance = child_process.exec("\"./resources/dnfcalc-api.exe\"", (_, stdout: any) => {
console.log(stdout)
})
console.log("server started.")
return
}
const execCommand = process.platform === "win32" ? "python" : "python3"
instance = child_process.spawn(execCommand, [join(__dirname, "../../../api/main.py"), `${port}`])
if (instance) {
console.log("server started.")
instance.stdout?.pipe(process.stdout)
instance.stderr?.pipe(process.stderr)
instance.stdin?.pipe(process.stdin)
}
})
}
/**
* 判断端口是否被占用
* @param port 端口号
* @returns 该端口是否被占用
*/
function judgeServerOpen(port: number): Promise<boolean> {
const order = `netstat -ano|findstr "${port}"`
return new Promise<boolean>(resolve => {
child_process.exec(order, (_: any, stdout: any) => {
if (stdout === "") {
resolve(false)
} else {
resolve(true)
}
})
})
}
/**
* 关闭python的api服务
* @returns
*/
export function stopServer(): Promise<string> {
const order = process.platform == "win32" ? "taskkill /f /im dnfcalc-api.exe" : "taskkill /f /im python.exe"
return new Promise(resolve => {
if (instance) {
if (instance.kill(0) && instance.kill()) {
console.log("server stoped.")
}
resolve("server stoped.")
}
child_process.exec(order, (_, stdout: string) => {
// TODO 关闭python api
resolve(stdout)
})
})
}
+25
View File
@@ -0,0 +1,25 @@
import { builtinModules } from "node:module"
import { defineConfig } from "vite"
import pkg from "./package.json"
export default defineConfig({
root: __dirname,
build: {
outDir: "../../dist/app/main",
lib: {
entry: "index.ts",
formats: ["cjs"],
fileName: () => "[name].cjs"
},
/* from mode option */
minify: process.env.NODE_ENV === "production",
/* from mode option */
sourcemap: process.env.NODE_ENV == "development",
rollupOptions: {
external: ["electron",
...builtinModules,
...builtinModules.map((m) => `node:${m}`),
...Object.keys(pkg.devDependencies || {})]
}
}
})
+28
View File
@@ -0,0 +1,28 @@
import { ipcRenderer } from "electron"
import { useLoading } from "./loading"
const { appendLoading, removeLoading } = useLoading()
window.removeLoading = removeLoading
window.pywebview = {
api: ipcRenderer
}
// window.sharedObject = {
// storage: "test"
// }
/** docoment ready */
function domReady(condition: DocumentReadyState[] = ["complete", "interactive"]) {
return new Promise(resolve => {
if (condition.includes(document.readyState)) {
resolve(true)
} else {
document.addEventListener("readystatechange", () => {
if (condition.includes(document.readyState)) {
resolve(true)
}
})
}
})
}
domReady().then(appendLoading)
+71
View File
@@ -0,0 +1,71 @@
/**
* https://tobiasahlin.com/spinkit
* https://connoratherton.com/loaders
* https://projects.lukehaas.me/css-loaders
* https://matejkustec.github.io/SpinThatShit
*/
const safe = {
append(parent: HTMLElement, child: HTMLElement) {
if (!Array.from(parent.children).find(e => e === child)) {
return parent.appendChild(child)
}
},
remove(parent: HTMLElement, child: HTMLElement) {
if (Array.from(parent.children).find(e => e === child)) {
return parent.removeChild(child)
}
}
}
export function useLoading() {
const className = "loaders-css__square-spin"
const styleContent = `
@keyframes square-spin {
25% { transform: perspective(100px) rotateX(180deg) rotateY(0); }
50% { transform: perspective(100px) rotateX(180deg) rotateY(180deg); }
75% { transform: perspective(100px) rotateX(0) rotateY(180deg); }
100% { transform: perspective(100px) rotateX(0) rotateY(0); }
}
.${className} > div {
// animation-fill-mode: both;
width: 210px;
height: 213px;
// background: rgba(0,0,0,0);
background-image: url("/images/loading.gif");
// animation: square-spin 3s 0s cubic-bezier(0.09, 0.57, 0.49, 0.9) infinite;
}
.app-loading-wrap {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #282c34;
z-index: 9;
}
`
const oStyle = document.createElement("style")
const oDiv = document.createElement("div")
oStyle.id = "app-loading-style"
oStyle.innerHTML = styleContent
oDiv.className = "app-loading-wrap"
oDiv.innerHTML = `<div class="${className}"><div>
</div></div>`
return {
appendLoading() {
safe.append(document.head, oStyle)
safe.append(document.body, oDiv)
},
removeLoading() {
safe.remove(document.head, oStyle)
safe.remove(document.body, oDiv)
}
}
}
+20
View File
@@ -0,0 +1,20 @@
{
"name": "dcalc-preload",
"version": "2022.6.6",
"license": "GNU",
"scripts": {
"build": "vite build"
},
"engines": {
"node": ">=14.17.0"
},
"devDependencies": {
"electron": "22.3.12",
"vite": "^4.5.0"
},
"env": {
"//": "Used in build scripts",
"VITE_DEV_SERVER_HOST": "127.0.0.1",
"VITE_DEV_SERVER_PORT": 25252
}
}
+25
View File
@@ -0,0 +1,25 @@
import { builtinModules } from "node:module"
import { defineConfig } from "vite"
import pkg from "../../package.json"
export default defineConfig({
root: __dirname,
build: {
outDir: "../../dist/app/preload",
lib: {
entry: "index.ts",
formats: ["cjs"],
fileName: () => "[name].cjs"
},
/* from mode option */
sourcemap: process.env.NODE_ENV == "development",
/* from mode option */
minify: process.env.NODE_ENV === "production",
// emptyOutDir: true,
rollupOptions: {
external: ["electron", ...builtinModules,
...builtinModules.map((m) => `node:${m}`),
...Object.keys(pkg.devDependencies || {})]
}
}
})
Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Some files were not shown because too many files have changed in this diff Show More