Skip to content

Commit

Permalink
feat:功能完善
Browse files Browse the repository at this point in the history
  • Loading branch information
hellowmonkey committed Sep 17, 2022
1 parent c6b2ff9 commit d70e846
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dist-ssr
release
*.local

electron/data
# electron/data

# Editor directories and files
.idea
Expand Down
3 changes: 1 addition & 2 deletions electron/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { join } from "path";
import { tinifyKeys } from "../data/keys.json";
const isDev = process.env.NODE_ENV === "development";

export default {
isDev,
tinifyKeys,
tinifyKeys: ["4RxZwMzdcMT4ksdgYnVYJzMtn2R7cgCT", "XrHtLVmrnvnhGLHH2RCkRN9BPm7ZdJg1", "ZZtYtycXQk4d5P11NmFTt70YnJrJx1Qk"],
width: 1200,
height: 800,
title: "沃德工具箱",
Expand Down
3 changes: 3 additions & 0 deletions electron/data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"keyboard": "Alt+CommandOrControl+E"
}
24 changes: 19 additions & 5 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, SaveDialogOptions, Tray } from "electron";
import { join } from "path";
import { writeJSONSync } from "fs-extra";
import { compressImage, pngToIco } from "./image";
import { openDirectory, saveDialog, saveBase64File, selectDirectory, writeFile } from "./file";
import { notification } from "./helper";
import config from "../config";
import dataConfig from "../data/config.json";

let tray: Tray, win: BrowserWindow;

Expand Down Expand Up @@ -64,6 +66,9 @@ function createWindow() {
loading.show();
}
});
loading.on("close", () => {
showWin();
});

win.setIcon(config.icon);
win.removeMenu();
Expand All @@ -75,10 +80,9 @@ function createWindow() {
}
});
win.once("ready-to-show", () => {
win.show();
showWin();
loading.hide();
loading.close();
win.focus();
});
win.on("close", e => {
e.preventDefault();
Expand Down Expand Up @@ -127,9 +131,11 @@ app
});
})
.then(() => {
globalShortcut.register("Alt+CommandOrControl+A", () => {
toggleWin();
});
if (dataConfig.keyboard) {
globalShortcut.register(dataConfig.keyboard, () => {
toggleWin();
});
}
});

app.on("window-all-closed", () => {
Expand Down Expand Up @@ -198,3 +204,11 @@ ipcMain.handle("notification", (e, title: string, content: string) => {
ipcMain.handle("write-file", (e, filePath: string, buf: NodeJS.ArrayBufferView) => {
return writeFile(filePath, buf);
});

// 设置config
ipcMain.handle("set-config", (e, data: unknown) => {
Object.assign(dataConfig, data);
writeJSONSync(join(__dirname, "../data/config.json"), dataConfig);
app.relaunch({ args: process.argv.slice(1).concat(["--relaunch"]) });
app.exit(0);
});
5 changes: 5 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { contextBridge, ipcRenderer } from "electron";
import { keyboard } from "../data/config.json";

contextBridge.exposeInMainWorld("electronAPI", {
// 快捷键
keyboard,
// 设置进度条
setProgressBar: (...args: any[]) => ipcRenderer.send("set-progress-bar", ...args),
// 设置title
Expand All @@ -21,4 +24,6 @@ contextBridge.exposeInMainWorld("electronAPI", {
notification: (...args: any[]) => ipcRenderer.invoke("notification", ...args),
// 保存文件
writeFile: (...args: any[]) => ipcRenderer.invoke("write-file", ...args),
// 设置config
setConfig: (...args: any[]) => ipcRenderer.invoke("set-config", ...args),
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tool-box",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/electron/main/index.js",
"scripts": {
"dev": "vite",
Expand Down
11 changes: 9 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const isDev = import.meta.env.DEV;
const isElectron = typeof electronAPI !== "undefined";
import { version } from "../../package.json";
import { productName } from "../../electron-builder.json";

const releaseName = `${productName}-Windows-Setup-${version}.exe`;
const releaseUrl = `/release/${releaseName}`;

export default {
isDev,
isElectron,
title: "沃德工具箱",
releaseUrl: "https://github.com",
dbName: "tool",
dbVersion: 1,
version,
productName,
releaseName,
releaseUrl,
};
2 changes: 2 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare module "*.vue" {
}

declare let electronAPI: {
keyboard: string;
setProgressBar: (progress: number) => void;
setTitle: (title: string) => void;
compressImage: (filePath: string, targetPath?: string, width?: number) => Promise<{ fileSize: number; targetSize: number }>;
Expand All @@ -21,6 +22,7 @@ declare let electronAPI: {
pngToIco: (filePath: string, size?: number) => Promise<string>;
notification: (title: string, content: string) => Promise<void>;
writeFile: (filePath: string, buf: NodeJS.ArrayBufferView) => Promise<void>;
setConfig: (data: unknown) => void;
};

declare module "qrcode-decoder";
Loading

0 comments on commit d70e846

Please sign in to comment.