Skip to content

Commit 9c75403

Browse files
Merge pull request #134 from DiamondLightSource/fix-symbol-images
Fix Symbol relative paths
2 parents f4765c5 + f05670b commit 9c75403

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

src/ui/widgets/EmbeddedDisplay/opiParser.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,9 @@ export const OPI_COMPLEX_PARSERS: ComplexParserDict = {
751751
points: opiParsePoints
752752
};
753753

754-
function opiPatchRules(widgetDescription: WidgetDescription): void {
754+
function opiPatchRules(
755+
widgetDescription: WidgetDescription
756+
): WidgetDescription {
755757
/* Re-index simple parsers so we can find the correct one
756758
for the opi prop. */
757759
const opiPropParsers: ParserDict = {};
@@ -770,6 +772,7 @@ function opiPatchRules(widgetDescription: WidgetDescription): void {
770772
});
771773
}
772774
});
775+
return widgetDescription;
773776
}
774777

775778
function normalisePath(path: string, parentDir?: string): string {
@@ -784,7 +787,7 @@ function normalisePath(path: string, parentDir?: string): string {
784787
function opiPatchPaths(
785788
widgetDescription: WidgetDescription,
786789
parentDir?: string
787-
): void {
790+
): WidgetDescription {
788791
log.debug(`opiPatchPaths ${parentDir}`);
789792
// file: OpiFile type
790793
if (
@@ -824,9 +827,22 @@ function opiPatchPaths(
824827
}
825828
}
826829
}
830+
831+
// symbols: list of string file paths
832+
if (widgetDescription["symbols"] && parentDir) {
833+
widgetDescription["symbols"] = widgetDescription["symbols"].map(
834+
(symbol: string) => {
835+
if (symbol.startsWith("http")) return symbol;
836+
return normalisePath(symbol, parentDir);
837+
}
838+
);
839+
}
840+
return widgetDescription;
827841
}
828842

829-
function opiPatchActions(widgetDescription: WidgetDescription): void {
843+
function opiPatchActions(
844+
widgetDescription: WidgetDescription
845+
): WidgetDescription {
830846
if (
831847
widgetDescription.type === "actionbutton" &&
832848
widgetDescription.text &&
@@ -849,6 +865,7 @@ function opiPatchActions(widgetDescription: WidgetDescription): void {
849865
};
850866
}
851867
}
868+
return widgetDescription;
852869
}
853870

854871
export const OPI_PATCHERS: PatchFunction[] = [

src/ui/widgets/EmbeddedDisplay/parser.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export type ComplexParserDict = {
4646
[key: string]: (value: any) => GenericProp | Promise<GenericProp>;
4747
};
4848

49-
export type PatchFunction = (props: WidgetDescription, path?: string) => void;
49+
export type PatchFunction = (
50+
props: WidgetDescription,
51+
path?: string
52+
) => WidgetDescription;
5053

5154
/* Take an object representing a widget and return our widget description. */
5255
export async function genericParser(
@@ -153,7 +156,7 @@ export async function parseWidget(
153156
filepath?: string
154157
): Promise<WidgetDescription> {
155158
const targetWidget = getTargetWidget(props);
156-
const widgetDescription = await genericParser(
159+
let widgetDescription = await genericParser(
157160
props,
158161
targetWidget,
159162
simpleParsers,
@@ -162,7 +165,7 @@ export async function parseWidget(
162165
);
163166
// Execute patch functions.
164167
for (const patcher of patchFunctions) {
165-
patcher(widgetDescription, filepath);
168+
widgetDescription = patcher(widgetDescription, filepath);
166169
}
167170
/* Child widgets */
168171
const childWidgets = toArray(props[childrenName]);

src/ui/widgets/Image/image.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const ImageProps = {
2727
onClick: FuncPropOpt,
2828
overflow: BoolPropOpt,
2929
backgroundColor: ColorPropOpt,
30-
transparent: BoolPropOpt
30+
transparent: BoolPropOpt,
31+
preserveRatio: BoolPropOpt
3132
};
3233

3334
export const ImageComponent = (
@@ -37,7 +38,8 @@ export const ImageComponent = (
3738
rotation = 0,
3839
flipHorizontal,
3940
flipVertical,
40-
stretchToFit = false
41+
stretchToFit = false,
42+
preserveRatio = false
4143
} = props;
4244

4345
const onClick = (event: React.MouseEvent<HTMLDivElement>): void => {
@@ -67,6 +69,10 @@ export const ImageComponent = (
6769
imageFileName = imageFileName + new Date().getTime();
6870
}
6971

72+
// In Phoebus, the aspect ratio of svgs is always maintained even when
73+
// stretchToFit is true. Also accept preserveRatio property passed from
74+
// Symbol widget
75+
const ratio = imageFileName.includes(".svg") ? true : preserveRatio;
7076
return (
7177
<div style={style} onClick={onClick}>
7278
<img
@@ -77,7 +83,7 @@ export const ImageComponent = (
7783
transform: `rotate(${rotation}deg) scaleX(${
7884
flipHorizontal ? -1 : 1
7985
}) scaleY(${flipVertical ? -1 : 1})`,
80-
objectFit: stretchToFit ? "fill" : "none",
86+
objectFit: ratio || !stretchToFit ? "contain" : "fill",
8187
objectPosition: "top left"
8288
}}
8389
/>

src/ui/widgets/Symbol/__snapshots__/symbol.test.tsx.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports[`<Symbol /> from .bob file > matches snapshot (using fallback symbol) 1`
77
>
88
<img
99
src="https://cs-web-symbol.diamond.ac.uk/catalogue/default_symbol.png"
10-
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: none; object-position: top left;"
10+
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: contain; object-position: top left;"
1111
/>
1212
</div>
1313
</DocumentFragment>
@@ -20,7 +20,7 @@ exports[`<Symbol /> from .bob file > matches snapshot (with index) 1`] = `
2020
>
2121
<img
2222
src="img 3.svg"
23-
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: none; object-position: top left;"
23+
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: contain; object-position: top left;"
2424
/>
2525
</div>
2626
</DocumentFragment>
@@ -33,7 +33,7 @@ exports[`<Symbol /> from .bob file > matches snapshot (with rotation) 1`] = `
3333
>
3434
<img
3535
src="img 1.gif"
36-
style="display: block; transform: rotate(45deg) scaleX(1) scaleY(1); object-fit: none; object-position: top left;"
36+
style="display: block; transform: rotate(45deg) scaleX(1) scaleY(1); object-fit: contain; object-position: top left;"
3737
/>
3838
</div>
3939
</DocumentFragment>
@@ -46,7 +46,7 @@ exports[`<Symbol /> from .bob file > matches snapshot (without index) 1`] = `
4646
>
4747
<img
4848
src="img 1.gif"
49-
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: none; object-position: top left;"
49+
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: contain; object-position: top left;"
5050
/>
5151
</div>
5252
</DocumentFragment>
@@ -59,7 +59,7 @@ exports[`<Symbol /> from .opi file > matches snapshot (with rotation) 1`] = `
5959
>
6060
<img
6161
src="img 1.gif"
62-
style="display: block; transform: rotate(45deg) scaleX(1) scaleY(1); object-fit: none; object-position: top left;"
62+
style="display: block; transform: rotate(45deg) scaleX(1) scaleY(1); object-fit: contain; object-position: top left;"
6363
/>
6464
</div>
6565
</DocumentFragment>
@@ -72,7 +72,7 @@ exports[`<Symbol /> from .opi file > matches snapshot 1`] = `
7272
>
7373
<img
7474
src="img 1.gif"
75-
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: none; object-position: top left;"
75+
style="display: block; transform: rotate(0deg) scaleX(1) scaleY(1); object-fit: contain; object-position: top left;"
7676
/>
7777
</div>
7878
<div

src/ui/widgets/Symbol/symbol.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ const SymbolProps = {
5555
arrayIndex: FloatPropOpt,
5656
enabled: BoolPropOpt,
5757
fallbackSymbol: StringPropOpt,
58-
transparent: BoolPropOpt
58+
transparent: BoolPropOpt,
59+
preserveRatio: BoolPropOpt
5960
};
6061

6162
export type SymbolComponentProps = InferWidgetProps<typeof SymbolProps> &
@@ -76,6 +77,7 @@ export const SymbolComponent = (props: SymbolComponentProps): JSX.Element => {
7677
backgroundColor = "white",
7778
showBooleanLabel = false,
7879
enabled = true,
80+
preserveRatio = true,
7981
pvData
8082
} = props;
8183
const { value } = getPvValueAndName(pvData);
@@ -158,7 +160,7 @@ export const SymbolComponent = (props: SymbolComponentProps): JSX.Element => {
158160
transparent
159161
imageFile={imageFile}
160162
onClick={onClick}
161-
stretchToFit={false}
163+
stretchToFit={preserveRatio ? false : true}
162164
overflow={true}
163165
/>
164166
{isBob ? (

0 commit comments

Comments
 (0)