|
| 1 | +import cls from 'classnames'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { AINativeConfigService, SlotLocation, SlotRenderer, getIcon, useInjectable } from '@opensumi/ide-core-browser'; |
| 5 | +import { Icon } from '@opensumi/ide-core-browser/lib/components'; |
| 6 | +import { EnhanceIcon } from '@opensumi/ide-core-browser/lib/components/ai-native'; |
| 7 | +import { VIEW_CONTAINERS } from '@opensumi/ide-core-browser/lib/layout/view-id'; |
| 8 | +import { AbstractContextMenuService, ICtxMenuRenderer, MenuId } from '@opensumi/ide-core-browser/lib/menu/next'; |
| 9 | +import { CommandService } from '@opensumi/ide-core-common'; |
| 10 | +import { IMainLayoutService } from '@opensumi/ide-main-layout'; |
| 11 | + |
| 12 | +import { AI_MENU_BAR_LEFT, AI_MENU_BAR_RIGHT } from '../layout-config'; |
| 13 | + |
| 14 | +import styles from './menu-bar.module.less'; |
| 15 | + |
| 16 | +const AIMenuBarRender = () => { |
| 17 | + const contextmenuService = useInjectable<AbstractContextMenuService>(AbstractContextMenuService); |
| 18 | + const ctxMenuRenderer = useInjectable<ICtxMenuRenderer>(ICtxMenuRenderer); |
| 19 | + |
| 20 | + const iconRef = React.useRef<HTMLDivElement | null>(null); |
| 21 | + const [anchor, setAnchor] = React.useState<{ x: number; y: number } | undefined>(undefined); |
| 22 | + |
| 23 | + React.useEffect(() => { |
| 24 | + if (iconRef.current) { |
| 25 | + const rect = iconRef.current.getBoundingClientRect(); |
| 26 | + const { x, y, height } = rect; |
| 27 | + |
| 28 | + setAnchor({ |
| 29 | + x, |
| 30 | + y: y + height + 4, |
| 31 | + }); |
| 32 | + } |
| 33 | + }, []); |
| 34 | + |
| 35 | + const extraTopMenus = React.useMemo( |
| 36 | + () => |
| 37 | + contextmenuService.createMenu({ |
| 38 | + id: MenuId.AIMenuBarTopExtra, |
| 39 | + }), |
| 40 | + [contextmenuService], |
| 41 | + ); |
| 42 | + |
| 43 | + const handleClick = React.useCallback(() => { |
| 44 | + if (!anchor) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const menuNodes = extraTopMenus.getMergedMenuNodes(); |
| 49 | + extraTopMenus.dispose(); |
| 50 | + |
| 51 | + ctxMenuRenderer.show({ |
| 52 | + anchor, |
| 53 | + menuNodes, |
| 54 | + }); |
| 55 | + }, [anchor, extraTopMenus]); |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + <EnhanceIcon |
| 60 | + wrapperClassName={styles.ai_enhance_menu} |
| 61 | + className={styles.extra_top_icon} |
| 62 | + ref={iconRef} |
| 63 | + onClick={handleClick} |
| 64 | + > |
| 65 | + <Icon className={cls(getIcon('down'), styles.caret_icon)} /> |
| 66 | + </EnhanceIcon> |
| 67 | + </> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +export const AIMenuBarView = () => { |
| 72 | + const commandService = useInjectable<CommandService>(CommandService); |
| 73 | + const mainLayoutService = useInjectable<IMainLayoutService>(IMainLayoutService); |
| 74 | + const aiNativeConfigService = useInjectable<AINativeConfigService>(AINativeConfigService); |
| 75 | + const [isVisiablePanel, setIsVisiablePanel] = React.useState<boolean>(false); |
| 76 | + |
| 77 | + React.useEffect(() => { |
| 78 | + requestAnimationFrame(() => { |
| 79 | + setIsVisiablePanel(isVisiable()); |
| 80 | + }); |
| 81 | + }, []); |
| 82 | + |
| 83 | + const handleLeftMenuVisiable = React.useCallback(() => { |
| 84 | + commandService.executeCommand('main-layout.left-panel.toggle'); |
| 85 | + requestAnimationFrame(() => { |
| 86 | + setIsVisiablePanel(isVisiable()); |
| 87 | + }); |
| 88 | + }, []); |
| 89 | + |
| 90 | + const isVisiable = React.useCallback(() => { |
| 91 | + const tabbarService = mainLayoutService.getTabbarService(SlotLocation.left); |
| 92 | + return !!tabbarService.currentContainerId; |
| 93 | + }, [mainLayoutService]); |
| 94 | + |
| 95 | + return ( |
| 96 | + <div |
| 97 | + id={VIEW_CONTAINERS.MENUBAR} |
| 98 | + className={styles.menu_bar_view} |
| 99 | + style={{ height: aiNativeConfigService.appConfig.layoutViewSize?.menubarHeight }} |
| 100 | + > |
| 101 | + <div className={styles.container}> |
| 102 | + <div className={styles.left}> |
| 103 | + <EnhanceIcon |
| 104 | + wrapperClassName={styles.enhance_menu} |
| 105 | + icon={isVisiablePanel ? 'left-nav-open' : 'left-nav-close'} |
| 106 | + onClick={handleLeftMenuVisiable} |
| 107 | + /> |
| 108 | + <span className={styles.dividing}></span> |
| 109 | + <div className={styles.top_menus_bar}> |
| 110 | + <AIMenuBarRender /> |
| 111 | + </div> |
| 112 | + <SlotRenderer slot={AI_MENU_BAR_LEFT} flex={1} overflow={'initial'} /> |
| 113 | + </div> |
| 114 | + <div className={styles.right}> |
| 115 | + <SlotRenderer slot={AI_MENU_BAR_RIGHT} flex={1} overflow={'initial'} /> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + ); |
| 120 | +}; |
0 commit comments