import React from "react"; import { computed, makeObservable } from "mobx"; import { observer } from "mobx-react"; import { capitalize } from "eez-studio-shared/string"; import { ButtonAction } from "eez-studio-ui/action"; import type { IShortcut } from "shortcuts/interfaces"; import type { InstrumentAppStore } from "instrument/window/app-store"; import { shortcutsToolbarRegistry } from "instrument/window/shortcuts"; export const ShortcutButton = observer( class ShortcutButton extends React.Component<{ appStore: InstrumentAppStore; shortcut: IShortcut; executeShortcut: () => void; isActive: boolean; }> { constructor(props: any) { super(props); makeObservable(this, { keybinding: computed }); } get keybinding() { return ( (this.props.shortcut.keybinding && this.props.shortcut.keybinding .split("+") .map(x => capitalize(x)) .join(" + ")) || "" ); } render() { let text; let title; if (this.keybinding) { text = ( <> {this.keybinding} {this.props.shortcut.name} ); title = `${this.keybinding}: ${this.props.shortcut.name}`; } else { text = this.props.shortcut.name; title = this.props.shortcut.name; } return ( ); } } ); export const ShortcutsToolbar = observer( class ShortcutsToolbar extends React.Component<{ appStore: InstrumentAppStore; style?: React.CSSProperties; executeShortcut: (shortcut: IShortcut) => void; }> { constructor(props: any) { super(props); makeObservable(this, { shortcuts: computed }); } divRef = React.createRef(); componentDidMount() { if (this.divRef.current) { shortcutsToolbarRegistry.registerShortcutsToolbar( this.divRef.current, this.props.appStore.shortcutsStore ); } } componentWillUnmount() { if (this.divRef.current) { shortcutsToolbarRegistry.unregisterShortcutsToolbar( this.divRef.current ); } } get shortcuts() { return Array.from( this.props.appStore.shortcutsStore.instrumentShortcuts .get() .values() ) .filter(s => s.showInToolbar) .sort((s1, s2) => { if (s1.toolbarButtonPosition < s2.toolbarButtonPosition) { return -1; } if (s1.toolbarButtonPosition > s2.toolbarButtonPosition) { return 1; } let name1 = s1.name.toLocaleLowerCase(); let name2 = s2.name.toLocaleLowerCase(); return name1 < name2 ? -1 : name1 > name2 ? 1 : 0; }); } render() { if (this.shortcuts.length === 0) { return null; } return (
{this.shortcuts.map(shortcut => ( this.props.executeShortcut(shortcut) } isActive={ this.divRef.current == shortcutsToolbarRegistry.activeShortcutsToolbar && this.divRef.current ? true : false } /> ))} {this.props.appStore.shortcutsStore .isAnyMissingShortcuts && ( )}
); } } );