fix(desktop): shortcut scope switching logic

- Replaced the use of `useShortcutScope` with `useConditionalHotkeyScope` in the Modal component for improved hotkey handling.
- Updated the `useSwitchHotkeyScope` hook to maintain the current active scopes using `useLayoutEffect`.
- Modified the SubviewLayout to conditionally enable the Escape key hotkey based on the active scope.

These changes streamline hotkey management and enhance user experience across modal and subview interactions.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2025-05-22 13:40:23 +08:00
parent f52cf91d9b
commit 582303fcbf
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
3 changed files with 12 additions and 14 deletions

View File

@ -30,8 +30,8 @@ import { AppErrorBoundary } from "~/components/common/AppErrorBoundary"
import { SafeFragment } from "~/components/common/Fragment"
import { m } from "~/components/common/Motion"
import { ErrorComponentType } from "~/components/errors/enum"
import { ElECTRON_CUSTOM_TITLEBAR_HEIGHT } from "~/constants"
import { useSwitchHotKeyScope } from "~/hooks/common"
import { ElECTRON_CUSTOM_TITLEBAR_HEIGHT, HotkeyScope } from "~/constants"
import { useConditionalHotkeyScope } from "~/hooks/common"
import { modalStackAtom } from "./atom"
import { MODAL_STACK_Z_INDEX, modalMontionConfig } from "./constants"
@ -192,7 +192,7 @@ export const ModalInternal = memo(function Modal({
}
}, [currentIsClosing])
useShortcutScope()
useConditionalHotkeyScope(HotkeyScope.Modal, true)
const modalStyle = resizeableStyle
const { handleSelectStart, handleDetectSelectEnd, isSelectingRef } = useModalSelect()
@ -403,16 +403,6 @@ export const ModalInternal = memo(function Modal({
)
})
const useShortcutScope = () => {
const switchHotkeyScope = useSwitchHotKeyScope()
useEffect(() => {
switchHotkeyScope("Modal")
return () => {
switchHotkeyScope("Home")
}
}, [switchHotkeyScope])
}
const ModalContext: FC<
PropsWithChildren & {
modalContextProps: CurrentModalContentProps

View File

@ -28,6 +28,10 @@ export const useConditionalHotkeyScopeFn = (scope: HotkeyScope, replaceAll = tru
const { enableScope, disableScope, activeScopes } = useHotkeysContext()
const currentScopeRef = useRef(activeScopes)
useLayoutEffect(() => {
currentScopeRef.current = activeScopes
}, [activeScopes])
return useCallback(() => {
const currentScope = currentScopeRef.current
if (replaceAll) {

View File

@ -13,6 +13,7 @@ import { NavigationType, Outlet, useLocation, useNavigate, useNavigationType } f
import { FABContainer, FABPortable } from "~/components/ui/fab"
import { HotkeyScope } from "~/constants"
import { useConditionalHotkeyScope } from "~/hooks/common"
import { useHotkeyScope } from "~/providers/hotkey-provider"
import { useSubViewTitleValue } from "./hooks"
@ -65,7 +66,10 @@ export function SubviewLayout() {
navigate(-1)
}
}
useHotkeys("esc", backHandler)
const activeScope = useHotkeyScope()
useHotkeys("Escape", backHandler, {
enabled: activeScope.includes(HotkeyScope.SubLayer),
})
return (
<div className="relative flex size-full">
<div