Files
cherry-studio/src/renderer/src/components/TopView/index.tsx
T
Phantom a21fc91915 fix: unexpected quitting full screen mode (#9200)
* fix(Inputbar): 修正拼写错误,将expend改为expand

* fix: 修复Escape键事件冒泡问题并改进全屏处理

修复多个组件中Escape键事件未阻止冒泡的问题
添加全屏控制IPC通道
将全屏退出逻辑移至渲染进程处理
移除主进程中冗余的全屏退出处理代码

* fix(SelectModelPopup): 修复键盘事件处理并移除无效的useEffect

将键盘事件监听从window移动到Modal容器,避免事件冒泡问题
移除无效的useEffect并更新键盘事件类型定义

* fix(QuickPanel): 拦截window上的keydown事件

* fix(QuickPanel): 修复事件监听器移除时未使用相同参数的问题

* fix(TopView): 修复左侧导航栏布局崩坏问题

* fix: 修正变量名拼写错误,将expended改为expanded

* Revert "fix(SelectModelPopup): 修复键盘事件处理并移除无效的useEffect"

This reverts commit 4211780b95.
2025-08-19 21:32:53 +08:00

119 lines
3.2 KiB
TypeScript

import { loggerService } from '@logger'
import TopViewMinappContainer from '@renderer/components/MinApp/TopViewMinappContainer'
import { useAppInit } from '@renderer/hooks/useAppInit'
import { useShortcuts } from '@renderer/hooks/useShortcuts'
import { message, Modal } from 'antd'
import React, { PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react'
import { Box } from '../Layout'
let onPop = () => {}
let onShow = ({ element, id }: { element: React.FC | React.ReactNode; id: string }) => {
element
id
}
let onHide = (id: string) => {
id
}
let onHideAll = () => {}
interface Props {
children?: React.ReactNode
}
type ElementItem = {
id: string
element: React.FC | React.ReactNode
}
const logger = loggerService.withContext('TopView')
const TopViewContainer: React.FC<Props> = ({ children }) => {
const [elements, setElements] = useState<ElementItem[]>([])
const elementsRef = useRef<ElementItem[]>([])
elementsRef.current = elements
const [messageApi, messageContextHolder] = message.useMessage()
const [modal, modalContextHolder] = Modal.useModal()
const { shortcuts } = useShortcuts()
const enableQuitFullScreen = shortcuts.find((item) => item.key === 'exit_fullscreen')?.enabled
useAppInit()
useEffect(() => {
window.message = messageApi
window.modal = modal
}, [messageApi, modal])
onPop = () => {
const views = [...elementsRef.current]
views.pop()
elementsRef.current = views
setElements(elementsRef.current)
}
onShow = ({ element, id }: ElementItem) => {
if (!elementsRef.current.find((el) => el.id === id)) {
elementsRef.current = elementsRef.current.concat([{ element, id }])
setElements(elementsRef.current)
}
}
onHide = (id: string) => {
elementsRef.current = elementsRef.current.filter((el) => el.id !== id)
setElements(elementsRef.current)
}
onHideAll = () => {
setElements([])
elementsRef.current = []
}
const FullScreenContainer: React.FC<PropsWithChildren> = useCallback(({ children }) => {
return (
<Box flex={1} position="absolute" w="100%" h="100%" className="topview-fullscreen-container">
<Box position="absolute" w="100%" h="100%" onClick={onPop} />
{children}
</Box>
)
}, [])
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
logger.debug('keydown', e)
if (!enableQuitFullScreen) return
if (e.key === 'Escape' && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
window.api.setFullScreen(false)
}
}
window.addEventListener('keydown', handleKeyDown)
return () => {
window.removeEventListener('keydown', handleKeyDown)
}
})
return (
<>
{children}
{messageContextHolder}
{modalContextHolder}
<TopViewMinappContainer />
{elements.map(({ element: Element, id }) => (
<FullScreenContainer key={`TOPVIEW_${id}`}>
{typeof Element === 'function' ? <Element /> : Element}
</FullScreenContainer>
))}
</>
)
}
export const TopView = {
show: (element: React.FC | React.ReactNode, id: string) => onShow({ element, id }),
hide: (id: string) => onHide(id),
clear: () => onHideAll(),
pop: onPop
}
export default TopViewContainer