Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 119 additions & 108 deletions ui/src/plugins/dev.perfetto.WidgetsPage/demos/nodegraph_demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,61 +530,6 @@ export function NodeGraphDemo(): m.Component<NodeGraphDemoAttrs> {
});
};

const addNode = (
factory: (id: string, x: number, y: number) => NodeData,
toNodeId?: string,
) => {
const id = uuidv4();

let x: number;
let y: number;

// Use API to find optimal placement if available
if (graphApi && !toNodeId) {
const tempNode = factory(id, 0, 0);
const config = NODE_CONFIGS[tempNode.type];
const placement = graphApi.findPlacementForNode({
id: tempNode.id,
inputs: config.inputs,
outputs: config.outputs,
canDockTop: config.canDockTop,
canDockBottom: config.canDockBottom,
});
x = placement.x;
y = placement.y;
} else {
// Fallback to random position
x = 100 + Math.random() * 200;
y = 50 + Math.random() * 200;
}

const newNode = factory(id, x, y);

updateStore((draft) => {
draft.nodes.set(newNode.id, newNode);

if (toNodeId) {
const parentNode = draft.nodes.get(toNodeId);
if (parentNode) {
newNode.nextId = parentNode.nextId;
parentNode.nextId = id;
}

// Find any connection connected to the bottom port of this node
const bottomConnectionIdx = draft.connections.findIndex(
(c) => c.fromNode === toNodeId && c.fromPort === 0,
);
if (bottomConnectionIdx > -1) {
draft.connections[bottomConnectionIdx] = {
...draft.connections[bottomConnectionIdx],
fromNode: id,
fromPort: 0,
};
}
}
});
};

const removeNode = (nodeId: string) => {
updateStore((draft) => {
const nodeToDelete = draft.nodes.get(nodeId);
Expand Down Expand Up @@ -864,59 +809,6 @@ export function NodeGraphDemo(): m.Component<NodeGraphDemoAttrs> {
];
}

function renderAddNodeMenu(toNode: string) {
return [
m(MenuItem, {
label: 'Select',
icon: 'filter_alt',
onclick: () => addNode(createSelectNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.select.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Filter',
icon: 'filter_list',
onclick: () => addNode(createFilterNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.filter.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Sort',
icon: 'sort',
onclick: () => addNode(createSortNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.sort.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Join',
icon: 'join',
onclick: () => addNode(createJoinNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.join.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Union',
icon: 'merge',
onclick: () => addNode(createUnionNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.union.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Result',
icon: 'output',
onclick: () => addNode(createResultNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.result.hue}, 60%, 50%)`,
},
}),
];
}

// Find root nodes (not referenced by any other node's nextId)
function getRootNodeIds(nodes: Map<string, NodeData>): string[] {
const referenced = new Set<string>();
Expand All @@ -940,6 +832,125 @@ export function NodeGraphDemo(): m.Component<NodeGraphDemoAttrs> {
console.log('Generated SQL queries for result nodes:', queries);
}

function renderAddNodeMenu(toNode: string) {
return [
m(MenuItem, {
label: 'Select',
icon: 'filter_alt',
onclick: () => addNode(createSelectNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.select.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Filter',
icon: 'filter_list',
onclick: () => addNode(createFilterNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.filter.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Sort',
icon: 'sort',
onclick: () => addNode(createSortNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.sort.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Join',
icon: 'join',
onclick: () => addNode(createJoinNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.join.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Union',
icon: 'merge',
onclick: () => addNode(createUnionNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.union.hue}, 60%, 50%)`,
},
}),
m(MenuItem, {
label: 'Result',
icon: 'output',
onclick: () => addNode(createResultNode, toNode),
style: {
borderLeft: `4px solid hsl(${NODE_CONFIGS.result.hue}, 60%, 50%)`,
},
}),
];
}

const addNode = (
factory: (id: string, x: number, y: number) => NodeData,
toNodeId?: string,
) => {
const id = uuidv4();

let x: number;
let y: number;

// Use API to find optimal placement if available
if (graphApi && !toNodeId) {
const tempNode = factory(id, 0, 0);
const config = NODE_CONFIGS[tempNode.type];
const placement = graphApi.findPlacementForNode({
id,
inputs: config.inputs,
outputs: config.outputs?.map((out) => {
return {...out, contextMenuItems: renderAddNodeMenu(tempNode.id)};
}),
content: renderNodeContent(tempNode, () => {}),
canDockBottom: config.canDockBottom,
canDockTop: config.canDockTop,
accentBar: attrs.accentBars,
titleBar: attrs.titleBars
? {title: tempNode.type.toUpperCase()}
: undefined,
hue: attrs.colors ? config.hue : undefined,
contextMenuItems: attrs.contextMenus
? renderNodeContextMenu(tempNode)
: undefined,
});
x = placement.x;
y = placement.y;
} else {
// Fallback to random position
x = 100 + Math.random() * 200;
y = 50 + Math.random() * 200;
}

const newNode = factory(id, x, y);

updateStore((draft) => {
draft.nodes.set(newNode.id, newNode);

if (toNodeId) {
const parentNode = draft.nodes.get(toNodeId);
if (parentNode) {
newNode.nextId = parentNode.nextId;
parentNode.nextId = id;
}

// Find any connection connected to the bottom port of this node
const bottomConnectionIdx = draft.connections.findIndex(
(c) => c.fromNode === toNodeId && c.fromPort === 0,
);
if (bottomConnectionIdx > -1) {
draft.connections[bottomConnectionIdx] = {
...draft.connections[bottomConnectionIdx],
fromNode: id,
fromPort: 0,
};
}
}
});
};

// Render a model node and its chain
function renderNodeChain(nodeData: NodeData): Node {
const hasNext = nodeData.nextId !== undefined;
Expand Down
4 changes: 2 additions & 2 deletions ui/src/widgets/nodegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1766,8 +1766,8 @@ export function NodeGraph(): m.Component<NodeGraphAttrs> {

// Find non-overlapping position starting from center
const finalPos = findNearestNonOverlappingPosition(
centerX,
centerY,
centerX - dims.width / 2,
centerY - dims.height / 2,
tempNode.id,
nodes,
dims.width,
Expand Down