import { Injectable, NgZone } from '@angular/core'; import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut'; import { EMPTY, Observable, catchError, empty, from } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ShortcutService { bound: Map = new Map(); constructor(private zone: NgZone) { } register(shortcut: string, handler: ShortcutHandler) { this.bound.set(handler, shortcut); return from(register(shortcut, (s) => { this.zone.run(() => handler(s)); })); } unregister(handler: ShortcutHandler) { const shortcut = this.bound.get(handler); this.bound.delete(handler); return shortcut ? from(unregister(shortcut)) : EMPTY; } rebind(shortcut: string, handler: ShortcutHandler) { const prevShortcut = this.bound.get(handler)!; this.register(shortcut, handler).subscribe( { error: (_err) => { this.register(prevShortcut, handler); console.log("Got error during binding, rebinding previous"); return EMPTY; } }); } }