Initial hotkeys for frontend

main
Talon 2024-08-30 16:15:15 +02:00
parent 36a67a2ca1
commit 1f82842293
1 changed files with 26 additions and 1 deletions

View File

@ -36,6 +36,8 @@ export class MainView extends View {
private messageElementMap: Map<number, UINode> = new Map();
private hotkeyMap: Map<string, () => void> = new Map();
public onActivate(): void {
if (!state.currentChannel) {
if (state.defaultChannelId) {
@ -71,11 +73,14 @@ export class MainView extends View {
}
}
});
}
document.addEventListener("keydown", (e) => this.handleHotkey(e));
}
public onDeactivate(): void {
clearInterval(this.updateInterval);
// unregister hotkey
document.removeEventListener("keydown", (e) => this.handleHotkey(e));
}
public onCreate(): void {
@ -124,6 +129,7 @@ export class MainView extends View {
});
this.channelInfoButton.onClick(() => this.handleChannelInfoButton());
this.imageInput.onClick(async () => this.handleImageButton());
this.setHotkeys();
}
public onDestroy(): void {
@ -564,4 +570,23 @@ export class MainView extends View {
const photo = await new TakePhotoDialog().open();
this.uploadImage(photo);
}
private setHotkeys() {
this.hotkeyMap.set("s", () => this.openSettingsDialog());
this.hotkeyMap.set("c", () => this.channelSwitcher.focus());
this.hotkeyMap.set("x", () => this.handleChannelInfoButton());
this.hotkeyMap.set("f", () => this.openSearchDialog());
this.hotkeyMap.set("v", () => this.handleVoiceMessageButton());
this.hotkeyMap.set(" ", () => this.messageInput.focus());
}
private handleHotkey(e: KeyboardEvent) {
if (e.altKey) {
const action = this.hotkeyMap.get(e.key);
if (action) {
e.preventDefault();
action();
}
}
}
}