Skip to content

Commit

Permalink
Merge pull request #8652 from ever-co/fix/desktop-window-helper
Browse files Browse the repository at this point in the history
[Fix] Electron Store & Logger
  • Loading branch information
rahul-rocket authored Dec 30, 2024
2 parents 5879cb9 + 5b19da9 commit e9b1e51
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/desktop-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './lib/interfaces/base-window';
export * from './lib/interfaces/ibase-window';
export * from './lib/interfaces/iwindow-config';
export * from './lib/interfaces/iwindow.manager';
export * from './lib/electron-helpers';
export * from './lib/interfaces/types';
11 changes: 3 additions & 8 deletions packages/desktop-core/src/lib/concretes/window-config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { BrowserWindowConstructorOptions } from 'electron';
import { Store } from '../electron-helpers'
import { IWindowConfig } from '../interfaces';

import log from 'electron-log';
console.log = log.log;
Object.assign(console, log.functions);

const Store = require('electron-store');
const store = new Store();

export class WindowConfig implements IWindowConfig {
private _options?: BrowserWindowConstructorOptions;
private _path: string;
Expand All @@ -16,6 +10,7 @@ export class WindowConfig implements IWindowConfig {
constructor(hash: string, path: string, options?: BrowserWindowConstructorOptions) {
this._hash = hash;
this._path = path;

this.options = {
webPreferences: {
nodeIntegration: true,
Expand All @@ -26,7 +21,7 @@ export class WindowConfig implements IWindowConfig {
title: '',
show: false,
center: true,
icon: store.get('filePath').iconPath,
icon: Store.get('filePath')?.iconPath,
...options
};
}
Expand Down
13 changes: 13 additions & 0 deletions packages/desktop-core/src/lib/electron-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ElectronLog from 'electron-log';
import ElectronStore from 'electron-store';
import { StoreSchema } from './interfaces/types';

// Set up the logger
console.log = ElectronLog.log;
Object.assign(console, ElectronLog.functions);

// Create a typed store instance
const Store = new ElectronStore<StoreSchema>();

// Export the logger and store
export { ElectronLog, Store };
32 changes: 32 additions & 0 deletions packages/desktop-core/src/lib/interfaces/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Represents the structure of a file path object used in the application.
*/
export interface FilePath {
/**
* The path to the icon file associated with this file path.
*/
iconPath: string;
}

/**
* Defines the structure of the Electron Store schema for type safety and consistency.
*
* The `StoreSchema` interface represents the schema used in the Electron Store.
* Each property in the schema corresponds to a specific key in the store and
* ensures type safety when accessing or modifying the data.
*
* Example:
* ```
* const store = new Store<StoreSchema>();
* store.set('filePath', { iconPath: '/path/to/icon.png' });
* const filePath = store.get('filePath');
* console.log(filePath.iconPath); // Outputs: '/path/to/icon.png'
* ```
*/
export interface StoreSchema {
/**
* Represents the file path configuration stored in the Electron Store.
* Includes the `iconPath` property which specifies the path to an icon.
*/
filePath: FilePath;
}
58 changes: 42 additions & 16 deletions packages/desktop-window/src/lib/desktop-window-about.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
import * as remoteMain from '@electron/remote/main';
import { WindowManager, RegisteredWindow, Store } from '@gauzy/desktop-core';
import { BrowserWindow, Menu } from 'electron';
import * as url from 'url';

import log from 'electron-log';
import { WindowManager, RegisteredWindow } from '@gauzy/desktop-core';
console.log = log.log;
Object.assign(console, log.functions);

const Store = require('electron-store');
const store = new Store();

export async function createAboutWindow(filePath, preloadPath?) {
/**
* Creates or shows the 'About' window for the application.
*
* This function checks if an 'About' window already exists. If it does, the window is shown.
* Otherwise, it creates a new 'About' window using specified settings and registers it
* with the window manager. The function also sets up event listeners for window actions.
*
* @param {string} filePath - The file path to load in the 'About' window.
* @param {string | undefined} [preloadPath] - Optional path to the preload script for the 'About' window.
* @returns {Promise<Electron.BrowserWindow>} - The created or shown BrowserWindow instance.
*/
export async function createAboutWindow(filePath: string, preloadPath?: string): Promise<Electron.BrowserWindow> {
const mainWindowSettings: Electron.BrowserWindowConstructorOptions = windowSetting(preloadPath);
const manager = WindowManager.getInstance();

const allwindows = BrowserWindow.getAllWindows();
const aboutWindows = allwindows.find((win) => win.getTitle() === 'About');
if (aboutWindows) {
aboutWindows.show();
return aboutWindows;
// Check if an 'About' window already exists and show it
const allWindows = BrowserWindow.getAllWindows();
const aboutWindow = allWindows.find((win) => win.getTitle() === 'About');
if (aboutWindow) {
aboutWindow.show();
return aboutWindow;
}

// Create a new 'About' window
const window = new BrowserWindow(mainWindowSettings);
remoteMain.enable(window.webContents);

const launchPath = url.format({
pathname: filePath,
protocol: 'file:',
slashes: true,
hash: '/about'
});

window.setIcon(store.get('filePath').iconPath);
// Set the window icon from the store
window.setIcon(Store.get('filePath').iconPath);
window.hide();

await window.loadURL(launchPath);
window.setMenu(null);

// Set up event listeners for the window
window.on('show', () => {
Menu.getApplicationMenu().getMenuItemById('gauzy-about').enabled = false;
});
Expand All @@ -45,14 +55,28 @@ export async function createAboutWindow(filePath, preloadPath?) {
event.preventDefault();
});

// Register the window with the window manager
manager.register(RegisteredWindow.ABOUT, window);

// Send a message to hide the menu if a preload path is provided
if (preloadPath) {
window.webContents.send('hide-menu');
}

return window;
}

const windowSetting = (preloadPath) => {
/**
* Configures and returns the settings for an Electron BrowserWindow.
*
* This function allows customization of the BrowserWindow's settings, including
* web preferences, dimensions, and other options. If a `preloadPath` is provided,
* it sets the preload script in the web preferences.
*
* @param {string | undefined} preloadPath - Optional path to the preload script for the BrowserWindow.
* @returns {Electron.BrowserWindowConstructorOptions} - The configured settings for the BrowserWindow.
*/
const windowSetting = (preloadPath?: string): Electron.BrowserWindowConstructorOptions => {
const mainWindowSettings: Electron.BrowserWindowConstructorOptions = {
frame: true,
resizable: false,
Expand All @@ -72,8 +96,10 @@ const windowSetting = (preloadPath) => {
maximizable: false,
show: false
};

if (preloadPath) {
mainWindowSettings.webPreferences.preload = preloadPath;
}

return mainWindowSettings;
};

0 comments on commit e9b1e51

Please sign in to comment.