(Unreleased) Should now ignore next/prev hotkeys if the overlay isn't visible. Also improved binding/unbinding to be more rxjs-ish
parent
a8334cbd03
commit
21333908ec
@ -1,45 +1,53 @@
|
||||
import { Injectable, NgZone } from '@angular/core';
|
||||
import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut';
|
||||
import { EMPTY, from } from 'rxjs';
|
||||
import { EMPTY, Observable, Subscriber, TeardownLogic, from } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ShortcutService {
|
||||
bound: Map<ShortcutHandler, string> = new Map<ShortcutHandler, string>();
|
||||
private internalHandlers: Map<string, [ShortcutHandler, Subscriber<string>, () => void]> = new Map<string, [ShortcutHandler, Subscriber<string>, () => void]>();
|
||||
|
||||
constructor(private zone: NgZone) {
|
||||
}
|
||||
register(shortcut: string, handler: ShortcutHandler) {
|
||||
this.bound.set(handler, shortcut);
|
||||
|
||||
return from(register(shortcut, (s) => {
|
||||
this.zone.run(() => handler(s));
|
||||
}));
|
||||
}
|
||||
register(shortcut: string) {
|
||||
return new Observable<string>((subscriber) => {
|
||||
|
||||
let originalHandler: ShortcutHandler = (s) => this.zone.run(() => subscriber.next(s));
|
||||
|
||||
const teardown = () => {
|
||||
unregister(shortcut);
|
||||
this.internalHandlers.delete(shortcut);
|
||||
};
|
||||
|
||||
unregister(handler: ShortcutHandler) {
|
||||
const shortcut = this.bound.get(handler);
|
||||
this.bound.delete(handler);
|
||||
this.internalHandlers.set(shortcut, [originalHandler, subscriber, teardown]);
|
||||
|
||||
return shortcut ? from(unregister(shortcut)) : EMPTY;
|
||||
register(shortcut, originalHandler)
|
||||
|
||||
return teardown;
|
||||
});
|
||||
}
|
||||
|
||||
rebind(shortcut: string, handler: ShortcutHandler) {
|
||||
const prevShortcut = this.bound.get(handler);
|
||||
this.register(shortcut, handler).subscribe(
|
||||
{
|
||||
error: (_err) => {
|
||||
if (prevShortcut) {
|
||||
this.register(prevShortcut, handler);
|
||||
}
|
||||
return EMPTY;
|
||||
}
|
||||
});
|
||||
unregister(shortcut: string) {
|
||||
this.internalHandlers.get(shortcut)?.[1].complete();
|
||||
this.internalHandlers.delete(shortcut);
|
||||
}
|
||||
|
||||
|
||||
rebind_from_to(previousShortcut: string, nextShortcut: string) {
|
||||
const oldHandler = [...this.bound.entries()].find((entry: [ShortcutHandler, string]) => entry[1] === previousShortcut)?.[0];
|
||||
this.rebind(nextShortcut, oldHandler!);
|
||||
let [oldHandler, subscriber, teardown] = this.internalHandlers.get(previousShortcut)!;
|
||||
|
||||
subscriber.remove(teardown);
|
||||
teardown();
|
||||
|
||||
teardown = () => {
|
||||
unregister(nextShortcut);
|
||||
this.internalHandlers.delete(nextShortcut);
|
||||
};
|
||||
|
||||
register(nextShortcut, oldHandler);
|
||||
|
||||
this.internalHandlers.set(nextShortcut, [oldHandler, subscriber, teardown]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue