1 Star 0 Fork 76

famez/koodo-reader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.js 6.87 KB
一键复制 编辑 原始数据 按行查看 历史
troyeguo 提交于 2022-04-16 21:21 . fix bug
const {
app,
BrowserWindow,
Menu,
ipcMain,
dialog,
powerSaveBlocker,
nativeTheme,
} = require("electron");
const path = require("path");
const isDev = require("electron-is-dev");
const Store = require("electron-store");
const store = new Store();
const fs = require("fs");
const configDir = app.getPath("userData");
const dirPath = path.join(configDir, "uploads");
let mainWin;
let readerWindow;
const singleInstance = app.requestSingleInstanceLock();
var filePath = null;
if (process.platform != "darwin" && process.argv.length >= 2) {
filePath = process.argv[1];
}
let options = {
width: 1050,
height: 660,
webPreferences: {
webSecurity: false,
nodeIntegration: true,
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegrationInSubFrames: true,
allowRunningInsecureContent: true,
enableRemoteModule: true,
},
};
// Single Instance Lock
if (!singleInstance) {
app.quit();
if (filePath) {
fs.writeFileSync(
path.join(dirPath, "log.json"),
JSON.stringify({ filePath })
);
}
} else {
app.on("second-instance", (event, argv, workingDir) => {
if (mainWin) {
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
});
}
const createMainWin = () => {
mainWin = new BrowserWindow(options);
if (!isDev) {
Menu.setApplicationMenu(null);
}
const urlLocation = isDev
? "http://localhost:3000"
: `file://${path.join(__dirname, "./build/index.html")}`;
mainWin.loadURL(urlLocation);
mainWin.on("close", () => {
mainWin = null;
});
ipcMain.handle("open-book", (event, config) => {
let { url, isMergeWord, isFullscreen, isPreventSleep } = config;
store.set({
url,
isMergeWord: isMergeWord ? isMergeWord : "no",
isFullscreen: isFullscreen ? isFullscreen : "no",
isPreventSleep: isPreventSleep ? isPreventSleep : "no",
});
let id;
if (isPreventSleep === "yes") {
id = powerSaveBlocker.start("prevent-display-sleep");
console.log(powerSaveBlocker.isStarted(id));
}
if (isFullscreen === "yes") {
readerWindow = new BrowserWindow(options);
readerWindow.loadURL(url);
readerWindow.maximize();
} else {
readerWindow = new BrowserWindow({
...options,
width: parseInt(store.get("windowWidth")),
height: parseInt(store.get("windowHeight")),
x: parseInt(store.get("windowX")),
y: parseInt(store.get("windowY")),
frame: isMergeWord === "yes" ? false : true,
hasShadow: isMergeWord === "yes" ? false : true,
transparent: isMergeWord === "yes" ? true : false,
});
readerWindow.loadURL(url);
}
readerWindow.on("close", (event) => {
if (!readerWindow.isDestroyed()) {
let bounds = readerWindow.getBounds();
store.set({
windowWidth: bounds.width,
windowHeight: bounds.height,
windowX: bounds.x,
windowY: bounds.y,
});
}
if (isPreventSleep && !readerWindow.isDestroyed()) {
id && powerSaveBlocker.stop(id);
}
// readerWindow && readerWindow.destroy();
// readerWindow = null;
});
event.returnValue = "success";
});
ipcMain.handle("change-path", async (event) => {
var path = await dialog.showOpenDialog({
properties: ["openDirectory"],
});
return path;
});
ipcMain.on("storage-location", (event, arg) => {
event.returnValue = path.join(dirPath, "data");
});
ipcMain.on("user-data", (event, arg) => {
event.returnValue = dirPath;
});
ipcMain.handle("hide-reader", (event, arg) => {
if (readerWindow && readerWindow.isFocused()) {
readerWindow.minimize();
event.returnvalue = true;
} else if (mainWin && mainWin.isFocused()) {
mainWin.minimize();
event.returnvalue = true;
} else {
event.returnvalue = false;
}
});
ipcMain.handle("open-console", (event, arg) => {
mainWin.webContents.openDevTools();
event.returnvalue = true;
});
ipcMain.handle("focus-on-main", (event, arg) => {
if (mainWin) {
if (!mainWin.isVisible()) mainWin.show();
mainWin.focus();
}
});
ipcMain.handle("create-new-main", (event, arg) => {
if (!mainWin) {
createMainWin();
}
});
ipcMain.handle("enter-fullscreen", () => {
if (readerWindow) {
readerWindow.setFullScreen(true);
console.log("enter full");
}
});
ipcMain.handle("exit-fullscreen", () => {
if (readerWindow) {
readerWindow.setFullScreen(false);
console.log("exit full");
}
});
ipcMain.handle("switch-moyu", (event, arg) => {
let id;
if (store.get("isPreventSleep") === "yes") {
id = powerSaveBlocker.start("prevent-display-sleep");
console.log(powerSaveBlocker.isStarted(id));
}
if (readerWindow) {
readerWindow.close();
Object.assign(options, {
width: parseInt(store.get("windowWidth")),
height: parseInt(store.get("windowHeight")),
x: parseInt(store.get("windowX")),
y: parseInt(store.get("windowY")),
frame: store.get("isMergeWord") !== "yes" ? false : true,
hasShadow: store.get("isMergeWord") !== "yes" ? false : true,
transparent: store.get("isMergeWord") !== "yes" ? true : false,
});
store.set(
"isMergeWord",
store.get("isMergeWord") !== "yes" ? "yes" : "no"
);
readerWindow = new BrowserWindow(options);
readerWindow.loadURL(store.get("url"));
readerWindow.on("close", (event) => {
if (!readerWindow.isDestroyed()) {
let bounds = readerWindow.getBounds();
store.set({
windowWidth: bounds.width,
windowHeight: bounds.height,
windowX: bounds.x,
windowY: bounds.y,
});
}
if (store.get("isPreventSleep") && !readerWindow.isDestroyed()) {
id && powerSaveBlocker.stop(id);
}
// readerWindow && readerWindow.destroy();
// readerWindow = null;
});
}
event.returnvalue = false;
});
ipcMain.on("get-dirname", (event, arg) => {
event.returnValue = __dirname;
});
ipcMain.on("system-color", (event, arg) => {
event.returnValue = nativeTheme.shouldUseDarkColors || false;
});
ipcMain.on("check-main-open", (event, arg) => {
event.returnValue = mainWin ? true : false;
});
ipcMain.on("get-file-data", function (event) {
if (fs.existsSync(path.join(dirPath, "log.json"))) {
const _data = JSON.parse(
fs.readFileSync(path.join(dirPath, "log.json"), "utf8") || "{}"
);
if (_data && _data.filePath) {
filePath = _data.filePath;
fs.writeFileSync(path.join(dirPath, "log.json"), "");
}
}
event.returnValue = filePath;
filePath = null;
});
};
app.on("ready", () => {
createMainWin();
});
app.on("window-all-closed", () => {
app.quit();
});
app.on("open-file", (e, pathToFile) => {
filePath = pathToFile;
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/famez/koodo-reader.git
[email protected]:famez/koodo-reader.git
famez
koodo-reader
koodo-reader
master

搜索帮助