(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 { Injectable, NgZone } from '@angular/core';
|
||||||
import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut';
|
import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut';
|
||||||
import { EMPTY, from } from 'rxjs';
|
import { EMPTY, Observable, Subscriber, TeardownLogic, from } from 'rxjs';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class ShortcutService {
|
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) {
|
constructor(private zone: NgZone) {
|
||||||
}
|
}
|
||||||
register(shortcut: string, handler: ShortcutHandler) {
|
|
||||||
this.bound.set(handler, shortcut);
|
|
||||||
|
|
||||||
return from(register(shortcut, (s) => {
|
register(shortcut: string) {
|
||||||
this.zone.run(() => handler(s));
|
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) {
|
this.internalHandlers.set(shortcut, [originalHandler, subscriber, teardown]);
|
||||||
const shortcut = this.bound.get(handler);
|
|
||||||
this.bound.delete(handler);
|
|
||||||
|
|
||||||
return shortcut ? from(unregister(shortcut)) : EMPTY;
|
register(shortcut, originalHandler)
|
||||||
|
|
||||||
|
return teardown;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
rebind(shortcut: string, handler: ShortcutHandler) {
|
unregister(shortcut: string) {
|
||||||
const prevShortcut = this.bound.get(handler);
|
this.internalHandlers.get(shortcut)?.[1].complete();
|
||||||
this.register(shortcut, handler).subscribe(
|
this.internalHandlers.delete(shortcut);
|
||||||
{
|
|
||||||
error: (_err) => {
|
|
||||||
if (prevShortcut) {
|
|
||||||
this.register(prevShortcut, handler);
|
|
||||||
}
|
|
||||||
return EMPTY;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
rebind_from_to(previousShortcut: string, nextShortcut: string) {
|
rebind_from_to(previousShortcut: string, nextShortcut: string) {
|
||||||
const oldHandler = [...this.bound.entries()].find((entry: [ShortcutHandler, string]) => entry[1] === previousShortcut)?.[0];
|
let [oldHandler, subscriber, teardown] = this.internalHandlers.get(previousShortcut)!;
|
||||||
this.rebind(nextShortcut, oldHandler!);
|
|
||||||
|
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