Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions ui/desktop/src/components/ui/BackButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useCallback } from 'react';
import { ArrowLeft } from 'lucide-react';
import { Button } from './button';
import type { VariantProps } from 'class-variance-authority';
Expand All @@ -21,15 +21,46 @@ const BackButton: React.FC<BackButtonProps> = ({
showText = true,
...props
}) => {
const handleExit = () => {
const handleExit = useCallback(() => {
if (onClick) {
onClick(); // Custom onClick handler passed via props
} else if (window.history.length > 1) {
window.history.back(); // Navigate to the previous page
} else {
console.warn('No history to go back to');
}
};
}, [onClick]);

// Set up mouse back button event listener.
useEffect(() => {
const handleMouseBack = () => {
handleExit();
};

if (window.electron) {
const mouseBackHandler = (e: MouseEvent) => {
// MouseButton 3 or 4 is typically back button.
if (e.button === 3 || e.button === 4) {
handleExit();
e.preventDefault();
}
};

window.electron.on('mouse-back-button-clicked', handleMouseBack);

// Also listen for mouseup events directly, for better OS compatibility.
document.addEventListener('mouseup', mouseBackHandler);

return () => {
if (window.electron) {
window.electron.off('mouse-back-button-clicked', handleMouseBack);
}
document.removeEventListener('mouseup', mouseBackHandler);
};
}

return undefined;
}, [handleExit]);

return (
<Button
Expand Down
15 changes: 15 additions & 0 deletions ui/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Tray,
} from 'electron';
import { Buffer } from 'node:buffer';
import { MouseUpEvent } from './types/electron';
import fs from 'node:fs/promises';
import fsSync from 'node:fs';
import started from 'electron-squirrel-startup';
Expand Down Expand Up @@ -769,6 +770,20 @@ const createChat = async (
}
});

mainWindow.on('app-command', (e, cmd) => {
if (cmd === 'browser-backward') {
mainWindow.webContents.send('mouse-back-button-clicked');
e.preventDefault();
}
});

mainWindow.webContents.on('mouse-up', (_event: MouseUpEvent, mouseButton: number) => {
// MouseButton 3 is the back button.
if (mouseButton === 3) {
mainWindow.webContents.send('mouse-back-button-clicked');
}
});

windowMap.set(windowId, mainWindow);
// Handle window closure
mainWindow.on('closed', () => {
Expand Down
11 changes: 11 additions & 0 deletions ui/desktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type ElectronAPI = {
setWakelock: (enable: boolean) => Promise<boolean>;
getWakelockState: () => Promise<boolean>;
openNotificationsSettings: () => Promise<boolean>;
onMouseBackButtonClicked: (callback: () => void) => void;
offMouseBackButtonClicked: (callback: () => void) => void;
on: (
channel: string,
callback: (event: Electron.IpcRendererEvent, ...args: unknown[]) => void
Expand Down Expand Up @@ -174,6 +176,15 @@ const electronAPI: ElectronAPI = {
setWakelock: (enable: boolean) => ipcRenderer.invoke('set-wakelock', enable),
getWakelockState: () => ipcRenderer.invoke('get-wakelock-state'),
openNotificationsSettings: () => ipcRenderer.invoke('open-notifications-settings'),
onMouseBackButtonClicked: (callback: () => void) => {
// Wrapper that ignores the event parameter.
const wrappedCallback = (_event: Electron.IpcRendererEvent) => callback();
ipcRenderer.on('mouse-back-button-clicked', wrappedCallback);
return wrappedCallback;
},
offMouseBackButtonClicked: (callback: () => void) => {
ipcRenderer.removeListener('mouse-back-button-clicked', callback);
},
on: (
channel: string,
callback: (event: Electron.IpcRendererEvent, ...args: unknown[]) => void
Expand Down
14 changes: 14 additions & 0 deletions ui/desktop/src/types/electron.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare namespace ElectronTypes {
interface Event {
preventDefault: () => void;
sender: unknown;
}

interface IpcRendererEvent extends Event {
senderId: number;
}

interface MouseUpEvent extends Event {
button: number;
}
}
13 changes: 13 additions & 0 deletions ui/desktop/src/types/electron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface ElectronEvent {
preventDefault: () => void;
sender: unknown;
}

export interface IpcRendererEvent extends ElectronEvent {
senderId: number;
}

// Mouse event
export interface MouseUpEvent extends ElectronEvent {
button: number;
}
1 change: 1 addition & 0 deletions ui/desktop/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './electron';
Loading