|
1 |
| -export function CCComponentEditorNodePinPropertyEditor() {} |
| 1 | +import { Button, Popover, Stack, TextField, Typography } from "@mui/material"; |
| 2 | +import { zip } from "lodash-es"; |
| 3 | +import nullthrows from "nullthrows"; |
| 4 | +import { useState } from "react"; |
| 5 | +import invariant from "tiny-invariant"; |
| 6 | +import { rect } from "../../../../common/rect"; |
| 7 | +import { IntrinsicComponentDefinition } from "../../../../store/intrinsics/base"; |
| 8 | +import { CCNodePinStore } from "../../../../store/nodePin"; |
| 9 | +import { useStore } from "../../../../store/react"; |
| 10 | +import getCCComponentEditorRendererNodeGeometry from "../renderer/Node.geometry"; |
| 11 | +import { useComponentEditorStore } from "../store"; |
| 12 | + |
| 13 | +export function CCComponentEditorNodePinPropertyEditor() { |
| 14 | + const { store } = useStore(); |
| 15 | + const componentEditorStore = useComponentEditorStore(); |
| 16 | + const target = componentEditorStore((s) => s.nodePinPropertyEditorTarget); |
| 17 | + const setTarget = componentEditorStore( |
| 18 | + (s) => s.setNodePinPropertyEditorTarget, |
| 19 | + ); |
| 20 | + const [newBitWidthList, setNewBitWidthList] = useState<number[] | null>(null); // null means no change |
| 21 | + if (!target) return null; |
| 22 | + |
| 23 | + const componentPin = nullthrows( |
| 24 | + store.componentPins.get(target.componentPinId), |
| 25 | + ); |
| 26 | + const component = nullthrows(store.components.get(componentPin.componentId)); |
| 27 | + const nodePins = store.nodePins |
| 28 | + .getManyByNodeIdAndComponentPinId(target.nodeId, target.componentPinId) |
| 29 | + .toSorted((a, b) => a.order - b.order); |
| 30 | + invariant( |
| 31 | + nodePins.every((p) => p.userSpecifiedBitWidth !== null), |
| 32 | + "NodePinPropertyEditor can only be used for node pins with user specified bit width", |
| 33 | + ); |
| 34 | + const componentPinAttributes = nullthrows( |
| 35 | + IntrinsicComponentDefinition.intrinsicComponentPinAttributesByComponentPinId.get( |
| 36 | + target.componentPinId, |
| 37 | + ), |
| 38 | + "NodePinPropertyEditor can only be used for intrinsic component pins", |
| 39 | + ); |
| 40 | + |
| 41 | + const getBoundingClientRect = (): DOMRect => { |
| 42 | + const geometry = getCCComponentEditorRendererNodeGeometry( |
| 43 | + store, |
| 44 | + target.nodeId, |
| 45 | + ); |
| 46 | + const nodePinCanvasPositions = nodePins.map((nodePin) => |
| 47 | + componentEditorStore |
| 48 | + .getState() |
| 49 | + .fromStageToCanvas( |
| 50 | + nullthrows(geometry.nodePinPositionById.get(nodePin.id)), |
| 51 | + ), |
| 52 | + ); |
| 53 | + const bounds = rect.shift( |
| 54 | + rect.bounds(nodePinCanvasPositions), |
| 55 | + componentEditorStore.getState().getRendererPosition(), |
| 56 | + ); |
| 57 | + return new DOMRect( |
| 58 | + bounds.position.x, |
| 59 | + bounds.position.y, |
| 60 | + bounds.size.x, |
| 61 | + bounds.size.y, |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + const bitWidthList = |
| 66 | + newBitWidthList ?? |
| 67 | + nodePins.map((nodePin) => nullthrows(nodePin.userSpecifiedBitWidth)); |
| 68 | + |
| 69 | + const isTouched = Boolean(newBitWidthList); |
| 70 | + const isValid = bitWidthList.every((bitWidth) => bitWidth > 0); |
| 71 | + |
| 72 | + const onClose = () => { |
| 73 | + setTarget(null); |
| 74 | + setNewBitWidthList(null); |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Popover |
| 79 | + open |
| 80 | + anchorEl={{ nodeType: 1, getBoundingClientRect }} |
| 81 | + transformOrigin={{ |
| 82 | + vertical: "center", |
| 83 | + horizontal: componentPin.type === "input" ? "right" : "left", |
| 84 | + }} |
| 85 | + slotProps={{ paper: { sx: { p: 2, width: 250 } } }} |
| 86 | + onClose={onClose} |
| 87 | + > |
| 88 | + <Typography variant="h6"> |
| 89 | + {componentPin.name} ({component.name}) |
| 90 | + </Typography> |
| 91 | + <Typography variant="caption" color="text.secondary"> |
| 92 | + Specify the bit width to assign to the pin. |
| 93 | + </Typography> |
| 94 | + <form |
| 95 | + onSubmit={(e) => { |
| 96 | + e.preventDefault(); |
| 97 | + let maxOrder = 0; |
| 98 | + for (const [nodePin, bitWidth] of zip(nodePins, bitWidthList)) { |
| 99 | + // Create new NodePin |
| 100 | + if (!nodePin && bitWidth) { |
| 101 | + store.nodePins.register( |
| 102 | + CCNodePinStore.create({ |
| 103 | + componentPinId: target.componentPinId, |
| 104 | + nodeId: target.nodeId, |
| 105 | + order: ++maxOrder, |
| 106 | + userSpecifiedBitWidth: bitWidth, |
| 107 | + }), |
| 108 | + ); |
| 109 | + continue; |
| 110 | + } |
| 111 | + // Delete old NodePin |
| 112 | + if (nodePin && !bitWidth) { |
| 113 | + store.nodePins.unregister(nodePin.id); |
| 114 | + continue; |
| 115 | + } |
| 116 | + // Update NodePin |
| 117 | + if (nodePin && bitWidth) { |
| 118 | + maxOrder = nodePin.order; // nodePins are sorted by order |
| 119 | + if (nodePin.userSpecifiedBitWidth !== bitWidth) |
| 120 | + store.nodePins.update(nodePin.id, { |
| 121 | + userSpecifiedBitWidth: bitWidth, |
| 122 | + }); |
| 123 | + continue; |
| 124 | + } |
| 125 | + throw new Error("Unreachable"); |
| 126 | + } |
| 127 | + onClose(); |
| 128 | + }} |
| 129 | + > |
| 130 | + <Stack gap={0.5} sx={{ mt: 1 }}> |
| 131 | + {bitWidthList.map((bitWidth, index) => { |
| 132 | + return ( |
| 133 | + <TextField |
| 134 | + // biome-ignore lint/suspicious/noArrayIndexKey: bitWidth is only identified by index in this component |
| 135 | + key={index} |
| 136 | + type="number" |
| 137 | + value={bitWidth || ""} |
| 138 | + slotProps={{ htmlInput: { min: 1 } }} |
| 139 | + onChange={(e) => { |
| 140 | + const newValue = Number.parseInt(e.target.value, 10); |
| 141 | + if (newValue >= 0 || e.target.value === "") |
| 142 | + setNewBitWidthList(bitWidthList.with(index, newValue || 0)); |
| 143 | + }} |
| 144 | + error={bitWidth <= 0} |
| 145 | + size="small" |
| 146 | + /> |
| 147 | + ); |
| 148 | + })} |
| 149 | + </Stack> |
| 150 | + <Stack direction="row" gap={1} sx={{ mt: 1 }}> |
| 151 | + {componentPinAttributes.isSplittable && ( |
| 152 | + <> |
| 153 | + <Button |
| 154 | + type="button" |
| 155 | + variant="outlined" |
| 156 | + size="small" |
| 157 | + onClick={() => { |
| 158 | + setNewBitWidthList([...bitWidthList, 1]); |
| 159 | + }} |
| 160 | + > |
| 161 | + Add |
| 162 | + </Button> |
| 163 | + <Button |
| 164 | + type="button" |
| 165 | + variant="outlined" |
| 166 | + size="small" |
| 167 | + disabled={bitWidthList.length <= 1} |
| 168 | + onClick={() => { |
| 169 | + setNewBitWidthList(bitWidthList.slice(0, -1)); |
| 170 | + }} |
| 171 | + > |
| 172 | + Remove |
| 173 | + </Button> |
| 174 | + </> |
| 175 | + )} |
| 176 | + <Button |
| 177 | + type="submit" |
| 178 | + variant="contained" |
| 179 | + size="small" |
| 180 | + sx={{ ml: "auto" }} |
| 181 | + disabled={!isTouched || !isValid} |
| 182 | + > |
| 183 | + Apply |
| 184 | + </Button> |
| 185 | + </Stack> |
| 186 | + </form> |
| 187 | + </Popover> |
| 188 | + ); |
| 189 | +} |
0 commit comments