From b8ce6d4a8139acbc3d03a18f67f8d3779bee07f8 Mon Sep 17 00:00:00 2001 From: Neel Gondalia Date: Wed, 7 Dec 2022 10:41:40 -0500 Subject: [PATCH] Created a widget for properties view in react-components Changes made: - Implemented a properties widget in react-components - Change theia implementation to use the widget from react-components This will move the properties widget to the react-components npm package, so it can be used by both the theia extension and vscode extension. Signed-off-by: Neel Gondalia ngondalia@blackberry.com --- .../trace-explorer-tooltip-widget.tsx | 77 ++++++++++++++++++ .../trace-explorer-tooltip-widget.tsx | 78 +++++-------------- 2 files changed, 98 insertions(+), 57 deletions(-) create mode 100644 packages/react-components/src/trace-explorer/trace-explorer-tooltip-widget.tsx diff --git a/packages/react-components/src/trace-explorer/trace-explorer-tooltip-widget.tsx b/packages/react-components/src/trace-explorer/trace-explorer-tooltip-widget.tsx new file mode 100644 index 000000000..f1578e85f --- /dev/null +++ b/packages/react-components/src/trace-explorer/trace-explorer-tooltip-widget.tsx @@ -0,0 +1,77 @@ +import * as React from 'react'; +import { signalManager, Signals } from 'traceviewer-base/lib/signals/signal-manager'; + +export interface ReactPropertiesWidgetProps { + id: string, + title: string, + handleSourcecodeLookup: (e: React.MouseEvent) => void; +} + +export interface ReactPropertiesWidgetState { + tooltip: { [key: string]: string }; +} + +export class ReactItemPropertiesWidget extends React.Component { + + constructor(props: ReactPropertiesWidgetProps) { + super(props); + this.state = { + tooltip: {} + }; + signalManager().on(Signals.TOOLTIP_UPDATED, this._onTooltip); + } + + componentWillUnmount(): void { + signalManager().off(Signals.TOOLTIP_UPDATED, this._onTooltip); + } + + render(): React.ReactNode { + return ( +
+
+ {this.renderTooltip()} +
+
+ ); + } + + private renderTooltip() { + const tooltipArray: JSX.Element[] = []; + if (this.state.tooltip) { + Object.entries(this.state.tooltip).forEach(([key, value]) => { + if (key === 'Source') { + const sourceCodeInfo = value; + const matches = sourceCodeInfo.match('(.*):(\\d+)'); + let fileLocation; + let line; + if (matches && matches.length === 3) { + fileLocation = matches[1]; + line = matches[2]; + } + tooltipArray.push(

{key + ': ' + sourceCodeInfo}

); + } else { + tooltipArray.push(

{key + ': ' + value}

); + } + }); + } else { + tooltipArray.push(

Select item to view properties

); + } + + return ( + + {tooltipArray.map(element => element)} + ); + } + + /** Tooltip Signal and Signal Handlers */ + protected _onTooltip = (tooltip: { [key: string]: string }): void => this.doHandleTooltipSignal(tooltip); + + private doHandleTooltipSignal(tooltipProps: { [key: string]: string }): void { + this.setState({tooltip: tooltipProps}); + } +} + diff --git a/theia-extensions/viewer-prototype/src/browser/trace-explorer/trace-explorer-sub-widgets/trace-explorer-tooltip-widget.tsx b/theia-extensions/viewer-prototype/src/browser/trace-explorer/trace-explorer-sub-widgets/trace-explorer-tooltip-widget.tsx index 908a6defd..32d001214 100644 --- a/theia-extensions/viewer-prototype/src/browser/trace-explorer/trace-explorer-sub-widgets/trace-explorer-tooltip-widget.tsx +++ b/theia-extensions/viewer-prototype/src/browser/trace-explorer/trace-explorer-sub-widgets/trace-explorer-tooltip-widget.tsx @@ -1,9 +1,9 @@ import { inject, injectable, postConstruct } from 'inversify'; -import { ReactWidget, Message } from '@theia/core/lib/browser'; +import { Message, ReactWidget, Widget } from '@theia/core/lib/browser'; import * as React from 'react'; import { EditorOpenerOptions, EditorManager } from '@theia/editor/lib/browser'; import URI from '@theia/core/lib/common/uri'; -import { Signals, signalManager } from 'traceviewer-base/lib/signals/signal-manager'; +import { ReactItemPropertiesWidget} from 'traceviewer-react-components/lib/trace-explorer/trace-explorer-tooltip-widget'; @injectable() export class TraceExplorerTooltipWidget extends ReactWidget { @@ -12,53 +12,37 @@ export class TraceExplorerTooltipWidget extends ReactWidget { @inject(EditorManager) protected readonly editorManager!: EditorManager; - tooltip?: { [key: string]: string } = undefined; - - private onTooltip = (tooltip?: { [key: string]: string }): void => this.doHandleTooltipSignal(tooltip); - @postConstruct() init(): void { this.id = TraceExplorerTooltipWidget.ID; this.title.label = TraceExplorerTooltipWidget.LABEL; - signalManager().on(Signals.TOOLTIP_UPDATED, this.onTooltip); this.update(); } dispose(): void { super.dispose(); - signalManager().off(Signals.TOOLTIP_UPDATED, this.onTooltip); } - private renderTooltip() { - const tooltipArray: JSX.Element[] = []; - if (this.tooltip) { - Object.entries(this.tooltip).forEach(([key, value]) => { - if (key === 'Source') { - const sourceCodeInfo = value; - const matches = sourceCodeInfo.match('(.*):(\\d+)'); - let fileLocation; - let line; - if (matches && matches.length === 3) { - fileLocation = matches[1]; - line = matches[2]; - } - tooltipArray.push(

{key + ': ' + sourceCodeInfo}

); - } else { - tooltipArray.push(

{key + ': ' + value}

); - } - }); - } else { - tooltipArray.push(

Select item to view properties

); - } - + render(): React.ReactNode { return ( - - {tooltipArray.map(element => element)} - ); +
+ +
+ ); + } + + protected onResize(msg: Widget.ResizeMessage): void { + super.onResize(msg); + this.update(); + } + + protected onAfterShow(msg: Message): void { + super.onAfterShow(msg); + this.update(); } protected handleSourcecodeLookup = (e: React.MouseEvent): void => this.doHandleSourcecodeLookup(e); @@ -103,24 +87,4 @@ export class TraceExplorerTooltipWidget extends ReactWidget { this.editorManager.open(new URI(fileLocation), opts); } } - - render(): React.ReactNode { - return ( -
-
- {this.renderTooltip()} -
-
- ); - } - - private doHandleTooltipSignal(tooltip?: { [key: string]: string }) { - this.tooltip = tooltip; - this.update(); - } - - protected onAfterShow(msg: Message): void { - super.onAfterShow(msg); - this.update(); - } }