Skip to content

Commit 18b0f9b

Browse files
committed
Context example
1 parent 971c575 commit 18b0f9b

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/screens/OavMover/OavControlHelper.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Box, Grid2, Stack, TextField, useTheme } from "@mui/material";
22
import { CoordNumberInput } from "../../components/CoordNumberInput";
3+
import { useContext } from "react";
4+
import { crosshairContext } from "./OavMover";
35

4-
export function BeamCentre({
5-
setCrosshairX,
6-
setCrosshairY,
7-
}: {
8-
setCrosshairX: React.Dispatch<React.SetStateAction<number>>;
9-
setCrosshairY: React.Dispatch<React.SetStateAction<number>>;
10-
}) {
6+
export function BeamCentre() {
117
const theme = useTheme();
8+
const ctx = useContext(crosshairContext);
9+
if (!ctx) throw new Error("BeamCentre must be used within OavMover");
1210
return (
1311
<Box
1412
bgcolor={theme.palette.background.paper}
@@ -24,16 +22,16 @@ export function BeamCentre({
2422
<Stack direction="row" spacing={2} padding={2}>
2523
<CoordNumberInput
2624
placeholder="x"
27-
onChange={(_e, val) => setCrosshairX(val ? val : 0)}
28-
defaultValue={200}
25+
onChange={(_e, val) => ctx.setCrosshairX(val ? val : 0)}
26+
defaultValue={ctx.crosshairX}
2927
min={0}
3028
max={1000}
3129
aria-label="X"
3230
/>
3331
<CoordNumberInput
3432
placeholder="y"
35-
onChange={(_e, val) => setCrosshairY(val ? val : 0)}
36-
defaultValue={200}
33+
onChange={(_e, val) => ctx.setCrosshairY(val ? val : 0)}
34+
defaultValue={ctx.crosshairY}
3735
min={0}
3836
max={750}
3937
aria-label="Y"
@@ -65,7 +63,7 @@ export function PixelsToMicrons({
6563
label="Pixels per micron"
6664
onChange={(e) =>
6765
setPixelsPerMicron(
68-
Number(e.target.value) ? Number(e.target.value) : 0,
66+
Number(e.target.value) ? Number(e.target.value) : 0
6967
)
7068
}
7169
defaultValue={1.25}

src/screens/OavMover/OavMover.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Box, Grid2, useTheme } from "@mui/material";
22
import { OavVideoStream } from "../../components/OavVideoStream";
3-
import { useState } from "react";
3+
import { useState, createContext } from "react";
44

55
import { submitAndRunPlanImmediately } from "../../blueapi/blueapi";
66
import { parseInstrumentSession, readVisitFromPv } from "../../blueapi/visit";
@@ -9,6 +9,17 @@ import { CoordinateSystem } from "./CoordinateSystem";
99
import { PresetPositionsSideDrawer } from "./PresetDrawer";
1010
import { BacklightControl, MoveArrows, ZoomControl } from "./OavSideBar";
1111

12+
interface CrosshairContextType {
13+
crosshairX: number;
14+
crosshairY: number;
15+
setCrosshairX: React.Dispatch<React.SetStateAction<number>>;
16+
setCrosshairY: React.Dispatch<React.SetStateAction<number>>;
17+
}
18+
19+
export const crosshairContext = createContext<CrosshairContextType | null>(
20+
null
21+
);
22+
1223
export function OavMover() {
1324
const [crosshairX, setCrosshairX] = useState<number>(200);
1425
const [crosshairY, setCrosshairY] = useState<number>(200);
@@ -31,12 +42,12 @@ export function OavMover() {
3142
onCoordClick={(x: number, y: number) => {
3243
const [x_um, y_um] = [x / pixelsPerMicron, y / pixelsPerMicron];
3344
console.log(
34-
`Clicked on position (${x}, ${y}) (px relative to beam centre) in original stream. Relative position in um (${x_um}, ${y_um}). Submitting to BlueAPI...`,
45+
`Clicked on position (${x}, ${y}) (px relative to beam centre) in original stream. Relative position in um (${x_um}, ${y_um}). Submitting to BlueAPI...`
3546
);
3647
const [x_int, y_int] = [Math.round(x), Math.round(y)];
3748
if (Number.isNaN(x_um) || Number.isNaN(y_um)) {
3849
console.log(
39-
"Not submitting plan while disconnected from PVs!",
50+
"Not submitting plan while disconnected from PVs!"
4051
);
4152
} else {
4253
// This is an example but not useful for actual production use.
@@ -46,7 +57,7 @@ export function OavMover() {
4657
instrumentSession: parseInstrumentSession(fullVisit),
4758
}).catch((error) => {
4859
console.log(
49-
`Failed to run plan gui_gonio_move_on_click, see console and logs for full error. Reason: ${error}`,
60+
`Failed to run plan gui_gonio_move_on_click, see console and logs for full error. Reason: ${error}`
5061
);
5162
});
5263
}
@@ -65,10 +76,11 @@ export function OavMover() {
6576
>
6677
<MoveArrows />
6778
<Grid2 size={3} padding={1} />
68-
<BeamCentre
69-
setCrosshairX={setCrosshairX}
70-
setCrosshairY={setCrosshairY}
71-
/>
79+
<crosshairContext.Provider
80+
value={{ crosshairX, crosshairY, setCrosshairX, setCrosshairY }}
81+
>
82+
<BeamCentre />
83+
</crosshairContext.Provider>
7284
<PixelsToMicrons setPixelsPerMicron={setPixelsPerMicron} />
7385
<BacklightControl
7486
label="backlight-pos"

0 commit comments

Comments
 (0)