Add basic logging, debugging and store.

This commit is contained in:
Patrick Fic
2025-03-11 15:49:22 -07:00
parent 87b261178c
commit 69c504c0c6
11 changed files with 2340 additions and 68 deletions

View File

@@ -1,7 +1,9 @@
import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { electronApp, is, optimizer } from "@electron-toolkit/utils";
import { app, BrowserWindow, shell } from "electron";
import log from "electron-log/main";
import { join } from "path";
import icon from "../../resources/icon.png?asset";
import ErrorTypeCheck from "../util/errorTypeCheck";
function createWindow(): void {
// Create the browser window.
@@ -10,65 +12,77 @@ function createWindow(): void {
height: 670,
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
...(process.platform === "linux" ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
}
})
preload: join(__dirname, "../preload/index.js"),
sandbox: false,
},
});
mainWindow.on('ready-to-show', () => {
mainWindow.show()
})
mainWindow.on("ready-to-show", () => {
mainWindow.show();
});
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
shell.openExternal(details.url);
return { action: "deny" };
});
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
if (is.dev && process.env["ELECTRON_RENDERER_URL"]) {
mainWindow.loadURL(process.env["ELECTRON_RENDERER_URL"]);
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
mainWindow.loadFile(join(__dirname, "../renderer/index.html"));
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
app.whenReady().then(async () => {
// Set app user model id for windows
electronApp.setAppUserModelId('com.electron')
electronApp.setAppUserModelId("com.electron");
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
app.on("browser-window-created", (_, window) => {
optimizer.watchWindowShortcuts(window);
});
// IPC test
ipcMain.on('ping', () => console.log('pong'))
//Dynamically load ipcMain handlers once ready.
try {
// Replace 'path/to/your/file' with the actual path to your file
const module = await import("./ipc/ipcMainConfig");
createWindow()
// You can now use anything exported from the module
// For example:
// module.someFunction()
log.debug("Successfully loaded ipcMainConfig", module);
} catch (error) {
log.error("Failed to load ipcMainconfig", {
...ErrorTypeCheck(error),
});
}
app.on('activate', function () {
createWindow();
app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
})
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

View File

@@ -0,0 +1,6 @@
import { ipcMain } from "electron";
import ipcTypes from "../../util/ipcTypes.json";
ipcMain.on(ipcTypes.toMain.test, (payload: any) =>
console.log("** Verify that ipcMain is loaded and working.", payload)
);

14
src/main/store/store.ts Normal file
View File

@@ -0,0 +1,14 @@
import Store from "electron-store";
const store = new Store({
defaults: {
filePaths: [],
runWatcherOnStartup: true,
polling: {
enabled: false,
pollingInterval: 30000,
},
},
});
export default store;