Skip to content

Commit

Permalink
(feat): initial implementation of close to system tray, organization …
Browse files Browse the repository at this point in the history
…of frontend structure
emanuelfranklyn committed Nov 13, 2024
1 parent b5e2c12 commit fa96292
Showing 28 changed files with 101 additions and 812 deletions.
42 changes: 26 additions & 16 deletions electron/backend.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Electron, { app, Tray, nativeImage, IpcMainEvent, ipcMain } from 'electron';
import { NextUrlWithParsedQuery } from 'next/dist/server/request-meta.js';
import Electron, { app, Tray, nativeImage, IpcMainEvent, ipcMain, Menu, MenuItem } from 'electron';
import { Logger, ConsoleEngine } from '@promisepending/logger.js';
import { BaseEventStructure } from './structures/index.js';
import isDev from 'electron-is-dev';
import { createServer } from 'node:http';
import { Window } from './helpers/index.js';
import serve from 'electron-serve';
import { events } from './events/index.js';
import { parse } from 'node:url';
import next from 'next';
import path from 'node:path';

@@ -49,6 +47,22 @@ export class Backend {
const icon = nativeImage.createFromPath('resources/icon.png');
this.systemTray = new Tray(icon);

const trayMenu = new Menu();
const trayMenuItem1 = new MenuItem({
label: 'Close',
type: 'normal',
click: (): void => {
process.exit(0);
},
});
trayMenu.append(trayMenuItem1);

this.systemTray.addListener('click', () => {
this.mainWindow.windowInstance.show();
});

this.systemTray.setContextMenu(trayMenu);

this.mainWindow = new Window(this, 'controller', {
width: 800,
height: 600,
@@ -61,18 +75,16 @@ export class Backend {

this.mainWindow.windowInstance.on('close', (event: Electron.Event) => {
event.preventDefault();
// Ask user if he really wants to close the application
this.mainWindow!.windowInstance.webContents.send('app.stop.ask');
this.logger.debug('Main window close event received');
});
this.mainWindow.windowInstance.hide();
// // Ask user if he really wants to close the application
// this.mainWindow!.windowInstance.webContents.send('app.stop.ask');
// this.logger.debug('Main window close event received');

this.mainWindow.windowInstance.once('closed', () => {
process.exit(0);
});

console.log(app.getAppPath());
// @ts-expect-error
const nextApp = next({
// this.mainWindow.windowInstance.once('closed', () => process.exit(0));

const nextApp = (next as unknown as typeof next.default)({
dev: isDev,
dir: path.resolve(app.getAppPath(), '..', 'renderer'),
});
@@ -84,12 +96,10 @@ export class Backend {
this.logger.info('> Starting on http://localhost:' + (process.env.SMP_PORT || 3000));
// Create a new native HTTP server (which supports hot code reloading)
createServer((request: any, res: any) => {
console.log(request);
const parsedUrl = parse(request.url);
requestHandler(request, res, parsedUrl);
requestHandler(request, res);
}).listen(process.env.SMP_PORT || 3000, () => {
this.logger.info('> Ready on http://localhost:' + (process.env.SMP_PORT || 3000));
this.mainWindow.loadURL('/');
this.mainWindow.loadURL('/lang/home');
});

}
3 changes: 2 additions & 1 deletion electron/helpers/Window.ts
Original file line number Diff line number Diff line change
@@ -22,13 +22,14 @@ export class Window {
this.restore();
this.ensureVisibleOnSomeDisplay();

console.log(import.meta.dirname);
const browserOptions: BrowserWindowConstructorOptions = {
...this.state,
...options,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
preload: path.join(import.meta.dirname, '..', 'preload.js'),
preload: path.join(import.meta.dirname, '..', 'preload', 'preload.cjs'),
...options.webPreferences,
},
};
10 changes: 8 additions & 2 deletions electron/preload.ts → electron/preload/preload.cts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
import Store from 'electron-store';
import path from 'node:path';
import fs from 'node:fs';

let Store;

// eslint-disable-next-line unicorn/prefer-top-level-await
(async (): Promise<void> => {
Store = await import('electron-store');
})();

const handler = {
send(channel: string, value: unknown): void {
ipcRenderer.send(channel, value);
@@ -46,7 +52,7 @@ const store = {
delete: (key: string) => void;
clear: () => void;
} {
const createdStore = new Store(options) as any;
const createdStore = new Store.default(options) as any;

return {
get: (key: string): unknown => { return createdStore.get(key); },
13 changes: 13 additions & 0 deletions electron/preload/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "NodeNext",
"esModuleInterop": false,
"moduleResolution": "NodeNext",
"target": "ES2017",
"lib": ["dom", "ES5"],
"allowSyntheticDefaultImports": true
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "preload.cts"]
}
2 changes: 1 addition & 1 deletion electron/tsconfig.json
Original file line number Diff line number Diff line change
@@ -20,5 +20,5 @@
"outDir": "../main"
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx", "**/*.js"]
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "preload/preload.cts"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
},
"scripts": {
"clean": "rimraf dist main renderer/out renderer/.next",
"dev": "npm run build && electron main/background.js",
"dev": "npm run build-electron && electron main/background.js",
"build-renderer": "next build renderer",
"build-electron": "tsc -p electron/",
"build": "npm run build-renderer && npm run build-electron",

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions renderer/app/(controller)/controller/present/[project]/page.tsx

This file was deleted.

This file was deleted.

This file was deleted.

44 changes: 0 additions & 44 deletions renderer/app/(controller)/controller/setup/[project]/Form.tsx

This file was deleted.

32 changes: 0 additions & 32 deletions renderer/app/(controller)/controller/setup/[project]/page.tsx

This file was deleted.

This file was deleted.

131 changes: 0 additions & 131 deletions renderer/app/(controller)/home/home.module.css

This file was deleted.

107 changes: 0 additions & 107 deletions renderer/app/(controller)/home/page.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions renderer/app/(controller)/layout.tsx

This file was deleted.

5 changes: 0 additions & 5 deletions renderer/app/(exhibition)/exhibition/exibition.module.css

This file was deleted.

6 changes: 0 additions & 6 deletions renderer/app/(exhibition)/exhibition/page.tsx

This file was deleted.

15 changes: 15 additions & 0 deletions renderer/app/[lang]/home/home.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#homePage {
--homePage-background: var(--gray-900);
--homePage-color: var(--white);

background: var(--homePage-background);
color: var(--homePage-color);
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
padding: 1rem;
overflow: hidden;
}
12 changes: 12 additions & 0 deletions renderer/app/[lang]/home/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client';
import { ReactElement } from 'react';
import styles from './home.module.css';
import Link from 'next/link';

export default function Page(): ReactElement {
return (
<div id={styles.homePage}>
<Link href='/login'>DEBUG</Link>
</div>
);
}
Empty file.
8 changes: 8 additions & 0 deletions renderer/app/[lang]/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Link from 'next/link';
import React from 'react';

export default function Login(): React.ReactElement {
return <div>
<Link href='/home'>DEBUG</Link>
</div>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import { modalBuilder } from '../components/modal/modal';
import { modalBuilder } from './components/modal/modal';
import { ReactElement, useEffect } from 'react';

export default function CloseAppHandler(): ReactElement {
2 changes: 1 addition & 1 deletion renderer/app/components/header/header.tsx
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ export default function Header(): React.ReactElement {
<div id={styles.draggableRegion}></div>
<div id={styles.headerContent}>
<div id={styles.leftSide}>
<h1 id={styles.appTitle}>GreenLight Presentation</h1>
<h1 id={styles.appTitle}>Whats&lt;Pending&gt;</h1>
</div>
<div id={styles.rightSide}>
<div id={styles.actionButtons}>
File renamed without changes.
9 changes: 8 additions & 1 deletion renderer/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React from 'react';
import './globals.css';
import Header from './components/header/header';
import CloseAppHandler from './closeAppHandler';
import styles from './general.module.css';

export default function Layout({ children }: { children: React.ReactNode }): React.ReactElement {
return (
@@ -9,7 +12,11 @@ export default function Layout({ children }: { children: React.ReactNode }): Rea
</head>
<body>
<div id='appContainer'>
{children}
<Header />
<CloseAppHandler />
<div id={styles.controlContainer}>
{children}
</div>
</div>
</body>
</html>
9 changes: 5 additions & 4 deletions renderer/app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { redirect } from 'next/navigation.js';
import React from 'react';

export default function notFound(): void {
// redirect to home page
redirect('/home');
export default function notFound(): React.ReactElement {
return (<div>
<h1>404</h1>
</div>);
}

0 comments on commit fa96292

Please sign in to comment.