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.
47 lines
1.3 KiB
47 lines
1.3 KiB
import { Injectable, NgZone } from '@angular/core';
|
|
import { ShortcutHandler, register, unregister } from '@tauri-apps/api/globalShortcut';
|
|
import { Observable, Subscriber } from 'rxjs';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ShortcutService {
|
|
private internalHandlers: Map<string, [ShortcutHandler, Subscriber<string>, () => void]> = new Map<string, [ShortcutHandler, Subscriber<string>, () => void]>();
|
|
|
|
constructor(private zone: NgZone) {}
|
|
|
|
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);
|
|
};
|
|
|
|
this.internalHandlers.set(shortcut, [originalHandler, subscriber, teardown]);
|
|
|
|
register(shortcut, originalHandler)
|
|
|
|
return teardown;
|
|
});
|
|
}
|
|
|
|
rebind_from_to(previousShortcut: string, nextShortcut: string) {
|
|
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]);
|
|
}
|
|
}
|