Using decorators, you can efficiently manage Tray events. Here’s how you can listen to Tray events using the @TrayListener
decorator:
@TrayListener(TrayEventEnum.CLICK)
onTrayClick(event: TrayEventType[TrayEventEnum.CLICK]) {
if (this.window?.isVisible()) {
this.window?.hide();
} else {
this.window?.show();
}
console.log('Tray clicked');
}
@TrayListener(TrayEventEnum.DOUBLE_CLICK)
onTrayDoubleClick(event: TrayEventType[TrayEventEnum.DOUBLE_CLICK]) {
console.log('Tray double-clicked');
}
Similarly, you can use decorators to handle BrowserWindow events effectively. For instance, to manage the close
event, use the @WindowListener
decorator:
@WindowListener(WindowEventEnum.CLOSED)
onWindowClose(event: WindowEventType[WindowEventEnum.CLOSED]) {
this.window = null;
console.log('Window closed');
}
You can also use decorators to manage IPC events effectively. For example, to handle the UPDATE_TRAY_TEXT
event from the main process, you can use the @IpcListener
decorator:
@IpcListener(IPCChannelEnum.UPDATE_TRAY_TEXT)
onUpdateText(event: IpcMainEvent, timeLeft: IPCChannelType[IPCChannelEnum.UPDATE_TRAY_TEXT]): void {
console.log(timeLeft);
}
Use the useElectronService
hook to get ipcRenderer.
const { ipcRenderer } = useElectronService();
if (ipcRenderer) {
ipcRenderer?.send(IPCChannelEnum.UPDATE_TRAY_TEXT, "hello");
}
Use the @injectable
decorator to mark your classes for dependency injection.
import { injectable } from 'inversify';
@injectable()
export class FileService {
// Implementation
}
Inject dependencies into your classes using the @inject
decorator.
@injectable()
export class MainWindow extends MainWindowBaseClass implements OnAppReady {
private readonly TRAY_ICON_PATH = '/src/assets/icons/favicon.256x256.png';
constructor(@inject(FileService) protected readonly fileService: FileService) {
super();
}
// Other methods and properties
}
Configure the DI container with the services you need.
const container = useProvide([FileService]);
Resolve your main class with its dependencies from the container.
const mainWindowWithDependencies = container.resolve(MainWindow);