Skip to content

Commit d82f01e

Browse files
committed
Implemented the new calculateRotationTransform function
1 parent 59e9fa1 commit d82f01e

File tree

3 files changed

+18
-51
lines changed

3 files changed

+18
-51
lines changed

src/ui/widgets/ActionButton/actionButton.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { MacroContext } from "../../../types/macros";
1919
import { ExitFileContext, FileContext } from "../../../misc/fileContext";
2020
import { styled, Button as MuiButton, useTheme } from "@mui/material";
2121
import { WIDGET_DEFAULT_SIZES } from "../EmbeddedDisplay/bobParser";
22+
import { calculateRotationTransform } from "../utils";
2223

2324
export interface ActionButtonProps {
2425
onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
@@ -79,23 +80,11 @@ export const ActionButtonComponent = (
7980
const font = props.font?.css() ?? theme.typography;
8081
const border = props.border?.css() ?? null;
8182

82-
const inputWidth = rotationStep === 0 || rotationStep === 2 ? width : height;
83-
const inputHeight = rotationStep === 0 || rotationStep === 2 ? height : width;
84-
85-
const offset = width / 2 - height / 2;
86-
const transform = (function () {
87-
switch (rotationStep) {
88-
case 0: // 0 degrees
89-
case 2: // 180 degrees
90-
return `rotate(${rotationStep * -90}deg)`;
91-
case 1: // 90 degrees
92-
return `rotate(${rotationStep * -90}deg) translateY(${offset}px) translateX(${offset}px)`;
93-
case 3: // -90 degrees
94-
return `rotate(${rotationStep * -90}deg) translateY(${-offset}px) translateX(${-offset}px)`;
95-
default: // Unreachable
96-
return "";
97-
}
98-
})();
83+
const [inputWidth, inputHeight, transform] = calculateRotationTransform(
84+
rotationStep,
85+
width,
86+
height
87+
);
9988

10089
return (
10190
<Button

src/ui/widgets/Label/label.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "../propTypes";
1616
import { Typography as MuiTypography, styled, useTheme } from "@mui/material";
1717
import { WIDGET_DEFAULT_SIZES } from "../EmbeddedDisplay/bobParser";
18+
import { calculateRotationTransform } from "../utils";
1819

1920
const LabelProps = {
2021
macros: MacrosPropOpt,
@@ -72,23 +73,11 @@ export const LabelComponent = (
7273
const borderColor = props.border?.css().borderColor ?? "#000000";
7374
const borderStyle = props.border?.css().borderStyle ?? "solid";
7475

75-
const inputWidth = rotationStep === 0 || rotationStep === 2 ? width : height;
76-
const inputHeight = rotationStep === 0 || rotationStep === 2 ? height : width;
77-
78-
const offset = width / 2 - height / 2;
79-
const transform = (function () {
80-
switch (rotationStep) {
81-
case 0: // 0 degrees
82-
case 2: // 180 degrees
83-
return `rotate(${rotationStep * -90}deg)`;
84-
case 1: // 90 degrees
85-
return `rotate(${rotationStep * -90}deg) translateY(${offset}px) translateX(${offset}px)`;
86-
case 3: // -90 degrees
87-
return `rotate(${rotationStep * -90}deg) translateY(${-offset}px) translateX(${-offset}px)`;
88-
default: // Unreachable
89-
return "";
90-
}
91-
})();
76+
const [inputWidth, inputHeight, transform] = calculateRotationTransform(
77+
rotationStep,
78+
width,
79+
height
80+
);
9281

9382
// Since display is "flex", use "flex-start" and "flex-end" to align
9483
// the content.

src/ui/widgets/Readback/readback.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { registerWidget } from "../register";
1818
import { AlarmQuality, DType } from "../../../types/dtypes";
1919
import { TextField as MuiTextField, styled, useTheme } from "@mui/material";
2020
import { WIDGET_DEFAULT_SIZES } from "../EmbeddedDisplay/bobParser";
21+
import { calculateRotationTransform } from "../utils";
2122

2223
const ReadbackProps = {
2324
precision: IntPropOpt,
@@ -188,23 +189,11 @@ export const ReadbackComponent = (
188189
) * 16
189190
: theme.typography.fontSize;
190191

191-
const inputWidth = rotationStep === 0 || rotationStep === 2 ? width : height;
192-
const inputHeight = rotationStep === 0 || rotationStep === 2 ? height : width;
193-
194-
const offset = width / 2 - height / 2;
195-
const transform = (function () {
196-
switch (rotationStep) {
197-
case 0: // 0 degrees
198-
case 2: // 180 degrees
199-
return `rotate(${rotationStep * -90}deg)`;
200-
case 1: // 90 degrees
201-
return `rotate(${rotationStep * -90}deg) translateY(${offset}px) translateX(${offset}px)`;
202-
case 3: // -90 degrees
203-
return `rotate(${rotationStep * -90}deg) translateY(${-offset}px) translateX(${-offset}px)`;
204-
default: // Unreachable
205-
return "";
206-
}
207-
})();
192+
const [inputWidth, inputHeight, transform] = calculateRotationTransform(
193+
rotationStep,
194+
width,
195+
height
196+
);
208197

209198
// Calculate max number of rows based on the height of the widget and the height of the font
210199
// an extra row is then subtracted to make it fit nicer

0 commit comments

Comments
 (0)