aboutsummaryrefslogtreecommitdiff
path: root/src/content/controllers/KeymapController.ts
blob: 01ac8b5cd98bac53d417f35bb962fa7b84c6b7e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { injectable, inject } from "tsyringe";
import KeymapUseCase from "../usecases/KeymapUseCase";
import Key from "../../shared/settings/Key";
import OperatorFactory from "../operators/OperatorFactory";

@injectable()
export default class KeymapController {
  constructor(
    private keymapUseCase: KeymapUseCase,

    @inject("OperatorFactory")
    private readonly operatorFactory: OperatorFactory
  ) {}

  // eslint-disable-next-line complexity, max-lines-per-function
  press(key: Key): boolean {
    const nextOp = this.keymapUseCase.nextOps(key);
    if (nextOp === null) {
      return false;
    }

    // Do not await asynchronous methods to return a boolean immidiately. The
    // caller requires the synchronous response from the callback to identify
    // to continue of abandon the event propagation.
    this.operatorFactory
      .create(nextOp.op, nextOp.repeat)
      .run()
      .catch(console.error);

    return true;
  }

  onBlurWindow() {
    this.keymapUseCase.cancel();
  }
}