Skip to content

Commit

Permalink
Notifications bridged.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yyassin committed Jan 7, 2024
1 parent cc67eb7 commit c3aa0ac
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions client/electron/ipc/ipcActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const IPC_ACTIONS = {
UNMAXIMIZE_WINDOW: 'unmaximize',
MINIMIZE_WINDOW: 'minimize',
CLOSE_WINDOW: 'close',
HANDLE_NOTIFICATION: 'notification',
};
20 changes: 18 additions & 2 deletions client/electron/ipc/ipcHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { BrowserWindow, IpcMainEvent, ipcMain } from 'electron';
import { IPC_ACTIONS } from './ipcActions';
import { notification } from '../main';

export const shared = { global_RecvMaximizedEventFlag: false };

const { MAXIMIZE_WINDOW, UNMAXIMIZE_WINDOW, MINIMIZE_WINDOW, CLOSE_WINDOW } =
IPC_ACTIONS;
const {
MAXIMIZE_WINDOW,
UNMAXIMIZE_WINDOW,
MINIMIZE_WINDOW,
CLOSE_WINDOW,
HANDLE_NOTIFICATION,
} = IPC_ACTIONS;

const getWindow = (event: IpcMainEvent) => {
const webContents = event?.sender;
Expand All @@ -23,11 +29,21 @@ const handleWindowTitlebarEvent = (event: IpcMainEvent, type: string) => {
}
};

const handleNotification = (
event: IpcMainEvent,
{ title, body }: { title: string; body: string },
) => {
notification.title = title;
notification.body = body;
notification.show();
};

const eventToCallback = {
[MAXIMIZE_WINDOW]: handleWindowTitlebarEvent,
[UNMAXIMIZE_WINDOW]: handleWindowTitlebarEvent,
[MINIMIZE_WINDOW]: handleWindowTitlebarEvent,
[CLOSE_WINDOW]: handleWindowTitlebarEvent,
[HANDLE_NOTIFICATION]: handleNotification,
};

export const registerIPCHandlers = () => {
Expand Down
15 changes: 13 additions & 2 deletions client/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
globalShortcut,
ipcMain,
Menu,
Notification,
Tray,
} from 'electron';
import isDev from 'electron-is-dev';
Expand All @@ -25,15 +26,18 @@ process.env.PUBLIC = app.isPackaged
? process.env.DIST
: path.join(process.env.DIST, '../public');

const iconPath = path.join(process.env.PUBLIC ?? './', 'doodles-icon.png');

let win: BrowserWindow | null;
export let notification: Notification;
let tray: Tray | null;
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - [email protected]
const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL'];

let isClickThrough = false;
function createWindow() {
win = new BrowserWindow({
icon: path.join(process.env.PUBLIC ?? './', 'doodles-icon.png'),
icon: iconPath,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
Expand All @@ -43,7 +47,7 @@ function createWindow() {
frame: false,
title: 'Doodles',
});
tray = new Tray(path.join(process.env.PUBLIC ?? './', 'doodles-icon.png'));
tray = new Tray(iconPath);
tray.setIgnoreDoubleClickEvents(true);
const trayMenu = Menu.buildFromTemplate([
{
Expand Down Expand Up @@ -71,6 +75,13 @@ function createWindow() {
tray.setContextMenu(trayMenu);
tray.setToolTip('Doodles');

notification = new Notification({ icon: iconPath });
notification.on('click', () => {
if (!win?.isVisible() || win?.isMinimized()) {
win?.show();
}
});

// Test active push message to Renderer-process.
win.webContents.on('did-finish-load', () => {
win?.webContents.send('main-process-message', new Date().toLocaleString());
Expand Down
6 changes: 6 additions & 0 deletions client/src/hooks/useShortcut.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ZOOM } from '@/constants';
import { ipcAPI } from '@/data/ipc/ipcMessages';
import { clamp } from '@/lib/misc';
import { useAppStore } from '@/stores/AppStore';
import { useCanvasElementStore } from '@/stores/CanvasElementsStore';
Expand Down Expand Up @@ -106,6 +107,11 @@ export const useShortcuts = () => {
}
break;
}
case 'KeyR': {
if (e.ctrlKey) {
ipcAPI.notification({ title: 'Test', body: 'Test Body' });
}
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion client/src/views/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function SignInPage() {
};

return (
<Card>
<Card className="rounded-none">
<CardHeader className="space-y-1">
<CardTitle className="text-2xl">Log In</CardTitle>
<CardDescription>
Expand Down
2 changes: 1 addition & 1 deletion client/src/views/SignUpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export default function SignUp() {
>(null);

return (
<Card>
<Card className="rounded-none">
<CardHeader className="space-y-1">
<CardTitle className="text-2xl">Create an account</CardTitle>
<CardDescription>
Expand Down

0 comments on commit c3aa0ac

Please sign in to comment.