Skip to content

Commit b3faec6

Browse files
Add relative path patching for symbols and correct sizing
1 parent 5253615 commit b3faec6

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/ui/widgets/EmbeddedDisplay/opiParser.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,13 @@ function opiPatchPaths(
826826
}
827827

828828
// symbols: list of string file paths
829-
if ( widgetDescription["symbols"] && parentDir) {
830-
widgetDescription["symbols"] = widgetDescription["symbols"].map((symbol: string) => {
831-
if (symbol.startsWith("http")) return symbol;
832-
return normalisePath(symbol, parentDir);
833-
})
829+
if (widgetDescription["symbols"] && parentDir) {
830+
widgetDescription["symbols"] = widgetDescription["symbols"].map(
831+
(symbol: string) => {
832+
if (symbol.startsWith("http")) return symbol;
833+
return normalisePath(symbol, parentDir);
834+
}
835+
);
834836
}
835837
}
836838

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 ? "contain" : stretchToFit ? "fill" : "contain",
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)