import React from "react"; import { observer } from "mobx-react"; import classnames from "classnames"; import { LANGUAGE_ICON, CHANGES_ICON, VARIABLE_ICON, HIERARCHY_ICON, PROPERTIES_ICON, COMPONENTS_ICON, PALETTE_ICON, PAGES_ICON, PAGE_ICON, USER_WIDGETS_ICON, USER_WIDGET_ICON, LOG_ICON, WATCH_PANEL_ICON, QUEUE_PANEL_ICON, ACTIVE_FLOWS_PANEL_ICON, BREAKPOINTS_PANEL_ICON, PROJECT_EDITOR_SCRAPBOOK } from "project-editor/ui-components/icons"; const MATERIAL_PREFIX = "material:"; const SVG_PREFIX = "svg:"; const SVG_ICONS: { [icon: string]: JSX.Element } = { language: LANGUAGE_ICON, changes: CHANGES_ICON, variable: VARIABLE_ICON, hierarchy: HIERARCHY_ICON, properties: PROPERTIES_ICON, components: COMPONENTS_ICON, palette: PALETTE_ICON, pages: PAGES_ICON, page: PAGE_ICON, user_widgets: USER_WIDGETS_ICON, user_widget: USER_WIDGET_ICON, log: LOG_ICON, watch_panel: WATCH_PANEL_ICON, queue_panel: QUEUE_PANEL_ICON, active_flows_panel: ACTIVE_FLOWS_PANEL_ICON, breakpoints_panel: BREAKPOINTS_PANEL_ICON, "project-editor-scrapbook": PROJECT_EDITOR_SCRAPBOOK }; export const Icon = observer( class Icon extends React.Component< { icon: string | JSX.Element | React.ReactNode; size?: number; className?: string; style?: React.CSSProperties; onClick?: (event: React.MouseEvent) => void; overlayText?: string; attention?: boolean; title?: string; }, {} > { render() { const { size, style, className, onClick } = this.props; let icon = this.props.icon; let iconSize = size || 24; let result; if (typeof icon === "string" && icon.startsWith(SVG_PREFIX)) { icon = SVG_ICONS[icon.substring(4)]; } if (typeof icon === "string") { if (icon.startsWith(MATERIAL_PREFIX)) { let iconClassName = classnames( "EezStudio_Icon", "material-icons", className ); let iconStyle = { fontSize: iconSize + "px" }; if (style) { iconStyle = Object.assign(iconStyle, style); } const iconEl = ( {icon.slice(MATERIAL_PREFIX.length)} ); if (this.props.overlayText) { result = (
{iconEl} {this.props.overlayText}
); } else { result = iconEl; } } else { let iconStyle: React.CSSProperties = { objectFit: "contain" }; if (style) { iconStyle = Object.assign(iconStyle, style); } result = ( ); } } else { result = React.cloneElement(icon as React.ReactElement, { className: classnames("EezStudio_Icon", className), style, width: iconSize, height: iconSize, onClick: onClick, title: this.props.title }); } if (this.props.attention) { return (
{result}
); } return result; } } );