You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Nothing/src/app/services/shortcut.service.ts

40 lines
1.1 KiB

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<ShortcutHandler, string> = new Map<ShortcutHandler, string>();
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;
}
});
}
}