summaryrefslogtreecommitdiff
path: root/src/components/FpsMultiplierControl.tsx
blob: 2c50be1059e36e3b5105396caba32f38efc9b149 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { DialogButton, Focusable, PanelSectionRow } from "@decky/ui";
import { useEffect, useRef } from "react";
import { ConfigurationData } from "../config/configSchema";
import { MULTIPLIER } from "../config/generatedConfigSchema";
import t from "../i18n/i18n";

interface FpsMultiplierControlProps {
  config: ConfigurationData;
  onConfigChange: (fieldName: keyof ConfigurationData, value: boolean | number | string | string[]) => Promise<void>;
  autoFocus?: boolean;
  onAutoFocus?: () => void;
}

export function FpsMultiplierControl({
  config,
  onConfigChange,
  autoFocus = false,
  onAutoFocus,
}: FpsMultiplierControlProps) {
  const focusableRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (!autoFocus) return;
    const frame = requestAnimationFrame(() => {
      const target = focusableRef.current?.querySelector<HTMLElement>(
        '[role="button"], [role="slider"]',
      );
      target?.focus();
      onAutoFocus?.();
    });
    return () => cancelAnimationFrame(frame);
  }, [autoFocus, onAutoFocus]);

  return (
    <PanelSectionRow>
      <Focusable
        ref={focusableRef}
        noFocusRing
        style={{
          marginTop: "6px",
          marginBottom: "6px",
          display: "flex",
          justifyContent: "center",
          alignItems: "center",
        }}
        flow-children="horizontal"
      >
        <DialogButton
          style={{
            marginLeft: "0px",
            height: "30px",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            padding: "5px 0px 0px 0px",
            minWidth: "40px",
          }}
          onClick={() => void onConfigChange(MULTIPLIER, Math.max(1, config.multiplier - 1))}
          disabled={config.multiplier <= 1}
        >
          −
        </DialogButton>
        <div
          style={{
            marginLeft: "20px",
            marginRight: "20px",
            fontSize: "16px",
            fontWeight: "bold",
            color: config.multiplier > 4 ? "red" : "white",
            minWidth: "60px",
            textAlign: "center",
          }}
        >
          {config.multiplier < 2 ? t("MULTIPLIER_OFF", "OFF") : `${config.multiplier}X`}
        </div>
        <DialogButton
          style={{
            marginLeft: "0px",
            height: "30px",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            padding: "5px 0px 0px 0px",
            minWidth: "40px",
          }}
          onClick={() => void onConfigChange(MULTIPLIER, Math.min(4, config.multiplier + 1))}
          disabled={config.multiplier >= 4}
        >
          +
        </DialogButton>
      </Focusable>
    </PanelSectionRow>
  );
}