import React from "react"; import { observer } from "mobx-react"; import classNames from "classnames"; import { Loader } from "eez-studio-ui/loader"; import { Icon } from "eez-studio-ui/icon"; import { Menu, MenuItem } from "@electron/remote"; //////////////////////////////////////////////////////////////////////////////// export interface ITab { active: boolean; permanent: boolean; dragDisabled?: boolean; id: string | number; title: React.ReactNode; tooltipTitle?: string; icon?: React.ReactNode; loading: boolean; makeActive(): void; openInWindow?(): void; close?(): void; copyProjectPath?(): void; } ////////////////////////////////////////////////////////////////////////////// export const TabView = observer( class TabView extends React.Component<{ tab: ITab; index: number; moveTab?: (dragIndex: number, hoverIndex: number) => void; }> { onMouseUp = (e: React.MouseEvent) => { const { tab } = this.props; if (e.button === 1) { if (tab.close) { tab.close(); } } }; onMouseDown = () => { const { tab } = this.props; tab.makeActive(); }; onContextMenu = (event: React.MouseEvent) => { const { tab } = this.props; event.preventDefault(); const menu = new Menu(); if (tab.openInWindow) { menu.append( new MenuItem({ label: "Open in New Window", click: () => tab.openInWindow!() }) ); } if (tab.copyProjectPath) { menu.append( new MenuItem({ label: "Copy Project Path", click: () => tab.copyProjectPath!() }) ); } if (tab.close) { menu.append( new MenuItem({ label: "Close", click: () => tab.close!() }) ); } if (menu.items.length > 0) { menu.popup({}); } }; onClose = (e: any) => { const { tab } = this.props; e.stopPropagation(); if (tab.close) { tab.close(); } }; render() { const { tab } = this.props; let className = classNames("EezStudio_Tab", { active: tab.active, permanent: tab.permanent }); let closeIcon: JSX.Element | undefined; if (tab.close) { closeIcon = ( close ); } let icon; if (typeof tab.icon == "string") { icon = ; } else { icon = tab.icon; } let title; if (typeof tab.title === "string") { title = ( <> {icon} {tab.title} ); } else { title = ( <> {icon} {tab.title} ); } const opacity = 1; return (
{ ev.dataTransfer.setData( "application/eez-studio-tab", this.props.index.toString() ); ev.dataTransfer.effectAllowed = "move"; }} onDragOver={ev => { ev.preventDefault(); ev.stopPropagation(); if ( ev.dataTransfer.types.indexOf( "application/eez-studio-tab" ) == -1 || this.props.index == 0 ) { ev.dataTransfer.dropEffect = "none"; return; } ev.dataTransfer.dropEffect = "move"; }} onDrop={ev => { ev.preventDefault(); const dragIndex = parseInt( ev.dataTransfer.getData( "application/eez-studio-tab" ) ); if (this.props.moveTab) { this.props.moveTab(dragIndex, this.props.index); } }} >
{title} {tab.loading && ( )} {closeIcon}
); } } ); //////////////////////////////////////////////////////////////////////////////// export const TabsView = observer( class TabsView extends React.Component<{ tabs: ITab[]; addTabTitle?: string; addTabIcon?: string; addTabAttention?: boolean; moveTab?: (dragIndex: number, hoverIndex: number) => void; }> { render() { return (
{this.props.tabs.map((tab, index) => ( ))}
); } } );