Skip to content

Commit 6c92ecb

Browse files
authored
Merge pull request #25 from ibis-ssl/default-viewbox
デフォルトviewboxの大きさをパネルから変更可能に
2 parents cda4e23 + 4d8c808 commit 6c92ecb

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/crane_visualizer_panel.tsx

+22-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface SvgLayerArray {
2525
interface PanelConfig {
2626
backgroundColor: string;
2727
message: string;
28+
viewBoxWidth: number;
2829
namespaces: {
2930
[key: string]: {
3031
visible: boolean;
@@ -36,6 +37,7 @@ interface PanelConfig {
3637
const defaultConfig: PanelConfig = {
3738
backgroundColor: "#585858ff",
3839
message: "",
40+
viewBoxWidth: 10000,
3941
namespaces: {},
4042
};
4143

@@ -51,13 +53,21 @@ const CraneVisualizer: React.FC<{ context: PanelExtensionContext }> = ({ context
5153
const [latest_msg, setLatestMsg] = useState<SvgLayerArray>();
5254

5355
const resetViewBox = useCallback(() => {
54-
setViewBox("-5000 -3000 10000 6000");
55-
}, [setViewBox]);
56+
const x = -config.viewBoxWidth / 2;
57+
const aspectRatio = 0.6; // 元のアスペクト比 (6000 / 10000)
58+
const height = config.viewBoxWidth * aspectRatio;
59+
const y = -height / 2;
60+
setViewBox(`${x} ${y} ${config.viewBoxWidth} ${height}`);
61+
}, [setViewBox, config]);
5662

5763
useEffect(() => {
5864
const handleKeyDown = (event: KeyboardEvent) => {
5965
if (event.ctrlKey && event.key === "0") {
60-
resetViewBox();
66+
const x = -config.viewBoxWidth / 2;
67+
const aspectRatio = 0.6; // 元のアスペクト比 (6000 / 10000)
68+
const height = config.viewBoxWidth * aspectRatio;
69+
const y = -height / 2;
70+
setViewBox(`${x} ${y} ${config.viewBoxWidth} ${height}`);
6171
}
6272
};
6373

@@ -66,7 +76,7 @@ const CraneVisualizer: React.FC<{ context: PanelExtensionContext }> = ({ context
6676
return () => {
6777
document.removeEventListener("keydown", handleKeyDown);
6878
};
69-
}, [resetViewBox]);
79+
}, [resetViewBox, config]);
7080

7181
// トピックが設定されたときにサブスクライブする
7282
useEffect(() => {
@@ -94,6 +104,7 @@ const CraneVisualizer: React.FC<{ context: PanelExtensionContext }> = ({ context
94104
fields: {
95105
topic: { label: "トピック名", input: "string", value: topic },
96106
backgroundColor: { label: "背景色", input: "rgba", value: config.backgroundColor },
107+
viewBoxWidth: { label: "ViewBox 幅", input: "number", value: config.viewBoxWidth },
97108
},
98109
},
99110
namespaces: {
@@ -109,7 +120,12 @@ const CraneVisualizer: React.FC<{ context: PanelExtensionContext }> = ({ context
109120
setTopic(action.payload.value as string);
110121
} else if (path == "general.backgroundColor") {
111122
setConfig((prevConfig) => ({ ...prevConfig, backgroundColor: action.payload.value as string }));
112-
} else if (action.payload.path[0] == "namespaces") {
123+
} else if (path == "general.viewBoxWidth") {
124+
setConfig((prevConfig) => ({ ...prevConfig, viewBoxWidth: action.payload.value as number }));
125+
} else if (path == "general.viewBoxHeight") {
126+
setConfig((prevConfig) => ({ ...prevConfig, viewBoxHeight: action.payload.value as number }));
127+
}
128+
else if (action.payload.path[0] == "namespaces") {
113129
const pathParts = path.split(".");
114130
const namespacePath = pathParts.slice(1, -1);
115131
const leafNamespace = pathParts[pathParts.length - 1];
@@ -164,7 +180,7 @@ const CraneVisualizer: React.FC<{ context: PanelExtensionContext }> = ({ context
164180
context.watch("topics");
165181
context.watch("currentFrame");
166182

167-
}, [context]);
183+
}, [context, topic]);
168184

169185
useEffect(() => {
170186
if (messages) {

0 commit comments

Comments
 (0)