-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor hotkeys setting * update UI * refactor * fix toast
- Loading branch information
Showing
7 changed files
with
163 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
enjoy/src/renderer/components/preferences/hotkeys-settings.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { t } from "i18next"; | ||
import { | ||
AlertDialog, | ||
AlertDialogCancel, | ||
AlertDialogContent, | ||
AlertDialogFooter, | ||
AlertDialogHeader, | ||
AlertDialogTitle, | ||
Button, | ||
toast, | ||
} from "@renderer/components/ui"; | ||
import { HotKeysSettingsProviderContext } from "@renderer/context"; | ||
import { useContext, useMemo, useEffect } from "react"; | ||
|
||
export const HotkeysSettings = ({ | ||
open, | ||
name, | ||
keyName, | ||
onOpenChange, | ||
}: { | ||
open: boolean; | ||
name: string; | ||
keyName: string; | ||
onOpenChange: (open: boolean) => void; | ||
}) => { | ||
const { | ||
changeHotkey, | ||
currentHotkeys, | ||
recordingHotkeys, | ||
resetRecordingHotkeys, | ||
startRecordingHotkeys, | ||
stopRecordingHotkeys, | ||
isRecording, | ||
} = useContext(HotKeysSettingsProviderContext); | ||
|
||
const joinedKeys = useMemo( | ||
() => [...recordingHotkeys].join("+"), | ||
[recordingHotkeys] | ||
); | ||
|
||
const changeKeyMap = async () => { | ||
const ret = (await changeHotkey(keyName, recordingHotkeys)) as unknown as { | ||
error: "conflict" | "invalid"; | ||
data: string | string[]; | ||
input: string; | ||
}; | ||
stopRecordingHotkeys(); | ||
const { error, data, input } = ret ?? {}; | ||
|
||
if (error === "conflict") { | ||
toast.error( | ||
t("customizeShortcutsConflictToast", { | ||
input, | ||
otherHotkeyName: (data as string[]) | ||
.map((str) => t(str.charAt(0).toLowerCase() + str.slice(1))) | ||
.join(","), | ||
}) | ||
); | ||
} else if (error === "invalid") { | ||
toast.error(t("customizeShortcutsInvalidToast")); | ||
} else { | ||
toast.success(t("customizeShortcutsUpdated")); | ||
onOpenChange(false); | ||
} | ||
}; | ||
|
||
const reset = () => { | ||
stopRecordingHotkeys(); | ||
resetRecordingHotkeys(); | ||
}; | ||
|
||
// ensure recording disabled when dialog close | ||
useEffect(() => { | ||
return () => { | ||
stopRecordingHotkeys(); | ||
}; | ||
}, [open]); | ||
|
||
return ( | ||
<AlertDialog open={open} onOpenChange={onOpenChange}> | ||
<AlertDialogContent> | ||
<AlertDialogHeader> | ||
<AlertDialogTitle>{name}</AlertDialogTitle> | ||
</AlertDialogHeader> | ||
<div> | ||
{isRecording ? ( | ||
<div className=""> | ||
<div className="flex justify-center mb-4"> | ||
<Button variant="secondary"> | ||
{joinedKeys.length > 0 ? ( | ||
<span className="text-sm">{joinedKeys}</span> | ||
) : ( | ||
<span className="font-mono">-</span> | ||
)} | ||
</Button> | ||
</div> | ||
<div className="py-2 text-center text-sm text-muted-foreground"> | ||
{t("customizeShortcutsRecordingTip")} | ||
</div> | ||
</div> | ||
) : ( | ||
<div className=""> | ||
<div className="flex justify-center mb-4"> | ||
<Button | ||
variant="outline" | ||
className="font-mono" | ||
onClick={() => { | ||
startRecordingHotkeys(); | ||
}} | ||
> | ||
{currentHotkeys[keyName]} | ||
</Button> | ||
</div> | ||
<div className="py-2 text-center text-sm text-muted-foreground"> | ||
{t("customizeShortcutsTip")} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
<AlertDialogFooter> | ||
<Button disabled={!isRecording || !joinedKeys} onClick={changeKeyMap}> | ||
{t("save")} | ||
</Button> | ||
<AlertDialogCancel onClick={reset}>{t("cancel")}</AlertDialogCancel> | ||
</AlertDialogFooter> | ||
</AlertDialogContent> | ||
</AlertDialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.