Skip to content

Commit 30fdb15

Browse files
committed
Improve context menu data
1 parent 2d56f8a commit 30fdb15

4 files changed

Lines changed: 21 additions & 39 deletions

File tree

editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,13 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphMessageContext<'a>> for NodeG
778778
}
779779

780780
let context_menu_data = if let Some(node_id) = clicked_id {
781-
let currently_is_node = !network_interface.is_layer(&node_id, selection_network_path);
782-
ContextMenuData::ToggleLayer { node_id, currently_is_node }
781+
let currently_is_node = !network_interface.is_layer(&node_id, breadcrumb_network_path);
782+
let can_be_layer = network_interface.is_eligible_to_be_layer(&node_id, breadcrumb_network_path);
783+
ContextMenuData::ModifyNode {
784+
can_be_layer,
785+
currently_is_node,
786+
node_id,
787+
}
783788
} else {
784789
ContextMenuData::CreateNode { compatible_type: None }
785790
};

editor/src/messages/portfolio/document/node_graph/utility_types.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ pub struct BoxSelection {
152152
}
153153

154154
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, specta::Type)]
155+
#[serde(tag = "type", content = "data")]
155156
pub enum ContextMenuData {
156-
ToggleLayer {
157-
#[serde(rename = "nodeId")]
158-
node_id: NodeId,
157+
ModifyNode {
158+
#[serde(rename = "canBeLayer")]
159+
can_be_layer: bool,
159160
#[serde(rename = "currentlyIsNode")]
160161
currently_is_node: bool,
162+
#[serde(rename = "nodeId")]
163+
node_id: NodeId,
161164
},
162165
CreateNode {
163166
#[serde(rename = "compatibleType")]
164-
#[serde(default)]
165167
compatible_type: Option<String>,
166168
},
167169
}

frontend/src/components/views/Graph.svelte

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,6 @@
115115
return iconMap[icon] || "NodeNodes";
116116
}
117117
118-
function toggleLayerDisplay(displayAsLayer: boolean, toggleId: bigint) {
119-
let node = $nodeGraph.nodes.get(toggleId);
120-
if (node) editor.handle.setToNodeOrLayer(node.id, displayAsLayer);
121-
}
122-
123-
function canBeToggledBetweenNodeAndLayer(toggleDisplayAsLayerNodeId: bigint) {
124-
return $nodeGraph.nodes.get(toggleDisplayAsLayerNodeId)?.canBeLayer || false;
125-
}
126-
127118
function createNode(nodeType: string) {
128119
if ($nodeGraph.contextMenuInformation === undefined) return;
129120
@@ -224,29 +215,26 @@
224215
top: `${$nodeGraph.contextMenuInformation.contextMenuCoordinates.y * $nodeGraph.transform.scale + $nodeGraph.transform.y}px`,
225216
}}
226217
>
227-
{#if typeof $nodeGraph.contextMenuInformation.contextMenuData === "string" && $nodeGraph.contextMenuInformation.contextMenuData === "CreateNode"}
228-
<NodeCatalog on:selectNodeType={(e) => createNode(e.detail)} />
229-
{:else if $nodeGraph.contextMenuInformation.contextMenuData && "compatibleType" in $nodeGraph.contextMenuInformation.contextMenuData}
230-
<NodeCatalog initialSearchTerm={$nodeGraph.contextMenuInformation.contextMenuData.compatibleType || ""} on:selectNodeType={(e) => createNode(e.detail)} />
231-
{:else}
232-
{@const contextMenuData = $nodeGraph.contextMenuInformation.contextMenuData}
218+
{#if $nodeGraph.contextMenuInformation.contextMenuData.type == "CreateNode"}
219+
<NodeCatalog initialSearchTerm={$nodeGraph.contextMenuInformation.contextMenuData.data.compatibleType || ""} on:selectNodeType={(e) => createNode(e.detail)} />
220+
{:else if $nodeGraph.contextMenuInformation.contextMenuData.type == "ModifyNode"}
233221
<LayoutRow class="toggle-layer-or-node">
234222
<TextLabel>Display as</TextLabel>
235223
<RadioInput
236-
selectedIndex={contextMenuData.currentlyIsNode ? 0 : 1}
224+
selectedIndex={$nodeGraph.contextMenuInformation.contextMenuData.data.currentlyIsNode ? 0 : 1}
237225
entries={[
238226
{
239227
value: "node",
240228
label: "Node",
241-
action: () => toggleLayerDisplay(false, contextMenuData.nodeId),
229+
action: () => editor.handle.setToNodeOrLayer($nodeGraph.contextMenuInformation.contextMenuData.data.nodeId, false),
242230
},
243231
{
244232
value: "layer",
245233
label: "Layer",
246-
action: () => toggleLayerDisplay(true, contextMenuData.nodeId),
234+
action: () => editor.handle.setToNodeOrLayer($nodeGraph.contextMenuInformation.contextMenuData.data.nodeId, true),
247235
},
248236
]}
249-
disabled={!canBeToggledBetweenNodeAndLayer(contextMenuData.nodeId)}
237+
disabled={!$nodeGraph.contextMenuInformation.contextMenuData.data.canBeLayer}
250238
/>
251239
</LayoutRow>
252240
<Separator type="Section" direction="Vertical" />

frontend/src/messages.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,7 @@ export class UpdateClickTargets extends JsMessage {
3333
readonly clickTargets!: FrontendClickTargets | undefined;
3434
}
3535

36-
const ContextTupleToVec2 = Transform((data) => {
37-
if (data.obj.contextMenuInformation === undefined) return undefined;
38-
const contextMenuCoordinates = data.obj.contextMenuInformation.contextMenuCoordinates;
39-
let contextMenuData = data.obj.contextMenuInformation.contextMenuData;
40-
if (contextMenuData.ToggleLayer !== undefined) {
41-
contextMenuData = { nodeId: contextMenuData.ToggleLayer.nodeId, currentlyIsNode: contextMenuData.ToggleLayer.currentlyIsNode };
42-
} else if (contextMenuData.CreateNode !== undefined) {
43-
contextMenuData = { type: "CreateNode", compatibleType: contextMenuData.CreateNode.compatibleType };
44-
}
45-
return { contextMenuCoordinates, contextMenuData };
46-
});
47-
4836
export class UpdateContextMenuInformation extends JsMessage {
49-
@ContextTupleToVec2
5037
readonly contextMenuInformation!: ContextMenuInformation | undefined;
5138
}
5239

@@ -183,7 +170,7 @@ export type FrontendClickTargets = {
183170

184171
export type ContextMenuInformation = {
185172
contextMenuCoordinates: XY;
186-
contextMenuData: "CreateNode" | { type: "CreateNode"; compatibleType: string } | { nodeId: bigint; currentlyIsNode: boolean };
173+
contextMenuData: { type: "CreateNode"; data: { compatibleType: string | undefined } } | { type: "ModifyNode"; data: { canBeLayer: boolean; currentlyIsNode: boolean; nodeId: bigint } };
187174
};
188175

189176
export type FrontendGraphDataType = "General" | "Number" | "Artboard" | "Graphic" | "Raster" | "Vector" | "Color";

0 commit comments

Comments
 (0)